对于gn来说,工具链被抽象为一系列的工具描述,我们要做的就是写对应的工具描述文件出来。
首先在工程根目录建立一个文件.gn(注意文件的名字为空,后缀为.gn),文件中配置工具链描述文件所在的路径
buildconfig = "//build/buildconfig.gn"
该变量定义了构建工具链描述文件所在的路径,//表示工程根目录。
在buildconfig.gn中定义工具链。
set_default_toolchain("//build/toolchain:armcc")
上面说到了,工具链就是一系列的描述,所以修改工具链也就是修改这一系列的描述,以下为一个典型的armcc的描述文件
toolchain("armcc") {
tool("cc") {
depfile = "{{output}}.d"
command = "$MDK_DIR/ARM/ARMCC/bin/armcc.exe --c99 -g --apcs=interwork -DUSE_STDPERIPH_DRIVER --depend $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
depsformat = "gcc"
description = "CC {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("asm") {
depfile = "{{output}}.d"
command = "$MDK_DIR/ARM/ARMCC/bin/armasm.exe -g --apcs=interwork --xref --depend $depfile {{include_dirs}} {{asmflags}} {{source}} -o {{output}}"
depsformat = "gcc"
description = "ASM {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("alink") {
rspfile = "{{output}}.rsp"
command = "$MDK_DIR/ARM/ARMCC/bin/armar.exe -r {{output}} --via $rspfile"
description = "AR {{target_output_name}}{{output_extension}}"
rspfile_content = "{{inputs}}"
outputs = [
"{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
]
default_output_extension = ".a"
output_prefix = "lib"
}
tool("link") {
outfile = "bin/{{target_output_name}}{{output_extension}}"
rspfile = "$outfile.rsp"
command = "$MDK_DIR/ARM/ARMCC/bin/armlink.exe {{ldflags}} --info veneers --info unused --info totals --info sizes --symbols --callgraph --xref --strict --summary_stderr --info summarysizes --map --list bin/{{target_output_name}}.map -o $outfile --via $rspfile {{libs}}"
description = "LINK $outfile"
default_output_dir = "{{root_out_dir}}"
rspfile_content = "{{inputs}}"
default_output_extension = ".axf"
outputs = [
outfile,
]
}
tool("stamp") {
command = "cmd.exe /c echo > {{output}}"
description = "STAMP {{output}}"
}
tool("copy") {
command = "cp /y {{source}} {{output}}"
description = "COPY {{source}} {{output}}"
}
}
其可选的所有定义在该博客中进行介绍:
各个工具分开看,首先是编译器
tool("cc") {
depfile = "{{output}}.d"
command = "$MDK_DIR/ARM/ARMCC/bin/armcc.exe --c99 -g --apcs=interwork -DUSE_STDPERIPH_DRIVER --depend $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
depsformat = "gcc"
description = "CC {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("cc")表征为编译器,其依赖文件生成为.d文件command即为执行编译时需要运行的指令,编译器的固定参数的话可以直接写在toolchain的定义中,其他参数则由具体的编译实体定义,其中使用双大括号包起来的参数将在运行gn时进行展开以生成对应的inja文件。armcc的编译参数在此不进行解释,详细的作用自行查阅armcc的官方说明手册。
汇编器,汇编器与编译器类似,也没什么需要多说的。
tool("asm") {
depfile = "{{output}}.d"
command = "$MDK_DIR/ARM/ARMCC/bin/armasm.exe -g --apcs=interwork --xref --depend $depfile {{include_dirs}} {{asmflags}} {{source}} -o {{output}}"
depsformat = "gcc"
description = "ASM {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
静态库链接器
tool("alink") {
rspfile = "{{output}}.rsp"
command = "$MDK_DIR/ARM/ARMCC/bin/armar.exe -r {{output}} --via $rspfile"
description = "AR {{target_output_name}}{{output_extension}}"
rspfile_content = "{{inputs}}"
outputs = [
"{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
]
default_output_extension = ".a"
output_prefix = "lib"
}
静态库链接器在arm工具链中为armar,其主要作用就是把.o文件打包成.a文件(即静态库文件),需要注意这个地方配置了rsp文件,原因是在静态库打包时,可能会存在命令行长度过长的问题(windows限制命令行最大程度为8192个字符),超出该长度的话会导致链接失效,因此将链接参数写入rsp文件,armar读取rsp文件来做链接,这样就不会有字符长度限制了。
链接器
tool("link") {
outfile = "bin/{{target_output_name}}{{output_extension}}"
rspfile = "$outfile.rsp"
command = "$MDK_DIR/ARM/ARMCC/bin/armlink.exe {{ldflags}} --info veneers --info unused --info totals --info sizes --symbols --callgraph --xref --strict --summary_stderr --info summarysizes --map --list bin/{{target_output_name}}.map -o $outfile --via $rspfile {{libs}}"
description = "LINK $outfile"
default_output_dir = "{{root_out_dir}}"
rspfile_content = "{{inputs}}"
default_output_extension = ".axf"
outputs = [
outfile,
]
}
链接器在arm工具链中为armlink,起作用是完成完整链接,链接处最终生成文件axf,只要有了axf就可以进行下载和调试了!
其他工具
tool("stamp") {
command = "cmd.exe /c echo > {{output}}"
description = "STAMP {{output}}"
}
tool("copy") {
command = "cp /y {{source}} {{output}}"
description = "COPY {{source}} {{output}}"
}
其他工具主要包含了两个,stamp用于创建一个保存构建时间戳的文件,copy则用来进行必要的复制操作。
对于其中使用到的相关的工具链命令行参数均需要自行查阅对应的官方说明手册进行学习,这篇文章主要介绍构建系统,对于编译工具链的使用在此不进行赘述。
这样我们就配置好了最基本的armcc编译工具链,之后可以开始工程的架构了