Header Dependencies in Ninja
Ninja has the same problem as Make: build main.o: cc main.c says nothing about the headers main.c includes. It uses the same solution, the compiler's -MMD dependency file, but handles it better: instead of including makefile fragments, Ninja reads the file once, stores the list in its own binary database and deletes the file.
depfile: read the compiler's .d file
Two rule variables do it:
| Variable | Meaning |
|---|---|
depfile = $out.d |
after the command succeeds, read this Makefile-syntax dependency file |
deps = gcc |
store what it says in .ninja_deps and delete the .d file (use deps = msvc with cl /showIncludes) |
Without deps, Ninja re-reads every .d file at startup, which is slower for big projects and leaves them lying around. With deps = gcc everything goes into one compact log. GCC and Clang need -MMD -MF $out.d (or -MD) on the command line to write the file.
mkdir -p hdr && cd hdr
printf '#define GREETING "hello"\n' > config.h
printf 'int area(int w, int h);\n' > shapes.h
printf '#include "shapes.h"\nint area(int w, int h) { return w * h; }\n' > shapes.c
printf '#include <stdio.h>\n#include "config.h"\n#include "shapes.h"\nint main(void) { printf("%%s %%d\\n", GREETING, area(2, 3)); return 0; }\n' > main.c
cat > build.ninja <<'NJ'
rule cc
command = gcc -MMD -MF $out.d -c $in -o $out
depfile = $out.d
deps = gcc
description = CC $out
rule link
command = gcc $in -o $out
description = LINK $out
build main.o: cc main.c
build shapes.o: cc shapes.c
build app: link main.o shapes.o
NJ
ninja -j1 && ./app
ls *.d 2>/dev/null || echo "no .d files left: Ninja absorbed them"
echo "--- edit config.h (only main.c includes it):"
sleep 1 && sed -i 's/hello/goodbye/' config.h
ninja -j1 && ./app
echo "--- touch shapes.h (both include it):"
sleep 1 && touch shapes.h
ninja -j1[1/3] CC main.o
[2/3] CC shapes.o
[3/3] LINK app
hello 6
no .d files left: Ninja absorbed them
--- edit config.h (only main.c includes it):
[1/2] CC main.o
[2/2] LINK app
goodbye 6
--- touch shapes.h (both include it):
[1/3] CC main.o
[2/3] CC shapes.o
[3/3] LINK app
What Ninja stored
ninja -t deps prints the stored dependencies for each output. The deps mtime it also prints is a nanosecond time stamp that changes on every run, so this session replaces it with …:
mkdir -p hdr && cd hdr
printf '#define GREETING "hello"\n' > config.h
printf '#include <stdio.h>\n#include "config.h"\nint main(void) { puts(GREETING); return 0; }\n' > main.c
printf 'rule cc\n command = gcc -MMD -MF $out.d -c $in -o $out\n depfile = $out.d\n deps = gcc\nbuild main.o: cc main.c\n' > build.ninja
ninja -j1 >/dev/null
ninja -t deps | sed -E 's/deps mtime [0-9]+/deps mtime …/'
echo "--- with -MD instead of -MMD, system headers too:"
sed -i 's/-MMD/-MD/' build.ninja
ninja -j1 >/dev/null
ninja -t deps main.o | grep -c '/usr/include/' | sed 's/$/ system headers recorded/'
ls -a | grep ninjamain.o: #deps 2, deps mtime … (VALID)
main.c
config.h
--- with -MD instead of -MMD, system headers too:
24 system headers recorded
.ninja_deps
.ninja_log
build.ninja
Changing -MMD to -MD changed the command, so Ninja rebuilt main.o without being asked (it remembers each command; more in the next lessons). Now <stdio.h> and the headers it pulls in are tracked too, which only matters if system headers can change under you.
A deleted header is fine
Make needs -MP so that a header removed from the project doesn't break the build. Ninja doesn't: a dependency recorded in .ninja_deps that no longer exists simply makes the output dirty, so it gets rebuilt, and the new compile records the new list:
mkdir -p gone && cd gone
printf '#define N 3\n' > config.h
printf '#include "config.h"\nint main(void) { return N - 3; }\n' > main.c
printf 'rule cc\n command = gcc -MMD -MF $out.d -c $in -o $out\n depfile = $out.d\n deps = gcc\n description = CC $out\nbuild main.o: cc main.c\n' > build.ninja
ninja -j1
rm config.h
printf 'int main(void) { return 0; }\n' > main.c
ninja -j1
ninja -t deps | grep -c config.h[1/1] CC main.o
[1/1] CC main.o
0
Generated headers
The deps log only exists after the first successful compile. If main.c includes a header that is generated by another build statement, the first build has no way to know it must generate it first. Tell Ninja explicitly with an order-only input (|| version.h) on the compile, or an implicit one if a change must force a rebuild. CMake does this for you; it's also what ninja -t missingdeps checks: it scans the deps log for generated files that some output uses without a declared path to them.
Test yourself
What does deps = gcc add on top of depfile = $out.d?
- It runs gcc twice
- Ninja stores the dependencies in
.ninja_depsand deletes the.dfiles, so startup doesn't have to parse them - It enables C++ support
- It disables header tracking
Which compiler flags make GCC write the dependency file that depfile reads?
-g -O2-MMD -MF $out.d(or-MD)-E -P-Wall
Why might a clean build fail to compile a file that includes a generated header, even with deps = gcc?
- Ninja can't handle headers
- The deps log is only filled after the first compile, so the generator edge must be declared (e.g. as an order-only input)
- Generated headers must be in /usr/include
deps = gccdeletes generated headers