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