Field Notes: Ninja & Generated Builds
Ninja itself rarely goes wrong; the trouble comes from what it's given and how people use it. These notes are about working with generated build.ninja files day to day, in CI, and on the rare occasions you write one.
1. Never edit a generated build.ninja
A build.ninja produced by CMake or Meson is an output. The next time any CMakeLists.txt or meson.build changes, the generator rule rewrites it and your edit is gone, usually without anyone noticing. Change the input instead (a target property, an option, a toolchain file). If you need to see the effect of a flag quickly, override it on the generator's command line (cmake -DCMAKE_C_FLAGS=…, meson configure -Dc_args=…) and let Ninja's command log rebuild exactly what changed.
2. In CI, keep going: -k 0
By default Ninja stops scheduling new work after the first failure (-k 1). Locally that's what you want; in CI it means fixing one error per pipeline run. -k 0 keeps going and reports every independent failure:
mkdir -p k && cd k
printf 'int a(void) { return x; }\n' > a.c
printf 'int b(void) { return y; }\n' > b.c
printf 'int c(void) { return 0; }\n' > c.c
printf 'rule cc\n command = gcc -c $in -o $out\n description = CC $out\nbuild a.o: cc a.c\nbuild b.o: cc b.c\nbuild c.o: cc c.c\n' > build.ninja
ninja -j1 2>&1 | grep -E '^\[|^FAILED|^ninja:'
echo "=== -k 0"
ninja -j1 -k 0 2>&1 | grep -E '^\[|^FAILED|^ninja:'[1/3] CC a.o
FAILED: [code=1] a.o
ninja: build stopped: subcommand failed.
=== -k 0
[1/3] CC a.o
FAILED: [code=1] a.o
[2/3] CC b.o
FAILED: [code=1] b.o
[3/3] CC c.o
ninja: build stopped: cannot make progress due to previous errors.
With -k 0 Ninja tried every edge it could and ends with a different message (cannot make progress due to previous errors), but the exit status is still non-zero. The FAILED: line names the output and is followed by the full command and the compiler's messages (filtered out here). CI log parsers look for exactly that line. make -k is the Make equivalent.
3. Declare every output a command writes
A command that writes two files but declares one leaves Ninja blind to the other. Here a generator writes gen.c and gen.h, but only gen.c is declared. Delete gen.h (a git clean, a tidy-up script) and nothing knows how to bring it back:
mkdir -p outs && cd outs
cat > gen.sh <<'SH'
#!/bin/sh
echo '#define LIMIT 10' > gen.h
echo 'int limit(void) { return 10; }' > gen.c
SH
chmod +x gen.sh
printf '#include "gen.h"\nint limit(void);\nint main(void) { return limit() - LIMIT; }\n' > main.c
for decl in "gen.c" "gen.c | gen.h"; do
rm -f gen.c gen.h *.o .ninja_*
cat > build.ninja <<NJ
rule gen
command = ./gen.sh
description = GEN
rule cc
command = gcc -MMD -MF \$out.d -c \$in -o \$out
depfile = \$out.d
deps = gcc
description = CC \$out
build $decl: gen gen.sh
build main.o: cc main.c || gen.c
build gen.o: cc gen.c
NJ
echo "=== build $decl: gen gen.sh"
ninja -j1 >/dev/null && rm gen.h
ninja -j1 2>&1 | grep -E '^\[|^ninja:|error:'
done=== build gen.c: gen gen.sh
[1/1] CC main.o
main.c:1:10: fatal error: gen.h: No such file or directory
ninja: build stopped: subcommand failed.
=== build gen.c | gen.h: gen gen.sh
[1/3] GEN
[2/3] CC main.o
[3/3] CC gen.o
With gen.h declared as an implicit output (| gen.h after the outputs), Ninja knows that its producer must run again, and then recompiles both objects that use the regenerated files. (restat = 1 on the generator rule plus a write-if-changed script would spare gen.o; see The Build Log.) CMake's equivalent is listing every file in add_custom_command(OUTPUT …) or BYPRODUCTS. ninja -t missingdeps finds the related mistake of a consumer that uses a generated file without a dependency path to its producer.
4. Pools and -l before a bigger machine
If builds start failing randomly with "Killed" or cc1plus: out of memory, or the machine swaps, the problem is too many heavy jobs at once, not too few cores. Put links (and other memory hogs like LTO steps) in a pool with a small depth (Rules, Build Statements & Variables), and on shared CI machines add -l N so Ninja stops starting jobs when the load average is high. The scheduler model shows the arithmetic.
5. Customise the status line, not the output
NINJA_STATUS sets the progress prefix. Placeholders include %f (finished), %t (total), %r (running), %u (remaining), %p (percentage) and %e (elapsed seconds). It's handy in CI logs where a percentage is easier to read:
mkdir -p st && cd st
for f in a b c d; do printf 'int %s(void) { return 0; }\n' $f > $f.c; done
printf 'rule cc\n command = gcc -c $in -o $out\n description = CC $out\n' > build.ninja
for f in a b c d; do printf 'build %s.o: cc %s.c\n' $f $f >> build.ninja; done
NINJA_STATUS='[%p %f/%t, %u left] ' ninja -j1[ 25% 1/4, 3 left] CC a.o
[ 50% 2/4, 2 left] CC b.o
[ 75% 3/4, 1 left] CC c.o
[100% 4/4, 0 left] CC d.o
Leave -v off in normal CI runs (logs get huge); turn it on, or run ninja -t commands, when you need the exact command of a failing step. Ninja prints the full command of a failed edge anyway.
6. Smaller habits
- Run Ninja on a build directory:
ninja -C build.ninja: error: loading 'build.ninja': No such file or directoryalmost always means you're in the source tree. cmake --build buildandninja -C buildare interchangeable for a Ninja tree; scripts that usecmake --buildkeep working if someone switches generators.- To start over,
rm -rf buildand regenerate.ninja -t cleankeeps the configuration but removes outputs;ninja -t cleandeadremoves outputs that the current manifest no longer produces (useful after renaming targets). - Don't share one build directory between two different Ninja versions or two machines over a network drive; the logs and time stamps don't travel well.
- Ninja's
.ninja_loggrows over time; Ninja compacts it automatically, andninja -t recompactdoes it on demand. - When something looks wrong, ask before you guess:
ninja -d explainfor "why did it rebuild",ninja -t queryfor "what depends on this",ninja -t commandsfor "what exactly runs".
Test yourself
Why use ninja -k 0 in CI?
- It makes builds faster
- It keeps building after failures, so one run reports every independent error
- It disables the cache
- It hides warnings
A generator writes gen.c and gen.h, but only gen.c is declared. What breaks?
- Nothing
- If
gen.hdisappears, Ninja doesn't know to re-run the generator, and compiles fail - Ninja refuses to load the file
- gen.c is rebuilt every time
You edit a generated build.ninja to add a flag. What happens later?
- The edit is kept forever
- The next regeneration (after any CMakeLists.txt change) silently overwrites it
- Ninja rejects edited files
- CMake merges it in