214 points · by jgrahamc · 11y ago · 66 comments
Take a look at this C++ example: for a file mybin this creates mybin.o and then links it, spitting out a mybin executable:
env CXXFLAGS="-std=c++14 -Wall -Wextra -pedantic" LDLIBS="-lstdc++" make mybin
To see what's going on, and what kind of file types are supported: make --print-data-base | egrep 'COMPILE.cc|LINK.cc'
This not only shows you what is going on, but also what kind of environment variables are involved and can therefore be customized.For small projects I would create a config.mk (e.g. with CXXFLAGS, CXX, ...) and provide a small Makefile, something along the lines of:
include config.mk
all: mybin
mybin: mybin.o
watch:
while ! inotifywait -e modify *.cc; do make; done
clean:
$(RM) *.o mybin
.PHONY: all watch clean
This allows you to easily 1/ add dependencies for binaries and 2/ speed up the development proccess, using the make watch target (note: this does not include header dependencies).Disclaimer: Makefiles seem to be wonderful build-systems (not dependency managers!) for small projects; but as soon as you find yourself searching for hacks to build into your little Makefile, ditch it and switch to something serious. That's at least my experience.
start_ci:
watch time -p make clean all & echo $$! > tmp.ci.pid
stop_ci:
kill -9 `cat tmp.ci.pid`
Then you can start continuous integration by doing make start_ci
Then you can stop continuous integration by doing make stop_ci
Obviously, this is inspired by rc.d scripts.[0] https://github.com/lpsantil/rt0/blob/master/Makefile#L117
$$() might work, but untested.
[1] https://www.gnu.org/software/make/manual/html_node/Reference...
http://sourceforge.net/p/wordgrinder/code/ci/default/tree/Ma...
While it looks crazy, it has the advantage that it avoids pattern rules completely, which means that everything's explicit and we get to avoid some of make's more annoying misfeatures. It makes the makefiles vastly easier to reason about, as well as allowing things like building the same binary multiple times with different flags, which is really hard with traditional makefiles. The downside is that it looks crazy (and GNU make macros are not what you'd call well-designed, either).
As for switching to a different build system... like what? Every other build system I've seen is horrible in other ways, and make is at least ubiquitous. Every time I need to install some software which uses a non-make build system I get this horrible sinking feeling as I have to go and install the thing I need to install the thing.
...
Incidentally, the craziest thing I ever did in make was this:
http://cowlark.com/2013-10-19-insane-make/index.html
I wrote a compiler for a statically-typed language which targeted GNU make macros as a backend. Surprisingly, it actually worked, although I never got round to finishing the arbitrary-precision maths library needed by the runtime system...
What do you recommend? Autotools, bespoke shell scripts, or something else entirely?
Autotools has never be a great tool, only a necessary evil for when you need to package cross-Unixes software. I heard that CPack (http://www.cmake.org/Wiki/CMake:Packaging_With_CPack) do the job while being exactly as good as CMake (which can be a compliment... or not. YMMV).
So, if you use Rust, use Cargo, with Java use Maven, with C# use MSBuild, with ruby use Rake, etc. And C/C++ ? Well, for once if you can chose you're lucky. You can try CMake or premake.
The only thing you must do is get away of most of the Google's build projects: Gyp, Lunch, Ninja, etc. I have horrible experiences with those. Even worse than Rubygem's native compilation, which is quite a performance.
You need these things because you know what? One day someone else is going to want to modify your build system, to cross-compile for another target device, or to build a library instead of an executable, or to add a configuration option, or whatever. Or they are going to find that they need to optimise the build because it's too slow. And without these tools, those tasks are ridiculously complicated.
And lastly, it, would be wonderful if I could take an autotools or pure-makefile based project, and convert it to this system with a simple import statement - the resulting project build file wouldn't need to be particularly beautiful, because, with all those other tools that I listed above, I could clean things up by hand after the import.
The bad news is that as far as I am aware, no such tool exists. I assume it must be a hard problem to solve, because the pain that build systems cause is a real and ongoing concern for millions of developers around the world.
If you're the sort of person that writes these types of system, I would be your customer. Make the basic build system - where you can create and build projects - free, and make the debugging tools something that you have to pay for. I for one would be your customer.
All of which leads me to the bad news: I've never yet encountered a system that does these things, which is why I needed to include a plea to someone to make one.
You see, it heavily depends on your preferred environment.
[1] The documentation is actually a little dense, as qmake is a tool capable of building "real projects", but I found that a simple qmake -project && qmake && make (I think, at least you need a project file, and the let qmake make a makefile, and then use that...).
http://doc.qt.io/qt-5/qmake-running.html
See also: https://web.njit.edu/all_topics/Prog_Lang_Docs/html/qt/qmake...
You can make some horrendous messes but it's nice to have Python around instead of having to learn another intermediate language as in CMake's case.
> adherent of SCons.
o_O
> You can make some horrendous messes
:nod:
http://stackoverflow.com/questions/25589586/why-does-patsubs...
Someone pretty knowledgable chimed in, but we were both stumped. In the end I found a workaround, but never did get to the bottom of this peculiar behavior.
> EDIT: I had a theory that the % characters inside the patsubst
> were getting evaluated early, using the stem match (foo), so
> that the patsubst itself was looking something like this:
>
> $(patsubst foo.c,foo.o,bar.c baz.c)
That is what happened. The answer on SO explains how to work around
it. > To test this theory, I added a file called foo.c to foo_SRCS:
...
> That resulted in something even weirder:
> make: *** No rule to make target `foo.a', needed by `all'. Stop.
Which resulted in Make seeing the line as %.a: %.o bar.c baz.c
Now, Make will only choose to use a pattern rule if it knows how to
make all of the prerequisites. If Make doesn't know how to make foo.o
(which it wouldn't if you don't have an actual file named foo.c), then
it won't even consider the rule as a possibility to make foo.a.The example from the GNU Make manual for SECONDEXPANSION uses patsubst but is not a pattern rule.
You tried to use it with a pattern rule, but then the pattern rule interfered with the % in the patsubst, replacing it before the second expansion, which is when the patsubst ran.
All that said... you could probably re-architect your Makefile a bit to make it simpler. I bit more verbosity might be worth it to avoid SECONDEXPANSION tricks.
LANGS=nob sme sma smj
POS=V N A
and want to run something on all combinations of these, like ./something --pos=V --f1=nob.f --f2=sme.f nob.txt sme.txt > nob-sme.V
– how can I make a pattern goal for that? I invariably end up with some redundancy, where e.g. only the "V" in the example above turns into my % (and $ * ), e.g. nob-sme.%: nob.f sme.f nob.txt sme.txt pos/%
./something --pos=$* --f1=nob.f --f2=sme.f nob.txt sme.txt > $@
Fortunately I haven't yet had to do this for anything too large to just copy-paste stuff, but it feels like something someone would have solved at some point, I've just never seen examples like that in any of the make-alternatives I've looked at.Unfortunately this sort of thing is not very readable because make function parameters are numbered, not named.
define genrule
$(1)-$(2).$(3): $(1).f $(2).f $(1).txt $(2).txt pos/$(3)
./something --pos=$(3) --f1=$(1).f --f2=$(2).f $(1).txt $(2).txt > $$@
endef
$(foreach lang1,$(LANGS),\
$(foreach lang2,$(filter-out $(lang1),$(LANGS)),\
$(foreach pos,$(POS),\
$(eval $(call genrule,$(lang1),$(lang2),$(pos))))))That said, I'd bet money that Paul Smith (the maintainer of GNU Make) would vouch for John Graham-Cumming's expertise.
If your project is targeting a generic POSIX environment with build tools available and you want the portability, I can see why you might go with the lowest common denominator. For a lot of projects though, there are additional non-base dependencies (e.g. a python interpreter), and having a build dependency on GNU make is usually acceptable if you're using the extra features (e.g. % matching is a big convenience).
This is also lowest common denominator computing. If you work on a system or environment that will provide certain set of features why would you limit what you can do or learn based on what is possible for everyone? Should we also be limiting our belongings to the space of other peoples living conditions? Should I ride a mountain bike in the city because my road bike doesn't like rough terrain?
GNU make is still make, which in my opinion has quite some warts. So for my view portability is the biggest reason why to use make. If you want to be portable and the features outlined in the posix standard aren't enough for you, you might as well use something else.
But let's not pretend there's a commonly used make that only observes POSIX, I think the ones I've seen {b,g,n}make, makeapp and are mostly POSIX but non quite.
see http://www.dwheeler.com/essays/make.html
GNU Make tends to be available on all systems, include OS X and the BSDs.
Since you can't write a non-trivial makefile without conditionals, restricting yourself to POSIX make will lead you down the path of despair of using something like GNU automake.
OTOH, GNU make works even on Windows (with jobserver too!), so why bother with inferior alternative implementations.
If it isn't then porting GNU make is almost always the shorter path to getting something working.
It's wonderful!
The conversation in the office on this is fascinating... writing a complete emulation based on how the GNU Make Manual says https://www.gnu.org/software/make/manual/ .
http://www.sitepoint.com/using-gnu-make-front-end-developmen...
I'm happy so far. I'm an expert in neither make nor Grunt, but I'd still rather write a Makefile than a Gruntfile.