TExcelTAD
Home › GNU Make & Ninja › Ninja's Built-in Tools

Ninja's Built-in Tools

Make's debugging help is a set of command-line flags. Ninja instead ships subtools, run as ninja -t NAME [args]. They answer questions about the build graph without building anything, and they work on any build.ninja, including the large ones CMake and Meson generate, which is where they are most useful.

Tool Answers
targets [all | depth N | rule R] which outputs exist (and by which rule)
query T… what builds T (inputs) and what T feeds into (outputs)
commands T… every command needed to build T, in order
inputs T… every file T depends on, transitively
compdb [rules…] a compile_commands.json for editors and linters
graph [T…] the graph in Graphviz dot format
deps [T…] header dependencies stored by deps = gcc (previous lesson)
clean [T…] / cleandead delete outputs / delete outputs no longer in the manifest
rules, recompact, restat, missingdeps, browse list rules; maintain the logs; find undeclared generated-file deps; a web UI (needs Python)

Every session below uses the same small project: a library with two objects, an app, and a header generated from a text file.

targets, query, commands, inputs

bash · try it yourself
mkdir -p tools && cd tools
printf '1.4.2\n' > VERSION
printf 'int add(int a, int b) { return a + b; }\n' > add.c
printf 'int mul(int a, int b) { return a * b; }\n' > mul.c
printf '#include <stdio.h>\n#include "version.h"\nint add(int, int); int mul(int, int);\nint main(void) { printf("v%%s %%d\\n", VERSION, mul(add(1, 2), 4)); return 0; }\n' > main.c
cat > build.ninja <<'NJ'
cflags = -O2
rule cc
  command = gcc $cflags -MMD -MF $out.d -c $in -o $out
  depfile = $out.d
  deps = gcc
  description = CC $out
rule ar
  command = rm -f $out && ar rcs $out $in
  description = AR $out
rule link
  command = gcc $in -o $out
  description = LINK $out
rule version
  command = printf '#define VERSION "%s"\n' $(cat $in) > $out
  description = GEN $out

build version.h: version VERSION
build add.o: cc add.c
build mul.o: cc mul.c
build libmath.a: ar add.o mul.o
build main.o: cc main.c || version.h
build app: link main.o libmath.a
build all: phony app
default all
NJ
echo "== targets (top level only)"; ninja -t targets depth 1
echo "== targets of rule cc";        ninja -t targets rule cc
echo "== query libmath.a";           ninja -t query libmath.a
echo "== commands for app";          ninja -t commands app
echo "== inputs of app";             ninja -t inputs app
== targets (top level only)
all: phony
== targets of rule cc
add.o
main.o
mul.o
== query libmath.a
libmath.a:
  input: ar
    add.o
    mul.o
  outputs:
    app
== commands for app
printf '#define VERSION "%s"\n' $(cat VERSION) > version.h
gcc -O2 -MMD -MF main.o.d -c main.c -o main.o
gcc -O2 -MMD -MF add.o.d -c add.c -o add.o
gcc -O2 -MMD -MF mul.o.d -c mul.c -o mul.o
rm -f libmath.a && ar rcs libmath.a add.o mul.o
gcc main.o libmath.a -o app
== inputs of app
VERSION
add.c
add.o
libmath.a
main.c
main.o
mul.c
mul.o
version.h

commands is the quickest way to see what a generated build really runs (or to reproduce one failing compile by hand); query walks one step up and down the graph; targets rule cc is how you count translation units in a big CMake project. Note that inputs only knows declared edges until a build has filled the deps log.

compdb: compile_commands.json

Editors (clangd, CLion, VS Code) and tools like clang-tidy need to know the exact flags each file is compiled with. -t compdb writes the standard JSON compilation database, here limited to the cc rule:

bash · try it yourself
mkdir -p tools && cd tools
printf 'int add(int a, int b) { return a + b; }\n' > add.c
printf 'int main(void) { return 0; }\n' > main.c
printf 'rule cc\n  command = gcc -O2 -Iinclude -c $in -o $out\nrule link\n  command = gcc $in -o $out\nbuild add.o: cc add.c\nbuild main.o: cc main.c\nbuild app: link add.o main.o\n' > build.ninja
ninja -t compdb cc > compile_commands.json
cat compile_commands.json
[
  {
    "directory": "/home/learner/tools",
    "command": "gcc -O2 -Iinclude -c add.c -o add.o",
    "file": "add.c",
    "output": "add.o"
  },
  {
    "directory": "/home/learner/tools",
    "command": "gcc -O2 -Iinclude -c main.c -o main.o",
    "file": "main.c",
    "output": "main.o"
  }
]

