Ninja: A Small, Fast Executor
Ninja was written in 2011–2012 by Evan Martin for Chromium, whose Make-based build could take many seconds just to decide what needed rebuilding after a one-line change. Its design follows from one idea: the build description is generated by another program, so the executor can be dumb and fast. All the decisions a Makefile makes with variables, functions and conditionals happen earlier, in CMake, Meson, GN or a script. What reaches Ninja is a flat, explicit list of commands.
What Ninja leaves out, on purpose
| Make has | Ninja instead |
|---|---|
functions, conditionals, $(shell), include with remaking |
nothing: compute it in the generator |
| implicit and pattern rules | every output gets its own build statement |
| built-in rules and variables | none; rules are named and defined in the file |
-j1 by default |
parallel by default: number of CPUs + 2 |
recipes of many lines, each run by /bin/sh |
one command line per rule (run by /bin/sh -c on Unix, started directly on Windows) |
| rebuild if inputs are newer | rebuild if inputs are newer or the command changed |
header dependencies via included .d makefiles |
depfile / deps = gcc, stored in a compact binary log |
The result: Ninja starts fast, does a no-op build of a huge project in well under a second, and its output is a tidy [done/total] status line instead of a wall of commands.
A first build.ninja
A build.ninja has rules (named command templates) and build statements (outputs, a rule, inputs). Inside a rule, $in and $out are the build statement's inputs and outputs, space-separated:
mkdir -p rect && cd rect
printf 'int area(int w, int h);\n' > geometry.h
printf '#include "geometry.h"\nint area(int w, int h) { return w * h; }\n' > geometry.c
printf '#include <stdio.h>\n#include "geometry.h"\nint main(void) { printf("area = %%d\\n", area(6, 7)); return 0; }\n' > main.c
cat > build.ninja <<'NJ'
cflags = -Wall -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 geometry.o: cc geometry.c
build rect: link main.o geometry.o
NJ
ninja -j1
./rect
echo "--- again:"
ninja -j1
echo "--- after editing geometry.c, with -v for full commands:"
sleep 1 && touch geometry.c
ninja -j1 -v[1/3] CC main.o
[2/3] CC geometry.o
[3/3] LINK rect
area = 42
--- again:
ninja: no work to do.
--- after editing geometry.c, with -v for full commands:
[1/2] gcc -Wall -O2 -c geometry.c -o geometry.o
[2/2] gcc main.o geometry.o -o rect
Ninja rebuilt geometry.o and relinked; with -v the status line shows each full command instead of its description. Like Make, it compared time stamps; unlike Make, it would also have rebuilt if we had only changed cflags (more on that in The Build Log & -d explain).
Notice what the file does not say: nothing about headers (we'll add that with depfile in Header Dependencies in Ninja), and no default target. Without a default statement, ninja builds every output that isn't an input to something else (here only rect).
Ninja's command line
| Command | Does |
|---|---|
ninja |
build the default targets (from build.ninja in the current directory) |
ninja target… |
build specific outputs |
ninja -C dir |
change to dir first (the usual way to build a CMake or Meson build tree) |
ninja -f file |
use another build file |
ninja -j N / -k N / -l N |
parallel jobs / keep going until N failures (0 = never stop) / load limit |
ninja -n |
dry run |
ninja -v |
print full command lines |
ninja -d explain |
say why each command runs (The Build Log & -d explain) |
ninja -t tool |
built-in tools: targets, query, graph, compdb, clean… (Ninja's Built-in Tools) |
Should you hand-write build.ninja?
For real projects, no: that's not what it's for. A build.ninja repeats itself (one build line per object), has no way to glob for sources, and can't adapt to the platform. Use CMake (cmake -G Ninja) or Meson, which generate it (What CMake & Meson Generate). Hand-writing one is still worth doing once or twice, as this course does, because it's the clearest way to understand the files those tools produce. Small generated cases are fine too: a 30-line Python script that writes a build.ninja for a code generator or an asset pipeline is a common, sensible pattern.
Test yourself
What does Ninja build when you run ninja and the file has no default statement?
- Nothing
- The first build statement
- Every output that isn't used as an input elsewhere (the graph's roots)
- Everything, including intermediate files only
Which of these does Ninja's language deliberately NOT have?
- Variables
- Rules
- Conditionals and functions like
$(wildcard) - Build statements
How many jobs does Ninja run by default?
- 1
- Number of CPUs + 2
- Unlimited
- 4