build指定了编译的依赖关系等
rule指定了编译的规则也就是编译命令等,就是指定从依赖的文件如何形成目标文件的。
ninja中同样有一些变量,通过$来引用不用加任何括号的
注意,在rule中同样也是定义一些变量,但是这些变量是ninja内部指定的。下面是这些特殊变量的文档
A rule block contains a list of key = value declarations that
affect the processing of the rule. Here is a full list of special
keys.
command (required)
the command line to run. This string (after
$variables are expanded) is passed directly to sh -c without
interpretation by Ninja. Each rule may have only one command
declaration. To specify multiple commands use && (or similar) to
concatenate operations.
depfile
path to an optional Makefile that contains extra implicit dependencies (see the reference on dependency types). This is explicitly to support C/C++ header
dependencies; see the full discussion.
deps
(Available since Ninja 1.3.) if present, must be one of gcc or msvc to specify special dependency processing. See the full discussion. The generated database is
stored as .ninja_deps in the builddir, see the discussion of builddir.
msvc_deps_prefix
(Available since Ninja 1.5.) defines the string
which should be stripped from msvc’s /showIncludes output. Only
needed when deps = msvc and no English Visual Studio version is used.
description
a short description of the command, used to pretty-print
the command as it’s running. The -v flag controls whether to print
the full command or its description; if a command fails, the full command
line will always be printed before the command’s output.
generator
if present, specifies that this rule is used to
re-invoke the generator program. Files built using generator
rules are treated specially in two ways: firstly, they will not be
rebuilt if the command line changes; and secondly, they are not
cleaned by default.
in
the space-separated list of files provided as inputs to the build line
referencing this rule, shell-quoted if it appears in commands. ($in is
provided solely for convenience; if you need some subset or variant of this
list of files, just construct a new variable with that list and use
that instead.)
in_newline
the same as $in except that multiple inputs are
separated by newlines rather than spaces. (For use with $rspfile_content; this works around a bug in the MSVC linker where
it uses a fixed-size buffer for processing input.)
out
the space-separated list of files provided as outputs to the build line
referencing this rule, shell-quoted if it appears in commands.
restat
if present, causes Ninja to re-stat the command’s outputs
after execution of the command. Each output whose modification time
the command did not change will be treated as though it had never
needed to be built. This may cause the output’s reverse
dependencies to be removed from the list of pending build actions.
rspfile, rspfile_content
if present (both), Ninja will use a
response file for the given command, i.e. write the selected string
(rspfile_content) to the given file (rspfile) before calling the
command and delete the file after successful execution of the
command.
This is particularly useful on Windows OS, where the maximal length of
a command line is limited and response files must be used instead.
Use it like in the following example:
rule link
command = link.exe /OUT$out [usual link flags here] @$out.rsp
rspfile = $out.rsp
rspfile_content = $in
build myapp.exe: link a.obj b.obj [possibly many other .obj files]
同样只要是变量就需要赋值。注意rule的内容需要另起一行,并且必须要空格而不是tab按键。
编译语句的格式为
build 输出文件: 规则名 输入文件。注意这个语句中的输出文件和输入文件将会成为rule中的对应的$in,$out
可以在build语句的后面换行提供类似于key=value这样的键值对在当前的这个build中来临时shadow掉在当前的编译文件中的那些变量。也就是说这样指定的键值对本次会输入到对应的rule中去。