Without rule names, compdb lists every edge, links and phony targets included, which confuses some tools; always name the compile rules. CMake writes the same file itself with CMAKE_EXPORT_COMPILE_COMMANDS=ON. Toolkit: compile_commands.json & Editors covers how to get one from Make.

graph: draw the build

ninja -t graph prints the graph in Graphviz's dot language; dot -Tsvg (from the Graphviz package) turns it into a picture. Ninja names nodes by their memory addresses, which differ on every run, so this session renames them n1, n2, … in order of appearance:

bash · try it yourself
mkdir -p tools && cd tools
printf 'int add(int a, int b) { return a + b; }\n' > add.c
printf 'int main(void) { return 0; }\n' > main.c
printf 'rule cc\n  command = gcc -c $in -o $out\nrule link\n  command = gcc $in -o $out\nbuild add.o: cc add.c\nbuild main.o: cc main.c\nbuild app: link main.o add.o\n' > build.ninja
ninja -t graph app | awk '{
  while (match($0, /"0x[0-9a-f]+"/)) {
    a = substr($0, RSTART, RLENGTH)
    if (!(a in id)) id[a] = "n" (++n)
    $0 = substr($0, 1, RSTART - 1) id[a] substr($0, RSTART + RLENGTH)
  }
  print
}'
digraph ninja {
rankdir="LR"
node [fontsize=10, shape=box, height=0.25]
edge [fontsize=10]
n1 [label="app"]
n2 [label="link", shape=ellipse]
n2 -> n1
n3 -> n2 [arrowhead=none]
n4 -> n2 [arrowhead=none]
n3 [label="main.o"]
n5 -> n3 [label=" cc"]
n5 [label="main.c"]
n4 [label="add.o"]
n6 -> n4 [label=" cc"]
n6 [label="add.c"]
}

Files are boxes; an edge with several inputs gets an ellipse node named after its rule, and a single-input edge is drawn as a labelled arrow.

To get the picture, pipe the graph straight into dot (no renaming needed: the addresses only end up in invisible <title> elements). The SVG opens in any browser; here we look at its first lines and the labels Graphviz placed:

bash · try it yourself
mkdir -p tools && cd tools
printf 'int add(int a, int b) { return a + b; }\n' > add.c
printf 'int main(void) { return 0; }\n' > main.c
printf 'rule cc\n  command = gcc -c $in -o $out\nrule link\n  command = gcc $in -o $out\nbuild add.o: cc add.c\nbuild main.o: cc main.c\nbuild app: link main.o add.o\n' > build.ninja
ninja -t graph app | dot -Tsvg -o app.svg
file app.svg
head -n 9 app.svg
echo ...
grep -o '<text[^>]*>[^<]*</text>' app.svg | sed 's/<[^>]*>//g'
app.svg: SVG Scalable Vector Graphics image
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.43.0 (0)
 -->
<!-- Title: ninja Pages: 1 -->
<svg width="348pt" height="66pt"
 viewBox="0.00 0.00 348.00 65.50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 61.5)">
...
app
link
main.o
add.o
main.c
 cc
add.c
 cc

A 348×66 pt picture, laid out left to right (rankdir="LR"): the six files, the link ellipse and the two cc edge labels. Other output formats are one flag away: -Tpng, -Tpdf, or -Tplain for a text layout with coordinates. On a real project, give graph one target (ninja -t graph mylib | dot -Tsvg > g.svg): the full graph of a large codebase is too big to read.

clean

ninja -t clean deletes every file that a build statement produces (not the sources), -t clean app only what app needs, and -t clean -r cc only the outputs of one rule. cleandead removes outputs that an earlier version of build.ninja produced but the current one no longer mentions, using the build log:

bash · try it yourself
mkdir -p tools && cd tools
printf 'int main(void) { return 0; }\n' > main.c
printf 'int old(void) { return 0; }\n' > old.c
printf 'rule cc\n  command = gcc -c $in -o $out\nbuild main.o: cc main.c\nbuild old.o: cc old.c\n' > build.ninja
ninja -j1 >/dev/null && ls *.o
printf 'rule cc\n  command = gcc -c $in -o $out\nbuild main.o: cc main.c\n' > build.ninja
ninja -t cleandead
ninja -t clean
ls *.o 2>/dev/null || echo "no objects left"
main.o
old.o
Cleaning... 1 files.
Cleaning... 1 files.
no objects left

Test yourself

Which tool prints every command needed to build a target, in order?

  1. ninja -t query
  2. ninja -t commands
  3. ninja -t targets
  4. ninja -n

Why pass rule names to ninja -t compdb?

  1. It's required syntax
  2. To include only compile commands, not link or phony edges
  3. To make it faster
  4. To add include paths

What does ninja -t cleandead delete?

  1. All outputs
  2. Outputs recorded in the build log that the current build.ninja no longer produces
  3. Source files with no references
  4. The .ninja_log file