13 ms·
I Like Makefiles
- TedHerman 2y agoNot directly related, but I suggest reading the Wikipedia entry on Knife Game. It is a marvel to watch someone skilled at this.
- eternityforest 2y agoI used to hate makefiles, but really I just hated the way C/C++ make it a manual task to decide what to compile, as opposed to something like Python modules. Now I love Make, for non-C work.
- ttyprintk 2y agobusybox make is quite restricted, which is why I write for it.
- john-tells-all 2y agoI adore Make. I've written one (or more) for every single task or project I've touched in the last 20 years. No smarts. It's just a collection of snippets with a few variables. "make run", "make test", "make lint", that kind of thing. "make recent" = lint then run the most recently modified script. You could do the same thing with Bash or other shells, but then you get stuck into Developer Land. Things are so much more complicated, without giving extra value. Make is just a DSL saying "files like this, are made into files like that, by running this command or two". That's it. This is incredibly powerful!
- klysm 2y agoSomehow every make file I’ve encountered in the wild is a lot more than “that’s it”
- wruza 2y agoThat just rosy tinted glasses most of the historical users are wearing. It takes time and nerve to admit that you have decades of experience with a footgun that isn’t even trivial to use beyond tutorial/builtin use cases.
- rramadass 2y ago> Make is just a DSL saying "files like this, are made into files like that, by running this command or two". Nicely put. Decades ago i wrote a testing framework in java where you could specify your tests and their dependent classes using make-like syntax. So you could have a set of test classes which define the "baseline suite", then another layer of test classes which is dependent on the above and only run if the above is successful and so on. I really do not understand why folks today make everything so complicated. My advise has always been, stick to standard Unix tools and their way of doing things (tested and proven over time) unless you run into something which could absolutely not be done that way. Time is finite/limited and i prefer to spend it on System/Program Design/Modeling/Structure/Patterns etc. which are what is central to problem-solving; everything else is ancillary.
- elAhmo 2y agoLikewise! I haven't been using them in the past, but at my current position almost every repository has a Makefile. Running `make test` and knowing it will work, regardless of the stack, language, repo is a huge lifesaver.
- maccard 2y ago> Make is just a DSL saying "files like this, are made into files like that, by running this command or two". That's it. The problem with make isn’t make - it’s that what makes calling usually doesn’t do that anymore. On my last project we had a makefile that had 4 main commands - build test frontend deploy. Build and test called through to maven, frontend called npm, and deploy called docker + aws. All of those tools do their own internal state tracking, caching, incrementalness and don’t report what they’ve done, so it’s not possible to write a molecule that says “only deploy if build has been updated” because maven/cargo/dotnet/npm/go don’t expose that information.
- deepsun 2y agoSame things can be said about shell scripts in a bin/ folder.
- kstenerud 2y agoMakefiles are terrible tech. The problem is that they're slightly less bad than most other build system we've come up with, which makes them "useful" in a masochistic way. Build systems tend to commit one or more of the following sins: * Too basic: Once you try to build anything beyond a toy, it quickly becomes chaos. * Too complicated: The upfront required knowledge, bureaucracy, synchronization and boilerplate is ridiculous. The build system itself takes an order of magnitude more data and memory than the build target. * No standard library (or a substandard one that does things poorly or not at all): You must define everything yourself, leading to 10000 different incompatible implementations of the same build patterns. So now no one can just dive in and know what they're doing. * Too constricting: The interface wasn't built as a simple layer upon an expert layer. So now as soon as your needs evolve, you have to migrate away. * Too much magic: The hallmark of a poorly designed system. It doesn't have to be turtles all the way down, but it should be relatively close with few exceptions. * Cryptic or inconsistent syntax.
- sixthDot 2y ago> Once you try to build anything beyond a toy, it quickly becomes chaos. Of course the chaos is not caused by, "very hypotheticaly" let's say, a compiler or maybe a language without modules. How would you estimate that ? 20%, 40%, or 70%, true ?
- tomjen3 2y agoNot OP, but its not just that C/C++ lacks modules. I think that is missing the real issue. Any complicated program probably needs a custom developed tool to build it. As a simple example, imagine a program that uses a database - you want to keep the sources as SQL and generate classes from them. Thats a custom build step. Its just that in some languages and build systems (Node, Maven), we have abstracted this away by calling them plugins and they probably come from the same group that made the library you need. No such pluginsystem exists, as far as I am aware, for makefiles.
- ReleaseCandidat 2y agoGood luck writing Makefiles for Fortran, OCaml or (whenever they will really, actually work) C++ modules. There aren't many widely used build systems that can handle such dynamic dependencies without some special "magic" for these, the only one that I know of (with a significant number of users, so not Shake) is Buck 2 (Bazel and all C++ build systems use "special magic", you can't write in user rules).
- norir 2y agoFor me, make has two fatal flaws: 1) the lack of a builtin scripting language 2) poor recursion support The problem with the lack of a scripting language is that I either have to do horrible shell contortions to do simple things like using a temporary file in a recipe or write a standalone script that doesn't live in the Makefile which is needless indirection. This is exacerbated by Make interpreting newlines in the recipe as a separate shell invocation, which I consider a poor design choice. It also requires needless forking for many small tasks which could be done more efficiently in process. The lack of proper recursion means that I either have to use recursive make, which is largely considered an anti-pattern, or I have to use a flat directory structure. What Make does have going for it is ubiquity and good performance for small projects. It is the tool most projects should probably start with and only switch to something more advanced when its scalability issues become a genuine problem.
- saurik 2y agoYou can use .ONESHELL: to switch to the shell behavior you are wanting. https://www.gnu.org/software/make/manual/html_node/One-Shell.html https://www.gnu.org/software/make/manual/html_node/One-Shell...
- spc476 2y agoThat's for GNU make, not POSIX make [1]. For some people, they either won't, or can't, use GNU make. [1] https://pubs.opengroup.org/onlinepubs/9799919799/utilities/make.html https://pubs.opengroup.org/onlinepubs/9799919799/utilities/m...
- saurik 2y agoWho even implements an alternative implementation to GNU make? FWIW, no one "can't" use GNU make... even Apple uses GNU make (hell: they even ship GNU make, lol).
- tmtvl 2y agoBSD Make exists. Also, are you saying that no one is developing software under a contract where they aren't allowed to install software on the target machine? I'm also unsure whether GNU Make works on embedded systems.
- bluejekyll 2y agoTechnically all of these make targets look for files by the names of the targets. Each one should really be defined as .PHONY. That said, I used to write makefiles like this all the time, but have since switched to just and justfiles in recent years which make this the default behavior, and is generally simpler to use. Things like parameters are simpler. https://github.com/casey/just https://github.com/casey/just
- a-dub 2y agoyeah just is really cool but it's not really commonly installed so that's kind of annoying. i feel like we're due for some kind of newfangled coreutils distribution that packages up all the most common and useful newfangled utilities (just, ripgrep, and friends) and gets them everywhere you'd want them.
- xelamonster 2y agoI like `asdf` a lot for this, but I actually don't use it for either of those examples (though it does have plugins for them). Ripgrep is in most package repos by now and all my dev machines have a Rust toolchain installed so I can build and install `just` from source with a quick command.
- croemer 2y agoI think parent meant to have it pre installed in most distros, not just easily installable
- xelamonster 2y agoSure, really though I don't understand why installing a single binary which is available from several easy to use package managers somehow becomes an insurmountable barrier for people when `just` is involved. "If it's not already on my system I can't use it" seems like an absurd limitation to place on your projects.
- happy_bzy 2y agoIn those cases, what author really needs is just[1] not make. [1] https://just.systems/man/en/ https://just.systems/man/en/
- oguz-ismail 2y agojust is just another dependency. make is available everywhere
- metaltyphoon 2y agoI rather have one justfile than have one make and another nmake to support Windows
- OutOfHere 2y agoMake is available in a lot of places, but not everywhere. It has to explicitly be installed in containers and in some distributions.
- AdamJacobMuller 2y agoAgree with the sentiment here but I've been rewriting lots of things to use Justfiles instead https://github.com/casey/just https://github.com/casey/just Avoids lots of weird makefileisims
- OutOfHere 2y agoIt's true, although GPT has given Makefiles a second life by helping write them, delaying their demise.
- deleted 2y ago[deleted]
- aterp 2y agoIt's an interesting phenomenon. ChatGPT and other LLMs have really opened up previously "archaic" tooling like Make and Bash. I've "written" more Bash in the last year than my entire career previously, because LLMs are such good copilots for that.
- AdamJacobMuller 2y agoOh god bash is archaic now? I was not prepared to feel this old today.
- AdamJacobMuller 2y agoAgreed but my favorite thing is to take a Makefile and throw it into ChatGPT and have it give me a justfile and seeing it remove all the weird makefile patterns.
- metaltyphoon 2y agoSame. Bonus that the same file works on Windows too
- kccqzy 2y agoThe author is not even using the mtime-based dependency tracking. Also the targets are supposed to be PHONY but not marked as such. The author could have replaced it with a shell script that read $1 and matched on it to determine what to do.
- notpushkin 2y agoHere’s a one-line horror story for you (from a real project I’m working on): .PHONY: $(MAKECMDGOALS) > The author could have replaced it with a shell script that read $1 Sure, but `./build.sh dev` is a bit less obvious than `make dev`. Another reason to use Make even if you don’t have any non-phony steps is that you can add those later if needed. (I agree that the author should mark {dev,build,deploy} as phony though.)
- dima55 2y agoI got an even better one for you: `./dev.sh`. The author is doing it wrong, and giving Make a bad name.
- notpushkin 2y agoYou’ll also want ./build.sh and ./deploy.sh then. If each is 1-2 commands, I’d argue it’s a waste to use separate files here. > giving Make a bad name How so?
- Jach 2y agoThat's fine, the separate files are a benefit here. The only annoyance is clogging up the root project folder -- though some people don't seem to care about that. If they got too numerous (the 5 in OP's "more advanced" project would probably not be too numerous), I'd consider putting the scripts in their own folder. I might even call it 'make' just to mess with people. Then my commands are just make/build and make/deploy and so on (.sh unnecessary). But really, in OP's case, I just have no need for some of their simple wrappers. "npm run dev" is two characters longer than "make dev", pointless.
- silisili 2y agoMakefiles are ancient at this point, and work OK for what they were intended for. The problem is when people try to expand them to everything, and they end up some arcane file full of junk nobody understands. What is a phony target anyways? And even if you understand the concept, realize it makes no sense to passerby.
- instig007 2y ago> What is a phony target anyways? And even if you understand the concept, realize it makes no sense to passerby. This is an entry level bar to the profession, one level below that will be an insult to anyone who calls themselves an engineer.
- alextingle 2y agoWhat? You sound like someone who's complaining about all the "pointless" semi-colons in C++ code. If you can't be bothered to learn the basics of the language, your criticism isn't going to be worth much.
- silisili 2y ago> You sound like someone who's complaining about all the "pointless" semi-colons in C++ code Not sure how the two relate at all. One is a statement terminator, the other a rather complex system. > If you can't be bothered to learn the basics of the language, your criticism isn't going to be worth much. That's the entire point! Remember my complaint is not against Make as a build tool, but Make as a do everything(including deploy) system. There are countless options in the space. With most, you can generally open a config file or even bash script and figure out what's going on. Not so with Make, without learning as you call it. It's nonobvious. And I say that as someone who -does- understand it.
- staticshock 2y agoI love makefiles as well and do something similar to OP: for every repo I contribute to, especially if it's not one of mine, I'll create an uncommitted makefile to track all the shell commands that add up to my workflow within that codebase. The ability to create dependencies between targets is the cherry on top, but the main value is just the ability to create a cheat sheet of shell snippets accessible via `make <target>` from the root of the repo. Such a makefile is always (1) version controlled as a secret github gist (though, as personal rule, i never hardcode secrets into it), (2) committed & pushed on `make` (3) git ignored via `.git/info/exclude`. This has worked quite well for me. One downside with this approach is that the best syntax for passing parameters down to the target from the shell is to use environment variables, which is a little awkward. `NAME=value make target` is less pleasant than `make target --name=value` would have been.
- Izkata 2y agoTake out the dashes and that's a supported syntax for overriding variables: NAME := foo test: echo "name is $(NAME)" $ make test echo name is foo name is foo $ make test NAME=bar echo name is bar name is bar
- mugivarra69 2y agome too.
- OutOfHere 2y agoMakefile enthusiasts may enjoy this audio episode on Makefile: https://podcasters.spotify.com/pod/show/podgenai/episodes/Makefile-e2o0k88 https://podcasters.spotify.com/pod/show/podgenai/episodes/Ma...
- aterp 2y agoI don't hate the idea of AI generated podcasts (quite the opposite), but I can't shake that I can't trust what they're saying. High quality podcasts have fact checking and a reputation to uphold. On this, they just slap a disclaimer on that it might contain inaccuracies. Will give it a listen tho!
- OutOfHere 2y agoIt is actually only the good AI generated material that will even have that kind of disclaimer. The ones that want you believe in their lies will not even have any disclaimer. Having said this, after having listened to 100+ episodes of this podcast, I have yet to spot a single lie, although they may still exist with a very small probability. The reason why any fact checking step has not been added to this podcast is because it hasn't proven to be too necessary in the first place. I have a different project "newssurvey" which uses external data and has citations for claims, although I still need to add an extra citation verification step for it. Perhaps in time I will.
- dima55 2y agoThe author is confused about what Make is for, and frankly this kind of thing is why Make gets a bad rap. Make is for traversing a graph to determine what should be built, and how to parallelize the steps. Here he doesn't have a graph or any dependencies defined at all. He does have a weird scripting language though, which more or less nobody likes.
- larquin 2y agoMe too :)
- MrVandemar 2y agoI instictively know that makefiles would make a lot of things easier, but I've never found the right tutorial that would help me understand them. I'm not a 'C' programmer, and so much seems weighted to the idea that you're generating object files and linking and producing a.out. Any good tutorials or resources for learning that show a broader applicability for makefiles?
- tpoacher 2y agounironically, the info manual. It's great. the philosophical "c" centredness is true, but doesn't get in the way of using other languages. (there's things like indirect rules for compiling .c files auromatically for instance, but even this can be turned off)
- Izkata 2y ago> and so much seems weighted to the idea that you're generating object files and linking and producing a.out. Well that's kind of wrong (it's used for that but that's an extremely limited viewpoint). Here's a short introduction to get started: foo: bar baz This means "whenever bar has been updated, create or recreate foo by running baz". You run it with "make foo", and make will run "baz" by default in "sh". Here's an example in a totally different context: .PHONY: build build: node_modules node_modules: package.json yarn.lock yarn install touch node_modules With this, when you run "make build", it'll only do "yarn install" if node_modules's last-modified timestamp is older than both package.json and yarn.lock. The touch is there to mark it updated for the next time you run "make build", so it knows it doesn't have to do anything. Normally you wouldn't have to do that but make assumes the commands given will update the file, and "yarn install" won't necessarily update the directory's last-modified time. This example isn't terribly useful because "yarn install" is fast and doesn't do anything itself when it's up-to-date, but it should give ideas about how flexible make actually is. One of the big criticisms of how people use "make", and why people recommend things like "just" instead, is they don't bother to use that functionality (or any of the piles of stuff built on top of it like pattern matching) and would have just done: build: yarn install ...which appears to be how OP uses it.
- rramadass 2y ago
- jfndnfjfj 2y ago[flagged]
- deleted 2y ago[deleted]
- duped 2y agomake as a task runner is not too bad, but there are better alternatives today like just (as others have commented). make as a build system is ok until you hit the warts. - make/Makefiles aren't standardized, which is why automake exists. So now you're not writing Makefiles, but templates and generating the actual makefile. This doesn't matter if you own the whole toolchain, but most people don't, so this is what some folks do to guarantee their Makefiles are portable. - make cannot do any kind of dependency resolution, it assumes that whatever you need is right there. That leads to configure scripts, which like makefiles, are not standard, so you use autoconf/autoreconf to generate the configure script that runs before you can even run a target with make. - make (and adjacent tools like automake/autoconf/autorefconf) use mtime to determine if inputs are out of date. You can get into situations where building anything is impossible because inputs are out of date and running autoconf/autoreconf/automake/configure leaves them permanently out of date. (fwiw, many build systems can get away with using mtime if they can do proper dependency tracking) All in all the fundamental design flaw with make is that it's built with the unix philosophy in mind: do one thing well, which is "rebuild targets if their inputs are out of date." However this is an extremely limited tool and modern build systems have to do a lot of work on top to make it useful as more than a basic task runner.
- Izkata 2y ago> make cannot do any kind of dependency resolution, it assumes that whatever you need is right there. That leads to configure scripts, which like makefiles, are not standard The ancient convention there is "make configure", which sets up whatever "make [build]" needs.
- computerfriend 2y ago> make cannot do any kind of dependency resolution dependency: ... target: dependency ...
- duped 2y agoTracking build targets is not dependency management except with handwaving
- kickingvegas 2y agoFor me, make is definitely in the “stop worrying and learn to love it” bin. It will outlive all of us, so accept it and move on.
- ReleaseCandidat 2y agoWhy use Make if they already use Gulp? Why not put that in `package.json`'s `script` stanza? And never ever use Make as a script runner without declaring the targets `.PHONY`, there will be a day when somebody has a directory (or file) named `build` or `dev` in their project root.
- sfink 2y agoBecause my Rust, Python, bash, Perl, etc. projects aren't impressed with the `package.json` file I wave at them. Especially before I've installed npm or any other JS runtime on my system (let alone Gulp). As the article said, it's generally installed everywhere as soon as you install any dev-related stuff. So is bash, but it's a little clunkier for very basic usage. (`if [[ $1 = build ]]; then...`)
- sfink 2y agoI do this a fair amount as well. It's really just a way of documenting the configuration and idiosyncratic commands in one place, which happens to be executable. I will happily create (uncommitted) Makefiles with hardcoded paths and keys and things, since otherwise that information would go in my ~/NOTES file and there's too much in there already. My default target tends to echo out things that I told myself I needed to remember when coming back to the project. As soon as I notice I'm reaching for anything more than `.PHONY` targets and dead-simple filename dependencies, I stop and do the real build work in something else (callable via a make target, of course!) I know how to do complicated stuff with make, which means that I know I will do it wrong. Repeatedly. Or possibly eventually do it right, but then have to maintain the resulting fire-breathing hairball. (But to those complaining about not marking all the non-file targets `.PHONY`: lighten up. If the correctness matters so much that you're going to be messed up by a file named `all` or `build` or whatever, you've probably already gone too far down the rabbit hole and should switch to something else.)
- ReleaseCandidat 2y ago> If the correctness matters so much that you're going to be messed up by a file named `all` or `build`... That's not the problém. _We_ know what "dev is already up to dáte" means, but chances are people who don't know about `.PHONY` don't.
- paulddraper 2y agoI like Makefiles too. Just not this one.
- tempodox 2y ago> Even on my MacBook, I don't remember installing it explicitly. It's there, but last I checked, it only supports building with the Apple-supplied toolchains (Xcode). If you want to use anything else, do yourself a favor and install gmake(1) from MacPorts. For C/C++ projects that are simple enough, I can reuse the same Makefile with only minor changes, and it works on both Linux and macOS, with support for sanitizers and valgrind(1). CLion eats it up like it were candy, although the gathering of sources is automated with find(1), and even respects my `--sysroot` setting. CMake has good cross-platform capabilities, but GNU make gives me more control over the actual compiler and linker invocations, and its CLI is less horrible than that of CMake.
- saurik 2y agoThe version of make which ships with Xcode is old (as Apple has stopped updating GPL software in general, and hopefully new builds of GNU make are GPL3 anyway), but it is otherwise not in any way somehow tied to Xcode's toolchain: it is a normal copy of GNU make 3.81 which runs commands the same as any other copy of make (as you provide them, and using the path).
- tempodox 2y agoYep, could have been just bugs that I misinterpreted. Anyway, the stuff I want to do works with current gmake, but not with `/usr/bin/make` on macOS.
- Wololooo 2y agoI know this is not your main point, and you touched briefly on it but, cmake is absolutely terrible. It is supposed to make the building process more straight forward and painless but what it brings to the table is * Weird bugs: In one version of cmake would return the python version that I had to be 0.16 (????) and the only fix was to update the cmake version * Messy Structure: Under the guise of giving freedom to the person designing their build with no real enforcement of any rules, in any way, people can write their own absolute craptastic version of cmake scripts which really really is going to make you lose your mind (mind you some people leverage it but it is the exception not the rule) * Opaque process: Due to the fact that the structure can be messy you are never really quite sure of what it is doing and which compile flags are effectively fed to the compiler as well as sometimes totally ignoring the library you explicitly ask it to use and trying to find another one... OK maybe this is a bit superlative, but unfortunately for me, reflects well my experience with using cmake and feels like an extra item between me and getting things working painlessly... But I might have been doing it wrong.
- jojo14 2y agoIf you like Makefiles you should try Scons: https://scons.org/ https://scons.org/
- Jach 2y agoDespite its age, my first and only encounter with it was in building godot. I was impressed from the user side. (Until, due to godot's dev policies (which might have changed by now) and not the fault of scons, a git pull resulted in a broken build even with a scons --clean. (Issue was some generated files had their parents removed by some commit, and the generated files were marked to not be cleaned by scons for some windows reason I think. Solution was to just delete them or do a git clean.)) But when I looked at their scons files, and their proliferation into every subdirectory like .svn folders, eh... The simple cases are simple, sure, but that's the case with everything. I think I'd only use scons for a project approaching godot's complexity -- especially amount of build targets -- and language mix (i.e. mostly C++).
- ac130kz 2y agoI do like "just" being suggested, but I strongly prefer using a very simple bash run script for tasks that do not require "make"'s extras, especially given that most modern build tools do parallelism and artifact caching internally. Inspired by: https://github.com/adriancooney/Taskfile https://github.com/adriancooney/Taskfile https://death.andgravity.com/run-sh https://death.andgravity.com/run-sh
- morningsam 2y agoI would theoretically prefer that as well, but it doesn't give you shell completion of targets/tasks for free like Make does. So for "UX parity", you'd also have to write a completion script and get users to source it into their shells somehow, which isn't great.
- makapuf 2y agoBack in the time we had a complex system to prepare content (encoding, metadata extraction, ...), then we tried to shive that to Make: free parallelization, partial remakes when sth went bad, for 1/100 the code and almost nothing to install. Throw a GUI to observe the filesystem and voilà. Good times.
- jart 2y agoDon't be discouraged by all the people in this thread saying you're using make wrong. One of the things that makes make a great tool is how deceptively simple it is. Yes not using .PHONY can potentially get you in trouble. But for a small project that's the sort of trap you'll fall into a year later, if at all, and even then you'll only be scratching your head for an hour. 99% of the time you don't have to care about doing things the proper way. Make lets you just hit the ground running and only imposes as much complexity as you need to keep the thing from falling apart.
- ReleaseCandidat 2y ago> One of the things that makes make a great tool is how deceptively simple it is. One of the worst things of Make is how deceptively simple it looks. Make does exactly one thing: it takes input files, some dependencies and generates _exactly_one_ output file. To have rules which don't generate output (like `install` or `all` or `clean` or all targets in the article) we need to resort to a hack, a special magic target like `.PHONY` (which hasn't been part of POSIX up to the 2017 version - IEEE Std 1003.1-2017 - https://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/m..., only the current one - IEEE Std 1003.1-2024 - https://pubs.opengroup.org/onlinepubs/9799919799/utilities/make.html https://pubs.opengroup.org/onlinepubs/9799919799/utilities/m... includes `.PHONY`). If you want to generate more than one file (like an object file and a module or a precompiled header or ...) you are on your own to build some brittle hack to get that working. Don't forget that not every Make is GNU Make, BSD and other nix like Solaris/Illumos still exist. Don't get me wrong: Make has it's uses for sufficiently complex projects which aren't too complex yet to need some "better" build system. Problem is that such projects may get too complex when more code is added and they inevitably gain some sort of scripts/programs to generate Makefiles or parts of Makefiles (so, an ad hoc meta build system is created). And the problem isn't that they use it, but that they are proposing it as a solution to "everybody". And that their Makefile stops working as soon as there is a directory (or file) `build` (or `dev` or ...) in the project root.
- jart 2y agoI work on a project with 4.4 million lines of code and using a single Makefile with no generated code works fine. It's really not all that difficult.
- JodieBenitez 2y agoBetter article on the subject: https://rosszurowski.com/log/2022/makefiles https://rosszurowski.com/log/2022/makefiles
- 112233 2y agoIn short: Person puts short shell scripts "build", "deploy" and "dev" in a single makefile as targets, instead of putting each script in a separate well named file. Then concludes that make is a great build tool as long as you don't use it as a build tool ("nothing complicated"). Please do not do this. make is not a shell. Also, consider reading (and writing) build docs, instead of running make with random targets as soon as you see a makefile
- movedx 2y agoPersonally, I've always created shell scripts too. Or PowerShell scripts if I need Windows based support (like for Golang projects that might see Windows based usage.) I find they're way, way better and easier to work with. That being said, the only issue preventing me from using shell scripts well is shared code. Like common functions etc. So far I've simply used a common.sh script and sourced/imported it. Also, what do you do once you have 4-5 shell scripts at the root of your project? I find it gets messy quickly. I need a solution for this. But yes, I agree shell scripts are generally better.
- forrestthewoods 2y agoWrite a Python script instead? Easily cross platform. Nice libraries for CLI args. Easy to debug with a real debugger. Pretty nice?
- 112233 2y agoI'd take php over python any day. Everyone has their favorite python modules they like to use in scripts, half of them are not built or wrong version for whatever current distro I'm in, so I install stuff with pip, then something with requirements.txt breaks because of wrong pytorch version, I start reading about envs, much crying. At least with bash or make nobody expects you to provide external modules/libraries
- forrestthewoods 2y agoI’ve never so much as looked at a Php script in my life. I’d have to ask ChatGPT how to even run one. A build script should work with vanilla out the box Python. No extra modules needed. I mean if you can assume vanilla bash tooling then vanilla Python is perfectly fine. If there’s some Python library you really want then add it to repo. Easy peasy.
- dented42 2y agoMake has been my favourite unix tool for years and a really useful tool to have in your pocket. It’s simple, elegant, and powerful.
- dumpstate 2y agoI went through a similar phase of excitement, but then realised it's a confusing layer of indirection on top of 'whatever-your-project-default-is' (e.g. npm + package.json). But I always need Make for BE+FE (small) projects, e.g. backend build with go and frontend is an SPA - this is where the power of Make to track dependencies really shines.
- davidcalloway 2y agoI like Makefiles as well, and although many people have commented on the limitations and the fact that the author's usage of make is fairly simplistic, I think it's great to get started with the basics. Kudos to the author for writing this up and _not_ feeling the need to learn every last bit of make and do everything "properly" before sharing. I've worked on a team where GitLab CI pipelines replaced Makefiles, and I was asked not to commit a makefile to the project because it's a customized developer workflow. They were allergic to local testing, but I thought it was a great way to just store and share knowledge about how to build, test, clean, etc. Far easier to read the GitLab CI files (which yes of course were also necessary and served a different porpoise).
- morningsam 2y agoI kind of knew this was going to be about using make as a "task runner" instead of build system as soon as I read "ten years ago" :-)
- leiserfg 2y agoI "hate" Makefiles, for building it's faster to use meson, zig build, cmake+ninja. For this use case, https://github.com/adriancooney/Taskfile https://github.com/adriancooney/Taskfile is way more flexible and you don't need to mess with PHONY rules.
- nobodywillobsrv 2y agoMakefiles are like exec readme. Put complex stuff in scripts and call script from makefile.
- deleted 2y ago[deleted]
- Groxx 2y agoMake is great. When even a simple one misbehaves, like it almost always does, I get to spend a solid chunk of time ignoring my actual goals to play with a bit of computing history. And then I get to do it all over again a month later, when it breaks in yet another new way.
- mylons 2y agome too
- kitd 2y agoI don't often interact with make files so when I do, I usually need to have a reference at hand. This is the best I've found yet: https://makefiletutorial.com/ https://makefiletutorial.com/
- matheusmoreira 2y agoMore resources: https://make.mad-scientist.net https://make.mad-scientist.net https://www.cmcrossroads.com/users/john-graham-cumming https://www.cmcrossroads.com/users/john-graham-cumming https://nostarch.com/gnumake https://nostarch.com/gnumake https://gmsl.jgc.org https://gmsl.jgc.org
- globular-toast 2y agoAm I the only one who uses make properly? I don't build C projects with it at any more (I used to use autotools for that), I use it like OP does, except I do write PHONY and I do use dependencies. I simply find the declarative way easier than writing a script. I try to avoid scripts whenever there's a better tool for the job.
- BiteCode_dev 2y agoWhile I dislike makefiles for the same reasons many echoed in this thread, with AI it doesn't matter. It's such an ubuiquitous gormat Llm are well trained on them. So you don't have to read or edit it, just let the robot do it for you. It will do it well, and it's not something you do often anyway. Then you can enjoy the other qualities of the tool, like the fact it's already there on unix, or that it's more than a task runner. Although I prefer "just" or "doit", in 2024, make is fine again.
- iblaine 2y agoThe correct answer is I don’t like makefiles when they are abused. They have no state yet people try use them as such and create pain for others.
- twcoin 2y ago[dead]
- alex-moon 2y agoI also use make this way and have done for years. I even have the same kind of religious ritual the author has, like writing the Makefile is part of setting up the codebase and organising in my own head how the whole local dev environment is going to work. The only thing is, this isn't what make is actually for. A number of commenters have recommended Just - the one I've been using on my personal projects is Task - https://taskfile.dev/ https://taskfile.dev/ - which is pretty great. As other commenters have said, the problem is that make is installed everywhere already. I would love to see a task runner become standard to the same extent, and will have a look at Just if that's the one people are using.
- pletnes 2y agoThing is, make is not readily available on windows. It should’ve been in git bash, in my opinion, but just fills the gap in a cross-platform way
- jampekka 2y agoNo dev tools are readily available on Windows.
- metaltyphoon 2y agoOr macos
- droelf 2y agoPixi is native on Windows, can install a wide range of dev tools and has task running built into projects (alongside dependency management). https://pixi.sh/ https://pixi.sh/
- pletnes 2y agoI agree with everything, except that I moved to just, which runs fine on win/mac/nix and is a single-file no-dependency task runner made for this use case. It irons over a lot of warts like working directory, loads dotenv files, lets you write multiline scripts, it’s just magic.
- pletnes 2y agohttps://github.com/casey/just https://github.com/casey/just https://just.systems/ https://just.systems/
- hackrmn 2y agoI've been writing [GNU] Makefiles for years, and have a love-hate relationship with the [GNU Make] tool. I tend to push tooling to the limit, I think it's in part because I believe in "soundness of scope" -- a tool should have a well defined scope and within that scope "all bases should be covered". In practice that would mean, that with Make I am able to define the dependency graph of pre-requisites and targets (files that Make makes) such that it just about handles the graph resolution complexity for me -- _with variables_, that is. I love Make because it largely delivers on its promise -- and I am using it almost in _opposite_ to what the author describes. That is, I consider phony targets to be an "illegitimate" feature of Make, and avoid them like the plague. While convenient, targets in Make are heavily geared to be files, certainly most of the machinery in Make was written to assume so, and even the well-known (and documented) targets like "install" and "clean" leave a terrible taste in my mouth as of late, despite these being very conventional. The problem with phony targets is that they're hard to reason with by Make (unless you actually turn "install" and "clean" into files) and break half of the rest of its assumptions on what targets should be and how they behave. The rest of the problem is the linguistical aspect of it -- if I `make install` am I making an install program/script or what? These kind of vagaries have led me firmly away from ever using phony targets. As for the rest of it, Make is terribly archaic, but that also lends it strength since the archaic nature is very simple on the flip side. The "hate" part is me taking a dislike to its bare-bones, in my opinion insufficient variables facility, where only truly global variables (certainly sharing one namespace) exist and juggling these for any non-trivial Makefile is always a problem of its own. I am no novice with GNU Make, not any longer, but occasionally I need to look back into its manual to remember how e.g. the so-called double-colon rules work (when I suspect I might need one) and the real difference between the `=` and `?=` variable assignment, for when I want to provide default values and so on. Lately I've been playing with the idea of _implementing_ a [GNU] Make compatible tool just to see if the somewhat _patchy_ scope of [GNU] Make can be given more coverage -- for instance to see if adding locally-scoped variables and more flexible target definition can improve Make? What I mean is to experiment with an implementation that retains fundamental principle and mandate of Make -- dependency graph resolution and reliance on [normally] UNiX shell -- but "upgrading" the syntax and extending the semantics. Because while Make is useful, it's also at times terribly hard to work with. To paraphrase Bjarne Stroustrup (the man behind C++), "inside Make there is a neat sound idea struggling to get out".
- aden1ne 2y agoIn my first job out of university, I inherited a data pipeline that had been written in Make using ~40 Makefiles. Needless to say, it was a hell to debug.
- matheusmoreira 2y agoMakefiles are an eerily lisplike turing tarpit. I hand wrote the makefiles for my projects, they generate wonderfully organized build trees. Hell I use makefiles to manage my dotfiles repository, even blogged about it. https://www.matheusmoreira.com/articles/managing-dotfiles-with-make https://www.matheusmoreira.com/articles/managing-dotfiles-wi... The sanest way I've found to write makefiles is to think of it as a tool that maps input paths to output paths. When compiling a program, I want to map source/program.c to build/$(config)/objects/program.o so I write pattern rules that do just that. $(directories.build.objects)/%.o: $(directories.source)/%.c | directories $(strip $(CC) $(CFLAGS) -o $@ -c $<) Then I write make functions to convert paths in some tree to paths in another tree, which makes it easy to create lists of files which then match the pattern rules. source_to_object = $(patsubst $(directories.source)/%.c,$(directories.build.objects)/%.o,$(1)) These functions are then augmented by lots and lots of project specific variables to organize things... directories.source := source directories.build.root := build directories.build := $(directories.build.root)/$(config) directories.build.objects := $(directories.build)/objects sources.program := $(shell find $(directories.source) -type f) objects.program := $(call source_to_object,$(sources.program)) Then I add a real target which is supposed to get the makefile going. targets.program := $(directories.build)/program $(targets.program): $(objects.program) | directories $(strip $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^) Then I add a phony target for it. targets.phony += program program: $(targets.program) .PHONY: $(targets.phony) Then typing "make program" triggers the rule to make build/$(config)/program which depends on all the object files which will be linked into it which in turn is derived from a dynamically computed list of source files, and making those object files triggers the pattern rule which builds the entire thing. Using make without phony targets is insane. Without phony targets, I'd need to type things like "make build/aarch64/program" in order to get a build started. So I use phony targets for everything. Much easier to type "make program". It got to the point I created a phony-targets shell script which parses make's database output and processes it into a sort of help text for any given makefile's phony targets interface: https://github.com/matheusmoreira/.files/blob/master/~/.local/bin/phony-targets https://github.com/matheusmoreira/.files/blob/master/~/.loca...
- scrame 2y agoI'm not sure that using make to run npm is the win the author thinks it is.
- p4bl0 2y agoYes, Make is awesome. I use it for so many things. It's a great way to automate tasks. For example my personal website is built using a Makefile that calls bash scripts to rebuild the updated web pages, and I deploy it using a git push to my server and a git hook there that calls Make. However there are files that I don't want to put into the Git repository because they are blobs that may change often like PDFs of my teaching materials. It's okay, I have an "uploads" target in my Makefile that will upload only the modified PDFs to my server and this target is a dependency of the "deploy" target which does the git push so I don't even have to think about it. Also the updated PDFs for my courses materials are automatically put into my websites source tree by another Makefile that I use to manage and build my teaching materials and which let me either build the PDFs I use from my LaTeX sources or build from the same sources alternate versions of the materials for my students (without solutions to the lab sessions exercises for example) and automatically publish those to my local website version to be uploaded whenever I want to deploy the updated website. It's kind of Makefiles all the way down. I like Makefiles! =)
- hamdouni 2y agoI really was surprised that the main concern in the comments here was about the missing .PHONY. And kudos for this article that gives a point of view, then fixes the main HN concern ;-)
- multani 2y agoI like Makefiles too :) I use them more or less as a command runner, not often to build new targets based on sources (sometimes still). In particular, I like: * The ubiquity: it's easily available almost everywhere I touch and if not, it's usually a package install away. * The auto completion: I often define variables with default values at the top, but they can be both easily discoverable and their values can be changed just by typing `make VAR<tab>=foobar ...` * Chaining commands: make targets can be chained with `make target1 target2 target3 ...`. They will execute in the order specified. If I run this too often, I can usually create a 'ew make target that chains them all. Make is definitely not perfect and could be simpler. My biggest griefs are: * The abscons list of built-in variable. I can only remember a few of them ("dollar duck `$<` being my favorite), but they are not great to search in the docs and my brain is limited. * The "one line = one shell" is a bit of a PITA for the use case I have. I usually move more complicated script put of Makefiles. I thought I would have been bitten by the M4 indentation more often, but it's not really a problem for me anymore at this point (my editor _knows_ Makefiles since it's so ubiquitous and does the right thing) I have Just on my list of things to check, one day...
- Brian_K_White 2y ago"I usually move more complicated script put of Makefiles." We probably all should. But it is valuable a few different ways to have everything in a single file. Even though it's an ugly mess having a script within another script, with make variables mixed with shell variables mixed with make logic mixed with shell logic mixed with child proc output... the task at hand wants bits from both worlds at the same time, so the mess is also the value. You can rig up alternative answers for each of those, but really it just means make should probably have a more generic scripting language itself so you don't have to leave it to get something done, like how you don't have to leave awk to do non-record-processing stuff. People don't, but you can basically write anything you want in awk just like in shell or python. Or it should have less, be intentionally so limited that you can't embed any significant logic in the makefile and so it all lives somewhere else, either way, so long as it's all together, either all in the makefile or all in something the makefile runs.
- meling 2y agoI too like makefiles, but the sophistication that I’ve added to some mine are a bit much and I now wish I had explained, via comments, to myself what I did :-D That said, I wish there would come a new tool that could make things easier, that would gain wider appeal and adoption.
- MayeulC 2y agoI also like Makefiles, but some design choices are really dated. My latest gripe with it was its extremely poor support for filenames with spaces and special chars (;) in them: I just wanted to convert a flac library to MP3 files, it seemed well suited for the job at first glance. I don't know what tool could be a proper replacement. scons perhaps? ninja files are too verbose. just doesn't do dependencies, job control, etc.
- ivanjermakov 2y agoWhile these npm examples are easy to grasp, they do not reflect any strength of Make. With same success one could use package.json's script property, code would be pretty much identical.
- wonrax 2y agoExactly. If your project already uses npm and Node, why not just use npm scripts for trivial tasks like this? Adding an extra dependency (Make) to build a project doesn't make sense. Even if your project needs a more sophisticated build system that requires caching builds and managing conditional dependencies, turborepo and the like offer even better support for javascript codebases out of the box. Turborepo can be a workspace dependency so technically you don't even have to manually and separately install a build system to build your project.
- sl4v 2y agoAs a guy who doesn’t write code, but reads a lot of it, Makefiles have always been a PITA to deal with. But less so than cmake or other build systems.
- ris 2y agoMakefiles become a horrific technology the minute dynamic variables and the ensuing escaping games get involved. Massive footgun. Bash is already a fairly bonkers language (think from a junior engineer's perspective) and Makefiles so frequently compound that by an order of magnitude.
- regularfry 2y agoIt doesn't help that it's easy to trip over something where the right answer is "use the more recent version with the feature which fixes that" but because everyone's so used to make being ancient tech, they don't think it would be worth upgrading.
- dankobgd 2y agonobody likes makefile
- alkonaut 2y agoMake files: it’s a good build system in the same sense C is a good programming language. It’s also a terrible build system for the same reasons C is a terrible programming language.
- jll29 2y agoIt funny that make evokes such fierce arguments, almost like the semi-religious vi-vs-emacs wars of old. I agree fully with the OP, in particular I find it smart that he wraps anything in a top-level makefile, even if other, more sophisticated build tools are used. The advantage is standardization, not having to remember anything and to know that if you wrote it, you will just be able to type "make" and it will work. Let's say a C person wants to compile a Rust project, they would not have to look up how cargo works, but could simply type "make" (or "gmake"; I don't use GNU specifics, but try to be POSIX compliant, even if it is certainly true that almost 100% of makes are gmakes). Thanks for proposing the use of the timeless "make" as a sort of top-level build system driver; this will probably still work in 250 years.
- cantSpellSober 2y agoIt's funny such a simple title inspired a flamewar. The article itself is an insanely simple use case for make (that uses gulp in 2024?) that clearly no one read.
- zkldi 2y agocargo is a bad example as it's universally `cargo build`. Make on its own is great but most of the time I've worked with C projects it's been cmake/autotools + global pkg installs, which you Do have to frequently look up.
- mediumsmart 2y agoI vote to use make modestly and to release the candidate
- molszanski 2y agoHere is Makefile "starter" I use: https://github.com/awinecki/magicfile https://github.com/awinecki/magicfile People call this "self-documenting makefile". It migrated with me from company to company, from project to projects. Through node, php, aws, docker, server management, cert updates, file processing and many many more.
- dangsux 2y ago[dead]
- 1337shadow 2y agoAnd ... there is no make test
- lynx23 2y agoHmm, same here. Make is my go-to toll for writing top-level instructions on how to deal with a collection of files. (re)constructing data files (including dependency tracking of course), compiling files (implicit rules are great!) and all sort of things needing to go into the toplevel directory of a project. Its easy to write, almost no syntax overhead. Compare that to ant :-) And it can get arbitrarily complex if you need it to be. Projects like buildroot are a wonderful example of how much you can actually do with Makefiles, while staying relatively readable... Sure, if you are working in big tech, your code base is likely so large now that you want ninja or some other modern build system. But for the rest of us, Make is really sufficient and actually great value for the money...
- oneeyedpigeon 2y agoYup, I was sat here thinking "anyone complaining about Make really should go away and try using ant, then return here in a few hours licking their wounds, repentant".
- smitty1e 2y agoTake https://linuxfromscratch.org/ https://linuxfromscratch.org/ for a spin and see that Makefiles work fine; last a long time.
- gcarvalho 2y agoMany responses suggesting a simple bash script instead. One reason I like make is that I can just tab-autocomplete targets. No extra setup needed. If you’re using a single entry point script (e.g. do.sh) and handling $1 you don’t get that for free. And the moment you need to make your entry point script aware of “B requires A” then you’re going to half-bake something similar to make, anyway. Here’s [1] my ~80 line version for Python projects (micromamba + uv) which I’ve been pretty happy with. [1] https://github.com/giovannipcarvalho/micromamba.mk https://github.com/giovannipcarvalho/micromamba.mk
- deepsun 2y ago> tab-autocomplete bin/ also perfectly tab-autocompletes, don't understand what you mean. If you're having only one bin/do.sh script -- you're doing it wrong. And if you know $1 can only be either "prod" or "dev", then create scripts build-prod.sh and build-dev.sh. Simpler, installed on all systems, no quirky syntax with tabs and phonys.
- donatj 2y agoI use Make fairly similarly. One fairly major footgun the author is not avoiding is their targets will break if files or folders of the same name exist. You should always .PHONY any non-file targets though lest you want your build to break suddenly and confusingly when you add a folder named say "build" in the authors case. Make checks file modification dates by default to see if it can eliminate steps. Setting a target as a .PHONY indicates it's a "fake target" as in a command set to be run and not a file to be written. Makes default assumption is it's being ran to "make a file" so "make foo.html" or such. Here's a very simple example https://github.com/donatj/force-color.org/blob/dev/Makefile https://github.com/donatj/force-color.org/blob/dev/Makefile
- anymouse123456 2y agoHere are some things I love about Make: * Already installed * Does not require some random runtime * Does not require some random runtime version * Same build system for (nearly) all languages * The dependency tree can be constructed incrementally from simple, composable, verifiable building blocks * Incremental builds are trivial * Task bodies are (basically) shell scripts * Isolated complexity can be moved to external shell scripts and called from a task body * Unlike fad-language-build-systems learning is amortized across decades * It's not CMake
- xonix 2y agoYou could also like my tool: https://makesure.dev/ https://makesure.dev/ https://maximullaris.com/makesure-vs-make.html https://maximullaris.com/makesure-vs-make.html https://maximullaris.com/makesure-vs-just.html https://maximullaris.com/makesure-vs-just.html
- orbisvicis 2y agoConceptually I sometimes feel that install and clean targets should have prerequisites. The problem for clean is that make's only condition for evaluating a rule is "a prerequisite has a newer modification time" and not "the prerequisite exists". As for install, because each rule can only have a single target, the installation of each individual file would have to be a separate rule. As install destinations are usually subject to side effects (excluding nix or guix), there is usually a need to force an installation even if the prerequisites haven't been updated. And installations often require additional postprocessing that can't be captured by individual rules. So to reconsider, perhaps it makes little sense to for the install rule to have prerequisites. And forcing make to check the clean rules prerequisites might actually hamper performance, as opposed to a direct `rm`.
- sethammons 2y agoHere is a quality of life one for me (if you copy pasta, remember to switch spaces to tabs). When you type `make targets`, you get a list of available targets. Kinda like `just --list`. .PHONY: targets targets: @make -qp | awk -F: '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$$)/ {split($$1,A,/ /);for(i in A)print A[i]}' | sort
- desdenova 2y agoIf you like Makefiles to run random tasks, and insist on not using the proper syntax for that, use `just` instead. It uses the same syntax, but actually for running tasks and not producing files, so you don't need .PHONY statements.
- deleted 2y ago[deleted]
- shepherdjerred 2y agoMany have mentioned just, but I'm a much bigger fan of Earthly [0]. It allows you to write something similar to a Makefile, but everything runs in Docker. This gets you isolated builds with parallelism and caching built-in. I've found it to be great especially for small to medium projects. For some examples, I use it to publish my personal site/blog [1] and to build a C/C++/Fortran/Rust cross-compiler targeting macOS [2]. [0]: https://earthly.dev/ https://earthly.dev/ [1]: https://github.com/shepherdjerred/sjer.red/blob/main/Earthfile https://github.com/shepherdjerred/sjer.red/blob/main/Earthfi... [2]: https://github.com/shepherdjerred/macos-cross-compiler/blob/main/Earthfile https://github.com/shepherdjerred/macos-cross-compiler/blob/...
- ccorcos 2y agoI don’t get why you’d use make just to call an NPM script in your package.json
- raydev 2y agoPeople in this thread have already listed the terrible terrible things about make. It succeeds because it's simply everywhere. And rather than attempt to improve, people romanticize the good enough solution. I dream of a world where a "lessons learned make" is installed everywhere. How do we get that to happen?
- alextingle 2y agoFirst you have to appreciate that Make isn't as bad as you think it is. If you want to improve on it, then first you will really have to understand it. That's the pitfall that so many would-be build system authors fall into: _Holy crap, why is it so complicated? Surely something simpler would do the job... [YEARS LATER]... Shit, my build system is so ugly because of all those damn real-world edge cases I've had to fold into it._