TExcelTAD
Home › GNU Make & Ninja › The Build Log & -d explain

The Build Log & -d explain

Ninja keeps two files next to build.ninja: .ninja_deps (discovered header dependencies, two lessons back) and .ninja_log, a record of every command it ran. The log is what lets Ninja do something Make can't: rebuild an output because the command that produces it changed, even though no file did.

What's in .ninja_log

It's a tab-separated text file with one line per output, appended after each command finishes:

Field Meaning
1, 2 start and end time of the command, in milliseconds since the build started
3 the output's modification time (as Ninja recorded it)
4 the output path
5 a hash of the full command line

The times differ on every run, so this session prints only the stable fields:

bash · try it yourself
mkdir -p log && cd log
printf 'int main(void) { return 0; }\n' > main.c
printf 'int util(void) { return 1; }\n' > util.c
cat > build.ninja <<'NJ'
cflags = -O2
rule cc
  command = gcc $cflags -c $in -o $out
  description = CC $out
rule link
  command = gcc $in -o $out
  description = LINK $out
build main.o: cc main.c
build util.o: cc util.c
build app: link main.o util.o
NJ
ninja -j1 >/dev/null
head -1 .ninja_log
awk -F'\t' 'NR > 1 { print NF " fields:", $4, $5 }' .ninja_log
# ninja log v7
5 fields: main.o 2a4fbd0c86a9cf16
5 fields: util.o 5f33a6bd5fa75f69
5 fields: app 2a0e3e6e4c96d65b

The hash depends only on the command text, so it's the same on every machine that runs the same command; the paths, flags and compiler name are all part of it.

-d explain: why does this run?

ninja -d explain prints the reason before every command it runs. It's the Ninja counterpart of make --trace. The reasons include nanosecond time stamps, which this session strips with sed:

bash · try it yourself
mkdir -p log && cd log
printf 'int main(void) { return 0; }\n' > main.c
printf 'int util(void) { return 1; }\n' > util.c
printf 'cflags = -O2\nrule cc\n  command = gcc $cflags -c $in -o $out\n  description = CC $out\nrule link\n  command = gcc $in -o $out\n  description = LINK $out\nbuild main.o: cc main.c\nbuild util.o: cc util.c\nbuild app: link main.o util.o\n' > build.ninja
explain() { ninja -j1 -d explain 2>&1 | sed -E 's/ \([0-9]+ vs [0-9]+\)//'; }
echo "== first build"; explain
echo "== edit util.c"; sleep 1 && touch util.c; explain
echo "== change cflags"; sed -i 's/^cflags = -O2/cflags = -O0 -g/' build.ninja; explain
echo "== delete an output"; rm main.o; explain
== first build
ninja explain: output main.o doesn't exist
[1/3] CC main.o
ninja explain: output util.o doesn't exist
[2/3] CC util.o
ninja explain: main.o is dirty
ninja explain: util.o is dirty
[3/3] LINK app
== edit util.c
ninja explain: output util.o older than most recent input util.c
[1/2] CC util.o
ninja explain: util.o is dirty
[2/2] LINK app
== change cflags
ninja explain: command line changed for main.o
[1/3] CC main.o
ninja explain: command line changed for util.o
[2/3] CC util.o
ninja explain: main.o is dirty
ninja explain: util.o is dirty
[3/3] LINK app
== delete an output
ninja explain: output main.o doesn't exist
[1/2] CC main.o
ninja explain: main.o is dirty
[2/2] LINK app

The third case is the important one: switching from -O2 to -O0 -g rebuilt everything that uses that command, automatically. In Make, the same edit to a Makefile variable leaves stale -O2 objects in place until you make clean:

bash · try it yourself
mkdir -p mk && cd mk
printf 'int main(void) { return 0; }\n' > main.c
printf 'CFLAGS = -O2\napp: main.o\n\t$(CC) $^ -o $@\n%%.o: %%.c\n\t$(CC) $(CFLAGS) -c 
lt; -o $@\n' > Makefile make -j1 sed -i 's/^CFLAGS = -O2/CFLAGS = -O0 -g/' Makefile make -j1
cc -O2 -c main.c -o main.o
cc main.o -o app
make: 'app' is up to date.

(Some Makefiles fake this by making every object depend on the Makefile itself, or on a stamp file holding the flags. That works, but it rebuilds everything for unrelated edits too.)

restat: stop when an output didn't really change

Some commands often leave their output unchanged: a code generator run on an edited input that produces the same code, or a "write the file only if its content differs" step. With restat = 1 on the rule, Ninja checks the output's time stamp again after the command, and if it didn't change, it treats everything downstream as clean:

bash · try it yourself
mkdir -p restat && cd restat
printf 'apple\nbanana\n' > words.txt
cat > update-if-changed.sh <<'SH'
#!/bin/sh
# usage: update-if-changed.sh SRC OUT  -> write OUT only if the content differs
sort "$1" > "$2.tmp"
if cmp -s "$2.tmp" "$2"; then rm "$2.tmp"; else mv "$2.tmp" "$2"; fi
SH
chmod +x update-if-changed.sh
cat > build.ninja <<'NJ'
rule sortwords
  command = ./update-if-changed.sh $in $out
  description = SORT $out
  restat = 1
rule count
  command = wc -l < $in > $out
  description = COUNT $out
build sorted.txt: sortwords words.txt
build count.txt: count sorted.txt
NJ
ninja -j1
echo "--- reorder the input (same words):"
sleep 1 && printf 'banana\napple\n' > words.txt
ninja -j1 -d explain 2>&1 | sed -E 's/ \([0-9]+ vs [0-9]+\)//'
ninja -j1
[1/2] SORT sorted.txt
[2/2] COUNT count.txt
--- reorder the input (same words):
ninja explain: recorded mtime of sorted.txt older than most recent input words.txt
[1/2] SORT sorted.txt
ninja: no work to do.

SORT ran ([1/2]: Ninja had planned two steps), produced identical content and didn't touch sorted.txt; thanks to restat, the planned COUNT step was dropped and the build ended there. Note the wording of the reason, recorded mtime of sorted.txt: for restat rules Ninja compares against the time it stored in the log, not only the file on disk. The next run still has nothing to do, because the log now records that sorted.txt is up to date with respect to words.txt. CMake uses restat for its own re-run checks and for custom commands with BYPRODUCTS.

Using the log for timing

Because each line holds a start and end time, .ninja_log doubles as a profile of the last build. A one-liner lists the slowest steps (the numbers vary from run to run, so this one isn't executed here):

bash
awk -F'\t' 'NR > 1 { printf "%6d ms  %s\n", $2 - $1, $4 }' .ninja_log | sort -rn | head

The open-source ninjatracing script converts the same log into a Chrome trace (chrome://tracing or ui.perfetto.dev) that shows every job on a timeline, which makes the critical path and idle cores visible. The Build Performance course uses it.

Test yourself

You change a compiler flag in build.ninja but no source file. What does Ninja do?

  1. Nothing, all outputs are newer than their inputs
  2. Rebuilds the outputs whose command line changed, detected through the command hash in .ninja_log
  3. Deletes the build directory
  4. Asks you to run ninja -t clean

What does restat = 1 on a rule do?

  1. Re-runs the command twice
  2. After the command, re-checks the output's time stamp and skips downstream work if it didn't change
  3. Resets the build log
  4. Restarts Ninja

Which field of .ninja_log lets Ninja detect a changed command?

  1. The start time
  2. The output's mtime
  3. The command hash (5th field)
  4. The line number