4 ms·
No one wants to manually do dependency management in even a moderately sized project. I really haven't found an ideal way to have these -MM -MT flags integrated
by kccqzy 9y ago
No one wants to manually do dependency management in even a moderately sized project. I really haven't found an ideal way to have these -MM -MT flags integrated into Makefiles; I've tried having an awk script automatically modify the Makefile as the build is happening, but of course the updated dependencies will only work for later builds, so it's only good for updating the dependencies. Any other approaches HNers used and really liked?
- erlehmann_ 9y agoThere exists DJB's redo approach [0], which i implemented [1], where dependencies and non-existence dependencies are only recorded after the build. A typical dofile is a shell script, so you do not need to learn another language. Targets also automatically depend on their own build rules (I have seen such a thing only in makefiles authored by DJB). I wrote a blog post to show how to integrate dependency output for both dependency and non-existence dependency generation [2]. The game “Liberation Circuit” can be built with my redo implementation; you can output a dependency graph usable with Graphviz [4] using “redo-dot”. There is only one other redo implementation that I would recommend, the one from Jonathan de Boyne Pollard [5], who rightly notices that compilers should output information about non-existence dependencies [6]. I would not recommend the redo implementation from Avery Pennarun [7], which is often referenced (and introduced me to the concept), mainly because it is not implemented well: It manages to be both larger and slower than my shell script implementation, yet the documentation says this about the sqlite dependency (classic case of premature optimization): > I don't think we can reach the performance we want with dependency/build/lock information stored in plain text files [0] http://cr.yp.to/redo.html http://cr.yp.to/redo.html [1] http://news.dieweltistgarnichtso.net/bin/redo-sh.html http://news.dieweltistgarnichtso.net/bin/redo-sh.html [2] http://news.dieweltistgarnichtso.net/posts/redo-gcc-automatic-dependencies.html http://news.dieweltistgarnichtso.net/posts/redo-gcc-automati... [3] https://github.com/linleyh/liberation-circuit https://github.com/linleyh/liberation-circuit [4] https://en.wikipedia.org/wiki/Graphviz https://en.wikipedia.org/wiki/Graphviz [5] http://jdebp.eu./Softwares/redo/ http://jdebp.eu./Softwares/redo/ [6] http://jdebp.eu./FGA/introduction-to-redo.html#CompilerDeficiencies http://jdebp.eu./FGA/introduction-to-redo.html#CompilerDefic... [7] https://github.com/apenwarr/redo https://github.com/apenwarr/redo
- weeber 9y agoSimply add: -include $(OBJS:%.o=%.d) in your makefile (with -MMD in CFLAGS).
- rossy 9y agoThis is what I do too, and it seems perfect to me for adding set-and-forget dependency awareness to an ordinary Makefile. The first build with a new .c file will work. Its .d file won't exist, but the -include directive silently ignores this because of the - prefix, and it will be built anyway, because its corresponding .o doesn't exist either. Subsequent builds will use the dependency information in the .d. Also consider adding -MP to CFLAGS to prevent errors if you delete a .h file.
- chubot 9y agoDon't you need a pattern rule for %.d? GNU Make doesn't seem to come with this rule. There is a profoundly ugly example here: https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html https://www.gnu.org/software/make/manual/html_node/Automatic... %.d: %.c @set -e; rm -f $@; \ $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ rm -f $@.$$$$ I copied it into a running example here for anyone curious: https://github.com/oilshell/blog-code/tree/master/dotd https://github.com/oilshell/blog-code/tree/master/dotd
- rossy 9y agoIf you have -MD or -MMD in your CFLAGS, GCC (and Clang) will generate the .d files during compilation without you having to add anything else to the Makefile, and without requiring ugly sed magic.
- chubot 9y agoI just tried this, and it seems wrong on two counts: 1) The sed magic is required for adding the dependencies of the .d file itself. For example, if the timestamp of your header changes, it may have acquired new dependencies, and a new .d file has to be generated BEFORE make determines if the .c file is out of date. See: https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html https://www.gnu.org/software/make/manual/html_node/Automatic... The purpose of the sed command is to translate (for example): main.o : main.c defs.h into: main.o main.d : main.c defs.h The first line isn't correct because the .d file itself has no dependencies. 2) By the time you are compiling, it's too late to generate .d. The .d files are there to determine IF you need to compile. EDIT: I am trying to generate a test case that shows this fails, but it seems to actually work. Hm yes I'm convinced it works, but I have to think about why. I guess one way of saying it is that the previous .d file is always correct. Hm.
- vvanders 9y agoUse Rust. Kinda sorta kidding, not-kidding. Usually I'd wrangle CMake into submission since I tend have diverse targets(Win32, Android, Linux, OSX, etc) however Rust(and moreso Cargo) makes this a pleasure to do. Even native dependencies are straightforward unless there's some sort of build-fu going on(like luajit). You can just pull in the GCC crate, it'll shell out to clang/gcc/msvc and since you're using it in a build.rs you can configure it however you want since it's just another Rust program.