summaryrefslogtreecommitdiffstats
path: root/manual.asciidoc
blob: 51d38d5c13c6b8610f3e550d9d89fa050c62575c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
Ninja
=====

Introduction
------------

Ninja is yet another build system.  It takes as input a file
describing interdependencies of files (typically source code and
output executables) and orchestrates building them, _quickly_.

Ninja joins a sea of other build systems.  Its distinguishing goal is
to be fast.  It is born from my work on the Chromium browser project,
which has well over 20,000 source files and whose other build systems
(including one built from custom non-recursive Makefiles) take tens of
seconds to rebuild after changing one file, even on a fast computer.

Here are some of the design goals of Ninja:

* very fast (i.e., instant) incremental builds, even for very large
  projects.

* very little implicit policy; "explicit is better than implicit".

* get dependencies correct, and in particular situations that are
  difficult to get right with Makefiles (e.g. outputs should have an
  implicit dependency on the command line used to generate them; using
  gcc's `-M` flags for header dependencies).

* when convenience and speed are in conflict, prefer speed.

Some explicit _non-goals_:

* convenient syntax for writing build files by hand.  _You should
  generate your ninja files using another program_.  This is how we
  sidestep many policy decisions.

* built-in rules. _Out of the box, ninja has no rules for
  e.g. compiling C code._

* build-time customization of the build. _Options belong in
  the program that generates the ninja files_.

To restate, Ninja manages to be simpler and faster than other build
systems by being much more stupid.  It has no built-in behavior
describing how to build C source, or build libraries, or install
binaries.  You instead decide this policy when you create your
project's `.ninja` files.  Customization and configuration are out of
scope; instead you should provide customization in the system that
generates your `.ninja` files, like how autoconf provides
`./configure`.

Conceptual overview
~~~~~~~~~~~~~~~~~~~
Ninja evaluates a graph of dependencies between files, and runs
whichever commands are necessary to make your build target up to date.
If you are familiar with Make, Ninja is very similar.

A build file (default name: `build.ninja`) provides a list of _rules_
-- short names for longer commands, like how to run the compiler --
along with a list of _build_ statements saying how to build files
using the rules -- a given source file and its output.

Conceptually, `build` statements describe the dependency graph of your
project, while `rule` statements describe how to generate the files
along a given edge of the graph.

Feature differences from make
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* A rule may point at where extra implicit dependency information
  lives.  This makes getting header dependencies right for C nearly
  trivial.

* Rules can provide shorter descriptions of the command being run, so
  you can print e.g. `CC foo.o` instead of a long command line while
  running.

* Command output is always buffered.  This means commands running in
  parallel don't interleave their output, and when a command fails we
  can put its failure output next to the full command line that
  produced the failure.

Getting started
---------------

The included `bootstrap.sh` should hopefully produce a working `ninja`
binary, by first blindly compiling all non-test files together then
re-building ninja using itself.

Usage is currently just

----------------
ninja target
----------------

where `target` is a known output described by `build.ninja` in the
current directory.


Creating .ninja files
~~~~~~~~~~~~~~~~~~~~~
Here's a basic `.ninja` file that demonstrates most of the syntax.

---------------------------------
cflags = -Wall

rule cc
  command = gcc $cflags -c $in -o $out

build foo.o: cc foo.c
---------------------------------

Rules
~~~~~

Rules begin with a line consisting of the `rule` keyword and a name
for the rule.  Then follows an indented set of `key = value` lines.

The basic example above declares a new rule named `cc`, along with the
command to run.  (In the context of a rule, the `command` key is
special and defines the command to run.  A full list of special keys
is provided in <<ref_rule,the reference>>.)

Variables begin with a `$` and are described more fully later, but
here `$in` expands to the list of input files (`foo.c`) and `$out` to
the output file (`foo.o`) for the command.


Build statements
~~~~~~~~~~~~~~~~

Build statements begin with the `build` keyword, and have the format
+build _outputs_: _rulename_ _inputs_+.  Such a declaration says that
all of the output files are derived from the input files.  When the output
files are missing or when the inputs change, ninja will run the rule
to regenerate the outputs.

The basic example above describes how to build `foo.o`, using the `cc`
rule.

A build statement may be followed by an indented set of `key = value`
pairs, much like a rule.  These bindings will shadow any bindings when
evaluating the variables in the command.  For example:

----------------
cflags = -Wall -Werror
rule cc
  command = gcc $cflags -c $in -o $out

# If left unspecified, builds get the outer $cflags.
build foo.o: cc foo.c

# But you can can shadow variables like cflags for a particular build.
build special.o: cc.special.c
  cflags = -Wall
----------------

In the scope of a `build` block (including in the evaluation of its associated
`rule`), the variable `$in` is the list of inputs and the variable `$out` is
the list of outputs.  If you need anything more complicated than this (for
example, the basename of the first input), pass that in as an extra variable,
like how `cflags` is passed above.


Variables
~~~~~~~~~
Despite the non-goal of being convenient to write by hand, to keep
build files readable (debuggable), Ninja supports declaring bindings
(variables).  A declaration like the following

----------------
cflags = -g
----------------

Can be used on the right side of an equals sign like this:

----------------
rule cc
  command = gcc $cflags -c $in -o $out
----------------

Variables can also be referenced using curly braces like `${in}`.

Variables might better be called "bindings", in that a given variable
cannot be changed, only shadowed.  Within a larger Ninja project,
different _scopes_ allow variable values to be overridden.  XXX finish
describing scopes.

Ninja file reference
--------------------
A file is a series of declarations.  A declaration can be one of:

1. A rule declaration, which begins with +rule _rulename_+.
2. A build edge, which looks like +build _outputs_: _rulename_ _inputs_+
3. Variable declarations, which look like +_variable_ = _value_+.
4. References to more files, which look like +subninja _path_+.

Comments begin with `#` and extend to the end of the line.

Newlines are significant, but they can be escaped by putting a `\`
before them.

Whitespace is only significant if it's at the beginning of a line.  If
a line is intended more than the previous one, it's considered part of
its parent's scope; if it is indented less than the previous one, it
closes the previous scope.

Rule variables
~~~~~~~~~~~~~~
[[ref_rule]]

A `rule` block contains a list of `key = value` declarations that
affect the processing of the rule.  Here is a full list of special
keys.

`command` (_required_):: the command line to run.

`depfile`:: path to an optional `Makefile` that contains _implicit
  dependencies_.  Implicit dependencies are inputs to a build that are
  not given on the command line; the best example is how `gcc` has the
  `-M` family of flags to output the list of headers a given `.c` file
  depends on.
+
----
rule cc
  depfile = $out.d
  command = gcc -MMD -MF $out.d [other gcc flags here]
----

`description`:: a short description of the command, used to pretty-print
  the command as it's running.  The `-v` flag controls whether to print
  the full command or its description; if a command fails, the full command
  line will always be printed before the command's output.

`in`, `out`:: the space-separated list of inputs and outputs to the `build`
  line referencing this `rule`.

Evaluation and scoping
~~~~~~~~~~~~~~~~~~~~~~
XXX talk about where variables live, nested scopes etc