1 基本用法
先看一下我们的例子
$ ls
bar.c build.ninja foo.c out special.c
Build.ninja文件的内如如下:
cflags = -Wall
rule cc
command = gcc $cflags -c $in -o $out
# If left unspecified, builds get the outer $cflags.
build foo.o: cc foo.c
# But you can shadow variables like cflags for a particular build.
build special.o: cc special.c
# The variable was only shadowed for the scope of special.o;
# Subsequent build lines get the outer (original) cflags.
build bar.o: cc bar.c
build foo: phony out/build.ninja
执行编译命令:
$ ninja -f build.ninja
[3/3] gcc -Wall -c foo.c -o foo.o
foo.c: In function ‘main’:
foo.c:4: warning: control reaches end of non-void function
[3/3] gcc -Wall -c special.c -o special.o
special.c: In function ‘main’:
special.c:4: warning: control reaches end of non-void function
[3/3] gcc -Wall -c bar.c -o bar.o
bar.c: In function ‘main’:
bar.c:4: warning: control reaches end of non-void function
最后按照规则生成三个.o文件
ninja$ ls
bar.c bar.o build.ninja foo.c foo.o out special.c special.o
2 More details
1. phony:主要是用来创建给目标创建别名
build foo: phony some/file/in/a/faraway/subdir/foo
比如在android out目录下的combined-sp9850ka_1h10_native.ninja
builddir = out
include out/build-sp9850ka_1h10_native.ninja
include out/soong/build.ninja
build out/combined-sp9850ka_1h10_native.ninja: phony out/soong/build.ninja
如下面我们的例子
build bar.o: cc bar.c
build test_phony: phony
build all: phony test_phony bar.o
当我们执行ninja all的时候,bar.o和test_phony build被执行。
2 default
build test_phony: phony
build all: phony test_phony bar.o
default all
当执行ninja –f build.ninja不指定目标的时候,默认编译default下的规则
3 ninja log
在当前的编译目录下存放这.ninja_log
4 depfile
rule cc
depfile = $out.d
command = gcc -MMD -MF $out.d [other gcc flags here]
--MMD 告诉gcc output header dependencies, -MF 告诉在哪写入这些dependencies
5 deps
rule g.cc.aidl
command = ${g.cc.aidlCmd} -d${out}.d -ninja ${aidlFlags} ${in} ${outDir} ${out}
depfile = ${out}.d
deps = gcc
deps=gcc 输出gcc-style dependencies
6 词法语法
$ followed by a newline
行连接符,同shell中的\
$ followed by text
如${g.bootstrap.srcDir}/Android.bp | $builder 变量 |后面是编译规则
${varname}
同 $变量
$ followed by space
后面跟着一个空格,表示一个空格
$:
表示一个:
$$
表示一个$符号
7 其他语法的使用详见:https://ninja-build.org/manual.html#_the_literal_phony_literal_rule