9 Ninja is yet another build system. It takes as input the
10 interdependencies of files (typically source code and output
11 executables) and orchestrates building them, _quickly_.
13 Ninja joins a sea of other build systems. Its distinguishing goal is
14 to be fast. It is born from
15 http://neugierig.org/software/chromium/notes/2011/02/ninja.html[my
16 work on the Chromium browser project], which has over 30,000 source
17 files and whose other build systems (including one built from custom
18 non-recursive Makefiles) would take ten seconds to start building
19 after changing one file. Ninja is under a second.
21 Philosophical overview
22 ~~~~~~~~~~~~~~~~~~~~~~
24 Where other build systems are high-level languages, Ninja aims to be
27 Build systems get slow when they need to make decisions. When you are
28 in a edit-compile cycle you want it to be as fast as possible -- you
29 want the build system to do the minimum work necessary to figure out
30 what needs to be built immediately.
32 Ninja contains the barest functionality necessary to describe
33 arbitrary dependency graphs. Its lack of syntax makes it impossible
34 to express complex decisions.
36 Instead, Ninja is intended to be used with a separate program
37 generating its input files. The generator program (like the
38 `./configure` found in autotools projects) can analyze system
39 dependencies and make as many decisions as possible up front so that
40 incremental builds stay fast. Going beyond autotools, even build-time
41 decisions like "which compiler flags should I use?" or "should I
42 build a debug or release-mode binary?" belong in the `.ninja` file
48 Here are the design goals of Ninja:
50 * very fast (i.e., instant) incremental builds, even for very large
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
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
68 * when convenience and speed are in conflict, prefer speed.
70 Some explicit _non-goals_:
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.
76 * built-in rules. _Out of the box, Ninja has no rules for
77 e.g. compiling C code._
79 * build-time customization of the build. _Options belong in
80 the program that generates the ninja files_.
82 * build-time decision-making ability such as conditionals or search
83 paths. _Making decisions is slow._
85 To restate, Ninja is faster than other build systems because it is
86 painfully simple. You must tell Ninja exactly what to do when you
87 create your project's `.ninja` files.
92 Ninja is closest in spirit and functionality to Make, relying on
93 simple dependencies between file timestamps.
95 But fundamentally, make has a lot of _features_: suffix rules,
96 functions, built-in rules that e.g. search for RCS files when building
97 source. Make's language was designed to be written by humans. Many
98 projects find make alone adequate for their build problems.
100 In contrast, Ninja has almost no features; just those necessary to get
101 builds correct while punting most complexity to generation of the
102 ninja input files. Ninja by itself is unlikely to be useful for most
105 Here are some of the features Ninja adds to Make. (These sorts of
106 features can often be implemented using more complicated Makefiles,
107 but they are not part of make itself.)
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.
113 * A build edge may have multiple outputs.
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.
119 * Output directories are always implicitly created before running the
120 command that relies on them.
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
126 * Builds are always run in parallel, based by default on the number of
127 CPUs your system has. Underspecified build dependencies will result
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.
136 Using Ninja for your project
137 ----------------------------
139 Ninja currently works on Unix-like systems and Windows. It's seen the
140 most testing on Linux (and has the best performance there) but it runs
141 fine on Mac OS X and FreeBSD.
143 If your project is small, Ninja's speed impact is likely unnoticeable.
144 (However, even for small projects it sometimes turns out that Ninja's
145 limited syntax forces simpler build rules that result in faster
146 builds.) Another way to say this is that if you're happy with the
147 edit-compile cycle time of your project already then Ninja won't help.
149 There are many other build systems that are more user-friendly or
150 featureful than Ninja itself. For some recommendations: the Ninja
151 author found http://gittup.org/tup/[the tup build system] influential
152 in Ninja's design, and thinks https://github.com/apenwarr/redo[redo]'s
153 design is quite clever.
155 Ninja's benefit comes from using it in conjunction with a smarter
158 https://gn.googlesource.com/gn/[gn]:: The meta-build system used to
159 generate build files for Google Chrome and related projects (v8,
160 node.js), as well as Google Fuschia. gn can generate Ninja files for
161 all platforms supported by Chrome.
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.
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,
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.
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).
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`.)
191 Environment variables
192 ~~~~~~~~~~~~~~~~~~~~~
194 Ninja supports one environment variable to control its behavior:
195 `NINJA_STATUS`, the progress status printed before the rule being run.
197 Several placeholders are available:
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.
211 The default progress status is `"[%f/%t] "` (note the trailing space
212 to separate from the build rule). Another example of possible progress status
213 could be `"[%u/%r/%f] "`.
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:
222 `query`:: dump the inputs and outputs of a given target.
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:
230 ninja -t browse --port=8000 --no-browser mytarget
233 `graph`:: output a file in the syntax used by `graphviz`, a automatic
234 graph layout tool. Use it like:
237 ninja -t graph mytarget | dot -Tpng -ograph.png
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.
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.
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.
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
268 If used like +ninja -t clean -r _rules_+ it removes all files built using
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+
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._
282 `deps`:: show all dependencies stored in the `.ninja_deps` file. When given a
283 target, show just the target's dependencies. _Available since Ninja 1.4._
285 `recompact`:: recompact the `.ninja_deps` file. _Available since Ninja 1.4._
288 Writing your own Ninja files
289 ----------------------------
291 The remainder of this manual is only useful if you are constructing
292 Ninja files yourself: for example, if you're writing a meta-build
293 system or supporting a new language.
298 Ninja evaluates a graph of dependencies between files, and runs
299 whichever commands are necessary to make your build target up to date
300 as determined by file modification times. If you are familiar with
301 Make, Ninja is very similar.
303 A build file (default name: `build.ninja`) provides a list of _rules_
304 -- short names for longer commands, like how to run the compiler --
305 along with a list of _build_ statements saying how to build files
306 using the rules -- which rule to apply to which inputs to produce
309 Conceptually, `build` statements describe the dependency graph of your
310 project, while `rule` statements describe how to generate the files
311 along a given edge of the graph.
316 Here's a basic `.ninja` file that demonstrates most of the syntax.
317 It will be used as an example for the following sections.
319 ---------------------------------
323 command = gcc $cflags -c $in -o $out
325 build foo.o: cc foo.c
326 ---------------------------------
330 Despite the non-goal of being convenient to write by hand, to keep
331 build files readable (debuggable), Ninja supports declaring shorter
332 reusable names for strings. A declaration like the following
338 can be used on the right side of an equals sign, dereferencing it with
339 a dollar sign, like this:
343 command = gcc $cflags -c $in -o $out
346 Variables can also be referenced using curly braces like `${in}`.
348 Variables might better be called "bindings", in that a given variable
349 cannot be changed, only shadowed. There is more on how shadowing works
350 later in this document.
355 Rules declare a short name for a command line. They begin with a line
356 consisting of the `rule` keyword and a name for the rule. Then
357 follows an indented set of `variable = value` lines.
359 The basic example above declares a new rule named `cc`, along with the
360 command to run. In the context of a rule, the `command` variable
361 defines the command to run, `$in` expands to the list of
362 input files (`foo.c`), and `$out` to the output files (`foo.o`) for the
363 command. A full list of special variables is provided in
364 <<ref_rule,the reference>>.
369 Build statements declare a relationship between input and output
370 files. They begin with the `build` keyword, and have the format
371 +build _outputs_: _rulename_ _inputs_+. Such a declaration says that
372 all of the output files are derived from the input files. When the
373 output files are missing or when the inputs change, Ninja will run the
374 rule to regenerate the outputs.
376 The basic example above describes how to build `foo.o`, using the `cc`
379 In the scope of a `build` block (including in the evaluation of its
380 associated `rule`), the variable `$in` is the list of inputs and the
381 variable `$out` is the list of outputs.
383 A build statement may be followed by an indented set of `key = value`
384 pairs, much like a rule. These variables will shadow any variables
385 when evaluating the variables in the command. For example:
388 cflags = -Wall -Werror
390 command = gcc $cflags -c $in -o $out
392 # If left unspecified, builds get the outer $cflags.
393 build foo.o: cc foo.c
395 # But you can shadow variables like cflags for a particular build.
396 build special.o: cc special.c
399 # The variable was only shadowed for the scope of special.o;
400 # Subsequent build lines get the outer (original) cflags.
401 build bar.o: cc bar.c
405 For more discussion of how scoping works, consult <<ref_scope,the
408 If you need more complicated information passed from the build
409 statement to the rule (for example, if the rule needs "the file
410 extension of the first input"), pass that through as an extra
411 variable, like how `cflags` is passed above.
413 If the top-level Ninja file is specified as an output of any build
414 statement and it is out of date, Ninja will rebuild and reload it
415 before building the targets requested by the user.
417 Generating Ninja files from code
418 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
420 `misc/ninja_syntax.py` in the Ninja distribution is a tiny Python
421 module to facilitate generating Ninja files. It allows you to make
422 Python calls like `ninja.rule(name='foo', command='bar',
423 depfile='$out.d')` and it will generate the appropriate syntax. Feel
424 free to just inline it into your project's build system if it's
434 The special rule name `phony` can be used to create aliases for other
435 targets. For example:
438 build foo: phony some/file/in/a/faraway/subdir/foo
441 This makes `ninja foo` build the longer path. Semantically, the
442 `phony` rule is equivalent to a plain rule where the `command` does
443 nothing, but phony rules are handled specially in that they aren't
444 printed when run, logged (see below), nor do they contribute to the
445 command count printed as part of the build process.
447 `phony` can also be used to create dummy targets for files which
448 may not exist at build time. If a phony build statement is written
449 without any dependencies, the target will be considered out of date if
450 it does not exist. Without a phony build statement, Ninja will report
451 an error if the file does not exist and is required by the build.
454 Default target statements
455 ~~~~~~~~~~~~~~~~~~~~~~~~~
457 By default, if no targets are specified on the command line, Ninja
458 will build every output that is not named as an input elsewhere.
459 You can override this behavior using a default target statement.
460 A default target statement causes Ninja to build only a given subset
461 of output files if none are specified on the command line.
463 Default target statements begin with the `default` keyword, and have
464 the format +default _targets_+. A default target statement must appear
465 after the build statement that declares the target as an output file.
466 They are cumulative, so multiple statements may be used to extend
467 the list of default targets. For example:
474 This causes Ninja to build the `foo`, `bar` and `baz` targets by
482 For each built file, Ninja keeps a log of the command used to build
483 it. Using this log Ninja can know when an existing output was built
484 with a different command line than the build files specify (i.e., the
485 command line changed) and knows to rebuild the file.
487 The log file is kept in the build root in a file called `.ninja_log`.
488 If you provide a variable named `builddir` in the outermost scope,
489 `.ninja_log` will be kept in that directory instead.
493 Version compatibility
494 ~~~~~~~~~~~~~~~~~~~~~
496 _Available since Ninja 1.2._
498 Ninja version labels follow the standard major.minor.patch format,
499 where the major version is increased on backwards-incompatible
500 syntax/behavioral changes and the minor version is increased on new
501 behaviors. Your `build.ninja` may declare a variable named
502 `ninja_required_version` that asserts the minimum Ninja version
503 required to use the generated file. For example,
506 ninja_required_version = 1.1
509 declares that the build file relies on some feature that was
510 introduced in Ninja 1.1 (perhaps the `pool` syntax), and that
511 Ninja 1.1 or greater must be used to build. Unlike other Ninja
512 variables, this version requirement is checked immediately when
513 the variable is encountered in parsing, so it's best to put it
514 at the top of the build file.
516 Ninja always warns if the major versions of Ninja and the
517 `ninja_required_version` don't match; a major version change hasn't
518 come up yet so it's difficult to predict what behavior might be
522 C/C++ header dependencies
523 ~~~~~~~~~~~~~~~~~~~~~~~~~
525 To get C/C++ header dependencies (or any other build dependency that
526 works in a similar way) correct Ninja has some extra functionality.
528 The problem with headers is that the full list of files that a given
529 source file depends on can only be discovered by the compiler:
530 different preprocessor defines and include paths cause different files
531 to be used. Some compilers can emit this information while building,
532 and Ninja can use that to get its dependencies perfect.
534 Consider: if the file has never been compiled, it must be built anyway,
535 generating the header dependencies as a side effect. If any file is
536 later modified (even in a way that changes which headers it depends
537 on) the modification will cause a rebuild as well, keeping the
538 dependencies up to date.
540 When loading these special dependencies, Ninja implicitly adds extra
541 build edges such that it is not an error if the listed dependency is
542 missing. This allows you to delete a header file and rebuild without
543 the build aborting due to a missing input.
548 `gcc` (and other compilers like `clang`) support emitting dependency
549 information in the syntax of a Makefile. (Any command that can write
550 dependencies in this form can be used, not just `gcc`.)
552 To bring this information into Ninja requires cooperation. On the
553 Ninja side, the `depfile` attribute on the `build` must point to a
554 path where this data is written. (Ninja only supports the limited
555 subset of the Makefile syntax emitted by compilers.) Then the command
556 must know to write dependencies into the `depfile` path.
557 Use it like in the following example:
562 command = gcc -MMD -MF $out.d [other gcc flags here]
565 The `-MMD` flag to `gcc` tells it to output header dependencies, and
566 the `-MF` flag tells it where to write them.
571 _(Available since Ninja 1.3.)_
573 It turns out that for large projects (and particularly on Windows,
574 where the file system is slow) loading these dependency files on
577 Ninja 1.3 can instead process dependencies just after they're generated
578 and save a compacted form of the same information in a Ninja-internal
581 Ninja supports this processing in two forms.
583 1. `deps = gcc` specifies that the tool outputs `gcc`-style dependencies
584 in the form of Makefiles. Adding this to the above example will
585 cause Ninja to process the `depfile` immediately after the
586 compilation finishes, then delete the `.d` file (which is only used
589 2. `deps = msvc` specifies that the tool outputs header dependencies
590 in the form produced by Visual Studio's compiler's
591 http://msdn.microsoft.com/en-us/library/hdkef6tk(v=vs.90).aspx[`/showIncludes`
592 flag]. Briefly, this means the tool outputs specially-formatted lines
593 to its stdout. Ninja then filters these lines from the displayed
594 output. No `depfile` attribute is necessary, but the localized string
595 in front of the the header file path. For instance
596 `msvc_deps_prefix = Note: including file:`
597 for a English Visual Studio (the default). Should be globally defined.
600 msvc_deps_prefix = Note: including file:
603 command = cl /showIncludes -c $in /Fo$out
606 If the include directory directives are using absolute paths, your depfile
607 may result in a mixture of relative and absolute paths. Paths used by other
608 build rules need to match exactly. Therefore, it is recommended to use
609 relative paths in these cases.
615 _Available since Ninja 1.1._
617 Pools allow you to allocate one or more rules or edges a finite number
618 of concurrent jobs which is more tightly restricted than the default
621 This can be useful, for example, to restrict a particular expensive rule
622 (like link steps for huge executables), or to restrict particular build
623 statements which you know perform poorly when run concurrently.
625 Each pool has a `depth` variable which is specified in the build file.
626 The pool is then referred to with the `pool` variable on either a rule
627 or a build statement.
629 No matter what pools you specify, ninja will never run more concurrent jobs
630 than the default parallelism, or the number of jobs specified on the command
634 # No more than 4 links at a time.
638 # No more than 1 heavy object at a time.
639 pool heavy_object_pool
649 # The link_pool is used here. Only 4 links will run concurrently.
650 build foo.exe: link input.obj
652 # A build statement can be exempted from its rule's pool by setting an
653 # empty pool. This effectively puts the build statement back into the default
654 # pool, which has infinite depth.
655 build other.exe: link input.obj
658 # A build statement can specify a pool directly.
659 # Only one of these builds will run at a time.
660 build heavy_object1.obj: cc heavy_obj1.cc
661 pool = heavy_object_pool
662 build heavy_object2.obj: cc heavy_obj2.cc
663 pool = heavy_object_pool
670 _Available since Ninja 1.5._
672 There exists a pre-defined pool named `console` with a depth of 1. It has
673 the special property that any task in the pool has direct access to the
674 standard input, output and error streams provided to Ninja, which are
675 normally connected to the user's console (hence the name) but could be
676 redirected. This can be useful for interactive tasks or long-running tasks
677 which produce status updates on the console (such as test suites).
679 While a task in the `console` pool is running, Ninja's regular output (such
680 as progress status and output from concurrent tasks) is buffered until
686 A file is a series of declarations. A declaration can be one of:
688 1. A rule declaration, which begins with +rule _rulename_+, and
689 then has a series of indented lines defining variables.
691 2. A build edge, which looks like +build _output1_ _output2_:
692 _rulename_ _input1_ _input2_+. +
693 Implicit dependencies may be tacked on the end with +|
694 _dependency1_ _dependency2_+. +
695 Order-only dependencies may be tacked on the end with +||
696 _dependency1_ _dependency2_+. (See <<ref_dependencies,the reference on
699 Implicit outputs _(available since Ninja 1.7)_ may be added before
700 the `:` with +| _output1_ _output2_+ and do not appear in `$out`.
701 (See <<ref_outputs,the reference on output types>>.)
703 3. Variable declarations, which look like +_variable_ = _value_+.
705 4. Default target statements, which look like +default _target1_ _target2_+.
707 5. References to more files, which look like +subninja _path_+ or
708 +include _path_+. The difference between these is explained below
709 <<ref_scope,in the discussion about scoping>>.
711 6. A pool declaration, which looks like +pool _poolname_+. Pools are explained
712 <<ref_pool, in the section on pools>>.
717 Ninja is mostly encoding agnostic, as long as the bytes Ninja cares
718 about (like slashes in paths) are ASCII. This means e.g. UTF-8 or
719 ISO-8859-1 input files ought to work.
721 Comments begin with `#` and extend to the end of the line.
723 Newlines are significant. Statements like `build foo bar` are a set
724 of space-separated tokens that end at the newline. Newlines and
725 spaces within a token must be escaped.
727 There is only one escape character, `$`, and it has the following
730 `$` followed by a newline:: escape the newline (continue the current line
731 across a line break).
733 `$` followed by text:: a variable reference.
735 `${varname}`:: alternate syntax for `$varname`.
737 `$` followed by space:: a space. (This is only necessary in lists of
738 paths, where a space would otherwise separate filenames. See below.)
740 `$:` :: a colon. (This is only necessary in `build` lines, where a colon
741 would otherwise terminate the list of outputs.)
743 `$$`:: a literal `$`.
745 A `build` or `default` statement is first parsed as a space-separated
746 list of filenames and then each name is expanded. This means that
747 spaces within a variable will result in spaces in the expanded
752 build $spaced/baz other$ file: ...
753 # The above build line has two outputs: "foo bar/baz" and "other file".
756 In a `name = value` statement, whitespace at the beginning of a value
757 is always stripped. Whitespace at the beginning of a line after a
758 line continuation is also stripped.
761 two_words_with_one_space = foo $
763 one_word_with_no_space = foo$
767 Other whitespace is only significant if it's at the beginning of a
768 line. If a line is indented more than the previous one, it's
769 considered part of its parent's scope; if it is indented less than the
770 previous one, it closes the previous scope.
776 Two variables are significant when declared in the outermost file scope.
778 `builddir`:: a directory for some Ninja output files. See <<ref_log,the
779 discussion of the build log>>. (You can also store other build output
782 `ninja_required_version`:: the minimum version of Ninja required to process
783 the build correctly. See <<ref_versioning,the discussion of versioning>>.
790 A `rule` block contains a list of `key = value` declarations that
791 affect the processing of the rule. Here is a full list of special
794 `command` (_required_):: the command line to run. Each `rule` may
795 have only one `command` declaration. See <<ref_rule_command,the next
796 section>> for more details on quoting and executing multiple commands.
798 `depfile`:: path to an optional `Makefile` that contains extra
799 _implicit dependencies_ (see <<ref_dependencies,the reference on
800 dependency types>>). This is explicitly to support C/C++ header
801 dependencies; see <<ref_headers,the full discussion>>.
803 `deps`:: _(Available since Ninja 1.3.)_ if present, must be one of
804 `gcc` or `msvc` to specify special dependency processing. See
805 <<ref_headers,the full discussion>>. The generated database is
806 stored as `.ninja_deps` in the `builddir`, see <<ref_toplevel,the
807 discussion of `builddir`>>.
809 `msvc_deps_prefix`:: _(Available since Ninja 1.5.)_ defines the string
810 which should be stripped from msvc's /showIncludes output. Only
811 needed when `deps = msvc` and no English Visual Studio version is used.
813 `description`:: a short description of the command, used to pretty-print
814 the command as it's running. The `-v` flag controls whether to print
815 the full command or its description; if a command fails, the full command
816 line will always be printed before the command's output.
818 `generator`:: if present, specifies that this rule is used to
819 re-invoke the generator program. Files built using `generator`
820 rules are treated specially in two ways: firstly, they will not be
821 rebuilt if the command line changes; and secondly, they are not
824 `in`:: the space-separated list of files provided as inputs to the build line
825 referencing this `rule`, shell-quoted if it appears in commands. (`$in` is
826 provided solely for convenience; if you need some subset or variant of this
827 list of files, just construct a new variable with that list and use
830 `in_newline`:: the same as `$in` except that multiple inputs are
831 separated by newlines rather than spaces. (For use with
832 `$rspfile_content`; this works around a bug in the MSVC linker where
833 it uses a fixed-size buffer for processing input.)
835 `out`:: the space-separated list of files provided as outputs to the build line
836 referencing this `rule`, shell-quoted if it appears in commands.
838 `restat`:: if present, causes Ninja to re-stat the command's outputs
839 after execution of the command. Each output whose modification time
840 the command did not change will be treated as though it had never
841 needed to be built. This may cause the output's reverse
842 dependencies to be removed from the list of pending build actions.
844 `rspfile`, `rspfile_content`:: if present (both), Ninja will use a
845 response file for the given command, i.e. write the selected string
846 (`rspfile_content`) to the given file (`rspfile`) before calling the
847 command and delete the file after successful execution of the
850 This is particularly useful on Windows OS, where the maximal length of
851 a command line is limited and response files must be used instead.
853 Use it like in the following example:
857 command = link.exe /OUT$out [usual link flags here] @$out.rsp
859 rspfile_content = $in
861 build myapp.exe: link a.obj b.obj [possibly many other .obj files]
865 Interpretation of the `command` variable
866 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
867 Fundamentally, command lines behave differently on Unixes and Windows.
869 On Unixes, commands are arrays of arguments. The Ninja `command`
870 variable is passed directly to `sh -c`, which is then responsible for
871 interpreting that string into an argv array. Therefore the quoting
872 rules are those of the shell, and you can use all the normal shell
873 operators, like `&&` to chain multiple commands, or `VAR=value cmd` to
874 set environment variables.
876 On Windows, commands are strings, so Ninja passes the `command` string
877 directly to `CreateProcess`. (In the common case of simply executing
878 a compiler this means there is less overhead.) Consequently the
879 quoting rules are deterimined by the called program, which on Windows
880 are usually provided by the C library. If you need shell
881 interpretation of the command (such as the use of `&&` to chain
882 multiple commands), make the command execute the Windows shell by
883 prefixing the command with `cmd /c`. Ninja may error with "invalid parameter"
884 which usually indicates that the command line length has been exceeded.
890 There are two types of build outputs which are subtly different.
892 1. _Explicit outputs_, as listed in a build line. These are
893 available as the `$out` variable in the rule.
895 This is the standard form of output to be used for e.g. the
896 object file of a compile command.
898 2. _Implicit outputs_, as listed in a build line with the syntax +|
899 _out1_ _out2_+ + before the `:` of a build line _(available since
900 Ninja 1.7)_. The semantics are identical to explicit outputs,
901 the only difference is that implicit outputs don't show up in the
904 This is for expressing outputs that don't show up on the
905 command line of the command.
911 There are three types of build dependencies which are subtly different.
913 1. _Explicit dependencies_, as listed in a build line. These are
914 available as the `$in` variable in the rule. Changes in these files
915 cause the output to be rebuilt; if these file are missing and
916 Ninja doesn't know how to build them, the build is aborted.
918 This is the standard form of dependency to be used e.g. for the
919 source file of a compile command.
921 2. _Implicit dependencies_, either as picked up from
922 a `depfile` attribute on a rule or from the syntax +| _dep1_
923 _dep2_+ on the end of a build line. The semantics are identical to
924 explicit dependencies, the only difference is that implicit dependencies
925 don't show up in the `$in` variable.
927 This is for expressing dependencies that don't show up on the
928 command line of the command; for example, for a rule that runs a
929 script, the script itself should be an implicit dependency, as
930 changes to the script should cause the output to rebuild.
932 Note that dependencies as loaded through depfiles have slightly different
933 semantics, as described in the <<ref_rule,rule reference>>.
935 3. _Order-only dependencies_, expressed with the syntax +|| _dep1_
936 _dep2_+ on the end of a build line. When these are out of date, the
937 output is not rebuilt until they are built, but changes in order-only
938 dependencies alone do not cause the output to be rebuilt.
940 Order-only dependencies can be useful for bootstrapping dependencies
941 that are only discovered during build time: for example, to generate a
942 header file before starting a subsequent compilation step. (Once the
943 header is used in compilation, a generated dependency file will then
944 express the implicit dependency.)
946 File paths are compared as is, which means that an absolute path and a
947 relative path, pointing to the same file, are considered different by Ninja.
952 Variables are expanded in paths (in a `build` or `default` statement)
953 and on the right side of a `name = value` statement.
955 When a `name = value` statement is evaluated, its right-hand side is
956 expanded immediately (according to the below scoping rules), and
957 from then on `$name` expands to the static string as the result of the
958 expansion. It is never the case that you'll need to "double-escape" a
959 value to prevent it from getting expanded twice.
961 All variables are expanded immediately as they're encountered in parsing,
962 with one important exception: variables in `rule` blocks are expanded
963 when the rule is _used_, not when it is declared. In the following
964 example, the `demo` rule prints "this is a demo of bar".
968 command = echo "this is a demo of $foo"
975 Evaluation and scoping
976 ~~~~~~~~~~~~~~~~~~~~~~
978 Top-level variable declarations are scoped to the file they occur in.
980 Rule declarations are also scoped to the file they occur in.
981 _(Available since Ninja 1.6)_
983 The `subninja` keyword, used to include another `.ninja` file,
984 introduces a new scope. The included `subninja` file may use the
985 variables and rules from the parent file, and shadow their values for the file's
986 scope, but it won't affect values of the variables in the parent.
988 To include another `.ninja` file in the current scope, much like a C
989 `#include` statement, use `include` instead of `subninja`.
991 Variable declarations indented in a `build` block are scoped to the
992 `build` block. The full lookup order for a variable expanded in a
993 `build` block (or the `rule` is uses) is:
995 1. Special built-in variables (`$in`, `$out`).
997 2. Build-level variables from the `build` block.
999 3. Rule-level variables from the `rule` block (i.e. `$command`).
1000 (Note from the above discussion on expansion that these are
1001 expanded "late", and may make use of in-scope bindings like `$in`.)
1003 4. File-level variables from the file that the `build` line was in.
1005 5. Variables from the file that included that file using the