Merge pull request #1118 from nico/posixspawn2
[platform/upstream/ninja.git] / doc / manual.asciidoc
1 The Ninja build system
2 ======================
3
4
5 Introduction
6 ------------
7
8 Ninja is yet another build system.  It takes as input the
9 interdependencies of files (typically source code and output
10 executables) and orchestrates building them, _quickly_.
11
12 Ninja joins a sea of other build systems.  Its distinguishing goal is
13 to be fast.  It is born from
14 http://neugierig.org/software/chromium/notes/2011/02/ninja.html[my
15 work on the Chromium browser project], which has over 30,000 source
16 files and whose other build systems (including one built from custom
17 non-recursive Makefiles) would take ten seconds to start building
18 after changing one file.  Ninja is under a second.
19
20 Philosophical overview
21 ~~~~~~~~~~~~~~~~~~~~~~
22
23 Where other build systems are high-level languages, Ninja aims to be
24 an assembler.
25
26 Build systems get slow when they need to make decisions.  When you are
27 in a edit-compile cycle you want it to be as fast as possible -- you
28 want the build system to do the minimum work necessary to figure out
29 what needs to be built immediately.
30
31 Ninja contains the barest functionality necessary to describe
32 arbitrary dependency graphs.  Its lack of syntax makes it impossible
33 to express complex decisions.
34
35 Instead, Ninja is intended to be used with a separate program
36 generating its input files.  The generator program (like the
37 `./configure` found in autotools projects) can analyze system
38 dependencies and make as many decisions as possible up front so that
39 incremental builds stay fast.  Going beyond autotools, even build-time
40 decisions like "which compiler flags should I use?"  or "should I
41 build a debug or release-mode binary?"  belong in the `.ninja` file
42 generator.
43
44 Design goals
45 ~~~~~~~~~~~~
46
47 Here are the design goals of Ninja:
48
49 * very fast (i.e., instant) incremental builds, even for very large
50   projects.
51
52 * very little policy about how code is built.  Different projects and
53   higher-level build systems have different opinions about how code
54   should be built; for example, should built objects live alongside
55   the sources or should all build output go into a separate directory?
56   Is there a "package" rule that builds a distributable package of
57   the project?  Sidestep these decisions by trying to allow either to
58   be implemented, rather than choosing, even if that results in
59   more verbosity.
60
61 * get dependencies correct, and in particular situations that are
62   difficult to get right with Makefiles (e.g. outputs need an implicit
63   dependency on the command line used to generate them; to build C
64   source code you need to use gcc's `-M` flags for header
65   dependencies).
66
67 * when convenience and speed are in conflict, prefer speed.
68
69 Some explicit _non-goals_:
70
71 * convenient syntax for writing build files by hand.  _You should
72   generate your ninja files using another program_.  This is how we
73   can sidestep many policy decisions.
74
75 * built-in rules. _Out of the box, Ninja has no rules for
76   e.g. compiling C code._
77
78 * build-time customization of the build. _Options belong in
79   the program that generates the ninja files_.
80
81 * build-time decision-making ability such as conditionals or search
82   paths. _Making decisions is slow._
83
84 To restate, Ninja is faster than other build systems because it is
85 painfully simple.  You must tell Ninja exactly what to do when you
86 create your project's `.ninja` files.
87
88 Comparison to Make
89 ~~~~~~~~~~~~~~~~~~
90
91 Ninja is closest in spirit and functionality to Make, relying on
92 simple dependencies between file timestamps.
93
94 But fundamentally, make has a lot of _features_: suffix rules,
95 functions, built-in rules that e.g. search for RCS files when building
96 source.  Make's language was designed to be written by humans.  Many
97 projects find make alone adequate for their build problems.
98
99 In contrast, Ninja has almost no features; just those necessary to get
100 builds correct while punting most complexity to generation of the
101 ninja input files.  Ninja by itself is unlikely to be useful for most
102 projects.
103
104 Here are some of the features Ninja adds to Make.  (These sorts of
105 features can often be implemented using more complicated Makefiles,
106 but they are not part of make itself.)
107
108 * Ninja has special support for discovering extra dependencies at build
109   time, making it easy to get <<ref_headers,header dependencies>>
110   correct for C/C++ code.
111
112 * A build edge may have multiple outputs.
113
114 * Outputs implicitly depend on the command line that was used to generate
115   them, which means that changing e.g. compilation flags will cause
116   the outputs to rebuild.
117
118 * Output directories are always implicitly created before running the
119   command that relies on them.
120
121 * Rules can provide shorter descriptions of the command being run, so
122   you can print e.g. `CC foo.o` instead of a long command line while
123   building.
124
125 * Builds are always run in parallel, based by default on the number of
126   CPUs your system has.  Underspecified build dependencies will result
127   in incorrect builds.
128
129 * Command output is always buffered.  This means commands running in
130   parallel don't interleave their output, and when a command fails we
131   can print its failure output next to the full command line that
132   produced the failure.
133
134
135 Using Ninja for your project
136 ----------------------------
137
138 Ninja currently works on Unix-like systems and Windows. It's seen the
139 most testing on Linux (and has the best performance there) but it runs
140 fine on Mac OS X and FreeBSD.
141
142 If your project is small, Ninja's speed impact is likely unnoticeable.
143 (However, even for small projects it sometimes turns out that Ninja's
144 limited syntax forces simpler build rules that result in faster
145 builds.)  Another way to say this is that if you're happy with the
146 edit-compile cycle time of your project already then Ninja won't help.
147
148 There are many other build systems that are more user-friendly or
149 featureful than Ninja itself.  For some recommendations: the Ninja
150 author found http://gittup.org/tup/[the tup build system] influential
151 in Ninja's design, and thinks https://github.com/apenwarr/redo[redo]'s
152 design is quite clever.
153
154 Ninja's benefit comes from using it in conjunction with a smarter
155 meta-build system.
156
157 http://code.google.com/p/gyp/[gyp]:: The meta-build system used to
158 generate build files for Google Chrome and related projects (v8,
159 node.js).  gyp can generate Ninja files for all platforms supported by
160 Chrome. See the
161 https://chromium.googlesource.com/chromium/src/+/master/docs/ninja_build.md[Chromium Ninja documentation for more details].
162
163 https://cmake.org/[CMake]:: A widely used meta-build system that
164 can generate Ninja files on Linux as of CMake version 2.8.8.  Newer versions
165 of CMake support generating Ninja files on Windows and Mac OS X too.
166
167 https://github.com/ninja-build/ninja/wiki/List-of-generators-producing-ninja-build-files[others]:: Ninja ought to fit perfectly into other meta-build software
168 like http://industriousone.com/premake[premake].  If you do this work,
169 please let us know!
170
171 Running Ninja
172 ~~~~~~~~~~~~~
173
174 Run `ninja`.  By default, it looks for a file named `build.ninja` in
175 the current directory and builds all out-of-date targets.  You can
176 specify which targets (files) to build as command line arguments.
177
178 There is also a special syntax `target^` for specifying a target
179 as the first output of some rule containing the source you put in
180 the command line, if one exists. For example, if you specify target as
181 `foo.c^` then `foo.o` will get built (assuming you have those targets
182 in your build files).
183
184 `ninja -h` prints help output.  Many of Ninja's flags intentionally
185 match those of Make; e.g `ninja -C build -j 20` changes into the
186 `build` directory and runs 20 build commands in parallel.  (Note that
187 Ninja defaults to running commands in parallel anyway, so typically
188 you don't need to pass `-j`.)
189
190
191 Environment variables
192 ~~~~~~~~~~~~~~~~~~~~~
193
194 Ninja supports one environment variable to control its behavior:
195 `NINJA_STATUS`, the progress status printed before the rule being run.
196
197 Several placeholders are available:
198
199 `%s`:: The number of started edges.
200 `%t`:: The total number of edges that must be run to complete the build.
201 `%p`:: The percentage of started edges.
202 `%r`:: The number of currently running edges.
203 `%u`:: The number of remaining edges to start.
204 `%f`:: The number of finished edges.
205 `%o`:: Overall rate of finished edges per second
206 `%c`:: Current rate of finished edges per second (average over builds
207 specified by `-j` or its default)
208 `%e`:: Elapsed time in seconds.  _(Available since Ninja 1.2.)_
209 `%%`:: A plain `%` character.
210
211 The default progress status is `"[%s/%t] "` (note the trailing space
212 to separate from the build rule). Another example of possible progress status
213 could be `"[%u/%r/%f] "`.
214
215 Extra tools
216 ~~~~~~~~~~~
217
218 The `-t` flag on the Ninja command line runs some tools that we have
219 found useful during Ninja's development.  The current tools are:
220
221 [horizontal]
222 `query`:: dump the inputs and outputs of a given target.
223
224 `browse`:: browse the dependency graph in a web browser.  Clicking a
225 file focuses the view on that file, showing inputs and outputs.  This
226 feature requires a Python installation. By default port 8000 is used
227 and a web browser will be opened. This can be changed as follows:
228 +
229 ----
230 ninja -t browse --port=8000 --no-browser mytarget
231 ----
232 +
233 `graph`:: output a file in the syntax used by `graphviz`, a automatic
234 graph layout tool.  Use it like:
235 +
236 ----
237 ninja -t graph mytarget | dot -Tpng -ograph.png
238 ----
239 +
240 In the Ninja source tree, `ninja graph.png`
241 generates an image for Ninja itself.  If no target is given generate a
242 graph for all root targets.
243
244 `targets`:: output a list of targets either by rule or by depth.  If used
245 like +ninja -t targets rule _name_+ it prints the list of targets
246 using the given rule to be built.  If no rule is given, it prints the source
247 files (the leaves of the graph).  If used like
248 +ninja -t targets depth _digit_+ it
249 prints the list of targets in a depth-first manner starting by the root
250 targets (the ones with no outputs). Indentation is used to mark dependencies.
251 If the depth is zero it prints all targets. If no arguments are provided
252 +ninja -t targets depth 1+ is assumed. In this mode targets may be listed
253 several times. If used like this +ninja -t targets all+ it
254 prints all the targets available without indentation and it is faster
255 than the _depth_ mode.
256
257 `commands`:: given a list of targets, print a list of commands which, if
258 executed in order, may be used to rebuild those targets, assuming that all
259 output files are out of date.
260
261 `clean`:: remove built files. By default it removes all built files
262 except for those created by the generator.  Adding the `-g` flag also
263 removes built files created by the generator (see <<ref_rule,the rule
264 reference for the +generator+ attribute>>).  Additional arguments are
265 targets, which removes the given targets and recursively all files
266 built for them.
267 +
268 If used like +ninja -t clean -r _rules_+ it removes all files built using
269 the given rules.
270 +
271 Files created but not referenced in the graph are not removed. This
272 tool takes in account the +-v+ and the +-n+ options (note that +-n+
273 implies +-v+).
274
275 `compdb`:: given a list of rules, each of which is expected to be a
276 C family language compiler rule whose first input is the name of the
277 source file, prints on standard output a compilation database in the
278 http://clang.llvm.org/docs/JSONCompilationDatabase.html[JSON format] expected
279 by the Clang tooling interface.
280 _Available since Ninja 1.2._
281
282
283 Writing your own Ninja files
284 ----------------------------
285
286 The remainder of this manual is only useful if you are constructing
287 Ninja files yourself: for example, if you're writing a meta-build
288 system or supporting a new language.
289
290 Conceptual overview
291 ~~~~~~~~~~~~~~~~~~~
292
293 Ninja evaluates a graph of dependencies between files, and runs
294 whichever commands are necessary to make your build target up to date
295 as determined by file modification times.  If you are familiar with
296 Make, Ninja is very similar.
297
298 A build file (default name: `build.ninja`) provides a list of _rules_
299 -- short names for longer commands, like how to run the compiler --
300 along with a list of _build_ statements saying how to build files
301 using the rules -- which rule to apply to which inputs to produce
302 which outputs.
303
304 Conceptually, `build` statements describe the dependency graph of your
305 project, while `rule` statements describe how to generate the files
306 along a given edge of the graph.
307
308 Syntax example
309 ~~~~~~~~~~~~~~
310
311 Here's a basic `.ninja` file that demonstrates most of the syntax.
312 It will be used as an example for the following sections.
313
314 ---------------------------------
315 cflags = -Wall
316
317 rule cc
318   command = gcc $cflags -c $in -o $out
319
320 build foo.o: cc foo.c
321 ---------------------------------
322
323 Variables
324 ~~~~~~~~~
325 Despite the non-goal of being convenient to write by hand, to keep
326 build files readable (debuggable), Ninja supports declaring shorter
327 reusable names for strings.  A declaration like the following
328
329 ----------------
330 cflags = -g
331 ----------------
332
333 can be used on the right side of an equals sign, dereferencing it with
334 a dollar sign, like this:
335
336 ----------------
337 rule cc
338   command = gcc $cflags -c $in -o $out
339 ----------------
340
341 Variables can also be referenced using curly braces like `${in}`.
342
343 Variables might better be called "bindings", in that a given variable
344 cannot be changed, only shadowed.  There is more on how shadowing works
345 later in this document.
346
347 Rules
348 ~~~~~
349
350 Rules declare a short name for a command line.  They begin with a line
351 consisting of the `rule` keyword and a name for the rule.  Then
352 follows an indented set of `variable = value` lines.
353
354 The basic example above declares a new rule named `cc`, along with the
355 command to run.  In the context of a rule, the `command` variable
356 defines the command to run, `$in` expands to the list of
357 input files (`foo.c`), and `$out` to the output files (`foo.o`) for the
358 command.  A full list of special variables is provided in
359 <<ref_rule,the reference>>.
360
361 Build statements
362 ~~~~~~~~~~~~~~~~
363
364 Build statements declare a relationship between input and output
365 files.  They begin with the `build` keyword, and have the format
366 +build _outputs_: _rulename_ _inputs_+.  Such a declaration says that
367 all of the output files are derived from the input files.  When the
368 output files are missing or when the inputs change, Ninja will run the
369 rule to regenerate the outputs.
370
371 The basic example above describes how to build `foo.o`, using the `cc`
372 rule.
373
374 In the scope of a `build` block (including in the evaluation of its
375 associated `rule`), the variable `$in` is the list of inputs and the
376 variable `$out` is the list of outputs.
377
378 A build statement may be followed by an indented set of `key = value`
379 pairs, much like a rule.  These variables will shadow any variables
380 when evaluating the variables in the command.  For example:
381
382 ----------------
383 cflags = -Wall -Werror
384 rule cc
385   command = gcc $cflags -c $in -o $out
386
387 # If left unspecified, builds get the outer $cflags.
388 build foo.o: cc foo.c
389
390 # But you can shadow variables like cflags for a particular build.
391 build special.o: cc special.c
392   cflags = -Wall
393
394 # The variable was only shadowed for the scope of special.o;
395 # Subsequent build lines get the outer (original) cflags.
396 build bar.o: cc bar.c
397
398 ----------------
399
400 For more discussion of how scoping works, consult <<ref_scope,the
401 reference>>.
402
403 If you need more complicated information passed from the build
404 statement to the rule (for example, if the rule needs "the file
405 extension of the first input"), pass that through as an extra
406 variable, like how `cflags` is passed above.
407
408 If the top-level Ninja file is specified as an output of any build
409 statement and it is out of date, Ninja will rebuild and reload it
410 before building the targets requested by the user.
411
412 Generating Ninja files from code
413 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
414
415 `misc/ninja_syntax.py` in the Ninja distribution is a tiny Python
416 module to facilitate generating Ninja files.  It allows you to make
417 Python calls like `ninja.rule(name='foo', command='bar',
418 depfile='$out.d')` and it will generate the appropriate syntax.  Feel
419 free to just inline it into your project's build system if it's
420 useful.
421
422
423 More details
424 ------------
425
426 The `phony` rule
427 ~~~~~~~~~~~~~~~~
428
429 The special rule name `phony` can be used to create aliases for other
430 targets.  For example:
431
432 ----------------
433 build foo: phony some/file/in/a/faraway/subdir/foo
434 ----------------
435
436 This makes `ninja foo` build the longer path.  Semantically, the
437 `phony` rule is equivalent to a plain rule where the `command` does
438 nothing, but phony rules are handled specially in that they aren't
439 printed when run, logged (see below), nor do they contribute to the
440 command count printed as part of the build process.
441
442 `phony` can also be used to create dummy targets for files which
443 may not exist at build time.  If a phony build statement is written
444 without any dependencies, the target will be considered out of date if
445 it does not exist.  Without a phony build statement, Ninja will report
446 an error if the file does not exist and is required by the build.
447
448
449 Default target statements
450 ~~~~~~~~~~~~~~~~~~~~~~~~~
451
452 By default, if no targets are specified on the command line, Ninja
453 will build every output that is not named as an input elsewhere.
454 You can override this behavior using a default target statement.
455 A default target statement causes Ninja to build only a given subset
456 of output files if none are specified on the command line.
457
458 Default target statements begin with the `default` keyword, and have
459 the format +default _targets_+.  A default target statement must appear
460 after the build statement that declares the target as an output file.
461 They are cumulative, so multiple statements may be used to extend
462 the list of default targets.  For example:
463
464 ----------------
465 default foo bar
466 default baz
467 ----------------
468
469 This causes Ninja to build the `foo`, `bar` and `baz` targets by
470 default.
471
472
473 [[ref_log]]
474 The Ninja log
475 ~~~~~~~~~~~~~
476
477 For each built file, Ninja keeps a log of the command used to build
478 it.  Using this log Ninja can know when an existing output was built
479 with a different command line than the build files specify (i.e., the
480 command line changed) and knows to rebuild the file.
481
482 The log file is kept in the build root in a file called `.ninja_log`.
483 If you provide a variable named `builddir` in the outermost scope,
484 `.ninja_log` will be kept in that directory instead.
485
486
487 [[ref_versioning]]
488 Version compatibility
489 ~~~~~~~~~~~~~~~~~~~~~
490
491 _Available since Ninja 1.2._
492
493 Ninja version labels follow the standard major.minor.patch format,
494 where the major version is increased on backwards-incompatible
495 syntax/behavioral changes and the minor version is increased on new
496 behaviors.  Your `build.ninja` may declare a variable named
497 `ninja_required_version` that asserts the minimum Ninja version
498 required to use the generated file.  For example,
499
500 -----
501 ninja_required_version = 1.1
502 -----
503
504 declares that the build file relies on some feature that was
505 introduced in Ninja 1.1 (perhaps the `pool` syntax), and that
506 Ninja 1.1 or greater must be used to build.  Unlike other Ninja
507 variables, this version requirement is checked immediately when
508 the variable is encountered in parsing, so it's best to put it
509 at the top of the build file.
510
511 Ninja always warns if the major versions of Ninja and the
512 `ninja_required_version` don't match; a major version change hasn't
513 come up yet so it's difficult to predict what behavior might be
514 required.
515
516 [[ref_headers]]
517 C/C++ header dependencies
518 ~~~~~~~~~~~~~~~~~~~~~~~~~
519
520 To get C/C++ header dependencies (or any other build dependency that
521 works in a similar way) correct Ninja has some extra functionality.
522
523 The problem with headers is that the full list of files that a given
524 source file depends on can only be discovered by the compiler:
525 different preprocessor defines and include paths cause different files
526 to be used.  Some compilers can emit this information while building,
527 and Ninja can use that to get its dependencies perfect.
528
529 Consider: if the file has never been compiled, it must be built anyway,
530 generating the header dependencies as a side effect.  If any file is
531 later modified (even in a way that changes which headers it depends
532 on) the modification will cause a rebuild as well, keeping the
533 dependencies up to date.
534
535 When loading these special dependencies, Ninja implicitly adds extra
536 build edges such that it is not an error if the listed dependency is
537 missing.  This allows you to delete a header file and rebuild without
538 the build aborting due to a missing input.
539
540 depfile
541 ^^^^^^^
542
543 `gcc` (and other compilers like `clang`) support emitting dependency
544 information in the syntax of a Makefile.  (Any command that can write
545 dependencies in this form can be used, not just `gcc`.)
546
547 To bring this information into Ninja requires cooperation.  On the
548 Ninja side, the `depfile` attribute on the `build` must point to a
549 path where this data is written.  (Ninja only supports the limited
550 subset of the Makefile syntax emitted by compilers.)  Then the command
551 must know to write dependencies into the `depfile` path.
552 Use it like in the following example:
553
554 ----
555 rule cc
556   depfile = $out.d
557   command = gcc -MMD -MF $out.d [other gcc flags here]
558 ----
559
560 The `-MMD` flag to `gcc` tells it to output header dependencies, and
561 the `-MF` flag tells it where to write them.
562
563 deps
564 ^^^^
565
566 _(Available since Ninja 1.3.)_
567
568 It turns out that for large projects (and particularly on Windows,
569 where the file system is slow) loading these dependency files on
570 startup is slow.
571
572 Ninja 1.3 can instead process dependencies just after they're generated
573 and save a compacted form of the same information in a Ninja-internal
574 database.
575
576 Ninja supports this processing in two forms.
577
578 1. `deps = gcc` specifies that the tool outputs `gcc`-style dependencies
579    in the form of Makefiles.  Adding this to the above example will
580    cause Ninja to process the `depfile` immediately after the
581    compilation finishes, then delete the `.d` file (which is only used
582    as a temporary).
583
584 2. `deps = msvc` specifies that the tool outputs header dependencies
585    in the form produced by Visual Studio's compiler's
586    http://msdn.microsoft.com/en-us/library/hdkef6tk(v=vs.90).aspx[`/showIncludes`
587    flag].  Briefly, this means the tool outputs specially-formatted lines
588    to its stdout.  Ninja then filters these lines from the displayed
589    output.  No `depfile` attribute is necessary, but the localized string
590    in front of the the header file path. For instance
591    `msvc_deps_prefix = Note: including file: `
592    for a English Visual Studio (the default). Should be globally defined.
593 +
594 ----
595 msvc_deps_prefix = Note: including file:
596 rule cc
597   deps = msvc
598   command = cl /showIncludes -c $in /Fo$out
599 ----
600
601 If the include directory directives are using absolute paths, your depfile
602 may result in a mixture of relative and absolute paths. Paths used by other
603 build rules need to match exactly. Therefore, it is recommended to use
604 relative paths in these cases.
605
606 [[ref_pool]]
607 Pools
608 ~~~~~
609
610 _Available since Ninja 1.1._
611
612 Pools allow you to allocate one or more rules or edges a finite number
613 of concurrent jobs which is more tightly restricted than the default
614 parallelism.
615
616 This can be useful, for example, to restrict a particular expensive rule
617 (like link steps for huge executables), or to restrict particular build
618 statements which you know perform poorly when run concurrently.
619
620 Each pool has a `depth` variable which is specified in the build file.
621 The pool is then referred to with the `pool` variable on either a rule
622 or a build statement.
623
624 No matter what pools you specify, ninja will never run more concurrent jobs
625 than the default parallelism, or the number of jobs specified on the command
626 line (with `-j`).
627
628 ----------------
629 # No more than 4 links at a time.
630 pool link_pool
631   depth = 4
632
633 # No more than 1 heavy object at a time.
634 pool heavy_object_pool
635   depth = 1
636
637 rule link
638   ...
639   pool = link_pool
640
641 rule cc
642   ...
643
644 # The link_pool is used here. Only 4 links will run concurrently.
645 build foo.exe: link input.obj
646
647 # A build statement can be exempted from its rule's pool by setting an
648 # empty pool. This effectively puts the build statement back into the default
649 # pool, which has infinite depth.
650 build other.exe: link input.obj
651   pool =
652
653 # A build statement can specify a pool directly.
654 # Only one of these builds will run at a time.
655 build heavy_object1.obj: cc heavy_obj1.cc
656   pool = heavy_object_pool
657 build heavy_object2.obj: cc heavy_obj2.cc
658   pool = heavy_object_pool
659
660 ----------------
661
662 The `console` pool
663 ^^^^^^^^^^^^^^^^^^
664
665 _Available since Ninja 1.5._
666
667 There exists a pre-defined pool named `console` with a depth of 1. It has
668 the special property that any task in the pool has direct access to the
669 standard input, output and error streams provided to Ninja, which are
670 normally connected to the user's console (hence the name) but could be
671 redirected. This can be useful for interactive tasks or long-running tasks
672 which produce status updates on the console (such as test suites).
673
674 While a task in the `console` pool is running, Ninja's regular output (such
675 as progress status and output from concurrent tasks) is buffered until
676 it completes.
677
678 Ninja file reference
679 --------------------
680
681 A file is a series of declarations.  A declaration can be one of:
682
683 1. A rule declaration, which begins with +rule _rulename_+, and
684    then has a series of indented lines defining variables.
685
686 2. A build edge, which looks like +build _output1_ _output2_:
687    _rulename_ _input1_ _input2_+. +
688    Implicit dependencies may be tacked on the end with +|
689    _dependency1_ _dependency2_+. +
690    Order-only dependencies may be tacked on the end with +||
691    _dependency1_ _dependency2_+.  (See <<ref_dependencies,the reference on
692    dependency types>>.)
693 +
694 Implicit outputs _(available since Ninja 1.7)_ may be added before
695 the `:` with +| _output1_ _output2_+ and do not appear in `$out`.
696 (See <<ref_outputs,the reference on output types>>.)
697
698 3. Variable declarations, which look like +_variable_ = _value_+.
699
700 4. Default target statements, which look like +default _target1_ _target2_+.
701
702 5. References to more files, which look like +subninja _path_+ or
703    +include _path_+.  The difference between these is explained below
704    <<ref_scope,in the discussion about scoping>>.
705
706 6. A pool declaration, which looks like +pool _poolname_+. Pools are explained
707    <<ref_pool, in the section on pools>>.
708
709 Lexical syntax
710 ~~~~~~~~~~~~~~
711
712 Ninja is mostly encoding agnostic, as long as the bytes Ninja cares
713 about (like slashes in paths) are ASCII.  This means e.g. UTF-8 or
714 ISO-8859-1 input files ought to work.
715
716 Comments begin with `#` and extend to the end of the line.
717
718 Newlines are significant.  Statements like `build foo bar` are a set
719 of space-separated tokens that end at the newline.  Newlines and
720 spaces within a token must be escaped.
721
722 There is only one escape character, `$`, and it has the following
723 behaviors:
724
725 `$` followed by a newline:: escape the newline (continue the current line
726 across a line break).
727
728 `$` followed by text:: a variable reference.
729
730 `${varname}`:: alternate syntax for `$varname`.
731
732 `$` followed by space:: a space.  (This is only necessary in lists of
733 paths, where a space would otherwise separate filenames.  See below.)
734
735 `$:` :: a colon.  (This is only necessary in `build` lines, where a colon
736 would otherwise terminate the list of outputs.)
737
738 `$$`:: a literal `$`.
739
740 A `build` or `default` statement is first parsed as a space-separated
741 list of filenames and then each name is expanded.  This means that
742 spaces within a variable will result in spaces in the expanded
743 filename.
744
745 ----
746 spaced = foo bar
747 build $spaced/baz other$ file: ...
748 # The above build line has two outputs: "foo bar/baz" and "other file".
749 ----
750
751 In a `name = value` statement, whitespace at the beginning of a value
752 is always stripped.  Whitespace at the beginning of a line after a
753 line continuation is also stripped.
754
755 ----
756 two_words_with_one_space = foo $
757     bar
758 one_word_with_no_space = foo$
759     bar
760 ----
761
762 Other whitespace is only significant if it's at the beginning of a
763 line.  If a line is indented more than the previous one, it's
764 considered part of its parent's scope; if it is indented less than the
765 previous one, it closes the previous scope.
766
767 [[ref_toplevel]]
768 Top-level variables
769 ~~~~~~~~~~~~~~~~~~~
770
771 Two variables are significant when declared in the outermost file scope.
772
773 `builddir`:: a directory for some Ninja output files.  See <<ref_log,the
774   discussion of the build log>>.  (You can also store other build output
775   in this directory.)
776
777 `ninja_required_version`:: the minimum version of Ninja required to process
778   the build correctly.  See <<ref_versioning,the discussion of versioning>>.
779
780
781 [[ref_rule]]
782 Rule variables
783 ~~~~~~~~~~~~~~
784
785 A `rule` block contains a list of `key = value` declarations that
786 affect the processing of the rule.  Here is a full list of special
787 keys.
788
789 `command` (_required_):: the command line to run.  Each `rule` may
790   have only one `command` declaration. See <<ref_rule_command,the next
791   section>> for more details on quoting and executing multiple commands.
792
793 `depfile`:: path to an optional `Makefile` that contains extra
794   _implicit dependencies_ (see <<ref_dependencies,the reference on
795   dependency types>>).  This is explicitly to support C/C++ header
796   dependencies; see <<ref_headers,the full discussion>>.
797
798 `deps`:: _(Available since Ninja 1.3.)_ if present, must be one of
799   `gcc` or `msvc` to specify special dependency processing.  See
800    <<ref_headers,the full discussion>>.  The generated database is
801    stored as `.ninja_deps` in the `builddir`, see <<ref_toplevel,the
802    discussion of `builddir`>>.
803
804 `msvc_deps_prefix`:: _(Available since Ninja 1.5.)_ defines the string
805   which should be stripped from msvc's /showIncludes output. Only
806   needed when `deps = msvc` and no English Visual Studio version is used.
807
808 `description`:: a short description of the command, used to pretty-print
809   the command as it's running.  The `-v` flag controls whether to print
810   the full command or its description; if a command fails, the full command
811   line will always be printed before the command's output.
812
813 `generator`:: if present, specifies that this rule is used to
814   re-invoke the generator program.  Files built using `generator`
815   rules are treated specially in two ways: firstly, they will not be
816   rebuilt if the command line changes; and secondly, they are not
817   cleaned by default.
818
819 `in`:: the space-separated list of files provided as inputs to the build line
820   referencing this `rule`, shell-quoted if it appears in commands.  (`$in` is
821   provided solely for convenience; if you need some subset or variant of this
822   list of files, just construct a new variable with that list and use
823   that instead.)
824
825 `in_newline`:: the same as `$in` except that multiple inputs are
826   separated by newlines rather than spaces.  (For use with
827   `$rspfile_content`; this works around a bug in the MSVC linker where
828   it uses a fixed-size buffer for processing input.)
829
830 `out`:: the space-separated list of files provided as outputs to the build line
831   referencing this `rule`, shell-quoted if it appears in commands.
832
833 `restat`:: if present, causes Ninja to re-stat the command's outputs
834   after execution of the command.  Each output whose modification time
835   the command did not change will be treated as though it had never
836   needed to be built.  This may cause the output's reverse
837   dependencies to be removed from the list of pending build actions.
838
839 `rspfile`, `rspfile_content`:: if present (both), Ninja will use a
840   response file for the given command, i.e. write the selected string
841   (`rspfile_content`) to the given file (`rspfile`) before calling the
842   command and delete the file after successful execution of the
843   command.
844 +
845 This is particularly useful on Windows OS, where the maximal length of
846 a command line is limited and response files must be used instead.
847 +
848 Use it like in the following example:
849 +
850 ----
851 rule link
852   command = link.exe /OUT$out [usual link flags here] @$out.rsp
853   rspfile = $out.rsp
854   rspfile_content = $in
855
856 build myapp.exe: link a.obj b.obj [possibly many other .obj files]
857 ----
858
859 [[ref_rule_command]]
860 Interpretation of the `command` variable
861 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
862 Fundamentally, command lines behave differently on Unixes and Windows.
863
864 On Unixes, commands are arrays of arguments.  The Ninja `command`
865 variable is passed directly to `sh -c`, which is then responsible for
866 interpreting that string into an argv array.  Therefore the quoting
867 rules are those of the shell, and you can use all the normal shell
868 operators, like `&&` to chain multiple commands, or `VAR=value cmd` to
869 set environment variables.
870
871 On Windows, commands are strings, so Ninja passes the `command` string
872 directly to `CreateProcess`.  (In the common case of simply executing
873 a compiler this means there is less overhead.)  Consequently the
874 quoting rules are deterimined by the called program, which on Windows
875 are usually provided by the C library.  If you need shell
876 interpretation of the command (such as the use of `&&` to chain
877 multiple commands), make the command execute the Windows shell by
878 prefixing the command with `cmd /c`.
879
880 [[ref_outputs]]
881 Build outputs
882 ~~~~~~~~~~~~~
883
884 There are two types of build outputs which are subtly different.
885
886 1. _Explicit outputs_, as listed in a build line.  These are
887    available as the `$out` variable in the rule.
888 +
889 This is the standard form of output to be used for e.g. the
890 object file of a compile command.
891
892 2. _Implicit outputs_, as listed in a build line with the syntax +|
893    _out1_ _out2_+ + before the `:` of a build line _(available since
894    Ninja 1.7)_.  The semantics are identical to explicit outputs,
895   the only difference is that implicit outputs don't show up in the
896   `$out` variable.
897 +
898 This is for expressing outputs that don't show up on the
899 command line of the command.
900
901 [[ref_dependencies]]
902 Build dependencies
903 ~~~~~~~~~~~~~~~~~~
904
905 There are three types of build dependencies which are subtly different.
906
907 1. _Explicit dependencies_, as listed in a build line.  These are
908    available as the `$in` variable in the rule.  Changes in these files
909    cause the output to be rebuilt; if these file are missing and
910    Ninja doesn't know how to build them, the build is aborted.
911 +
912 This is the standard form of dependency to be used e.g. for the
913 source file of a compile command.
914
915 2. _Implicit dependencies_, either as picked up from
916    a `depfile` attribute on a rule or from the syntax +| _dep1_
917    _dep2_+ on the end of a build line.  The semantics are identical to
918    explicit dependencies, the only difference is that implicit dependencies
919    don't show up in the `$in` variable.
920 +
921 This is for expressing dependencies that don't show up on the
922 command line of the command; for example, for a rule that runs a
923 script, the script itself should be an implicit dependency, as
924 changes to the script should cause the output to rebuild.
925 +
926 Note that dependencies as loaded through depfiles have slightly different
927 semantics, as described in the <<ref_rule,rule reference>>.
928
929 3. _Order-only dependencies_, expressed with the syntax +|| _dep1_
930    _dep2_+ on the end of a build line.  When these are out of date, the
931    output is not rebuilt until they are built, but changes in order-only
932    dependencies alone do not cause the output to be rebuilt.
933 +
934 Order-only dependencies can be useful for bootstrapping dependencies
935 that are only discovered during build time: for example, to generate a
936 header file before starting a subsequent compilation step.  (Once the
937 header is used in compilation, a generated dependency file will then
938 express the implicit dependency.)
939
940 File paths are compared as is, which means that an absolute path and a
941 relative path, pointing to the same file, are considered different by Ninja.
942
943 Variable expansion
944 ~~~~~~~~~~~~~~~~~~
945
946 Variables are expanded in paths (in a `build` or `default` statement)
947 and on the right side of a `name = value` statement.
948
949 When a `name = value` statement is evaluated, its right-hand side is
950 expanded immediately (according to the below scoping rules), and
951 from then on `$name` expands to the static string as the result of the
952 expansion.  It is never the case that you'll need to "double-escape" a
953 value to prevent it from getting expanded twice.
954
955 All variables are expanded immediately as they're encountered in parsing,
956 with one important exception: variables in `rule` blocks are expanded
957 when the rule is _used_, not when it is declared.  In the following
958 example, the `demo` rule prints "this is a demo of bar".
959
960 ----
961 rule demo
962   command = echo "this is a demo of $foo"
963
964 build out: demo
965   foo = bar
966 ----
967
968 [[ref_scope]]
969 Evaluation and scoping
970 ~~~~~~~~~~~~~~~~~~~~~~
971
972 Top-level variable declarations are scoped to the file they occur in.
973
974 Rule declarations are also scoped to the file they occur in.
975 _(Available since Ninja 1.6)_
976
977 The `subninja` keyword, used to include another `.ninja` file,
978 introduces a new scope.  The included `subninja` file may use the
979 variables and rules from the parent file, and shadow their values for the file's
980 scope, but it won't affect values of the variables in the parent.
981
982 To include another `.ninja` file in the current scope, much like a C
983 `#include` statement, use `include` instead of `subninja`.
984
985 Variable declarations indented in a `build` block are scoped to the
986 `build` block.  The full lookup order for a variable expanded in a
987 `build` block (or the `rule` is uses) is:
988
989 1. Special built-in variables (`$in`, `$out`).
990
991 2. Build-level variables from the `build` block.
992
993 3. Rule-level variables from the `rule` block (i.e. `$command`).
994    (Note from the above discussion on expansion that these are
995    expanded "late", and may make use of in-scope bindings like `$in`.)
996
997 4. File-level variables from the file that the `build` line was in.
998
999 5. Variables from the file that included that file using the
1000    `subninja` keyword.