8 ms·
Don'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 i
by jart 2y ago
Don'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.
- wruza 2y agoProjects of much smaller sizes often have recursive convoluted makefiles.
- chipdart 2y ago> Projects of much smaller sizes often have recursive convoluted makefiles. You name any technology and anyone can enumerate dozens of projects that use it wrong.
- wruza 2y agoI’d walk that before talking. Take any complex makefile system and turn it into a single “not really difficult” makefile without sacrificing anything important. Wins this argument and helps those who “use it wrong”.
- ReleaseCandidat 2y agoAnd I can show you thousands of "Hello World"s that use GNU Autotools or CMake ;) But seriously: can I take a look at it (Soource + Makefile)?
- Drawde 2y agoThis is most likely what is being referenced: https://github.com/jart/cosmopolitan/blob/master/Makefile https://github.com/jart/cosmopolitan/blob/master/Makefile I like how the includes are separated and commented. Also if you weren't already familiar with their work you might be interested in giving this a read: https://justine.lol/ape.html https://justine.lol/ape.html
- chipdart 2y ago> Don't be discouraged by all the people in this thread saying you're using make wrong. Fully agree, and I would add that it's far better to adopt the right tool for the job, even if you are not an expert, than avoiding the criticisms from perfectionists by adopting the wrong tool for the job. Everyone needs to start from somewhere, and once the ball is rolling then incremental changes are easy to add. Great job!
- johnnyanmac 2y agoPeople who want to call me out would be a lot more productive pointing me to some guides instead of chastising me over an ancient framework who's best documentation has been lost to time. And whose best practices are locked behing proprietary codebases. Little tips here and there are nice, but that doesn't teach me the mentality of how to achitect a makefile
- instig007 2y ago> Little tips here and there are nice, but that doesn't teach me the mentality of how to achitect a makefile What exactly are you missing from the official manual? - https://www.gnu.org/software/make/manual/make.html https://www.gnu.org/software/make/manual/make.html - https://devdocs.io/gnu_make/ https://devdocs.io/gnu_make/
- johnnyanmac 2y agoarchitecture, best practices, and pitfalls. The things you get flamed for online but aren't conviniently on some man doc to understand. Structure generally means most manuals are not the first resouece a learner should try to learn from. Great to have on hand, but manuals are not structured like a textbook that builds upon concepts needed to productively work with the subject.
- chipdart 2y ago> People who want to call me out would be a lot more productive pointing me to some guides instead of chastising me over an ancient framework who's best documentation has been lost to time. Fully agree. Don't get discouraged, and keep it up!
- matheusmoreira 2y agoEvery makefile recipe should produce exactly one output: $@. The makefile as a whole produces an arbitrary number of outputs since rules can depend on other rules. This leads us to a neat rule of thumb for phony targets: any recipe that does not touch $@ and only $@ should have $@ marked as phony. I find that keeping track of phony targets with a list makes things much easier. phonies := phonies += something something: ./do-something phonies += something-else something-else: something ./do-something-else # touches $@ and thus does not need to be phony create-file: ./generate-some-output > $@ .PHONY: $(phonies)
- Brian_K_White 2y agoI don't know if it's actually saner than just normal phonys but man I like it. What does it get you other than the ability to print the list of all phonys?
- matheusmoreira 2y agoIt's mostly so I can immediately see which targets are phonies. Every phony has a line directly above it adding it to the list of phonies. When a makefile gets complex enough we need all the help we can get. I use phony targets so much I wrote a shell script to parse the makefile database dump into some sort of help text. It doesn't depend on that variable at all. https://github.com/matheusmoreira/.files/blob/master/~/.local/bin/phony-targets https://github.com/matheusmoreira/.files/blob/master/~/.loca... Prints output like: phony1 phony2 dependency1 dependency2
- 153957 2y agoIt is also possible to define `.PHONY` multiple times, so you can simplify this to: .PHONY: something something: ./do-something .PHONY: something-else something-else: something ./do-something-else create-file: ./generate-some-output > $@
- matheusmoreira 2y ago
- gbuk2013 2y agoHere is a slightly more complex example of a Makefile I use when spinning up a new TypeScript project (but I switch out to use pnpm these days): https://github.com/borisovg/node-ts-template/blob/main/Makefile https://github.com/borisovg/node-ts-template/blob/main/Makef... I still wouldn’t say it’s that complicated - you do need to know your way around the syntax a bit but it’s less challenging than getting all the other tooling working in the first place. :)
- orbisvicis 2y agoI didn't know you could redefine .PHONY like that and what... all the phony targets are accumulated into a list?
- gbuk2013 2y agoNot just .PHONY - you can do that for any target: https://www.gnu.org/software/make/manual/html_node/Multiple-Rules.html https://www.gnu.org/software/make/manual/html_node/Multiple-... “One file can be the target of several rules. All the prerequisites mentioned in all the rules are merged into one list of prerequisites for the target.”
- anymouse123456 2y agoI've been a happy make user for 20+ years across many, many projects and many languages. I've never had issues with the .PHONY task that seems to bother people so much. It's simple, readable, editable, composable and already installed everywhere. It does what it says on the tin and not much else. FWIW, I also wrap up whatever fad (or nightmare) build system people use in other projects when I need to deal with them.
- jarule 2y agoIt's simple, readable, editable, composable I'll eat crow if wrong, but I'm guessing I know more about GNU make than you do. It is none of the four things you claim. Also, people who say "on the tin" need a good ass-kicking.
- anymouse123456 2y agoLol. Disagree, but can't argue with any of that.