drop the "rules" tool completely
[platform/upstream/ninja.git] / doc / manual.asciidoc
1 Ninja
2 =====
3 Evan Martin <martine@danga.com>
4
5
6 Introduction
7 ------------
8
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_.
12
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) can take ten seconds to start building after
19 changing one file.  Ninja is under a second.
20
21 Philosophical overview
22 ~~~~~~~~~~~~~~~~~~~~~~
23
24 Where other build systems are high-level languages, Ninja aims to be
25 an assembler.
26
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.
31
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.
35
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
43 generator.
44
45 Design goals
46 ~~~~~~~~~~~~
47
48 Here are the design goals of Ninja:
49
50 * very fast (i.e., instant) incremental builds, even for very large
51   projects.
52
53 * very little policy about how code is built.  Different projects and
54   higher-level build systems have different opinions about how code
55   should be built; for example, should built objects live alongside
56   the sources or should all build output go into a separate directory?
57   Is there an "package" rule that builds a distributable package of
58   the project?  Sidestep these decisions by trying to allow either to
59   be implemented, rather than choosing, even if that results in
60   more verbosity.
61
62 * get dependencies correct, and in particular situations that are
63   difficult to get right with Makefiles (e.g. outputs need an implicit
64   dependency on the command line used to generate them; to build C
65   source code you need to use gcc's `-M` flags for header
66   dependencies).
67
68 * when convenience and speed are in conflict, prefer speed.
69
70 Some explicit _non-goals_:
71
72 * convenient syntax for writing build files by hand.  _You should
73   generate your ninja files using another program_.  This is how we
74   can sidestep many policy decisions.
75
76 * built-in rules. _Out of the box, Ninja has no rules for
77   e.g. compiling C code._
78
79 * build-time customization of the build. _Options belong in
80   the program that generates the ninja files_.
81
82 * build-time decision-making ability such as conditionals or search
83   paths. _Making decisions is slow._
84
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.
88
89 Comparison to Make
90 ~~~~~~~~~~~~~~~~~~
91
92 Ninja is closest in spirit and functionality to make, relying on
93 simple dependencies between file timestamps.
94
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.
99
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
103 projects.
104
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.)
108
109 * A Ninja rule may point at a path for extra implicit dependency
110   information.  This makes it easy to get header dependencies correct
111   for C/C++ code.
112
113 * A build edge may have multiple outputs.
114
115 * Outputs implicitly depend on the command line that was used to generate
116   them, which means that changing e.g. compilation flags will cause
117   the outputs to rebuild.
118
119 * Output directories are always implicitly created before running the
120   command that relies on them.
121
122 * Rules can provide shorter descriptions of the command being run, so
123   you can print e.g. `CC foo.o` instead of a long command line while
124   building.
125
126 * Builds are always run in parallel, based by default on the number of
127   CPUs your system has.  Underspecified build dependencies will result
128   in incorrect builds.
129
130 * Command output is always buffered.  This means commands running in
131   parallel don't interleave their output, and when a command fails we
132   can print its failure output next to the full command line that
133   produced the failure.
134
135
136 Using Ninja for your project
137 ----------------------------
138
139 Ninja currently works on Unix-like systems. It's seen the most testing
140 on Linux (and has the best performance there) but it runs fine on Mac
141 OS X and FreeBSD.  Ninja has some preliminary Windows support but the
142 full details of the implementation -- like how to get C header
143 interdependencies correct and fast when using MSVC's compiler -- is
144 not yet complete.
145
146 If your project is small, Ninja's speed impact is likely unnoticeable.
147 Some build timing numbers are included below.  (However, even for
148 small projects it sometimes turns out that Ninja's limited syntax
149 forces simpler build rules that result in faster builds.)  Another way
150 to say this is that if you're happy with the edit-compile cycle time
151 of your project already then Ninja won't help.
152
153 There are many other build systems that are more user-friendly or
154 featureful than Ninja itself.  For some recommendations: the Ninja
155 author found http://gittup.org/tup/[the tup build system] influential
156 in Ninja's design, and thinks https://github.com/apenwarr/redo[redo]'s
157 design is quite clever.
158
159 Ninja's benefit comes from using it in conjunction with a smarter
160 meta-build system.
161
162 http://code.google.com/p/gyp/[gyp]:: The meta-build system used to
163 generate build files for Google Chrome.  gyp can generate Ninja files
164 for Linux and Mac and is used by many Chrome developers; support for
165 Windows is in progress.  See the
166 http://code.google.com/p/chromium/wiki/NinjaBuild[Chromium Ninja
167 documentation for more details].  gyp is relatively unpopular outside
168 of the Chrome and v8 world.
169
170 * For Chrome (~30k source files), Ninja reduced no-op builds from
171   around 15 seconds to under one second.
172 * https://plus.google.com/108996039294665965197/posts/SfhrFAhRyyd[A
173   Mozilla developer compares build systems]: "While chromium's full
174   build is 2.15x slower than firefox's, a nop build is 78.2x faster!
175   That is really noticeable during development. No incremental build
176   of firefox can be faster than 57.9s, which means that in practice
177   almost all of them will be over a minute."
178
179 http://www.cmake.org/[CMake]:: A widely used meta-build system that
180 can generate Ninja files on Linux as of CMake version 2.8.8.  (There
181 is some Mac and Windows support -- http://www.reactos.org[ReactOS]
182 uses Ninja on Windows for their buildbots, but those platforms are not
183 yet officially supported by CMake as the full test suite doesn't
184 pass.)
185
186 * For building Blender, one user reported "Single file rebuild is 0.97
187   sec, same on makefiles was 3.7sec."
188 * For building LLVM on Windows, one user reported no-op build times:
189   "ninja: 0.4s / MSBuild: 11s / jom: 53s".
190
191 others:: Ninja ought to fit perfectly into other meta-build software
192 like http://industriousone.com/premake[premake].  If you do this work,
193 please let us know!
194
195 Running Ninja
196 ~~~~~~~~~~~~~
197
198 Run `ninja`.  By default, it looks for a file named `build.ninja` in
199 the current directory and builds all out-of-date targets.  You can
200 specify which targets (files) to build as command line arguments.
201
202 `ninja -h` prints help output.  Many of Ninja's flags intentionally
203 match those of Make; e.g `ninja -C build -j 20` changes into the
204 `build` directory and runs 20 build commands in parallel.  (Note that
205 Ninja defaults to running commands in parallel anyway, so typically
206 you don't need to pass `-j`.)
207
208
209 Environment variables
210 ~~~~~~~~~~~~~~~~~~~~~
211
212 Ninja supports one environment variable to control its behavior.
213
214 `NINJA_STATUS`:: The progress status printed before the rule being run.
215 Several placeholders are available:
216 * `%s`: The number of started edges.
217 * `%t`: The total number of edges that must be run to complete the build.
218 * `%p`: The percentage of started edges.
219 * `%r`: The number of currently running edges.
220 * `%u`: The number of remaining edges to start.
221 * `%f`: The number of finished edges.
222 * `%o`: Overall rate of finished edges per second
223 * `%c`: Current rate of finished edges per second (average over builds specified by -j or its default)
224 * `%%`: A plain `%` character.
225 * The default progress status is `"[%s/%t] "` (note the trailing space
226 to separate from the build rule). Another example of possible progress status
227 could be `"[%u/%r/%f] "`.
228
229 Extra tools
230 ~~~~~~~~~~~
231
232 The `-t` flag on the Ninja command line runs some tools that we have
233 found useful during Ninja's development.  The current tools are:
234
235 [horizontal]
236 `query`:: dump the inputs and outputs of a given target.
237
238 `browse`:: browse the dependency graph in a web browser.  Clicking a
239 file focuses the view on that file, showing inputs and outputs.  This
240 feature requires a Python installation.
241
242 `graph`:: output a file in the syntax used by `graphviz`, a automatic
243 graph layout tool.  Use it like:
244 +
245 ----
246 ninja -t graph mytarget | dot -Tpng -ograph.png
247 ----
248 +
249 In the Ninja source tree, `ninja graph.png`
250 generates an image for Ninja itself.  If no target is given generate a
251 graph for all root targets.
252
253 `targets`:: output a list of targets either by rule or by depth.  If used
254 like +ninja -t targets rule _name_+ it prints the list of targets
255 using the given rule to be built.  If no rule is given, it prints the source
256 files (the leaves of the graph).  If used like
257 +ninja -t targets depth _digit_+ it
258 prints the list of targets in a depth-first manner starting by the root
259 targets (the ones with no outputs). Indentation is used to mark dependencies.
260 If the depth is zero it prints all targets. If no arguments are provided
261 +ninja -t targets depth 1+ is assumed. In this mode targets may be listed
262 several times. If used like this +ninja -t targets all+ it
263 prints all the targets available without indentation and it is faster
264 than the _depth_ mode.
265
266 `commands`:: given a list of targets, print a list of commands which, if
267 executed in order, may be used to rebuild those targets, assuming that all
268 output files are out of date.
269
270 `clean`:: remove built files. By default it removes all built files
271 except for those created by the generator.  Adding the `-g` flag also
272 removes built files created by the generator (see <<ref_rule,the rule
273 reference for the +generator+ attribute>>).  Additional arguments are
274 targets, which removes the given targets and recursively all files
275 built for them.
276 +
277 If used like +ninja -t clean -r _rules_+ it removes all files built using
278 the given rules.
279 +
280 Files created but not referenced in the graph are not removed. This
281 tool takes in account the +-v+ and the +-n+ options (note that +-n+
282 implies +-v+).
283
284
285
286 Writing your own Ninja files
287 ----------------------------
288
289 The remainder of this manual is only useful if you are constructing
290 Ninja files yourself: for example, if you're writing a meta-build
291 system or supporting a new language.
292
293 Conceptual overview
294 ~~~~~~~~~~~~~~~~~~~
295
296 Ninja evaluates a graph of dependencies between files, and runs
297 whichever commands are necessary to make your build target up to date.
298 If you are familiar with Make, Ninja is very similar.
299
300 A build file (default name: `build.ninja`) provides a list of _rules_
301 -- short names for longer commands, like how to run the compiler --
302 along with a list of _build_ statements saying how to build files
303 using the rules -- which rule to apply to which inputs to produce
304 which outputs.
305
306 Conceptually, `build` statements describe the dependency graph of your
307 project, while `rule` statements describe how to generate the files
308 along a given edge of the graph.
309
310 Syntax example
311 ~~~~~~~~~~~~~~
312
313 Here's a basic `.ninja` file that demonstrates most of the syntax.
314 It will be used as an example for the following sections.
315
316 ---------------------------------
317 cflags = -Wall
318
319 rule cc
320   command = gcc $cflags -c $in -o $out
321
322 build foo.o: cc foo.c
323 ---------------------------------
324
325 Variables
326 ~~~~~~~~~
327 Despite the non-goal of being convenient to write by hand, to keep
328 build files readable (debuggable), Ninja supports declaring shorter
329 reusable names for strings.  A declaration like the following
330
331 ----------------
332 cflags = -g
333 ----------------
334
335 can be used on the right side of an equals sign, dereferencing it with
336 a dollar sign, like this:
337
338 ----------------
339 rule cc
340   command = gcc $cflags -c $in -o $out
341 ----------------
342
343 Variables can also be referenced using curly braces like `${in}`.
344
345 Variables might better be called "bindings", in that a given variable
346 cannot be changed, only shadowed.  There is more on how shadowing works
347 later in this document.
348
349 Rules
350 ~~~~~
351
352 Rules declare a short name for a command line.  They begin with a line
353 consisting of the `rule` keyword and a name for the rule.  Then
354 follows an indented set of `variable = value` lines.
355
356 The basic example above declares a new rule named `cc`, along with the
357 command to run.  (In the context of a rule, the `command` variable is
358 special and defines the command to run.  A full list of special
359 variables is provided in <<ref_rule,the reference>>.)
360
361 Within the context of a rule, three additional special variables are
362 available: `$in` expands to the list of input files (`foo.c`) and
363 `$out` to the output file (`foo.o`) for the command. For use with
364 `$rspfile_content`, there is also `$in_newline`, which is the same as
365 `$in`, except that multiple inputs are separated by `\n`, rather than
366 spaces.
367
368
369 Build statements
370 ~~~~~~~~~~~~~~~~
371
372 Build statements declare a relationship between input and output
373 files.  They begin with the `build` keyword, and have the format
374 +build _outputs_: _rulename_ _inputs_+.  Such a declaration says that
375 all of the output files are derived from the input files.  When the
376 output files are missing or when the inputs change, Ninja will run the
377 rule to regenerate the outputs.
378
379 The basic example above describes how to build `foo.o`, using the `cc`
380 rule.
381
382 In the scope of a `build` block (including in the evaluation of its
383 associated `rule`), the variable `$in` is the list of inputs and the
384 variable `$out` is the list of outputs.
385
386 A build statement may be followed by an indented set of `key = value`
387 pairs, much like a rule.  These variables will shadow any variables
388 when evaluating the variables in the command.  For example:
389
390 ----------------
391 cflags = -Wall -Werror
392 rule cc
393   command = gcc $cflags -c $in -o $out
394
395 # If left unspecified, builds get the outer $cflags.
396 build foo.o: cc foo.c
397
398 # But you can can shadow variables like cflags for a particular build.
399 build special.o: cc special.c
400   cflags = -Wall
401
402 # The variable was only shadowed for the scope of special.o;
403 # Subsequent build lines get the outer (original) cflags.
404 build bar.o: cc bar.c
405
406 ----------------
407
408 For more discussion of how scoping works, consult <<ref_scope,the
409 reference>>.
410
411 If you need more complicated information passed from the build
412 statement to the rule (for example, if the rule needs "the file
413 extension of the first input"), pass that through as an extra
414 variable, like how `cflags` is passed above.
415
416 If the top-level Ninja file is specified as an output of any build
417 statement and it is out of date, Ninja will rebuild and reload it
418 before building the targets requested by the user.
419
420 Pools
421 ~~~~~
422
423 Pools allow you to allocate one or more rules or edges a finite number
424 of concurrent jobs which is more tightly restricted than the default
425 parallelism.
426
427 This can be useful, for example, to restrict a particular expensive rule
428 (like link steps for huge executables), or to restrict particular build
429 statements which you know perform poorly when run concurrently.
430
431 Each pool has a `depth` variable which is specified in the build file.
432 The pool is then referred to with the `pool` variable on either a rule
433 or a build statement.
434
435 No matter what pools you specify, ninja will never run more concurrent jobs
436 than the default parallelism, or the number of jobs specified on the command
437 line (with -j).
438
439 ----------------
440 # No more than 4 links at a time.
441 pool link_pool
442   depth = 4
443
444 # No more than 1 heavy object at a time.
445 pool heavy_object_pool
446   depth = 1
447
448 rule link
449   ...
450   pool = link_pool
451
452 rule cc
453   ...
454
455 # The link_pool is used here. Only 4 links will run concurrently.
456 build foo.exe: link input.obj
457
458 # A build statement can be exempted from its rule's pool by setting an
459 # empty pool. This effectively puts the build statement back into the default
460 # pool, which has infinite depth.
461 build other.exe: link input.obj
462   pool =
463
464 # A build statement can specify a pool directly.
465 # Only one of these builds will run at a time.
466 build heavy_object1.obj: cc heavy_obj1.cc
467   pool = heavy_object_pool
468 build heavy_object2.obj: cc heavy_obj2.cc
469   pool = heavy_object_pool
470
471 ----------------
472
473
474 Generating Ninja files from code
475 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
476
477 `misc/ninja_syntax.py` in the Ninja distribution is a tiny Python
478 module to facilitate generating Ninja files.  It allows you to make
479 Python calls like `ninja.rule(name='foo', command='bar',
480 depfile='$out.d')` and it will generate the appropriate syntax.  Feel
481 free to just inline it into your project's build system if it's
482 useful.
483
484
485 More details
486 ------------
487
488 The `phony` rule
489 ~~~~~~~~~~~~~~~~
490
491 The special rule name `phony` can be used to create aliases for other
492 targets.  For example:
493
494 ----------------
495 build foo: phony some/file/in/a/faraway/subdir/foo
496 ----------------
497
498 This makes `ninja foo` build the longer path.  Semantically, the
499 `phony` rule is equivalent to a plain rule where the `command` does
500 nothing, but phony rules are handled specially in that they aren't
501 printed when run, logged (see below), nor do they contribute to the
502 command count printed as part of the build process.
503
504 `phony` can also be used to create dummy targets for files which
505 may not exist at build time.  If a phony build statement is written
506 without any dependencies, the target will be considered out of date if
507 it does not exist.  Without a phony build statement, Ninja will report
508 an error if the file does not exist and is required by the build.
509
510
511 Default target statements
512 ~~~~~~~~~~~~~~~~~~~~~~~~~
513
514 By default, if no targets are specified on the command line, Ninja
515 will build every output that is not named as an input elsewhere.
516 You can override this behavior using a default target statement.
517 A default target statement causes Ninja to build only a given subset
518 of output files if none are specified on the command line.
519
520 Default target statements begin with the `default` keyword, and have
521 the format +default _targets_+.  A default target statement must appear
522 after the build statement that declares the target as an output file.
523 They are cumulative, so multiple statements may be used to extend
524 the list of default targets.  For example:
525
526 ----------------
527 default foo bar
528 default baz
529 ----------------
530
531 This causes Ninja to build the `foo`, `bar` and `baz` targets by
532 default.
533
534
535 The Ninja log
536 ~~~~~~~~~~~~~
537
538 For each built file, Ninja keeps a log of the command used to build
539 it.  Using this log Ninja can know when an existing output was built
540 with a different command line than the build files specify (i.e., the
541 command line changed) and knows to rebuild the file.
542
543 The log file is kept in the build root in a file called `.ninja_log`.
544 If you provide a variable named `builddir` in the outermost scope,
545 `.ninja_log` will be kept in that directory instead.
546
547
548 Ninja file reference
549 --------------------
550
551 A file is a series of declarations.  A declaration can be one of:
552
553 1. A rule declaration, which begins with +rule _rulename_+, and
554    then has a series of indented lines defining variables.
555
556 2. A build edge, which looks like +build _output1_ _output2_:
557    _rulename_ _input1_ _input2_+. +
558    Implicit dependencies may be tacked on the end with +|
559    _dependency1_ _dependency2_+. +
560    Order-only dependencies may be tacked on the end with +||
561    _dependency1_ _dependency2_+.  (See <<ref_dependencies,the reference on
562    dependency types>>.)
563
564 3. Variable declarations, which look like +_variable_ = _value_+.
565
566 4. Default target statements, which look like +default _target1_ _target2_+.
567
568 5. References to more files, which look like +subninja _path_+ or
569    +include _path_+.  The difference between these is explained below
570    <<ref_scope,in the discussion about scoping>>.
571
572 Lexical syntax
573 ~~~~~~~~~~~~~~
574
575 Ninja is mostly encoding agnostic, as long as the bytes Ninja cares
576 about (like slashes in paths) are ASCII.  This means e.g. UTF-8 or
577 ISO-8859-1 input files ought to work.  (To simplify some code, tabs
578 and carriage returns are currently disallowed; this could be fixed if
579 it really mattered to you.)
580
581 Comments begin with `#` and extend to the end of the line.
582
583 Newlines are significant.  Statements like `build foo bar` are a set
584 of space-separated tokens that end at the newline.  Newlines and
585 spaces within a token must be escaped.
586
587 There is only one escape character, `$`, and it has the following
588 behaviors:
589
590 [horizontal]
591 `$` followed by a newline:: escape the newline (continue the current line
592 across a line break).
593
594 `$` followed by text:: a variable reference.
595
596 `${varname}`:: alternate syntax for `$varname`.
597
598 `$` followed by space:: a space.  (This is only necessary in lists of
599 paths, where a space would otherwise separate filenames.  See below.)
600
601 `$:` :: a colon.  (This is only necessary in `build` lines, where a colon
602 would otherwise terminate the list of inputs.)
603
604 `$$`:: a literal `$`.
605
606 A `build` or `default` statement is first parsed as a space-separated
607 list of filenames and then each name is expanded.  This means that
608 spaces within a variable will result in spaces in the expanded
609 filename.
610
611 ----
612 spaced = foo bar
613 build $spaced/baz other$ file: ...
614 # The above build line has two outputs: "foo bar/baz" and "other file".
615 ----
616
617 In a `name = value` statement, whitespace at the beginning of a value
618 is always stripped.  Whitespace at the beginning of a line after a
619 line continuation is also stripped.
620
621 ----
622 two_words_with_one_space = foo $
623     bar
624 one_word_with_no_space = foo$
625     bar
626 ----
627
628 Other whitespace is only significant if it's at the beginning of a
629 line.  If a line is indented more than the previous one, it's
630 considered part of its parent's scope; if it is indented less than the
631 previous one, it closes the previous scope.
632
633 Rule variables
634 ~~~~~~~~~~~~~~
635 [[ref_rule]]
636
637 A `rule` block contains a list of `key = value` declarations that
638 affect the processing of the rule.  Here is a full list of special
639 keys.
640
641 `command` (_required_):: the command line to run.  This string (after
642   $variables are expanded) is passed directly to `sh -c` without
643   interpretation by Ninja. Each `rule` may have only one `command`
644   declaration. To specify multiple commands use `&&` (or similar) to
645   concatenate operations. 
646
647 `depfile`:: path to an optional `Makefile` that contains extra
648   _implicit dependencies_ (see <<ref_dependencies,the reference on
649   dependency types>>).  This is explicitly to support `gcc` and its `-M`
650   family of flags, which output the list of headers a given `.c` file
651   depends on.
652 +
653 Use it like in the following example:
654 +
655 ----
656 rule cc
657   depfile = $out.d
658   command = gcc -MMD -MF $out.d [other gcc flags here]
659 ----
660 +
661 When loading a `depfile`, Ninja implicitly adds edges such that it is
662 not an error if the listed dependency is missing.  This allows you to
663 delete a depfile-discovered header file and rebuild, without the build
664 aborting due to a missing input.
665
666 `description`:: a short description of the command, used to pretty-print
667   the command as it's running.  The `-v` flag controls whether to print
668   the full command or its description; if a command fails, the full command
669   line will always be printed before the command's output.
670
671 `generator`:: if present, specifies that this rule is used to
672   re-invoke the generator program.  Files built using `generator`
673   rules are treated specially in two ways: firstly, they will not be
674   rebuilt if the command line changes; and secondly, they are not
675   cleaned by default.
676
677 `restat`:: if present, causes Ninja to re-stat the command's outputs
678   after execution of the command.  Each output whose modification time
679   the command did not change will be treated as though it had never
680   needed to be built.  This may cause the output's reverse
681   dependencies to be removed from the list of pending build actions.
682
683 `rspfile`, `rspfile_content`:: if present (both), Ninja will use a
684   response file for the given command, i.e. write the selected string
685   (`rspfile_content`) to the given file (`rspfile`) before calling the
686   command and delete the file after successful execution of the
687   command.
688 +
689 This is particularly useful on Windows OS, where the maximal length of
690 a command line is limited and response files must be used instead.
691 +
692 Use it like in the following example:
693 +
694 ----
695 rule link
696   command = link.exe /OUT$out [usual link flags here] @$out.rsp
697   rspfile = $out.rsp
698   rspfile_content = $in
699
700 build myapp.exe: link a.obj b.obj [possibly many other .obj files]
701 ----
702
703 Finally, the special `$in` and `$out` variables expand to the
704 shell-quoted space-separated list of files provided to the `build`
705 line referencing this `rule`.
706
707 Build dependencies
708 ~~~~~~~~~~~~~~~~~~
709 [[ref_dependencies]]
710
711 There are three types of build dependencies which are subtly different.
712
713 1. _Explicit dependencies_, as listed in a build line.  These are
714    available as the `$in` variable in the rule.  Changes in these files
715    cause the output to be rebuilt; if these file are missing and
716    Ninja doesn't know how to build them, the build is aborted.
717 +
718 This is the standard form of dependency to be used for e.g. the
719 source file of a compile command.
720
721 2. _Implicit dependencies_, either as picked up from
722    a `depfile` attribute on a rule or from the syntax +| _dep1_
723    _dep2_+ on the end of a build line.  The semantics are identical to
724    explicit dependencies, the only difference is that implicit dependencies
725    don't show up in the `$in` variable.
726 +
727 This is for expressing dependencies that don't show up on the
728 command line of the command; for example, for a rule that runs a
729 script, the script itself should be an implicit dependency, as
730 changes to the script should cause the output to rebuild.
731 +
732 Note that dependencies as loaded through depfiles have slightly different
733 semantics, as described in the <<ref_rule,rule reference>>.
734
735 3. _Order-only dependencies_, expressed with the syntax +|| _dep1_
736    _dep2_+ on the end of a build line.  When these are out of date, the
737    output is not rebuilt until they are built, but changes in order-only
738    dependencies alone do not cause the output to be rebuilt.
739 +
740 Order-only dependencies can be useful for bootstrapping dependencies
741 that are only discovered during build time: for example, to generate a
742 header file before starting a subsequent compilation step.  (Once the
743 header is used in compilation, a generated dependency file will then
744 express the implicit dependency.)
745
746 Variable expansion
747 ~~~~~~~~~~~~~~~~~~
748
749 Variables are expanded in paths (in a `build` or `default` statement)
750 and on the right side of a `name = value` statement.
751
752 When a `name = value` statement is evaluated, its right-hand side is
753 expanded immediately (according to the below scoping rules), and
754 from then on `$name` expands to the static string as the result of the
755 expansion.  It is never the case that you'll need to "double-escape" a
756 value to prevent it from getting expanded twice.
757
758 All variables are expanded immediately as they're encountered in parsing,
759 with one important exception: variables in `rule` blocks are expanded
760 when the rule is _used_, not when it is declared.  In the following
761 example, the `demo` rule prints "this is a demo of bar".
762
763 ----
764 rule demo
765   command = echo "this is a demo of $foo'
766
767 build out: demo
768   foo = bar
769 ----
770
771 Evaluation and scoping
772 ~~~~~~~~~~~~~~~~~~~~~~
773 [[ref_scope]]
774
775 Top-level variable declarations are scoped to the file they occur in.
776
777 The `subninja` keyword, used to include another `.ninja` file,
778 introduces a new scope.  The included `subninja` file may use the
779 variables from the parent file, and shadow their values for the file's
780 scope, but it won't affect values of the variables in the parent.
781
782 To include another `.ninja` file in the current scope, much like a C
783 `#include` statement, use `include` instead of `subninja`.
784
785 Variable declarations indented in a `build` block are scoped to the
786 `build` block.  The full lookup order for a variable expanded in a
787 `build` block (or the `rule` is uses) is:
788
789 1. Special built-in variables (`$in`, `$out`).
790
791 2. Build-level variables from the `build` block.
792
793 3. Rule-level variables from the `rule` block (i.e. `$command`).
794    (Note from the above discussion on expansion that these are
795    expanded "late", and may make use of in-scope bindings like `$in`.)
796
797 4. File-level variables from the file that the `build` line was in.
798
799 5. Variables from the file that included that file using the
800    `subninja` keyword.
801