1695b41eeSopenharmony_ciThe Ninja build system
2695b41eeSopenharmony_ci======================
3695b41eeSopenharmony_civ1.12.0, Apr 2024
4695b41eeSopenharmony_ci
5695b41eeSopenharmony_ci
6695b41eeSopenharmony_ciIntroduction
7695b41eeSopenharmony_ci------------
8695b41eeSopenharmony_ci
9695b41eeSopenharmony_ciNinja is yet another build system.  It takes as input the
10695b41eeSopenharmony_ciinterdependencies of files (typically source code and output
11695b41eeSopenharmony_ciexecutables) and orchestrates building them, _quickly_.
12695b41eeSopenharmony_ci
13695b41eeSopenharmony_ciNinja joins a sea of other build systems.  Its distinguishing goal is
14695b41eeSopenharmony_cito be fast.  It is born from
15695b41eeSopenharmony_cihttp://neugierig.org/software/chromium/notes/2011/02/ninja.html[my
16695b41eeSopenharmony_ciwork on the Chromium browser project], which has over 30,000 source
17695b41eeSopenharmony_cifiles and whose other build systems (including one built from custom
18695b41eeSopenharmony_cinon-recursive Makefiles) would take ten seconds to start building
19695b41eeSopenharmony_ciafter changing one file.  Ninja is under a second.
20695b41eeSopenharmony_ci
21695b41eeSopenharmony_ciPhilosophical overview
22695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~
23695b41eeSopenharmony_ci
24695b41eeSopenharmony_ciWhere other build systems are high-level languages, Ninja aims to be
25695b41eeSopenharmony_cian assembler.
26695b41eeSopenharmony_ci
27695b41eeSopenharmony_ciBuild systems get slow when they need to make decisions.  When you are
28695b41eeSopenharmony_ciin an edit-compile cycle you want it to be as fast as possible -- you
29695b41eeSopenharmony_ciwant the build system to do the minimum work necessary to figure out
30695b41eeSopenharmony_ciwhat needs to be built immediately.
31695b41eeSopenharmony_ci
32695b41eeSopenharmony_ciNinja contains the barest functionality necessary to describe
33695b41eeSopenharmony_ciarbitrary dependency graphs.  Its lack of syntax makes it impossible
34695b41eeSopenharmony_cito express complex decisions.
35695b41eeSopenharmony_ci
36695b41eeSopenharmony_ciInstead, Ninja is intended to be used with a separate program
37695b41eeSopenharmony_cigenerating its input files.  The generator program (like the
38695b41eeSopenharmony_ci`./configure` found in autotools projects) can analyze system
39695b41eeSopenharmony_cidependencies and make as many decisions as possible up front so that
40695b41eeSopenharmony_ciincremental builds stay fast.  Going beyond autotools, even build-time
41695b41eeSopenharmony_cidecisions like "which compiler flags should I use?"  or "should I
42695b41eeSopenharmony_cibuild a debug or release-mode binary?"  belong in the `.ninja` file
43695b41eeSopenharmony_cigenerator.
44695b41eeSopenharmony_ci
45695b41eeSopenharmony_ciDesign goals
46695b41eeSopenharmony_ci~~~~~~~~~~~~
47695b41eeSopenharmony_ci
48695b41eeSopenharmony_ciHere are the design goals of Ninja:
49695b41eeSopenharmony_ci
50695b41eeSopenharmony_ci* very fast (i.e., instant) incremental builds, even for very large
51695b41eeSopenharmony_ci  projects.
52695b41eeSopenharmony_ci
53695b41eeSopenharmony_ci* very little policy about how code is built.  Different projects and
54695b41eeSopenharmony_ci  higher-level build systems have different opinions about how code
55695b41eeSopenharmony_ci  should be built; for example, should built objects live alongside
56695b41eeSopenharmony_ci  the sources or should all build output go into a separate directory?
57695b41eeSopenharmony_ci  Is there a "package" rule that builds a distributable package of
58695b41eeSopenharmony_ci  the project?  Sidestep these decisions by trying to allow either to
59695b41eeSopenharmony_ci  be implemented, rather than choosing, even if that results in
60695b41eeSopenharmony_ci  more verbosity.
61695b41eeSopenharmony_ci
62695b41eeSopenharmony_ci* get dependencies correct, and in particular situations that are
63695b41eeSopenharmony_ci  difficult to get right with Makefiles (e.g. outputs need an implicit
64695b41eeSopenharmony_ci  dependency on the command line used to generate them; to build C
65695b41eeSopenharmony_ci  source code you need to use gcc's `-M` flags for header
66695b41eeSopenharmony_ci  dependencies).
67695b41eeSopenharmony_ci
68695b41eeSopenharmony_ci* when convenience and speed are in conflict, prefer speed.
69695b41eeSopenharmony_ci
70695b41eeSopenharmony_ciSome explicit _non-goals_:
71695b41eeSopenharmony_ci
72695b41eeSopenharmony_ci* convenient syntax for writing build files by hand.  _You should
73695b41eeSopenharmony_ci  generate your ninja files using another program_.  This is how we
74695b41eeSopenharmony_ci  can sidestep many policy decisions.
75695b41eeSopenharmony_ci
76695b41eeSopenharmony_ci* built-in rules. _Out of the box, Ninja has no rules for
77695b41eeSopenharmony_ci  e.g. compiling C code._
78695b41eeSopenharmony_ci
79695b41eeSopenharmony_ci* build-time customization of the build. _Options belong in
80695b41eeSopenharmony_ci  the program that generates the ninja files_.
81695b41eeSopenharmony_ci
82695b41eeSopenharmony_ci* build-time decision-making ability such as conditionals or search
83695b41eeSopenharmony_ci  paths. _Making decisions is slow._
84695b41eeSopenharmony_ci
85695b41eeSopenharmony_ciTo restate, Ninja is faster than other build systems because it is
86695b41eeSopenharmony_cipainfully simple.  You must tell Ninja exactly what to do when you
87695b41eeSopenharmony_cicreate your project's `.ninja` files.
88695b41eeSopenharmony_ci
89695b41eeSopenharmony_ciComparison to Make
90695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~
91695b41eeSopenharmony_ci
92695b41eeSopenharmony_ciNinja is closest in spirit and functionality to Make, relying on
93695b41eeSopenharmony_cisimple dependencies between file timestamps.
94695b41eeSopenharmony_ci
95695b41eeSopenharmony_ciBut fundamentally, make has a lot of _features_: suffix rules,
96695b41eeSopenharmony_cifunctions, built-in rules that e.g. search for RCS files when building
97695b41eeSopenharmony_cisource.  Make's language was designed to be written by humans.  Many
98695b41eeSopenharmony_ciprojects find make alone adequate for their build problems.
99695b41eeSopenharmony_ci
100695b41eeSopenharmony_ciIn contrast, Ninja has almost no features; just those necessary to get
101695b41eeSopenharmony_cibuilds correct while punting most complexity to generation of the
102695b41eeSopenharmony_cininja input files.  Ninja by itself is unlikely to be useful for most
103695b41eeSopenharmony_ciprojects.
104695b41eeSopenharmony_ci
105695b41eeSopenharmony_ciHere are some of the features Ninja adds to Make.  (These sorts of
106695b41eeSopenharmony_cifeatures can often be implemented using more complicated Makefiles,
107695b41eeSopenharmony_cibut they are not part of make itself.)
108695b41eeSopenharmony_ci
109695b41eeSopenharmony_ci* Ninja has special support for discovering extra dependencies at build
110695b41eeSopenharmony_ci  time, making it easy to get <<ref_headers,header dependencies>>
111695b41eeSopenharmony_ci  correct for C/C++ code.
112695b41eeSopenharmony_ci
113695b41eeSopenharmony_ci* A build edge may have multiple outputs.
114695b41eeSopenharmony_ci
115695b41eeSopenharmony_ci* Outputs implicitly depend on the command line that was used to generate
116695b41eeSopenharmony_ci  them, which means that changing e.g. compilation flags will cause
117695b41eeSopenharmony_ci  the outputs to rebuild.
118695b41eeSopenharmony_ci
119695b41eeSopenharmony_ci* Output directories are always implicitly created before running the
120695b41eeSopenharmony_ci  command that relies on them.
121695b41eeSopenharmony_ci
122695b41eeSopenharmony_ci* Rules can provide shorter descriptions of the command being run, so
123695b41eeSopenharmony_ci  you can print e.g. `CC foo.o` instead of a long command line while
124695b41eeSopenharmony_ci  building.
125695b41eeSopenharmony_ci
126695b41eeSopenharmony_ci* Builds are always run in parallel, based by default on the number of
127695b41eeSopenharmony_ci  CPUs your system has.  Underspecified build dependencies will result
128695b41eeSopenharmony_ci  in incorrect builds.
129695b41eeSopenharmony_ci
130695b41eeSopenharmony_ci* Command output is always buffered.  This means commands running in
131695b41eeSopenharmony_ci  parallel don't interleave their output, and when a command fails we
132695b41eeSopenharmony_ci  can print its failure output next to the full command line that
133695b41eeSopenharmony_ci  produced the failure.
134695b41eeSopenharmony_ci
135695b41eeSopenharmony_ci
136695b41eeSopenharmony_ciUsing Ninja for your project
137695b41eeSopenharmony_ci----------------------------
138695b41eeSopenharmony_ci
139695b41eeSopenharmony_ciNinja currently works on Unix-like systems and Windows. It's seen the
140695b41eeSopenharmony_cimost testing on Linux (and has the best performance there) but it runs
141695b41eeSopenharmony_cifine on Mac OS X and FreeBSD.
142695b41eeSopenharmony_ci
143695b41eeSopenharmony_ciIf your project is small, Ninja's speed impact is likely unnoticeable.
144695b41eeSopenharmony_ci(However, even for small projects it sometimes turns out that Ninja's
145695b41eeSopenharmony_cilimited syntax forces simpler build rules that result in faster
146695b41eeSopenharmony_cibuilds.)  Another way to say this is that if you're happy with the
147695b41eeSopenharmony_ciedit-compile cycle time of your project already then Ninja won't help.
148695b41eeSopenharmony_ci
149695b41eeSopenharmony_ciThere are many other build systems that are more user-friendly or
150695b41eeSopenharmony_cifeatureful than Ninja itself.  For some recommendations: the Ninja
151695b41eeSopenharmony_ciauthor found http://gittup.org/tup/[the tup build system] influential
152695b41eeSopenharmony_ciin Ninja's design, and thinks https://github.com/apenwarr/redo[redo]'s
153695b41eeSopenharmony_cidesign is quite clever.
154695b41eeSopenharmony_ci
155695b41eeSopenharmony_ciNinja's benefit comes from using it in conjunction with a smarter
156695b41eeSopenharmony_cimeta-build system.
157695b41eeSopenharmony_ci
158695b41eeSopenharmony_cihttps://gn.googlesource.com/gn/[gn]:: The meta-build system used to
159695b41eeSopenharmony_cigenerate build files for Google Chrome and related projects (v8,
160695b41eeSopenharmony_cinode.js), as well as Google Fuchsia.  gn can generate Ninja files for
161695b41eeSopenharmony_ciall platforms supported by Chrome.
162695b41eeSopenharmony_ci
163695b41eeSopenharmony_cihttps://cmake.org/[CMake]:: A widely used meta-build system that
164695b41eeSopenharmony_cican generate Ninja files on Linux as of CMake version 2.8.8.  Newer versions
165695b41eeSopenharmony_ciof CMake support generating Ninja files on Windows and Mac OS X too.
166695b41eeSopenharmony_ci
167695b41eeSopenharmony_cihttps://github.com/ninja-build/ninja/wiki/List-of-generators-producing-ninja-build-files[others]:: Ninja ought to fit perfectly into other meta-build software
168695b41eeSopenharmony_cilike https://premake.github.io/[premake].  If you do this work,
169695b41eeSopenharmony_ciplease let us know!
170695b41eeSopenharmony_ci
171695b41eeSopenharmony_ciRunning Ninja
172695b41eeSopenharmony_ci~~~~~~~~~~~~~
173695b41eeSopenharmony_ci
174695b41eeSopenharmony_ciRun `ninja`.  By default, it looks for a file named `build.ninja` in
175695b41eeSopenharmony_cithe current directory and builds all out-of-date targets.  You can
176695b41eeSopenharmony_cispecify which targets (files) to build as command line arguments.
177695b41eeSopenharmony_ci
178695b41eeSopenharmony_ciThere is also a special syntax `target^` for specifying a target
179695b41eeSopenharmony_cias the first output of some rule containing the source you put in
180695b41eeSopenharmony_cithe command line, if one exists. For example, if you specify target as
181695b41eeSopenharmony_ci`foo.c^` then `foo.o` will get built (assuming you have those targets
182695b41eeSopenharmony_ciin your build files).
183695b41eeSopenharmony_ci
184695b41eeSopenharmony_ci`ninja -h` prints help output.  Many of Ninja's flags intentionally
185695b41eeSopenharmony_cimatch those of Make; e.g `ninja -C build -j 20` changes into the
186695b41eeSopenharmony_ci`build` directory and runs 20 build commands in parallel.  (Note that
187695b41eeSopenharmony_ciNinja defaults to running commands in parallel anyway, so typically
188695b41eeSopenharmony_ciyou don't need to pass `-j`.)
189695b41eeSopenharmony_ci
190695b41eeSopenharmony_ci
191695b41eeSopenharmony_ciEnvironment variables
192695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~
193695b41eeSopenharmony_ci
194695b41eeSopenharmony_ciNinja supports one environment variable to control its behavior:
195695b41eeSopenharmony_ci`NINJA_STATUS`, the progress status printed before the rule being run.
196695b41eeSopenharmony_ci
197695b41eeSopenharmony_ciSeveral placeholders are available:
198695b41eeSopenharmony_ci
199695b41eeSopenharmony_ci`%s`:: The number of started edges.
200695b41eeSopenharmony_ci`%t`:: The total number of edges that must be run to complete the build.
201695b41eeSopenharmony_ci`%p`:: The percentage of started edges.
202695b41eeSopenharmony_ci`%r`:: The number of currently running edges.
203695b41eeSopenharmony_ci`%u`:: The number of remaining edges to start.
204695b41eeSopenharmony_ci`%f`:: The number of finished edges.
205695b41eeSopenharmony_ci`%o`:: Overall rate of finished edges per second
206695b41eeSopenharmony_ci`%c`:: Current rate of finished edges per second (average over builds
207695b41eeSopenharmony_cispecified by `-j` or its default)
208695b41eeSopenharmony_ci`%e`:: Elapsed time in seconds. _(Available since Ninja 1.2.)_
209695b41eeSopenharmony_ci`%E`:: Remaining time (ETA) in seconds. _(Available since Ninja 1.12.)_
210695b41eeSopenharmony_ci`%w`:: Elapsed time in [h:]mm:ss format. _(Available since Ninja 1.12.)_
211695b41eeSopenharmony_ci`%W`:: Remaining time (ETA) in [h:]mm:ss format. _(Available since Ninja 1.12.)_
212695b41eeSopenharmony_ci`%P`:: The percentage (in ppp% format) of time elapsed out of predicted total runtime. _(Available since Ninja 1.12.)_
213695b41eeSopenharmony_ci`%%`:: A plain `%` character.
214695b41eeSopenharmony_ci
215695b41eeSopenharmony_ciThe default progress status is `"[%f/%t] "` (note the trailing space
216695b41eeSopenharmony_cito separate from the build rule). Another example of possible progress status
217695b41eeSopenharmony_cicould be `"[%u/%r/%f] "`.
218695b41eeSopenharmony_ci
219695b41eeSopenharmony_ciExtra tools
220695b41eeSopenharmony_ci~~~~~~~~~~~
221695b41eeSopenharmony_ci
222695b41eeSopenharmony_ciThe `-t` flag on the Ninja command line runs some tools that we have
223695b41eeSopenharmony_cifound useful during Ninja's development.  The current tools are:
224695b41eeSopenharmony_ci
225695b41eeSopenharmony_ci[horizontal]
226695b41eeSopenharmony_ci`query`:: dump the inputs and outputs of a given target.
227695b41eeSopenharmony_ci
228695b41eeSopenharmony_ci`browse`:: browse the dependency graph in a web browser.  Clicking a
229695b41eeSopenharmony_cifile focuses the view on that file, showing inputs and outputs.  This
230695b41eeSopenharmony_cifeature requires a Python installation. By default, port 8000 is used
231695b41eeSopenharmony_ciand a web browser will be opened. This can be changed as follows:
232695b41eeSopenharmony_ci+
233695b41eeSopenharmony_ci----
234695b41eeSopenharmony_cininja -t browse --port=8000 --no-browser mytarget
235695b41eeSopenharmony_ci----
236695b41eeSopenharmony_ci+
237695b41eeSopenharmony_ci`graph`:: output a file in the syntax used by `graphviz`, an automatic
238695b41eeSopenharmony_cigraph layout tool.  Use it like:
239695b41eeSopenharmony_ci+
240695b41eeSopenharmony_ci----
241695b41eeSopenharmony_cininja -t graph mytarget | dot -Tpng -ograph.png
242695b41eeSopenharmony_ci----
243695b41eeSopenharmony_ci+
244695b41eeSopenharmony_ciIn the Ninja source tree, `ninja graph.png`
245695b41eeSopenharmony_cigenerates an image for Ninja itself.  If no target is given generate a
246695b41eeSopenharmony_cigraph for all root targets.
247695b41eeSopenharmony_ci
248695b41eeSopenharmony_ci`targets`:: output a list of targets either by rule or by depth.  If used
249695b41eeSopenharmony_cilike +ninja -t targets rule _name_+ it prints the list of targets
250695b41eeSopenharmony_ciusing the given rule to be built.  If no rule is given, it prints the source
251695b41eeSopenharmony_cifiles (the leaves of the graph).  If used like
252695b41eeSopenharmony_ci+ninja -t targets depth _digit_+ it
253695b41eeSopenharmony_ciprints the list of targets in a depth-first manner starting by the root
254695b41eeSopenharmony_citargets (the ones with no outputs). Indentation is used to mark dependencies.
255695b41eeSopenharmony_ciIf the depth is zero it prints all targets. If no arguments are provided
256695b41eeSopenharmony_ci+ninja -t targets depth 1+ is assumed. In this mode targets may be listed
257695b41eeSopenharmony_ciseveral times. If used like this +ninja -t targets all+ it
258695b41eeSopenharmony_ciprints all the targets available without indentation and it is faster
259695b41eeSopenharmony_cithan the _depth_ mode.
260695b41eeSopenharmony_ci
261695b41eeSopenharmony_ci`commands`:: given a list of targets, print a list of commands which, if
262695b41eeSopenharmony_ciexecuted in order, may be used to rebuild those targets, assuming that all
263695b41eeSopenharmony_cioutput files are out of date.
264695b41eeSopenharmony_ci
265695b41eeSopenharmony_ci`inputs`:: given a list of targets, print a list of all inputs used to
266695b41eeSopenharmony_cirebuild those targets.
267695b41eeSopenharmony_ci_Available since Ninja 1.11._
268695b41eeSopenharmony_ci
269695b41eeSopenharmony_ci`clean`:: remove built files. By default, it removes all built files
270695b41eeSopenharmony_ciexcept for those created by the generator.  Adding the `-g` flag also
271695b41eeSopenharmony_ciremoves built files created by the generator (see <<ref_rule,the rule
272695b41eeSopenharmony_cireference for the +generator+ attribute>>).  Additional arguments are
273695b41eeSopenharmony_citargets, which removes the given targets and recursively all files
274695b41eeSopenharmony_cibuilt for them.
275695b41eeSopenharmony_ci+
276695b41eeSopenharmony_ciIf used like +ninja -t clean -r _rules_+ it removes all files built using
277695b41eeSopenharmony_cithe given rules.
278695b41eeSopenharmony_ci+
279695b41eeSopenharmony_ciFiles created but not referenced in the graph are not removed. This
280695b41eeSopenharmony_citool takes in account the +-v+ and the +-n+ options (note that +-n+
281695b41eeSopenharmony_ciimplies +-v+).
282695b41eeSopenharmony_ci
283695b41eeSopenharmony_ci`cleandead`:: remove files produced by previous builds that are no longer in the
284695b41eeSopenharmony_cibuild file. _Available since Ninja 1.10._
285695b41eeSopenharmony_ci
286695b41eeSopenharmony_ci`compdb`:: given a list of rules, each of which is expected to be a
287695b41eeSopenharmony_ciC family language compiler rule whose first input is the name of the
288695b41eeSopenharmony_cisource file, prints on standard output a compilation database in the
289695b41eeSopenharmony_cihttp://clang.llvm.org/docs/JSONCompilationDatabase.html[JSON format] expected
290695b41eeSopenharmony_ciby the Clang tooling interface.
291695b41eeSopenharmony_ci_Available since Ninja 1.2._
292695b41eeSopenharmony_ci
293695b41eeSopenharmony_ci`deps`:: show all dependencies stored in the `.ninja_deps` file. When given a
294695b41eeSopenharmony_citarget, show just the target's dependencies. _Available since Ninja 1.4._
295695b41eeSopenharmony_ci
296695b41eeSopenharmony_ci`missingdeps`:: given a list of targets, look for targets that depend on
297695b41eeSopenharmony_cia generated file, but do not have a properly (possibly transitive) dependency
298695b41eeSopenharmony_cion the generator.  Such targets may cause build flakiness on clean builds.
299695b41eeSopenharmony_ci+
300695b41eeSopenharmony_ciThe broken targets can be found assuming deps log / depfile dependency
301695b41eeSopenharmony_ciinformation is correct.  Any target that depends on a generated file (output
302695b41eeSopenharmony_ciof a generator-target) implicitly, but does not have an explicit or order-only
303695b41eeSopenharmony_cidependency path to the generator-target, is considered broken.
304695b41eeSopenharmony_ci+
305695b41eeSopenharmony_ciThe tool's findings can be verified by trying to build the listed targets in
306695b41eeSopenharmony_cia clean outdir without building any other targets.  The build should fail for
307695b41eeSopenharmony_cieach of them with a missing include error or equivalent pointing to the
308695b41eeSopenharmony_cigenerated file.
309695b41eeSopenharmony_ci_Available since Ninja 1.11._
310695b41eeSopenharmony_ci
311695b41eeSopenharmony_ci`recompact`:: recompact the `.ninja_deps` file. _Available since Ninja 1.4._
312695b41eeSopenharmony_ci
313695b41eeSopenharmony_ci`restat`:: updates all recorded file modification timestamps in the `.ninja_log`
314695b41eeSopenharmony_cifile. _Available since Ninja 1.10._
315695b41eeSopenharmony_ci
316695b41eeSopenharmony_ci`rules`:: output the list of all rules. It can be used to know which rule name
317695b41eeSopenharmony_cito pass to +ninja -t targets rule _name_+ or +ninja -t compdb+. Adding the `-d`
318695b41eeSopenharmony_ciflag also prints the description of the rules.
319695b41eeSopenharmony_ci
320695b41eeSopenharmony_ci`msvc`:: Available on Windows hosts only.
321695b41eeSopenharmony_ciHelper tool to invoke the `cl.exe` compiler with a pre-defined set of
322695b41eeSopenharmony_cienvironment variables, as in:
323695b41eeSopenharmony_ci+
324695b41eeSopenharmony_ci----
325695b41eeSopenharmony_cininja -t msvc -e ENVFILE -- cl.exe <arguments>
326695b41eeSopenharmony_ci----
327695b41eeSopenharmony_ci+
328695b41eeSopenharmony_ciWhere `ENVFILE` is a binary file that contains an environment block suitable
329695b41eeSopenharmony_cifor CreateProcessA() on Windows (i.e. a series of zero-terminated strings that
330695b41eeSopenharmony_cilook like NAME=VALUE, followed by an extra zero terminator). Note that this uses
331695b41eeSopenharmony_cithe local codepage encoding.
332695b41eeSopenharmony_ci+
333695b41eeSopenharmony_ciThis tool also supports a deprecated way of parsing the compiler's output when
334695b41eeSopenharmony_cithe `/showIncludes` flag is used, and generating a GCC-compatible depfile from it:
335695b41eeSopenharmony_ci+
336695b41eeSopenharmony_ci----
337695b41eeSopenharmony_cininja -t msvc -o DEPFILE [-p STRING] -- cl.exe /showIncludes <arguments>
338695b41eeSopenharmony_ci----
339695b41eeSopenharmony_ci+
340695b41eeSopenharmony_ciWhen using this option, `-p STRING` can be used to pass the localized line prefix
341695b41eeSopenharmony_cithat `cl.exe` uses to output dependency information. For English-speaking regions
342695b41eeSopenharmony_cithis is `"Note: including file: "` without the double quotes, but will be different
343695b41eeSopenharmony_cifor other regions.
344695b41eeSopenharmony_ci+
345695b41eeSopenharmony_ciNote that Ninja supports this natively now, with the use of `deps = msvc` and
346695b41eeSopenharmony_ci`msvc_deps_prefix` in Ninja files. Native support also avoids launching an extra
347695b41eeSopenharmony_citool process each time the compiler must be called, which can speed up builds
348695b41eeSopenharmony_cinoticeably on Windows.
349695b41eeSopenharmony_ci
350695b41eeSopenharmony_ci`wincodepage`:: Available on Windows hosts (_since Ninja 1.11_).
351695b41eeSopenharmony_ciPrints the Windows code page whose encoding is expected in the build file.
352695b41eeSopenharmony_ciThe output has the form:
353695b41eeSopenharmony_ci+
354695b41eeSopenharmony_ci----
355695b41eeSopenharmony_ciBuild file encoding: <codepage>
356695b41eeSopenharmony_ci----
357695b41eeSopenharmony_ci+
358695b41eeSopenharmony_ciAdditional lines may be added in future versions of Ninja.
359695b41eeSopenharmony_ci+
360695b41eeSopenharmony_ciThe `<codepage>` is one of:
361695b41eeSopenharmony_ci
362695b41eeSopenharmony_ci`UTF-8`::: Encode as UTF-8.
363695b41eeSopenharmony_ci
364695b41eeSopenharmony_ci`ANSI`::: Encode to the system-wide ANSI code page.
365695b41eeSopenharmony_ci
366695b41eeSopenharmony_ciWriting your own Ninja files
367695b41eeSopenharmony_ci----------------------------
368695b41eeSopenharmony_ci
369695b41eeSopenharmony_ciThe remainder of this manual is only useful if you are constructing
370695b41eeSopenharmony_ciNinja files yourself: for example, if you're writing a meta-build
371695b41eeSopenharmony_cisystem or supporting a new language.
372695b41eeSopenharmony_ci
373695b41eeSopenharmony_ciConceptual overview
374695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~~
375695b41eeSopenharmony_ci
376695b41eeSopenharmony_ciNinja evaluates a graph of dependencies between files, and runs
377695b41eeSopenharmony_ciwhichever commands are necessary to make your build target up to date
378695b41eeSopenharmony_cias determined by file modification times.  If you are familiar with
379695b41eeSopenharmony_ciMake, Ninja is very similar.
380695b41eeSopenharmony_ci
381695b41eeSopenharmony_ciA build file (default name: `build.ninja`) provides a list of _rules_
382695b41eeSopenharmony_ci-- short names for longer commands, like how to run the compiler --
383695b41eeSopenharmony_cialong with a list of _build_ statements saying how to build files
384695b41eeSopenharmony_ciusing the rules -- which rule to apply to which inputs to produce
385695b41eeSopenharmony_ciwhich outputs.
386695b41eeSopenharmony_ci
387695b41eeSopenharmony_ciConceptually, `build` statements describe the dependency graph of your
388695b41eeSopenharmony_ciproject, while `rule` statements describe how to generate the files
389695b41eeSopenharmony_cialong a given edge of the graph.
390695b41eeSopenharmony_ci
391695b41eeSopenharmony_ciSyntax example
392695b41eeSopenharmony_ci~~~~~~~~~~~~~~
393695b41eeSopenharmony_ci
394695b41eeSopenharmony_ciHere's a basic `.ninja` file that demonstrates most of the syntax.
395695b41eeSopenharmony_ciIt will be used as an example for the following sections.
396695b41eeSopenharmony_ci
397695b41eeSopenharmony_ci---------------------------------
398695b41eeSopenharmony_cicflags = -Wall
399695b41eeSopenharmony_ci
400695b41eeSopenharmony_cirule cc
401695b41eeSopenharmony_ci  command = gcc $cflags -c $in -o $out
402695b41eeSopenharmony_ci
403695b41eeSopenharmony_cibuild foo.o: cc foo.c
404695b41eeSopenharmony_ci---------------------------------
405695b41eeSopenharmony_ci
406695b41eeSopenharmony_ciVariables
407695b41eeSopenharmony_ci~~~~~~~~~
408695b41eeSopenharmony_ciDespite the non-goal of being convenient to write by hand, to keep
409695b41eeSopenharmony_cibuild files readable (debuggable), Ninja supports declaring shorter
410695b41eeSopenharmony_cireusable names for strings.  A declaration like the following
411695b41eeSopenharmony_ci
412695b41eeSopenharmony_ci----------------
413695b41eeSopenharmony_cicflags = -g
414695b41eeSopenharmony_ci----------------
415695b41eeSopenharmony_ci
416695b41eeSopenharmony_cican be used on the right side of an equals sign, dereferencing it with
417695b41eeSopenharmony_cia dollar sign, like this:
418695b41eeSopenharmony_ci
419695b41eeSopenharmony_ci----------------
420695b41eeSopenharmony_cirule cc
421695b41eeSopenharmony_ci  command = gcc $cflags -c $in -o $out
422695b41eeSopenharmony_ci----------------
423695b41eeSopenharmony_ci
424695b41eeSopenharmony_ciVariables can also be referenced using curly braces like `${in}`.
425695b41eeSopenharmony_ci
426695b41eeSopenharmony_ciVariables might better be called "bindings", in that a given variable
427695b41eeSopenharmony_cicannot be changed, only shadowed.  There is more on how shadowing works
428695b41eeSopenharmony_cilater in this document.
429695b41eeSopenharmony_ci
430695b41eeSopenharmony_ciRules
431695b41eeSopenharmony_ci~~~~~
432695b41eeSopenharmony_ci
433695b41eeSopenharmony_ciRules declare a short name for a command line.  They begin with a line
434695b41eeSopenharmony_ciconsisting of the `rule` keyword and a name for the rule.  Then
435695b41eeSopenharmony_cifollows an indented set of `variable = value` lines.
436695b41eeSopenharmony_ci
437695b41eeSopenharmony_ciThe basic example above declares a new rule named `cc`, along with the
438695b41eeSopenharmony_cicommand to run.  In the context of a rule, the `command` variable
439695b41eeSopenharmony_cidefines the command to run, `$in` expands to the list of
440695b41eeSopenharmony_ciinput files (`foo.c`), and `$out` to the output files (`foo.o`) for the
441695b41eeSopenharmony_cicommand.  A full list of special variables is provided in
442695b41eeSopenharmony_ci<<ref_rule,the reference>>.
443695b41eeSopenharmony_ci
444695b41eeSopenharmony_ciBuild statements
445695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~
446695b41eeSopenharmony_ci
447695b41eeSopenharmony_ciBuild statements declare a relationship between input and output
448695b41eeSopenharmony_cifiles.  They begin with the `build` keyword, and have the format
449695b41eeSopenharmony_ci+build _outputs_: _rulename_ _inputs_+.  Such a declaration says that
450695b41eeSopenharmony_ciall of the output files are derived from the input files.  When the
451695b41eeSopenharmony_cioutput files are missing or when the inputs change, Ninja will run the
452695b41eeSopenharmony_cirule to regenerate the outputs.
453695b41eeSopenharmony_ci
454695b41eeSopenharmony_ciThe basic example above describes how to build `foo.o`, using the `cc`
455695b41eeSopenharmony_cirule.
456695b41eeSopenharmony_ci
457695b41eeSopenharmony_ciIn the scope of a `build` block (including in the evaluation of its
458695b41eeSopenharmony_ciassociated `rule`), the variable `$in` is the list of inputs and the
459695b41eeSopenharmony_civariable `$out` is the list of outputs.
460695b41eeSopenharmony_ci
461695b41eeSopenharmony_ciA build statement may be followed by an indented set of `key = value`
462695b41eeSopenharmony_cipairs, much like a rule.  These variables will shadow any variables
463695b41eeSopenharmony_ciwhen evaluating the variables in the command.  For example:
464695b41eeSopenharmony_ci
465695b41eeSopenharmony_ci----------------
466695b41eeSopenharmony_cicflags = -Wall -Werror
467695b41eeSopenharmony_cirule cc
468695b41eeSopenharmony_ci  command = gcc $cflags -c $in -o $out
469695b41eeSopenharmony_ci
470695b41eeSopenharmony_ci# If left unspecified, builds get the outer $cflags.
471695b41eeSopenharmony_cibuild foo.o: cc foo.c
472695b41eeSopenharmony_ci
473695b41eeSopenharmony_ci# But you can shadow variables like cflags for a particular build.
474695b41eeSopenharmony_cibuild special.o: cc special.c
475695b41eeSopenharmony_ci  cflags = -Wall
476695b41eeSopenharmony_ci
477695b41eeSopenharmony_ci# The variable was only shadowed for the scope of special.o;
478695b41eeSopenharmony_ci# Subsequent build lines get the outer (original) cflags.
479695b41eeSopenharmony_cibuild bar.o: cc bar.c
480695b41eeSopenharmony_ci
481695b41eeSopenharmony_ci----------------
482695b41eeSopenharmony_ci
483695b41eeSopenharmony_ciFor more discussion of how scoping works, consult <<ref_scope,the
484695b41eeSopenharmony_cireference>>.
485695b41eeSopenharmony_ci
486695b41eeSopenharmony_ciIf you need more complicated information passed from the build
487695b41eeSopenharmony_cistatement to the rule (for example, if the rule needs "the file
488695b41eeSopenharmony_ciextension of the first input"), pass that through as an extra
489695b41eeSopenharmony_civariable, like how `cflags` is passed above.
490695b41eeSopenharmony_ci
491695b41eeSopenharmony_ciIf the top-level Ninja file is specified as an output of any build
492695b41eeSopenharmony_cistatement and it is out of date, Ninja will rebuild and reload it
493695b41eeSopenharmony_cibefore building the targets requested by the user.
494695b41eeSopenharmony_ci
495695b41eeSopenharmony_ciGenerating Ninja files from code
496695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
497695b41eeSopenharmony_ci
498695b41eeSopenharmony_ci`misc/ninja_syntax.py` in the Ninja distribution is a tiny Python
499695b41eeSopenharmony_cimodule to facilitate generating Ninja files.  It allows you to make
500695b41eeSopenharmony_ciPython calls like `ninja.rule(name='foo', command='bar',
501695b41eeSopenharmony_cidepfile='$out.d')` and it will generate the appropriate syntax.  Feel
502695b41eeSopenharmony_cifree to just inline it into your project's build system if it's
503695b41eeSopenharmony_ciuseful.
504695b41eeSopenharmony_ci
505695b41eeSopenharmony_ci
506695b41eeSopenharmony_ciMore details
507695b41eeSopenharmony_ci------------
508695b41eeSopenharmony_ci
509695b41eeSopenharmony_ciThe `phony` rule
510695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~
511695b41eeSopenharmony_ci
512695b41eeSopenharmony_ciThe special rule name `phony` can be used to create aliases for other
513695b41eeSopenharmony_citargets.  For example:
514695b41eeSopenharmony_ci
515695b41eeSopenharmony_ci----------------
516695b41eeSopenharmony_cibuild foo: phony some/file/in/a/faraway/subdir/foo
517695b41eeSopenharmony_ci----------------
518695b41eeSopenharmony_ci
519695b41eeSopenharmony_ciThis makes `ninja foo` build the longer path.  Semantically, the
520695b41eeSopenharmony_ci`phony` rule is equivalent to a plain rule where the `command` does
521695b41eeSopenharmony_cinothing, but phony rules are handled specially in that they aren't
522695b41eeSopenharmony_ciprinted when run, logged (see below), nor do they contribute to the
523695b41eeSopenharmony_cicommand count printed as part of the build process.
524695b41eeSopenharmony_ci
525695b41eeSopenharmony_ciWhen a `phony` target is used as an input to another build rule, the
526695b41eeSopenharmony_ciother build rule will, semantically, consider the inputs of the
527695b41eeSopenharmony_ci`phony` rule as its own. Therefore, `phony` rules can be used to group
528695b41eeSopenharmony_ciinputs, e.g. header files.
529695b41eeSopenharmony_ci
530695b41eeSopenharmony_ci`phony` can also be used to create dummy targets for files which
531695b41eeSopenharmony_cimay not exist at build time.  If a phony build statement is written
532695b41eeSopenharmony_ciwithout any dependencies, the target will be considered out of date if
533695b41eeSopenharmony_ciit does not exist.  Without a phony build statement, Ninja will report
534695b41eeSopenharmony_cian error if the file does not exist and is required by the build.
535695b41eeSopenharmony_ci
536695b41eeSopenharmony_ciTo create a rule that never rebuilds, use a build rule without any input:
537695b41eeSopenharmony_ci----------------
538695b41eeSopenharmony_cirule touch
539695b41eeSopenharmony_ci  command = touch $out
540695b41eeSopenharmony_cibuild file_that_always_exists.dummy: touch
541695b41eeSopenharmony_cibuild dummy_target_to_follow_a_pattern: phony file_that_always_exists.dummy
542695b41eeSopenharmony_ci----------------
543695b41eeSopenharmony_ci
544695b41eeSopenharmony_ci
545695b41eeSopenharmony_ciDefault target statements
546695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~
547695b41eeSopenharmony_ci
548695b41eeSopenharmony_ciBy default, if no targets are specified on the command line, Ninja
549695b41eeSopenharmony_ciwill build every output that is not named as an input elsewhere.
550695b41eeSopenharmony_ciYou can override this behavior using a default target statement.
551695b41eeSopenharmony_ciA default target statement causes Ninja to build only a given subset
552695b41eeSopenharmony_ciof output files if none are specified on the command line.
553695b41eeSopenharmony_ci
554695b41eeSopenharmony_ciDefault target statements begin with the `default` keyword, and have
555695b41eeSopenharmony_cithe format +default _targets_+.  A default target statement must appear
556695b41eeSopenharmony_ciafter the build statement that declares the target as an output file.
557695b41eeSopenharmony_ciThey are cumulative, so multiple statements may be used to extend
558695b41eeSopenharmony_cithe list of default targets.  For example:
559695b41eeSopenharmony_ci
560695b41eeSopenharmony_ci----------------
561695b41eeSopenharmony_cidefault foo bar
562695b41eeSopenharmony_cidefault baz
563695b41eeSopenharmony_ci----------------
564695b41eeSopenharmony_ci
565695b41eeSopenharmony_ciThis causes Ninja to build the `foo`, `bar` and `baz` targets by
566695b41eeSopenharmony_cidefault.
567695b41eeSopenharmony_ci
568695b41eeSopenharmony_ci
569695b41eeSopenharmony_ci[[ref_log]]
570695b41eeSopenharmony_ciThe Ninja log
571695b41eeSopenharmony_ci~~~~~~~~~~~~~
572695b41eeSopenharmony_ci
573695b41eeSopenharmony_ciFor each built file, Ninja keeps a log of the command used to build
574695b41eeSopenharmony_ciit.  Using this log Ninja can know when an existing output was built
575695b41eeSopenharmony_ciwith a different command line than the build files specify (i.e., the
576695b41eeSopenharmony_cicommand line changed) and knows to rebuild the file.
577695b41eeSopenharmony_ci
578695b41eeSopenharmony_ciThe log file is kept in the build root in a file called `.ninja_log`.
579695b41eeSopenharmony_ciIf you provide a variable named `builddir` in the outermost scope,
580695b41eeSopenharmony_ci`.ninja_log` will be kept in that directory instead.
581695b41eeSopenharmony_ci
582695b41eeSopenharmony_ci
583695b41eeSopenharmony_ci[[ref_versioning]]
584695b41eeSopenharmony_ciVersion compatibility
585695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~
586695b41eeSopenharmony_ci
587695b41eeSopenharmony_ci_Available since Ninja 1.2._
588695b41eeSopenharmony_ci
589695b41eeSopenharmony_ciNinja version labels follow the standard major.minor.patch format,
590695b41eeSopenharmony_ciwhere the major version is increased on backwards-incompatible
591695b41eeSopenharmony_cisyntax/behavioral changes and the minor version is increased on new
592695b41eeSopenharmony_cibehaviors.  Your `build.ninja` may declare a variable named
593695b41eeSopenharmony_ci`ninja_required_version` that asserts the minimum Ninja version
594695b41eeSopenharmony_cirequired to use the generated file.  For example,
595695b41eeSopenharmony_ci
596695b41eeSopenharmony_ci-----
597695b41eeSopenharmony_cininja_required_version = 1.1
598695b41eeSopenharmony_ci-----
599695b41eeSopenharmony_ci
600695b41eeSopenharmony_cideclares that the build file relies on some feature that was
601695b41eeSopenharmony_ciintroduced in Ninja 1.1 (perhaps the `pool` syntax), and that
602695b41eeSopenharmony_ciNinja 1.1 or greater must be used to build.  Unlike other Ninja
603695b41eeSopenharmony_civariables, this version requirement is checked immediately when
604695b41eeSopenharmony_cithe variable is encountered in parsing, so it's best to put it
605695b41eeSopenharmony_ciat the top of the build file.
606695b41eeSopenharmony_ci
607695b41eeSopenharmony_ciNinja always warns if the major versions of Ninja and the
608695b41eeSopenharmony_ci`ninja_required_version` don't match; a major version change hasn't
609695b41eeSopenharmony_cicome up yet so it's difficult to predict what behavior might be
610695b41eeSopenharmony_cirequired.
611695b41eeSopenharmony_ci
612695b41eeSopenharmony_ci[[ref_headers]]
613695b41eeSopenharmony_ciC/C++ header dependencies
614695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~
615695b41eeSopenharmony_ci
616695b41eeSopenharmony_ciTo get C/C++ header dependencies (or any other build dependency that
617695b41eeSopenharmony_ciworks in a similar way) correct Ninja has some extra functionality.
618695b41eeSopenharmony_ci
619695b41eeSopenharmony_ciThe problem with headers is that the full list of files that a given
620695b41eeSopenharmony_cisource file depends on can only be discovered by the compiler:
621695b41eeSopenharmony_cidifferent preprocessor defines and include paths cause different files
622695b41eeSopenharmony_cito be used.  Some compilers can emit this information while building,
623695b41eeSopenharmony_ciand Ninja can use that to get its dependencies perfect.
624695b41eeSopenharmony_ci
625695b41eeSopenharmony_ciConsider: if the file has never been compiled, it must be built anyway,
626695b41eeSopenharmony_cigenerating the header dependencies as a side effect.  If any file is
627695b41eeSopenharmony_cilater modified (even in a way that changes which headers it depends
628695b41eeSopenharmony_cion) the modification will cause a rebuild as well, keeping the
629695b41eeSopenharmony_cidependencies up to date.
630695b41eeSopenharmony_ci
631695b41eeSopenharmony_ciWhen loading these special dependencies, Ninja implicitly adds extra
632695b41eeSopenharmony_cibuild edges such that it is not an error if the listed dependency is
633695b41eeSopenharmony_cimissing.  This allows you to delete a header file and rebuild without
634695b41eeSopenharmony_cithe build aborting due to a missing input.
635695b41eeSopenharmony_ci
636695b41eeSopenharmony_cidepfile
637695b41eeSopenharmony_ci^^^^^^^
638695b41eeSopenharmony_ci
639695b41eeSopenharmony_ci`gcc` (and other compilers like `clang`) support emitting dependency
640695b41eeSopenharmony_ciinformation in the syntax of a Makefile.  (Any command that can write
641695b41eeSopenharmony_cidependencies in this form can be used, not just `gcc`.)
642695b41eeSopenharmony_ci
643695b41eeSopenharmony_ciTo bring this information into Ninja requires cooperation.  On the
644695b41eeSopenharmony_ciNinja side, the `depfile` attribute on the `build` must point to a
645695b41eeSopenharmony_cipath where this data is written.  (Ninja only supports the limited
646695b41eeSopenharmony_cisubset of the Makefile syntax emitted by compilers.)  Then the command
647695b41eeSopenharmony_cimust know to write dependencies into the `depfile` path.
648695b41eeSopenharmony_ciUse it like in the following example:
649695b41eeSopenharmony_ci
650695b41eeSopenharmony_ci----
651695b41eeSopenharmony_cirule cc
652695b41eeSopenharmony_ci  depfile = $out.d
653695b41eeSopenharmony_ci  command = gcc -MD -MF $out.d [other gcc flags here]
654695b41eeSopenharmony_ci----
655695b41eeSopenharmony_ci
656695b41eeSopenharmony_ciThe `-MD` flag to `gcc` tells it to output header dependencies, and
657695b41eeSopenharmony_cithe `-MF` flag tells it where to write them.
658695b41eeSopenharmony_ci
659695b41eeSopenharmony_cideps
660695b41eeSopenharmony_ci^^^^
661695b41eeSopenharmony_ci
662695b41eeSopenharmony_ci_(Available since Ninja 1.3.)_
663695b41eeSopenharmony_ci
664695b41eeSopenharmony_ciIt turns out that for large projects (and particularly on Windows,
665695b41eeSopenharmony_ciwhere the file system is slow) loading these dependency files on
666695b41eeSopenharmony_cistartup is slow.
667695b41eeSopenharmony_ci
668695b41eeSopenharmony_ciNinja 1.3 can instead process dependencies just after they're generated
669695b41eeSopenharmony_ciand save a compacted form of the same information in a Ninja-internal
670695b41eeSopenharmony_cidatabase.
671695b41eeSopenharmony_ci
672695b41eeSopenharmony_ciNinja supports this processing in two forms.
673695b41eeSopenharmony_ci
674695b41eeSopenharmony_ci1. `deps = gcc` specifies that the tool outputs `gcc`-style dependencies
675695b41eeSopenharmony_ci   in the form of Makefiles.  Adding this to the above example will
676695b41eeSopenharmony_ci   cause Ninja to process the `depfile` immediately after the
677695b41eeSopenharmony_ci   compilation finishes, then delete the `.d` file (which is only used
678695b41eeSopenharmony_ci   as a temporary).
679695b41eeSopenharmony_ci
680695b41eeSopenharmony_ci2. `deps = msvc` specifies that the tool outputs header dependencies
681695b41eeSopenharmony_ci   in the form produced by the Visual Studio compiler's
682695b41eeSopenharmony_ci   http://msdn.microsoft.com/en-us/library/hdkef6tk(v=vs.90).aspx[`/showIncludes`
683695b41eeSopenharmony_ci   flag].  Briefly, this means the tool outputs specially-formatted lines
684695b41eeSopenharmony_ci   to its stdout.  Ninja then filters these lines from the displayed
685695b41eeSopenharmony_ci   output.  No `depfile` attribute is necessary, but the localized string
686695b41eeSopenharmony_ci   in front of the header file path should be globally defined. For instance,
687695b41eeSopenharmony_ci   `msvc_deps_prefix = Note: including file:`
688695b41eeSopenharmony_ci   for an English Visual Studio (the default).
689695b41eeSopenharmony_ci+
690695b41eeSopenharmony_ci----
691695b41eeSopenharmony_cimsvc_deps_prefix = Note: including file:
692695b41eeSopenharmony_cirule cc
693695b41eeSopenharmony_ci  deps = msvc
694695b41eeSopenharmony_ci  command = cl /showIncludes -c $in /Fo$out
695695b41eeSopenharmony_ci----
696695b41eeSopenharmony_ci
697695b41eeSopenharmony_ciIf the include directory directives are using absolute paths, your depfile
698695b41eeSopenharmony_cimay result in a mixture of relative and absolute paths. Paths used by other
699695b41eeSopenharmony_cibuild rules need to match exactly. Therefore, it is recommended to use
700695b41eeSopenharmony_cirelative paths in these cases.
701695b41eeSopenharmony_ci
702695b41eeSopenharmony_ci[[ref_pool]]
703695b41eeSopenharmony_ciPools
704695b41eeSopenharmony_ci~~~~~
705695b41eeSopenharmony_ci
706695b41eeSopenharmony_ci_Available since Ninja 1.1._
707695b41eeSopenharmony_ci
708695b41eeSopenharmony_ciPools allow you to allocate one or more rules or edges a finite number
709695b41eeSopenharmony_ciof concurrent jobs which is more tightly restricted than the default
710695b41eeSopenharmony_ciparallelism.
711695b41eeSopenharmony_ci
712695b41eeSopenharmony_ciThis can be useful, for example, to restrict a particular expensive rule
713695b41eeSopenharmony_ci(like link steps for huge executables), or to restrict particular build
714695b41eeSopenharmony_cistatements which you know perform poorly when run concurrently.
715695b41eeSopenharmony_ci
716695b41eeSopenharmony_ciEach pool has a `depth` variable which is specified in the build file.
717695b41eeSopenharmony_ciThe pool is then referred to with the `pool` variable on either a rule
718695b41eeSopenharmony_cior a build statement.
719695b41eeSopenharmony_ci
720695b41eeSopenharmony_ciNo matter what pools you specify, ninja will never run more concurrent jobs
721695b41eeSopenharmony_cithan the default parallelism, or the number of jobs specified on the command
722695b41eeSopenharmony_ciline (with `-j`).
723695b41eeSopenharmony_ci
724695b41eeSopenharmony_ci----------------
725695b41eeSopenharmony_ci# No more than 4 links at a time.
726695b41eeSopenharmony_cipool link_pool
727695b41eeSopenharmony_ci  depth = 4
728695b41eeSopenharmony_ci
729695b41eeSopenharmony_ci# No more than 1 heavy object at a time.
730695b41eeSopenharmony_cipool heavy_object_pool
731695b41eeSopenharmony_ci  depth = 1
732695b41eeSopenharmony_ci
733695b41eeSopenharmony_cirule link
734695b41eeSopenharmony_ci  ...
735695b41eeSopenharmony_ci  pool = link_pool
736695b41eeSopenharmony_ci
737695b41eeSopenharmony_cirule cc
738695b41eeSopenharmony_ci  ...
739695b41eeSopenharmony_ci
740695b41eeSopenharmony_ci# The link_pool is used here. Only 4 links will run concurrently.
741695b41eeSopenharmony_cibuild foo.exe: link input.obj
742695b41eeSopenharmony_ci
743695b41eeSopenharmony_ci# A build statement can be exempted from its rule's pool by setting an
744695b41eeSopenharmony_ci# empty pool. This effectively puts the build statement back into the default
745695b41eeSopenharmony_ci# pool, which has infinite depth.
746695b41eeSopenharmony_cibuild other.exe: link input.obj
747695b41eeSopenharmony_ci  pool =
748695b41eeSopenharmony_ci
749695b41eeSopenharmony_ci# A build statement can specify a pool directly.
750695b41eeSopenharmony_ci# Only one of these builds will run at a time.
751695b41eeSopenharmony_cibuild heavy_object1.obj: cc heavy_obj1.cc
752695b41eeSopenharmony_ci  pool = heavy_object_pool
753695b41eeSopenharmony_cibuild heavy_object2.obj: cc heavy_obj2.cc
754695b41eeSopenharmony_ci  pool = heavy_object_pool
755695b41eeSopenharmony_ci
756695b41eeSopenharmony_ci----------------
757695b41eeSopenharmony_ci
758695b41eeSopenharmony_ciThe `console` pool
759695b41eeSopenharmony_ci^^^^^^^^^^^^^^^^^^
760695b41eeSopenharmony_ci
761695b41eeSopenharmony_ci_Available since Ninja 1.5._
762695b41eeSopenharmony_ci
763695b41eeSopenharmony_ciThere exists a pre-defined pool named `console` with a depth of 1. It has
764695b41eeSopenharmony_cithe special property that any task in the pool has direct access to the
765695b41eeSopenharmony_cistandard input, output and error streams provided to Ninja, which are
766695b41eeSopenharmony_cinormally connected to the user's console (hence the name) but could be
767695b41eeSopenharmony_ciredirected. This can be useful for interactive tasks or long-running tasks
768695b41eeSopenharmony_ciwhich produce status updates on the console (such as test suites).
769695b41eeSopenharmony_ci
770695b41eeSopenharmony_ciWhile a task in the `console` pool is running, Ninja's regular output (such
771695b41eeSopenharmony_cias progress status and output from concurrent tasks) is buffered until
772695b41eeSopenharmony_ciit completes.
773695b41eeSopenharmony_ci
774695b41eeSopenharmony_ci[[ref_ninja_file]]
775695b41eeSopenharmony_ciNinja file reference
776695b41eeSopenharmony_ci--------------------
777695b41eeSopenharmony_ci
778695b41eeSopenharmony_ciA file is a series of declarations.  A declaration can be one of:
779695b41eeSopenharmony_ci
780695b41eeSopenharmony_ci1. A rule declaration, which begins with +rule _rulename_+, and
781695b41eeSopenharmony_ci   then has a series of indented lines defining variables.
782695b41eeSopenharmony_ci
783695b41eeSopenharmony_ci2. A build edge, which looks like +build _output1_ _output2_:
784695b41eeSopenharmony_ci   _rulename_ _input1_ _input2_+. +
785695b41eeSopenharmony_ci   Implicit dependencies may be tacked on the end with +|
786695b41eeSopenharmony_ci   _dependency1_ _dependency2_+. +
787695b41eeSopenharmony_ci   Order-only dependencies may be tacked on the end with +||
788695b41eeSopenharmony_ci   _dependency1_ _dependency2_+.  (See <<ref_dependencies,the reference on
789695b41eeSopenharmony_ci   dependency types>>.)
790695b41eeSopenharmony_ci   Validations may be taked on the end with +|@ _validation1_ _validation2_+.
791695b41eeSopenharmony_ci   (See <<validations,the reference on validations>>.)
792695b41eeSopenharmony_ci+
793695b41eeSopenharmony_ciImplicit outputs _(available since Ninja 1.7)_ may be added before
794695b41eeSopenharmony_cithe `:` with +| _output1_ _output2_+ and do not appear in `$out`.
795695b41eeSopenharmony_ci(See <<ref_outputs,the reference on output types>>.)
796695b41eeSopenharmony_ci
797695b41eeSopenharmony_ci3. Variable declarations, which look like +_variable_ = _value_+.
798695b41eeSopenharmony_ci
799695b41eeSopenharmony_ci4. Default target statements, which look like +default _target1_ _target2_+.
800695b41eeSopenharmony_ci
801695b41eeSopenharmony_ci5. References to more files, which look like +subninja _path_+ or
802695b41eeSopenharmony_ci   +include _path_+.  The difference between these is explained below
803695b41eeSopenharmony_ci   <<ref_scope,in the discussion about scoping>>.
804695b41eeSopenharmony_ci
805695b41eeSopenharmony_ci6. A pool declaration, which looks like +pool _poolname_+. Pools are explained
806695b41eeSopenharmony_ci   <<ref_pool, in the section on pools>>.
807695b41eeSopenharmony_ci
808695b41eeSopenharmony_ci[[ref_lexer]]
809695b41eeSopenharmony_ciLexical syntax
810695b41eeSopenharmony_ci~~~~~~~~~~~~~~
811695b41eeSopenharmony_ci
812695b41eeSopenharmony_ciNinja is mostly encoding agnostic, as long as the bytes Ninja cares
813695b41eeSopenharmony_ciabout (like slashes in paths) are ASCII.  This means e.g. UTF-8 or
814695b41eeSopenharmony_ciISO-8859-1 input files ought to work.
815695b41eeSopenharmony_ci
816695b41eeSopenharmony_ciComments begin with `#` and extend to the end of the line.
817695b41eeSopenharmony_ci
818695b41eeSopenharmony_ciNewlines are significant.  Statements like `build foo bar` are a set
819695b41eeSopenharmony_ciof space-separated tokens that end at the newline.  Newlines and
820695b41eeSopenharmony_cispaces within a token must be escaped.
821695b41eeSopenharmony_ci
822695b41eeSopenharmony_ciThere is only one escape character, `$`, and it has the following
823695b41eeSopenharmony_cibehaviors:
824695b41eeSopenharmony_ci
825695b41eeSopenharmony_ci`$` followed by a newline:: escape the newline (continue the current line
826695b41eeSopenharmony_ciacross a line break).
827695b41eeSopenharmony_ci
828695b41eeSopenharmony_ci`$` followed by text:: a variable reference.
829695b41eeSopenharmony_ci
830695b41eeSopenharmony_ci`${varname}`:: alternate syntax for `$varname`.
831695b41eeSopenharmony_ci
832695b41eeSopenharmony_ci`$` followed by space:: a space.  (This is only necessary in lists of
833695b41eeSopenharmony_cipaths, where a space would otherwise separate filenames.  See below.)
834695b41eeSopenharmony_ci
835695b41eeSopenharmony_ci`$:` :: a colon.  (This is only necessary in `build` lines, where a colon
836695b41eeSopenharmony_ciwould otherwise terminate the list of outputs.)
837695b41eeSopenharmony_ci
838695b41eeSopenharmony_ci`$$`:: a literal `$`.
839695b41eeSopenharmony_ci
840695b41eeSopenharmony_ciA `build` or `default` statement is first parsed as a space-separated
841695b41eeSopenharmony_cilist of filenames and then each name is expanded.  This means that
842695b41eeSopenharmony_cispaces within a variable will result in spaces in the expanded
843695b41eeSopenharmony_cifilename.
844695b41eeSopenharmony_ci
845695b41eeSopenharmony_ci----
846695b41eeSopenharmony_cispaced = foo bar
847695b41eeSopenharmony_cibuild $spaced/baz other$ file: ...
848695b41eeSopenharmony_ci# The above build line has two outputs: "foo bar/baz" and "other file".
849695b41eeSopenharmony_ci----
850695b41eeSopenharmony_ci
851695b41eeSopenharmony_ciIn a `name = value` statement, whitespace at the beginning of a value
852695b41eeSopenharmony_ciis always stripped.  Whitespace at the beginning of a line after a
853695b41eeSopenharmony_ciline continuation is also stripped.
854695b41eeSopenharmony_ci
855695b41eeSopenharmony_ci----
856695b41eeSopenharmony_citwo_words_with_one_space = foo $
857695b41eeSopenharmony_ci    bar
858695b41eeSopenharmony_cione_word_with_no_space = foo$
859695b41eeSopenharmony_ci    bar
860695b41eeSopenharmony_ci----
861695b41eeSopenharmony_ci
862695b41eeSopenharmony_ciOther whitespace is only significant if it's at the beginning of a
863695b41eeSopenharmony_ciline.  If a line is indented more than the previous one, it's
864695b41eeSopenharmony_ciconsidered part of its parent's scope; if it is indented less than the
865695b41eeSopenharmony_ciprevious one, it closes the previous scope.
866695b41eeSopenharmony_ci
867695b41eeSopenharmony_ci[[ref_toplevel]]
868695b41eeSopenharmony_ciTop-level variables
869695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~~
870695b41eeSopenharmony_ci
871695b41eeSopenharmony_ciTwo variables are significant when declared in the outermost file scope.
872695b41eeSopenharmony_ci
873695b41eeSopenharmony_ci`builddir`:: a directory for some Ninja output files.  See <<ref_log,the
874695b41eeSopenharmony_ci  discussion of the build log>>.  (You can also store other build output
875695b41eeSopenharmony_ci  in this directory.)
876695b41eeSopenharmony_ci
877695b41eeSopenharmony_ci`ninja_required_version`:: the minimum version of Ninja required to process
878695b41eeSopenharmony_ci  the build correctly.  See <<ref_versioning,the discussion of versioning>>.
879695b41eeSopenharmony_ci
880695b41eeSopenharmony_ci
881695b41eeSopenharmony_ci[[ref_rule]]
882695b41eeSopenharmony_ciRule variables
883695b41eeSopenharmony_ci~~~~~~~~~~~~~~
884695b41eeSopenharmony_ci
885695b41eeSopenharmony_ciA `rule` block contains a list of `key = value` declarations that
886695b41eeSopenharmony_ciaffect the processing of the rule.  Here is a full list of special
887695b41eeSopenharmony_cikeys.
888695b41eeSopenharmony_ci
889695b41eeSopenharmony_ci`command` (_required_):: the command line to run.  Each `rule` may
890695b41eeSopenharmony_ci  have only one `command` declaration. See <<ref_rule_command,the next
891695b41eeSopenharmony_ci  section>> for more details on quoting and executing multiple commands.
892695b41eeSopenharmony_ci
893695b41eeSopenharmony_ci`depfile`:: path to an optional `Makefile` that contains extra
894695b41eeSopenharmony_ci  _implicit dependencies_ (see <<ref_dependencies,the reference on
895695b41eeSopenharmony_ci  dependency types>>).  This is explicitly to support C/C++ header
896695b41eeSopenharmony_ci  dependencies; see <<ref_headers,the full discussion>>.
897695b41eeSopenharmony_ci
898695b41eeSopenharmony_ci`deps`:: _(Available since Ninja 1.3.)_ if present, must be one of
899695b41eeSopenharmony_ci  `gcc` or `msvc` to specify special dependency processing.  See
900695b41eeSopenharmony_ci   <<ref_headers,the full discussion>>.  The generated database is
901695b41eeSopenharmony_ci   stored as `.ninja_deps` in the `builddir`, see <<ref_toplevel,the
902695b41eeSopenharmony_ci   discussion of `builddir`>>.
903695b41eeSopenharmony_ci
904695b41eeSopenharmony_ci`msvc_deps_prefix`:: _(Available since Ninja 1.5.)_ defines the string
905695b41eeSopenharmony_ci  which should be stripped from msvc's /showIncludes output. Only
906695b41eeSopenharmony_ci  needed when `deps = msvc` and no English Visual Studio version is used.
907695b41eeSopenharmony_ci
908695b41eeSopenharmony_ci`description`:: a short description of the command, used to pretty-print
909695b41eeSopenharmony_ci  the command as it's running.  The `-v` flag controls whether to print
910695b41eeSopenharmony_ci  the full command or its description; if a command fails, the full command
911695b41eeSopenharmony_ci  line will always be printed before the command's output.
912695b41eeSopenharmony_ci
913695b41eeSopenharmony_ci`dyndep`:: _(Available since Ninja 1.10.)_ Used only on build statements.
914695b41eeSopenharmony_ci  If present, must name one of the build statement inputs.  Dynamically
915695b41eeSopenharmony_ci  discovered dependency information will be loaded from the file.
916695b41eeSopenharmony_ci  See the <<ref_dyndep,dynamic dependencies>> section for details.
917695b41eeSopenharmony_ci
918695b41eeSopenharmony_ci`generator`:: if present, specifies that this rule is used to
919695b41eeSopenharmony_ci  re-invoke the generator program.  Files built using `generator`
920695b41eeSopenharmony_ci  rules are treated specially in two ways: firstly, they will not be
921695b41eeSopenharmony_ci  rebuilt if the command line changes; and secondly, they are not
922695b41eeSopenharmony_ci  cleaned by default.
923695b41eeSopenharmony_ci
924695b41eeSopenharmony_ci`in`:: the space-separated list of files provided as inputs to the build line
925695b41eeSopenharmony_ci  referencing this `rule`, shell-quoted if it appears in commands.  (`$in` is
926695b41eeSopenharmony_ci  provided solely for convenience; if you need some subset or variant of this
927695b41eeSopenharmony_ci  list of files, just construct a new variable with that list and use
928695b41eeSopenharmony_ci  that instead.)
929695b41eeSopenharmony_ci
930695b41eeSopenharmony_ci`in_newline`:: the same as `$in` except that multiple inputs are
931695b41eeSopenharmony_ci  separated by newlines rather than spaces.  (For use with
932695b41eeSopenharmony_ci  `$rspfile_content`; this works around a bug in the MSVC linker where
933695b41eeSopenharmony_ci  it uses a fixed-size buffer for processing input.)
934695b41eeSopenharmony_ci
935695b41eeSopenharmony_ci`out`:: the space-separated list of files provided as outputs to the build line
936695b41eeSopenharmony_ci  referencing this `rule`, shell-quoted if it appears in commands.
937695b41eeSopenharmony_ci
938695b41eeSopenharmony_ci`restat`:: if present, causes Ninja to re-stat the command's outputs
939695b41eeSopenharmony_ci  after execution of the command.  Each output whose modification time
940695b41eeSopenharmony_ci  the command did not change will be treated as though it had never
941695b41eeSopenharmony_ci  needed to be built.  This may cause the output's reverse
942695b41eeSopenharmony_ci  dependencies to be removed from the list of pending build actions.
943695b41eeSopenharmony_ci
944695b41eeSopenharmony_ci`rspfile`, `rspfile_content`:: if present (both), Ninja will use a
945695b41eeSopenharmony_ci  response file for the given command, i.e. write the selected string
946695b41eeSopenharmony_ci  (`rspfile_content`) to the given file (`rspfile`) before calling the
947695b41eeSopenharmony_ci  command and delete the file after successful execution of the
948695b41eeSopenharmony_ci  command.
949695b41eeSopenharmony_ci+
950695b41eeSopenharmony_ciThis is particularly useful on Windows OS, where the maximal length of
951695b41eeSopenharmony_cia command line is limited and response files must be used instead.
952695b41eeSopenharmony_ci+
953695b41eeSopenharmony_ciUse it like in the following example:
954695b41eeSopenharmony_ci+
955695b41eeSopenharmony_ci----
956695b41eeSopenharmony_cirule link
957695b41eeSopenharmony_ci  command = link.exe /OUT$out [usual link flags here] @$out.rsp
958695b41eeSopenharmony_ci  rspfile = $out.rsp
959695b41eeSopenharmony_ci  rspfile_content = $in
960695b41eeSopenharmony_ci
961695b41eeSopenharmony_cibuild myapp.exe: link a.obj b.obj [possibly many other .obj files]
962695b41eeSopenharmony_ci----
963695b41eeSopenharmony_ci
964695b41eeSopenharmony_ci[[ref_rule_command]]
965695b41eeSopenharmony_ciInterpretation of the `command` variable
966695b41eeSopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
967695b41eeSopenharmony_ciFundamentally, command lines behave differently on Unixes and Windows.
968695b41eeSopenharmony_ci
969695b41eeSopenharmony_ciOn Unixes, commands are arrays of arguments.  The Ninja `command`
970695b41eeSopenharmony_civariable is passed directly to `sh -c`, which is then responsible for
971695b41eeSopenharmony_ciinterpreting that string into an argv array.  Therefore, the quoting
972695b41eeSopenharmony_cirules are those of the shell, and you can use all the normal shell
973695b41eeSopenharmony_cioperators, like `&&` to chain multiple commands, or `VAR=value cmd` to
974695b41eeSopenharmony_ciset environment variables.
975695b41eeSopenharmony_ci
976695b41eeSopenharmony_ciOn Windows, commands are strings, so Ninja passes the `command` string
977695b41eeSopenharmony_cidirectly to `CreateProcess`.  (In the common case of simply executing
978695b41eeSopenharmony_cia compiler this means there is less overhead.)  Consequently, the
979695b41eeSopenharmony_ciquoting rules are determined by the called program, which on Windows
980695b41eeSopenharmony_ciare usually provided by the C library.  If you need shell
981695b41eeSopenharmony_ciinterpretation of the command (such as the use of `&&` to chain
982695b41eeSopenharmony_cimultiple commands), make the command execute the Windows shell by
983695b41eeSopenharmony_ciprefixing the command with `cmd /c`. Ninja may error with "invalid parameter"
984695b41eeSopenharmony_ciwhich usually indicates that the command line length has been exceeded.
985695b41eeSopenharmony_ci
986695b41eeSopenharmony_ci[[ref_outputs]]
987695b41eeSopenharmony_ciBuild outputs
988695b41eeSopenharmony_ci~~~~~~~~~~~~~
989695b41eeSopenharmony_ci
990695b41eeSopenharmony_ciThere are two types of build outputs which are subtly different.
991695b41eeSopenharmony_ci
992695b41eeSopenharmony_ci1. _Explicit outputs_, as listed in a build line.  These are
993695b41eeSopenharmony_ci   available as the `$out` variable in the rule.
994695b41eeSopenharmony_ci+
995695b41eeSopenharmony_ciThis is the standard form of output to be used for e.g. the
996695b41eeSopenharmony_ciobject file of a compile command.
997695b41eeSopenharmony_ci
998695b41eeSopenharmony_ci2. _Implicit outputs_, as listed in a build line with the syntax +|
999695b41eeSopenharmony_ci   _out1_ _out2_+ + before the `:` of a build line _(available since
1000695b41eeSopenharmony_ci   Ninja 1.7)_.  The semantics are identical to explicit outputs,
1001695b41eeSopenharmony_ci  the only difference is that implicit outputs don't show up in the
1002695b41eeSopenharmony_ci  `$out` variable.
1003695b41eeSopenharmony_ci+
1004695b41eeSopenharmony_ciThis is for expressing outputs that don't show up on the
1005695b41eeSopenharmony_cicommand line of the command.
1006695b41eeSopenharmony_ci
1007695b41eeSopenharmony_ci[[ref_dependencies]]
1008695b41eeSopenharmony_ciBuild dependencies
1009695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~
1010695b41eeSopenharmony_ci
1011695b41eeSopenharmony_ciThere are three types of build dependencies which are subtly different.
1012695b41eeSopenharmony_ci
1013695b41eeSopenharmony_ci1. _Explicit dependencies_, as listed in a build line.  These are
1014695b41eeSopenharmony_ci   available as the `$in` variable in the rule.  Changes in these files
1015695b41eeSopenharmony_ci   cause the output to be rebuilt; if these files are missing and
1016695b41eeSopenharmony_ci   Ninja doesn't know how to build them, the build is aborted.
1017695b41eeSopenharmony_ci+
1018695b41eeSopenharmony_ciThis is the standard form of dependency to be used e.g. for the
1019695b41eeSopenharmony_cisource file of a compile command.
1020695b41eeSopenharmony_ci
1021695b41eeSopenharmony_ci2. _Implicit dependencies_, either as picked up from
1022695b41eeSopenharmony_ci   a `depfile` attribute on a rule or from the syntax +| _dep1_
1023695b41eeSopenharmony_ci   _dep2_+ on the end of a build line.  The semantics are identical to
1024695b41eeSopenharmony_ci   explicit dependencies, the only difference is that implicit dependencies
1025695b41eeSopenharmony_ci   don't show up in the `$in` variable.
1026695b41eeSopenharmony_ci+
1027695b41eeSopenharmony_ciThis is for expressing dependencies that don't show up on the
1028695b41eeSopenharmony_cicommand line of the command; for example, for a rule that runs a
1029695b41eeSopenharmony_ciscript that reads a hardcoded file, the hardcoded file should
1030695b41eeSopenharmony_cibe an implicit dependency, as changes to the file should cause
1031695b41eeSopenharmony_cithe output to rebuild, even though it doesn't show up in the arguments.
1032695b41eeSopenharmony_ci+
1033695b41eeSopenharmony_ciNote that dependencies as loaded through depfiles have slightly different
1034695b41eeSopenharmony_cisemantics, as described in the <<ref_rule,rule reference>>.
1035695b41eeSopenharmony_ci
1036695b41eeSopenharmony_ci3. _Order-only dependencies_, expressed with the syntax +|| _dep1_
1037695b41eeSopenharmony_ci   _dep2_+ on the end of a build line.  When these are out of date, the
1038695b41eeSopenharmony_ci   output is not rebuilt until they are built, but changes in order-only
1039695b41eeSopenharmony_ci   dependencies alone do not cause the output to be rebuilt.
1040695b41eeSopenharmony_ci+
1041695b41eeSopenharmony_ciOrder-only dependencies can be useful for bootstrapping dependencies
1042695b41eeSopenharmony_cithat are only discovered during build time: for example, to generate a
1043695b41eeSopenharmony_ciheader file before starting a subsequent compilation step.  (Once the
1044695b41eeSopenharmony_ciheader is used in compilation, a generated dependency file will then
1045695b41eeSopenharmony_ciexpress the implicit dependency.)
1046695b41eeSopenharmony_ci
1047695b41eeSopenharmony_ciFile paths are compared as is, which means that an absolute path and a
1048695b41eeSopenharmony_cirelative path, pointing to the same file, are considered different by Ninja.
1049695b41eeSopenharmony_ci
1050695b41eeSopenharmony_ci[[validations]]
1051695b41eeSopenharmony_ciValidations
1052695b41eeSopenharmony_ci~~~~~~~~~~~
1053695b41eeSopenharmony_ci
1054695b41eeSopenharmony_ci_Available since Ninja 1.11._
1055695b41eeSopenharmony_ci
1056695b41eeSopenharmony_ciValidations listed on the build line cause the specified files to be
1057695b41eeSopenharmony_ciadded to the top level of the build graph (as if they were specified
1058695b41eeSopenharmony_cion the Ninja command line) whenever the build line is a transitive
1059695b41eeSopenharmony_cidependency of one of the targets specified on the command line or a
1060695b41eeSopenharmony_cidefault target.
1061695b41eeSopenharmony_ci
1062695b41eeSopenharmony_ciValidations are added to the build graph regardless of whether the output
1063695b41eeSopenharmony_cifiles of the build statement are dirty are not, and the dirty state of
1064695b41eeSopenharmony_cithe build statement that outputs the file being used as a validation
1065695b41eeSopenharmony_cihas no effect on the dirty state of the build statement that requested it.
1066695b41eeSopenharmony_ci
1067695b41eeSopenharmony_ciA build edge can list another build edge as a validation even if the second
1068695b41eeSopenharmony_ciedge depends on the first.
1069695b41eeSopenharmony_ci
1070695b41eeSopenharmony_ciValidations are designed to handle rules that perform error checking but
1071695b41eeSopenharmony_cidon't produce any artifacts needed by the build, for example, static
1072695b41eeSopenharmony_cianalysis tools.  Marking the static analysis rule as an implicit input
1073695b41eeSopenharmony_ciof the main build rule of the source files or of the rules that depend
1074695b41eeSopenharmony_cion the main build rule would slow down the critical path of the build,
1075695b41eeSopenharmony_cibut using a validation would allow the build to proceed in parallel with
1076695b41eeSopenharmony_cithe static analysis rule once the main build rule is complete.
1077695b41eeSopenharmony_ci
1078695b41eeSopenharmony_ciVariable expansion
1079695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~
1080695b41eeSopenharmony_ci
1081695b41eeSopenharmony_ciVariables are expanded in paths (in a `build` or `default` statement)
1082695b41eeSopenharmony_ciand on the right side of a `name = value` statement.
1083695b41eeSopenharmony_ci
1084695b41eeSopenharmony_ciWhen a `name = value` statement is evaluated, its right-hand side is
1085695b41eeSopenharmony_ciexpanded immediately (according to the below scoping rules), and
1086695b41eeSopenharmony_cifrom then on `$name` expands to the static string as the result of the
1087695b41eeSopenharmony_ciexpansion.  It is never the case that you'll need to "double-escape" a
1088695b41eeSopenharmony_civalue to prevent it from getting expanded twice.
1089695b41eeSopenharmony_ci
1090695b41eeSopenharmony_ciAll variables are expanded immediately as they're encountered in parsing,
1091695b41eeSopenharmony_ciwith one important exception: variables in `rule` blocks are expanded
1092695b41eeSopenharmony_ciwhen the rule is _used_, not when it is declared.  In the following
1093695b41eeSopenharmony_ciexample, the `demo` rule prints "this is a demo of bar".
1094695b41eeSopenharmony_ci
1095695b41eeSopenharmony_ci----
1096695b41eeSopenharmony_cirule demo
1097695b41eeSopenharmony_ci  command = echo "this is a demo of $foo"
1098695b41eeSopenharmony_ci
1099695b41eeSopenharmony_cibuild out: demo
1100695b41eeSopenharmony_ci  foo = bar
1101695b41eeSopenharmony_ci----
1102695b41eeSopenharmony_ci
1103695b41eeSopenharmony_ci[[ref_scope]]
1104695b41eeSopenharmony_ciEvaluation and scoping
1105695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~
1106695b41eeSopenharmony_ci
1107695b41eeSopenharmony_ciTop-level variable declarations are scoped to the file they occur in.
1108695b41eeSopenharmony_ci
1109695b41eeSopenharmony_ciRule declarations are also scoped to the file they occur in.
1110695b41eeSopenharmony_ci_(Available since Ninja 1.6)_
1111695b41eeSopenharmony_ci
1112695b41eeSopenharmony_ciThe `subninja` keyword, used to include another `.ninja` file,
1113695b41eeSopenharmony_ciintroduces a new scope.  The included `subninja` file may use the
1114695b41eeSopenharmony_civariables and rules from the parent file, and shadow their values for the file's
1115695b41eeSopenharmony_ciscope, but it won't affect values of the variables in the parent.
1116695b41eeSopenharmony_ci
1117695b41eeSopenharmony_ciTo include another `.ninja` file in the current scope, much like a C
1118695b41eeSopenharmony_ci`#include` statement, use `include` instead of `subninja`.
1119695b41eeSopenharmony_ci
1120695b41eeSopenharmony_ciVariable declarations indented in a `build` block are scoped to the
1121695b41eeSopenharmony_ci`build` block.  The full lookup order for a variable expanded in a
1122695b41eeSopenharmony_ci`build` block (or the `rule` is uses) is:
1123695b41eeSopenharmony_ci
1124695b41eeSopenharmony_ci1. Special built-in variables (`$in`, `$out`).
1125695b41eeSopenharmony_ci
1126695b41eeSopenharmony_ci2. Build-level variables from the `build` block.
1127695b41eeSopenharmony_ci
1128695b41eeSopenharmony_ci3. Rule-level variables from the `rule` block (i.e. `$command`).
1129695b41eeSopenharmony_ci   (Note from the above discussion on expansion that these are
1130695b41eeSopenharmony_ci   expanded "late", and may make use of in-scope bindings like `$in`.)
1131695b41eeSopenharmony_ci
1132695b41eeSopenharmony_ci4. File-level variables from the file that the `build` line was in.
1133695b41eeSopenharmony_ci
1134695b41eeSopenharmony_ci5. Variables from the file that included that file using the
1135695b41eeSopenharmony_ci   `subninja` keyword.
1136695b41eeSopenharmony_ci
1137695b41eeSopenharmony_ci[[ref_dyndep]]
1138695b41eeSopenharmony_ciDynamic Dependencies
1139695b41eeSopenharmony_ci--------------------
1140695b41eeSopenharmony_ci
1141695b41eeSopenharmony_ci_Available since Ninja 1.10._
1142695b41eeSopenharmony_ci
1143695b41eeSopenharmony_ciSome use cases require implicit dependency information to be dynamically
1144695b41eeSopenharmony_cidiscovered from source file content _during the build_ in order to build
1145695b41eeSopenharmony_cicorrectly on the first run (e.g. Fortran module dependencies).  This is
1146695b41eeSopenharmony_ciunlike <<ref_headers,header dependencies>> which are only needed on the
1147695b41eeSopenharmony_cisecond run and later to rebuild correctly.  A build statement may have a
1148695b41eeSopenharmony_ci`dyndep` binding naming one of its inputs to specify that dynamic
1149695b41eeSopenharmony_cidependency information must be loaded from the file.  For example:
1150695b41eeSopenharmony_ci
1151695b41eeSopenharmony_ci----
1152695b41eeSopenharmony_cibuild out: ... || foo
1153695b41eeSopenharmony_ci  dyndep = foo
1154695b41eeSopenharmony_cibuild foo: ...
1155695b41eeSopenharmony_ci----
1156695b41eeSopenharmony_ci
1157695b41eeSopenharmony_ciThis specifies that file `foo` is a dyndep file.  Since it is an input,
1158695b41eeSopenharmony_cithe build statement for `out` can never be executed before `foo` is built.
1159695b41eeSopenharmony_ciAs soon as `foo` is finished Ninja will read it to load dynamically
1160695b41eeSopenharmony_cidiscovered dependency information for `out`.  This may include additional
1161695b41eeSopenharmony_ciimplicit inputs and/or outputs.  Ninja will update the build graph
1162695b41eeSopenharmony_ciaccordingly and the build will proceed as if the information was known
1163695b41eeSopenharmony_cioriginally.
1164695b41eeSopenharmony_ci
1165695b41eeSopenharmony_ciDyndep file reference
1166695b41eeSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~
1167695b41eeSopenharmony_ci
1168695b41eeSopenharmony_ciFiles specified by `dyndep` bindings use the same <<ref_lexer,lexical syntax>>
1169695b41eeSopenharmony_cias <<ref_ninja_file,ninja build files>> and have the following layout.
1170695b41eeSopenharmony_ci
1171695b41eeSopenharmony_ci1. A version number in the form `<major>[.<minor>][<suffix>]`:
1172695b41eeSopenharmony_ci+
1173695b41eeSopenharmony_ci----
1174695b41eeSopenharmony_cininja_dyndep_version = 1
1175695b41eeSopenharmony_ci----
1176695b41eeSopenharmony_ci+
1177695b41eeSopenharmony_ciCurrently the version number must always be `1` or `1.0` but may have
1178695b41eeSopenharmony_cian arbitrary suffix.
1179695b41eeSopenharmony_ci
1180695b41eeSopenharmony_ci2. One or more build statements of the form:
1181695b41eeSopenharmony_ci+
1182695b41eeSopenharmony_ci----
1183695b41eeSopenharmony_cibuild out | imp-outs... : dyndep | imp-ins...
1184695b41eeSopenharmony_ci----
1185695b41eeSopenharmony_ci+
1186695b41eeSopenharmony_ciEvery statement must specify exactly one explicit output and must use
1187695b41eeSopenharmony_cithe rule name `dyndep`.  The `| imp-outs...` and `| imp-ins...` portions
1188695b41eeSopenharmony_ciare optional.
1189695b41eeSopenharmony_ci
1190695b41eeSopenharmony_ci3. An optional `restat` <<ref_rule,variable binding>> on each build statement.
1191695b41eeSopenharmony_ci
1192695b41eeSopenharmony_ciThe build statements in a dyndep file must have a one-to-one correspondence
1193695b41eeSopenharmony_cito build statements in the <<ref_ninja_file,ninja build file>> that name the
1194695b41eeSopenharmony_cidyndep file in a `dyndep` binding.  No dyndep build statement may be omitted
1195695b41eeSopenharmony_ciand no extra build statements may be specified.
1196695b41eeSopenharmony_ci
1197695b41eeSopenharmony_ciDyndep Examples
1198695b41eeSopenharmony_ci~~~~~~~~~~~~~~~
1199695b41eeSopenharmony_ci
1200695b41eeSopenharmony_ciFortran Modules
1201695b41eeSopenharmony_ci^^^^^^^^^^^^^^^
1202695b41eeSopenharmony_ci
1203695b41eeSopenharmony_ciConsider a Fortran source file `foo.f90` that provides a module
1204695b41eeSopenharmony_ci`foo.mod` (an implicit output of compilation) and another source file
1205695b41eeSopenharmony_ci`bar.f90` that uses the module (an implicit input of compilation).  This
1206695b41eeSopenharmony_ciimplicit dependency must be discovered before we compile either source
1207695b41eeSopenharmony_ciin order to ensure that `bar.f90` never compiles before `foo.f90`, and
1208695b41eeSopenharmony_cithat `bar.f90` recompiles when `foo.mod` changes.  We can achieve this
1209695b41eeSopenharmony_cias follows:
1210695b41eeSopenharmony_ci
1211695b41eeSopenharmony_ci----
1212695b41eeSopenharmony_cirule f95
1213695b41eeSopenharmony_ci  command = f95 -o $out -c $in
1214695b41eeSopenharmony_cirule fscan
1215695b41eeSopenharmony_ci  command = fscan -o $out $in
1216695b41eeSopenharmony_ci
1217695b41eeSopenharmony_cibuild foobar.dd: fscan foo.f90 bar.f90
1218695b41eeSopenharmony_ci
1219695b41eeSopenharmony_cibuild foo.o: f95 foo.f90 || foobar.dd
1220695b41eeSopenharmony_ci  dyndep = foobar.dd
1221695b41eeSopenharmony_cibuild bar.o: f95 bar.f90 || foobar.dd
1222695b41eeSopenharmony_ci  dyndep = foobar.dd
1223695b41eeSopenharmony_ci----
1224695b41eeSopenharmony_ci
1225695b41eeSopenharmony_ciIn this example the order-only dependencies ensure that `foobar.dd` is
1226695b41eeSopenharmony_cigenerated before either source compiles.  The hypothetical `fscan` tool
1227695b41eeSopenharmony_ciscans the source files, assumes each will be compiled to a `.o` of the
1228695b41eeSopenharmony_cisame name, and writes `foobar.dd` with content such as:
1229695b41eeSopenharmony_ci
1230695b41eeSopenharmony_ci----
1231695b41eeSopenharmony_cininja_dyndep_version = 1
1232695b41eeSopenharmony_cibuild foo.o | foo.mod: dyndep
1233695b41eeSopenharmony_cibuild bar.o: dyndep |  foo.mod
1234695b41eeSopenharmony_ci----
1235695b41eeSopenharmony_ci
1236695b41eeSopenharmony_ciNinja will load this file to add `foo.mod` as an implicit output of
1237695b41eeSopenharmony_ci`foo.o` and implicit input of `bar.o`.  This ensures that the Fortran
1238695b41eeSopenharmony_cisources are always compiled in the proper order and recompiled when
1239695b41eeSopenharmony_cineeded.
1240695b41eeSopenharmony_ci
1241695b41eeSopenharmony_ciTarball Extraction
1242695b41eeSopenharmony_ci^^^^^^^^^^^^^^^^^^
1243695b41eeSopenharmony_ci
1244695b41eeSopenharmony_ciConsider a tarball `foo.tar` that we want to extract.  The extraction time
1245695b41eeSopenharmony_cican be recorded with a `foo.tar.stamp` file so that extraction repeats if
1246695b41eeSopenharmony_cithe tarball changes, but we also would like to re-extract if any of the
1247695b41eeSopenharmony_cioutputs is missing.  However, the list of outputs depends on the content
1248695b41eeSopenharmony_ciof the tarball and cannot be spelled out explicitly in the ninja build file.
1249695b41eeSopenharmony_ciWe can achieve this as follows:
1250695b41eeSopenharmony_ci
1251695b41eeSopenharmony_ci----
1252695b41eeSopenharmony_cirule untar
1253695b41eeSopenharmony_ci  command = tar xf $in && touch $out
1254695b41eeSopenharmony_cirule scantar
1255695b41eeSopenharmony_ci  command = scantar --stamp=$stamp --dd=$out $in
1256695b41eeSopenharmony_cibuild foo.tar.dd: scantar foo.tar
1257695b41eeSopenharmony_ci  stamp = foo.tar.stamp
1258695b41eeSopenharmony_cibuild foo.tar.stamp: untar foo.tar || foo.tar.dd
1259695b41eeSopenharmony_ci  dyndep = foo.tar.dd
1260695b41eeSopenharmony_ci----
1261695b41eeSopenharmony_ci
1262695b41eeSopenharmony_ciIn this example the order-only dependency ensures that `foo.tar.dd` is
1263695b41eeSopenharmony_cibuilt before the tarball extracts.  The hypothetical `scantar` tool
1264695b41eeSopenharmony_ciwill read the tarball (e.g. via `tar tf`) and write `foo.tar.dd` with
1265695b41eeSopenharmony_cicontent such as:
1266695b41eeSopenharmony_ci
1267695b41eeSopenharmony_ci----
1268695b41eeSopenharmony_cininja_dyndep_version = 1
1269695b41eeSopenharmony_cibuild foo.tar.stamp | file1.txt file2.txt : dyndep
1270695b41eeSopenharmony_ci  restat = 1
1271695b41eeSopenharmony_ci----
1272695b41eeSopenharmony_ci
1273695b41eeSopenharmony_ciNinja will load this file to add `file1.txt` and `file2.txt` as implicit
1274695b41eeSopenharmony_cioutputs of `foo.tar.stamp`, and to mark the build statement for `restat`.
1275695b41eeSopenharmony_ciOn future builds, if any implicit output is missing the tarball will be
1276695b41eeSopenharmony_ciextracted again.  The `restat` binding tells Ninja to tolerate the fact
1277695b41eeSopenharmony_cithat the implicit outputs may not have modification times newer than
1278695b41eeSopenharmony_cithe tarball itself (avoiding re-extraction on every build).
1279