13 ms·
Make for hipsters
- adrianratnapala 10y agoThe depressing thing is that after decades of trying, there still does doesn't seem to be anything better than Make. And Make is terrible. At least if want have to deal with more than one directory.
- peterwwillis 10y agoHere's a more in-depth and easy to read guide for beginners: http://www.computerhope.com/unix/umake.htm http://www.computerhope.com/unix/umake.htm If you want to see some interesting/convoluted Makefiles, get a very complex, large piece of software (something from Gnome, possibly) and use autoconf & automake to generate the Makefiles, then pour through them to see how they tick.
- voltagex_ 10y agoFixing autoconf/automake issues (particularly when autotools is upgraded) is one of the least fun things to do. Make is fine. Automake is horrific.
- nneonneo 10y agoFor the target audience (total Make beginners) poring through autogenerated makefiles might not be such good advice. It's functionally equivalent to telling people to learn Bash by studying a `configure` script, or learning JavaScript by studying EmScripten output. If you're decent with Make and want to learn advanced tricks, then by all means go right ahead, but 95% of Make users will never need to delve into such details.
- treve 10y agoA better title would have been 'Make 101'. Its an absolute beginners tutorial.
- joelg 10y agoA more precise title would have been 'Make 101'. I also wouldn't have read an article by that title, although (not knowing any make) I'm glad that I did read this one.
- miiiiiike 10y agoI use make to manage JS and container builds. Couldn't imagine using anything else right now.
- deleted 10y ago[deleted]
- toomim 10y agoI don't see what this has to do with hipsters. The fonts are not very cool.
- oneeyedpigeon 10y agoAbuse of a top-level domain which occurs at the end of the author's name - that gets about a 7/10 on my hipsterometer.
- pawadu 10y agoNot knowing something any CS graduate should already know and then feel so great about discovering it you just have to write a blog post about it? that gets a 8/10 on my hipsterometer
- macca321 10y agoI own forhipsters.com, what does that score?
- imtringued 10y ago5/10 but only because it's not forhipste.rs
- bluejekyll 10y agoAnswer me this, does anyone have a good cross-platform method of writing Make files? I guess with Windows now having bash, I can actually just target bash scripts?
- voltagex_ 10y agoWrite make files, or write CMake files.
- tiddlydum 10y agoYou _can_ use the GNU Autotools on Windows, though you need a shell installed (and a vaguely sane POSIX environment). CMake does a good job targeting a bunch of environments, including Windows.
- hawski 10y agoTake a look at makefiles from various suckless projects. For example: http://git.suckless.org/dwm/tree/Makefile http://git.suckless.org/dwm/tree/Makefile Their makefiles work with GNU Make and BSD Make, don't know about nmake (AFAIR Windows).
- zelos 10y agoCMake? Something about having to write a build system for my build system always makes me feel like I'm doing something wrong, though ;-)
- shoo 10y agoThis introduction neglects to mention that Makefile rules are primarily rules to derive files (usually from other files). Make will not re-run a rule when the target (file) generated by the rule is already up to date. From this perspective, a well structured Makefile can be thought of as a pipeline/graph of functions, that derive values (files) from previously known values (files). Expressing a build process in this kind of way makes it possible to support incremental / distributed build in a fairly natural / obvious way. It is less obvious how to support incremental / distributed builds for the more general kind of imperative build script that does a bunch of side effects (e.g. the style of build script you'd get using something like ant).
- nneonneo 10y agoFor anyone who's curious: you can use a ".PHONY" target to force targets to be rebuilt (i.e. that they don't correspond to real files). All dependencies of the .PHONY pseudo-target will be rebuilt unconditionally when encountered. Typically, "all", "clean", "install", etc. will all be phony targets as the goal isn't to literally make a file called "all". The syntax is simple: .PHONY: all clean install marks "all", "clean" and "install" as phony. Even if the file "all" happens to exist, "make all" will still trigger the build.
- Bromskloss 10y ago> All dependencies of the .PHONY pseudo-target will be rebuilt unconditionally when encountered. To be clear, they will rebuilt only if they are not already up to date relative to their own dependencies, right?
- rahkiin 10y agoyes. Phony is only about making sure that 'all' is seen as target, not as file or folder
- delinka 10y agoThis is only an issue if you do indeed have files or folders named the same as targets, right? For example, if you do have an "all" file, you want make to ignore the file when considering the 'all' target, you'd apply .PHONY to the 'all' target.
- capote 10y agoDo so many people who go on this site not know how to make makefiles? Also why is it for hipsters? Or is the article for hipsters? Why does the author call his own published notes factually inaccurate? Why is it hacky to use a makefile? Is this all a big ruse?
- voltagex_ 10y agoI dunno. Maybe it's a reaction to the myriad build tools developed for Javascript recently. Could they have been done with makefiles instead?
- capote 10y agoI suppose so. But there are always many tools for everything. And everyone can select his favorite.
- voltagex_ 10y agoThe problem is if you want support (even commercial help) you'll need to follow some kind of prevailing trend. If I've got a system with grunt or gulp and I need help, I can probably find a local web developer. I'm not so sure if I can find someone who's an expert in Makefiles at the moment.
- capote 10y agoYou already found at least one. Also try anyone with a degree in computer science. I'd wager it's in most first year courses; it was in 3 of mine. Also you could consult anyone who spends a lot of time working on/programming for Linux and/or in C/C++. And it's really easy, and there's tons of literature on it on the internet. And it has a manpage.
- adrianN 10y agoI never encountered make in any assignment during my studies. It certainly wasn't mentioned in the lectures. At my university, you didn't learn any tooling at all, if you thought you needed it you had to teach yourself.
- nkuttler 10y agoMake is a great tool for automating simple things, but if you use a programming language you should probably use native automation tools for common tasks. If you have any non-trivial logic or want to enable code-reuse make doesn't seem like the right choice either.
- jacquesm 10y agoI hate this. Every programming language that bolts on its own build environment. Make is language agnostic and that's a huge benefit, it means you have to learn about just one build tool and its peculiarities and that will allow you to build many different projects in different languages. All these languages with their half-baked build tools that won't accept that they may have to play nice with other languages and their build tools are not helping. Of course it is great when you write a language to also throw in a build tool, but in the end if the build tool re-implements 30% or so of make in a broken way I don't see the point. Make does have its limitations, but most ordinary projects get nowhere close to reaching those.
- gurkendoktor 10y ago> Make is language agnostic and that's a huge benefit, it means you have to learn about just one build tool and its peculiarities and that will allow you to build many different projects in different languages. You need to learn the Make build tool (easy) and then you need to learn bash scripting to get anywhere (argh!). The OP has linked an example Makefile and I wouldn't be able to maintain/debug it without reading plenty of manpages: https://github.com/Financial-Times/n-makefile/blob/master/Makefile https://github.com/Financial-Times/n-makefile/blob/master/Ma...
- pawadu 10y agoI am not sure if you can get rid of some those commands with any other build tool. There is simply no build tool that includes everything, so sometimes some scripting will be required. Then, why not go with bash that you hopefully already know? Also, a lot of the complexity is due to him being new to make: asset%: @if [ -e webpack.config.js ]; then ... fi
- heja2009 10y ago> You need to create a makefile to tell make what to do. Nope. You can say "make hello" and have make automatically use its default rules - which can be configured - to e.g. compile hello.c into a hello executable. I use this frequently.
- nneonneo 10y agoMake's default implicit rule set is very useful for C, C++, Fortran, Pascal, and basically nothing else. Out of curiousity: how do you configure the implicit ruleset? I wasn't able to find documentation on that (the closest I found was https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_10.html#SEC95 https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter..., which explicitly documents the Make implicit rules but says nothing about changing/augmenting them).
- orodley 10y agoMany implicit rules make use of variables, which you can change as you see fit. For example, creating an object file from a C file will do something like "$(CC) $(CFLAGS) $< -o $@". You can change CC to change the compiler used, and add stuff to CFLAGS to add compiler options, all without touching the rule itself. Other than that there's not much configuration, but the predefined rules are usually quite simple, so it's pretty easy to just write your own implicit rules if the predefined ones don't suit your needs.
- leni536 10y agoI hate implicit rules. Also the %: %.c implicit rule is especially idiotic, it causes to check this rule for every single file, so if you have hello.h, then it will search for hello.h.c too.
- db48x 10y agoWell, you either force people name their files correctly, or you make the computer do a little extra bookkeeping so that they don't have to. This is the silliest thing to hate about make that I've ever heard of.
- nneonneo 10y agoThe single biggest piece of advice I can give for Make is to make sure your text editor uses real tabs in Makefiles. My personal preference for everything is spaces (mostly a habit learned from Python's PEP 8), but Make doesn't honour spaces for indentation - only tabs. Despite being an obvious usability hole, this has never been fixed. Exacerbating the issue is the poor error message given upon encountering a space-indented line in Make: Makefile:2: *** missing separator. Stop. Make is ubiquitous and fairly easy to use, but it's warts like this that remind you that it is fundamentally a build system from the far past.
- johndalton 10y ago> Despite being an obvious usability hole, this has never been fixed. Well, which implementation of make should be fixed? The thing about these truly ubiquitous nix tools is that eventually people discover that there are unixes other than Linux. …that said, a quick google found that this is* fixed in GNU make; check the docs for .RECIPEPREFIX here: https://www.gnu.org/software/make/manual/html_node/Special-Variables.html https://www.gnu.org/software/make/manual/html_node/Special-V...
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- aaronbasssett 10y agoI mostly work in Python as well, but I always include an .editorconfig file in each project to automatically set things like [Makefile] indent_style = tab http://editorconfig.org/ http://editorconfig.org/ https://github.com/aaronbassett/EditorConfig/blob/master/.editorconfig https://github.com/aaronbassett/EditorConfig/blob/master/.ed...
- efaref 10y ago> Despite being an obvious usability hole, this has never been fixed. You can now change the prefix for your recipes by setting the .RECIPEPREFIX special variable (https://www.gnu.org/software/make/manual/html_node/Special-Variables.html https://www.gnu.org/software/make/manual/html_node/Special-V...). That being said, with a properly configured editor I've actually found that TAB is an ideal character for the recipe prefix.
- ThePhysicist 10y agoI routinely use a Makefile for building my frontend resources, which works quite well: https://github.com/adewes/gitboard/blob/master/Makefile https://github.com/adewes/gitboard/blob/master/Makefile Using the inotify tools it's even possible to automatically trigger a rebuild whenever a file changes. For me, no need to use Gulp anymore, as Makefiles are easier to reason about, more composable and make use of existing infrastructure instead of reinventing the wheel over and over again.
- leni536 10y agoOne really neat thing about make: If you include other makefiles then before including them make checks if there are prerequisites of these files and rebuilds them as needed. One can use this to automatically generate header dependencies of translation units for C/C++ projects. Some of the relevant lines in one of my Makefiles: include ${DEPENDS} ${DEPDIR}/%.d: ${SRCDIR}/%.${SRCEXT} @mkdir -p ${shell dirname $@} ; \ $(CC) -MM ${INCLUDE} -mmcu='${AVRTARGET}' $< >> $@ If any file in the "include" line needs updating then it is generated by gcc and included in the same make run. I didn't find a good documentation about this feature and I discovered this with make's pretty verbose -d option. Previously I did something like this using multiple makefiles and recursively calling make which is generally frowned upon (for valid reasons).
- efaref 10y agoI think these days you're better off using -MMD to generate header dependencies at the same time as compiling, rather than doing it in a separate rule. %.o: %.c $(CC) $(CFLAGS) -MMD -c $< -include $(OBJECTS:.o=.d)
- leni536 10y agoIs this always correct? The %.d files don't depend explicitly on the %.c files so they aren't rebuilt every time at the beginning of the make run, so old dependencies are included after a change in header dependencies. I'm still trying to construct an example where such a makefile breaks though.
- efaref 10y agoThe only case it becomes problematic is if you delete a header file, as make won't know how to remake it and won't recompile the C file (removing the dependency) until it has done so. There are two options to resolve this: Also specify -MP on the GCC command line, which causes GCC to emit an empty rule for all the header files, like this: foo.o: foo.c foo.h bar.h foo.h: bar.h: Alternatively, you can write a generic empty header creation rule: %.h: Which has the same effect.
- jbergens 10y agoAnother way is using npm-install-changed for the "npm install" problem and fix everything else with other npm scripts like npm-run-all. Use make if you like it but if everything else is npm you can solve most problems using "npm run" and npm scripts. https://github.com/oNaiPs/npm-install-changed https://github.com/oNaiPs/npm-install-changed http://blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/ http://blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool... [edit: formatting]
- majewsky 10y agoSupplying even a trivial Makefile is advantageous if your target audience knows it. They'll see a Makefile and just do `make && make check && make install` without needing to browse your README. For example, Debian's packaging process is heavily optimized towards Makefiles that supply the standard targets, and you can reduce a lot of friction for packagers if you supply a wellbehaving Makefile with your program. (Even if developers would usually `go build` or whatever instead of `make`.)
- jbergens 10y agoyes it is good to adapt to your target audience. If that is linux users a makefile might be the best. For a lot of other users like javascript web developers npm might be better. For end users on windows an msi or exe installation file is probably best. Makefiles are unusual on windows which makes them a somewhat bad solution even for developers on windows machines. If you want to create system that works on multiple platforms there are probably better ways. At least make sure to not use some *nix-only syntax in the makefile or calling grep or awk.
- JdeBP 10y agoMakefiles are only considered unusual on Windows if one ignores decades of .MAK files. (-:
- sleepychu 10y agoI feel like the author just structured their build dependencies wrong. I used to work on a project that had a 16,000 line Makefile, where almost the entire file was actively in use. So you can abuse any build system.
- ashitlerferad 10y agoPop make quiz: what is the difference between these two Makefiles? ---------------------------------------------------- foo: bar bar: ---------------------------------------------------- .PHONY: foo foo:
- jhjjhj 10y agoif bar happens to already exist no rules will execute for the first.
- teddyh 10y agoMeanwhile, there’s a perfectly good introduction in the GNU Make manual: https://www.gnu.org/software/make/manual/make.html#Introduction https://www.gnu.org/software/make/manual/make.html#Introduct... Also available in print: https://shop.fsf.org/books-docs/gnu-make-version-381 https://shop.fsf.org/books-docs/gnu-make-version-381
- pawadu 10y agothank you for posting this
- pdkl95 10y agoUnless your distro excludes the Texinfo docs, the same manual is probably also available locally: info make GNU tools often use man pages for a quick reference of the options, return codes, etc. Unfortunately, as info(1) isn't as well known as man(1), it's easy to miss that many GNU tools have detailed manuals.
- groovy2shoes 10y agoI learned how to use make by reading OpenBSD's man page for it[1]. It's written extremely well, and is concise yet comprehensive. Of course, it's specific to BSD make, but most of the concepts are portable to GNU make with some caveats. FreeBSD and NetBSD, iirc, both have similarly high-quality manuals for their respective makes, and I've come to favor BSD make in general, using bmake[2] on Linux and Cygwin for my own projects (bmake being a portable, packaged-up version of NetBSD's make). [1]: http://man.openbsd.org/OpenBSD-current/man1/make.1 http://man.openbsd.org/OpenBSD-current/man1/make.1 [2]: http://www.crufty.net/help/sjg/bmake.htm http://www.crufty.net/help/sjg/bmake.htm
- alpyne 10y agoMaking Make Make Sense from @izs is great: https://www.youtube.com/watch?v=dsqBSgdQz_8 https://www.youtube.com/watch?v=dsqBSgdQz_8
- tereks 10y agoMade for hipster devs: HIPSTERsound.com
- zx2c4 10y agoIn case anyone's curious, I've been using Makefiles for web development for a long time. The src files of a webpage are un-minified javascript, less/sass files, and thumbnails. The dst files are the minified/built/thumbnailed output. This suits Make particularly well. https://gist.github.com/zx2c4/11de49b2780c787b3ed5e1ee39485747 https://gist.github.com/zx2c4/11de49b2780c787b3ed5e1ee394857... That's the Makefile I use on https://www.edgesecurity.com/ https://www.edgesecurity.com/ if you're interested. It also has in it some very simple ssh/rsync deployment mechanism. Since the deployment relies on the built files, I can simply run: $ make deploy And the entire webpage is built and deployed in one step. Every webpage I manage has a variant of this, with all sorts of rules for building and managing things. It turns out to be much easier than grunt or ant or random bash scripts or vagrant or whatever else kids use these days.
- VLM 10y agoSomething unmentioned so far is when you start getting "advanced" the make scene is much like the shell scene and its fairly easy to write something that only runs on GNU make or only on BSD make and there is plenty of opportunity for argument about that being a bug or feature etc. So there's that. Make being such a good idea and a popular standard, there's no shortage of people implementing not quite compatible make-like-systems. Another fun tip is make your makefile executable and try the first line like #!/usr/bin/env gmake or whatever seems appropriate. Or perhaps GNU make is installed as "make" on your OS or you use an "alternatives" link farm or whatever. Some people will flip out if you put arguments on that command line, others will pat you on the back and high-five. So there's that "fun".
- gkya 10y agomake is not used in this post. He wrote shell scripts with a single tab indentation.
- zeveb 10y agoMake is old and crufty, but it's still the best at what it does. What I'd really love right now is a program which can look at a Go source tree and write make rules such that I don't have to run go build or go install unless dependencies have actually changed. It's really annoying to be pushing new images when nothing has actually changed.
- koloron 10y ago> but it's still the best at what it does. If "what it does" means "build complex software projects" I think your claim is rather debatable. GHC is currently switching away from make after having already overhauled its make-based build system two times: http://research.microsoft.com/en-us/um/people/simonpj/papers/ghc-shake/ghc-shake.pdf http://research.microsoft.com/en-us/um/people/simonpj/papers...
- jsz0 10y agoI like to use makefiles for managing docker containers / images. For my needs it's a more practical and flexible solution than other docker orchestration / management tools I've tried. A few things I've learned that might be useful: * (cd /path/to/something && make goal) * You can use .DEFAULT_GOAL := goal_name to set the default target run by 'make' with no target name specified. * Adding .SILENT will suppress make entering/leaving directory messages. Probably wise to be careful with one until you're 100% sure you're not ending up in an unintended directory doing nasty things. * You can use $(COLOR) = `tput setaf x' and $(RESET) = `tput sgr0` to add colors to your make output. For example @echo "$(RED)ERROR!$(RESET)" * Use if ! -f conditionals to start goals that rely on external scripts/programs. Otherwise make will run until it hits the error which will leave you with a partially completed goal. goal: @if [ ! -f ./scripts/blah.sh ]; then echo "ERROR: ./scripts/blah.sh not found"; exit 1; fi @echo "without the if ! -f this command would run" @echo "and so would this one" @./scripts/blah.sh $(IMPORTANT_STUFFS) @echo "but not this one"
- mdaniel 10y ago> You can use .DEFAULT_GOAL := goal_name to set the default target run by 'make' with no target name specified. I had always, perhaps erroneously, thought that the very first goal in the file was the default one.
- moosingin3space 10y agoThis is true -- the first goal declared is the default.
- jsz0 10y agoIIRC .DEFAULT_GOAL is a relatively new feature. If it's not set goals will be processed in the normal order.
- 0xmohit 10y agoI prefer a help rule in the Makefile, like so: help: @grep -P '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' Assuming that the trimmed Makefile read: build: ## Build the binary clean: ## Clean the binary and intermediate files distclean: clean ## Clean dependencies, intermediate files and binary deps: ## Install dependencies all: deps build ## Get dependencies and build binary release: distclean all ## distclean + all Now issuing make help would display: all Get dependencies and build binary build Build the binary clean Clean the binary and intermediate files deps Install dependencies distclean Clean dependencies, intermediate files and binary release distclean + all
- jloughry 10y agoLovely! I'm going to start using this in all my Makefiles. Seriously, thanks. You solved a problem I had.
- urda 10y agoFirst off I wanted to say this is amazing and I'm going to roll it into my current and future Makefiles. Second, if you're on OS X the `-P` flag is going to have problems [1]. Works fine if you just replace `grep -P` with something like `ack`! [1] http://stackoverflow.com/q/16658333/169153 http://stackoverflow.com/q/16658333/169153
- spinlock 10y agoThis is great. I think `help` commands are fantastic things to add to projects right from the start. It really helps the next person to get up to speed.
- jklowden 10y agoSimplified: (no color, no grep) help: @awk -F ':.*##' '$$0 ~ FS \ {printf "%15s%s\n", $$1 ":", $$2}' \ $(MAKEFILE_LIST) | sort
- jloughry 10y ago
- profeta 10y agoThis tutorial is awful. You should read the manual, which is shorter than that page! make is NOT: alias: commands this will bite you when you least expect. Make is: targetfile: inputfiles commands to get targetfile to exist and you can use things like %.css_min: %.css sass stuff .PHONY: release # above is needed otherwise you won't build if there is a release/ dir release: release-min.css release-min.css: (list *.css_min files, there are helpers for it) cat *.css_min > release-min.css anyway. you get the idea. work with files and why you need those files.
- mercurysmessage 10y agoYea, you can just do what he is doing using shell scripts anyway. I don't see why you'd use make for it.
- nwmcsween 10y agoThe problem with $buildtool is that 99.99% of the time the people implemting it had no idea how make worked and invented something easier to use in the particular domain (grunt, gulp, webpack and many more). Make creates a DAG of files and resolves targets, the examples given in this article also fail to understand how make works.
- GFK_of_xmaspast 10y agoI guess make could be for hipsters in the same way that vinyl is for hipsters, i.e. obsolete technology that should have gone away by the end of the 80s.