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