r/programming • u/hapshaps • Dec 23 '17
"A simple makefile" is a unicorn
http://nibblestew.blogspot.dk/2017/12/a-simple-makefile-is-unicorn.html?m=1
151
Upvotes
r/programming • u/hapshaps • Dec 23 '17
141
u/[deleted] Dec 23 '17
This site could not be much uglier
14
u/arielby Dec 24 '17
CC=powerpc-unknown-linux-gnu-gcc CXX=powerpc-unknown-linux-gnu-g++ AS=powerpc-unknown-linux-gnu-as make. Done.An rpm spec etc. will set these env vars for you.
45
u/tar-x Dec 23 '17
Compared to what?
What is a "simple" build system?
These aren't fair complaints if the author can't answer that question.
32
u/HeadAche2012 Dec 23 '17 edited Dec 24 '17
This style works for me: https://pastebin.com/U1n4zKE9
Worst trend in makefiles is the use of recursive make files, there should be one makefile for an entire project, I dont care if you have 10 files or 10,000 -- Recursive make is wrong
Edit: Better version with working .d dep files https://pastebin.com/raw/g8QqfMTT
4
u/DreadedDreadnought Dec 24 '17
Suddenly debugging Maven errors and transitive dependencies does not seem so bad compared to writing my own build system in Make.
28
u/gigadude Dec 24 '17
Those who don't understand make are destined to recreate it, badly.
28
u/est31 Dec 23 '17
Make had been around back when Microsoft designed Visual Studio. It is an entirely intentional feature that it lacks Make support, as you then wouldn't have been locked in to Microsoft's proprietary solution. So everything is working here as Microsoft intended it.
7
u/bless-you-mlud Dec 24 '17
The only thing worse than make is every single build system that was meant to replace it.
16
u/[deleted] Dec 23 '17
But there are simple makefiles, also called "call a better build system and be there just so new user/developer can type
makeand get the app ready"7
u/skeeto Dec 24 '17 edited Dec 24 '17
I'm "That One Person" that insists on Makefiles unless something else is truly needed. When done correctly, they really are clean, portable, and configurable (without editing any files). Makefiles are certainly not perfect, though:
gcc -MDfrom the Makefile) and not worth consideration. The only practical option is to take things up a level and use another tool generate the Makefile. And I'd take a hand-written configure script over Autoconf any day.if,else) and GNU Make anti-features.However, Makefiles are more capable than people give them credit for, including this article:
This is where make shines, and, no, it doesn't require editing the Makefile. You just need to understand that macros can be overridden on the command line:
Debug build:
Optimized build:
If you want this option to stick without specifying it every time, set an environment variable and use the
-eoption:True. Same issues as out-of-source builds.
False. There's even a convention for this (
PREFIXandDESTDIR):True, but that's a problem with Visual Studio, not make. Any decent text editor can invoke make and process the compiler output.
False. That's just another build target.
make checkormake testis a common convention for building and running the unit tests.False (if by "manually" means "edit the Makefile"). This is just like choosing between an optimization or debug build:
False. As far as make is concerned, building a shared library is no different than building a regular old binary. In a portable Makefile, you have to specify the commands for each. Some versions of make have a pre-defined rule for binaries but not for shared libraries, which might be what the author is getting at, but it's not portable or reliable.
False. There's even special support in POSIX make for static libraries — particularly in interacting with
ar.False.
(Note: Mingw has some smarts around the .exe extension, but the filename extension can confuse make for cross-compiled Windows builds depending on how your Makefile is written.)
4
u/hzhou321 Dec 24 '17
If you understand your dependency structure, then write a simple makefile is straight forward, clean, and fast. If your dependency has dynamic nature (such as all c files in a directory), it is still cleaner to use a simple makefile but you should write a script using proper programming language (such as python or perl) to do that dynamic logic and output a simple makefile that is clean to read and fast to execute.
The pitfall starts when you trying to write a makefile that captures your dynamic logic (which includes debug/release build). Makefile is declarative, it is a hack to put imperative logic into it; it is worse to abuse its declarative logic to achieve your imperative goal.
In the OP's article, even the simplest example makefile has overwhelming more lines other than target: dependency -> rules. That is not a simple makefile.
If you understand your project structure and its dependency, and if you are programming, then write some script to generate simple makefiles are not difficult. Your build system won't be general, but it should be simple that any programmer can spend several minutes to understand and tweak. Unless, there is too much complexity in your building process, which is a symptom. You need restructure your project structure instead of complicate your building scripts.
Well, build system are necessary for folks who do not understand and do not want to understand how their projects are being build. I think that is actually the majority of programmers. So certainly, build system has its place.
6
u/[deleted] Dec 23 '17 edited Dec 23 '17
Just for fun I'm posting my own build script.
The main advantage this script has over 'make' is that you don't have to manually add each source file to the script. It automatically detects and compiles any new ones you put in the source folder. It even notices if you edit the build script itself (eg to change compiler flags).
Header file changes are not detected. It's a bit hard to "correctly" work out which source files to recompile when a header is changed, as #include <something.h> might be #ifdef'd out, and header files can #include each other, but I have an idea of a 'good-enough' solution that I might write soon. To force a full recompile (ala 'make clean && make') you 'rm build_bits_linux' then re-run the script.
I based this off a much more complex script that inferred information about how to compile sources based on directory hierarchy; namely to work out whether a source file should end up being its own executable or just a dependency of others. I still have that lying around, but it's not as pretty.
Public domain or similar alternative or similar equivalent
11
u/benchaney Dec 24 '17
This article is silly. Neither of the make files he shows are complex at all. Simple is a completely reasonable way to describe them, and his argument to the contrary is just weird:
Of course it requires external commands and shell syntax, that's how make does things. And the patsubst is completely unnecessary for what he is trying to accomplish. And it requires "magic expansion variables"? Do you mean variables? Oh no, how complex.
His list of features make is missing also severely undercut his point. Any of those features could be added with just a few more lines.
The most telling line of the article is this one.
Obviously the make files won't seem simple to people who don't understand make's syntax. This article isn't actually about discussing makes strengths and weaknesses. Its about making people who have no idea how it works feel good about never having bothered to learn.
3
u/bruce3434 Dec 24 '17
Firefox saves the day with its "reader view".
Nice writeup though.
4
u/broo20 Dec 24 '17 edited Dec 24 '17
What a stupid post - compared to bloated, slow build systems a "simple" makefile generally makes my projects compile in less than half the time of Visual Studio's build system or whatever. Who cares if I have to spend 30 seconds tweaking it on occasion, I'm still saving a ton of time.
3
u/[deleted] Dec 24 '17
Make just shouldn't be used anymore. Build systems like Cmake exists for a reason.
2
u/reini_urban Dec 24 '17
These are all examples for GNUmakefile. Not Makefile (the portable one) and BSDmakefile.
And Visual Studio compatibility is nothing to aim for. They are the ones who ignore the standards. Nowadays you don't need to bow down to them anymore as you have 2 free and better alternatives, mingw-gcc and clang.
The example above is riddled with bad practices. IDIR is usually called INC or INCDIR. A single ODIR will clash with same src basenames. And so on
2
u/mtreece Dec 24 '17
I'm surprised no one in this thread has mentioned autotools...
With a little bit of investment learning it (and yes, this is usually the biggest complaint), autotools kicks arse.
2
u/adr86 Dec 24 '17
I use makefiles so simple people laugh at me for using make at all:
It actually works for me for most things. I think the problem the blog author has is that his requirements are simply too complex. When given requirements like that, I just walk away. Not worth the trouble.
1
u/blazingkin Dec 24 '17
An example of a simple Makefile
1
u/c-smile Dec 24 '17
Yeah, just recently got the same argument on
idea: https://www.reddit.com/r/C_Programming/comments/7jtm2s/include_source_fooc_why_not/
0
u/skulgnome Dec 24 '17
Ooh, another blogger who can't into Makefiles.
-2
u/the_gnarts Dec 24 '17
Does anyone really care?
-5
u/hoosierEE Dec 24 '17
alias make='gcc main.c'fixes the root cause with brutal efficiency.-9
u/rain5 Dec 23 '17
not true! I use a very simple makefile.
-1
u/aazav Dec 24 '17
Shoot it!