Section 12.13

Ninja

Definition

Ninja is a fast build tool. It reads a file, normally build.ninja, that lists each output file, the input files it is made from and the command that makes it, and runs only the commands whose outputs are out of date, several at once. An output is out of date when it is missing, when an input was modified after it, or when its command has changed since the last build. Ninja files are meant to be written by a generator program such as CMake §12.12, not by hand.

The idea

Evan Martin wrote Ninja while working on the Chromium web browser, a project of over 30,000 source files. With the makefiles §12.14 then in use, a build took about ten seconds just to start after a change to one file; Ninja starts in under a second. Its manual explains the speed by simplicity: Ninja has no conditionals, no search paths and no built-in rules. A generator program such as CMake (a program that writes build files from a project description) makes every decision beforehand, and Ninja only compares modification times, recorded commands and the graph it was given.

Hence the central fact of this entry: Ninja is exactly as correct as its graph. A build file describes a directed acyclic graph §14.17, a set of arrows with no cycles, from input files to output files. If a command reads a file that the graph does not list among its inputs, Ninja cannot know that the output depends on it. When that file changes, Ninja reports that there is no work to do, and the old output survives.

How it works

A rule names a command template, for example rule cc followed by the indented line command = gcc -MD -MF $out.d -c $in -o $out, where $in stands for the inputs and $out for the outputs. A build statement applies a rule: build main.o: cc main.c says that main.o is made from main.c with rule cc. A build statement can list three kinds of input.

Headers are discovered during the build. With depfile = $out.d and deps = gcc in a rule, the compiler writes the list of headers it read (the compiler option §12.3 -MD), Ninja stores the list in its database .ninja_deps, and in the next build those headers count as implicit inputs.

When run, Ninja works in these steps.

  1. If build.ninja is itself the output of a build statement and is out of date, Ninja rebuilds and reloads it first. A generator such as CMake adds such a statement, so that a change to the project description runs the generator again.

  2. From the requested targets, or the default ones, Ninja walks the graph back to the source files, reading each file’s modification time and, from the log file .ninja_log, the command last used for each output.

  3. It marks an output dirty (out of date) when the output is missing; when one of its inputs is dirty; when the latest modification time among its explicit and implicit inputs, recorded headers included, is later than the output’s; or when its command differs from the recorded one.

  4. It runs the commands of dirty outputs in topological order §14.19, each after the commands that make its inputs, several at once; the option -j sets how many, and the default depends on the number of processors.

  5. After each command it updates .ninja_log and, for rules with deps, .ninja_deps. In a rule marked restat = 1, an output whose modification time the command left unchanged counts as not rebuilt, so the commands that depend on it may be skipped.

  6. By default it starts no new command after the first failure.

To find faults in a graph, ninja -d explain prints why each output is dirty, and ninja -t missingdeps (Ninja 1.11 and later) lists targets that use a generated file without any dependency path to the command that generates it.

Worked example

A code generator gen.py reads schema.json, whose name is written inside the script, and prints a header msg.h, which main.c includes. The build file says:

rule gen
command = python3 $in > $out
build msg.h: gen gen.py
rule cc
command = gcc -MD -MF $out.d -c $in -o $out
depfile = $out.d
deps = gcc
build main.o: cc main.c || msg.h

The order-only input makes sure that msg.h exists before the first compile; after it, the recorded header list makes msg.h an implicit input of main.o. The file schema.json appears nowhere in the build file.

Modification times, in seconds on one clock: gen.py 100, schema.json 150, main.c 160, msg.h 200, main.o 210. At time 300 the developer edits schema.json and runs ninja.

  1. msg.h: its only input, gen.py, has time 100, earlier than 200, and the command is unchanged, so msg.h is not dirty.

  2. main.o: its inputs are main.c (160) and, through the recorded headers, msg.h (200). The latest, 200, is earlier than 210, and no input is dirty, so main.o is not dirty.

  3. Ninja prints ninja: no work to do. The program is still built from the old schema.

The repair declares the real input: build msg.h: gen gen.py | schema.json. The latest input of msg.h is then max(100,300)=300\max(100, 300) = 300, later than 200, so gen.py runs again, finishing at, say, time 310. The latest input of main.o is now 310, later than 210, so main.c is compiled again. Building from scratch would also have given a correct program once, but the next change to schema.json would again be missed.

Stated precisely

Take a build statement ee with outputs OO. Let II be its explicit and implicit inputs together with the headers recorded for it in .ninja_deps, and write mtime(f)\mathrm{mtime}(f) for the modification time of file ff. Ninja runs the command of ee when at least one of these holds: (1) some output in OO does not exist; (2) some input in II is dirty, meaning that it is itself an output whose command will run; (3) maxi∈Imtime(i)>mtime(o)\max_{i \in I} \mathrm{mtime}(i) > \mathrm{mtime}(o) for some output o∈Oo \in O; (4) the command of ee differs from the one recorded in .ninja_log, unless the rule is marked generator, a flag for the rule that regenerates the build file. Order-only inputs play no part in these conditions. The manual gives further details for rules marked restat.

An incremental build, which reruns only dirty commands, produces the same outputs as a build from scratch under three assumptions.

  1. Complete graph: every file a command reads is in that command’s II, listed directly or recorded from a dependency file.

  2. Deterministic commands: a command given the same inputs produces the same output.

  3. Honest times: whenever a file’s content changes, its modification time becomes later than that of every output built from the old content.

Limits and common mistakes

Ninja tracks files, not meaning. Inputs it cannot see can change without a rebuild: a file opened by a name inside a script, an environment variable, the compiler program after an upgrade in place, a tool’s configuration file. Ninja does not compare contents either: saving an unchanged file causes rebuilds, and an input replaced by a file with an earlier time, for example one restored from an archive that keeps original times, does not look newer than its output.

A build from scratch hides a missing dependency once and does not repair it. Listing a file after || is correct only when a dependency file will report it after the first build, as for a generated header that a C file includes.

If nothing orders a generator before the commands that use its output, a build from scratch running several commands at once has a race condition §9.2: the compiler may run before the header exists, and whether the build fails depends on timing.

Ninja compares paths as written, so an absolute and a relative path to the same file are two different nodes. And a correct graph does not make a reproducible build §13.8: commands can still embed the date or the build directory’s path in their outputs.

Questions answered

Which missing dependency can let stale output survive a source change?

Any file that a build command reads but that the build graph does not list as an input of that command can let a stale output survive: when that file changes, the build tool sees no newer input and does not run the command again. Ninja, a build tool that reruns a command only when an output is missing, an input is newer than the output, or the command changed, is exactly as correct as the graph it is given. Typical missing dependencies are these.

Building from scratch hides the problem once. The lasting repair is to add the real input to the graph; ninja -d explain and ninja -t missingdeps help to find what is missing.

Related entries: CMake (§12.12), Make (§12.14), Transitive dependency (§10.43), Directed acyclic graph (DAG) (§14.17), Topological ordering (§14.19), Cache invalidation (§14.21), Hermetic build (§13.7). Further reading: [113].