博客迁移已迁移到https://www.guyuemeng.com

Overview

  1. 定义一个工具链配置,配置名为"gcc"
toolchain("gcc") {
  1. 工具链中工具配置(tool)
  tool("cc") {
  1. Compiler tools所支持头文件依赖(编译依赖.d,deps)
    depfile = "{{output}}.d"depsformat = "gcc"
  1. 编译选项(command: cc/cxx)
    command = "gcc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
  1. 工具链显示(description)
    description = "CC {{output}}"
  1. 库文件输出路径(outputs)
    outputs =[ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]

next:

  }tool("cxx") {depfile = "{{output}}.d"depsformat = "gcc"command = "g++ -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"description = "CXX {{output}}"outputs =[ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]}tool("alink") {
  1. 关联文件(rsp)
    rspfile = "{{output}}.rsp"rspfile_content = "{{inputs}}"
  1. 编译选项(command: a)
    command = "rm -f {{output}} && ar rcs {{output}} @$rspfile"

next:

    description = "AR {{target_output_name}}{{output_extension}}"outputs =[ "{{target_out_dir}}/{{target_output_name}}{{output_extension}}" ]default_output_extension = ".a"output_prefix = "lib"}tool("solink") {soname = "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.so".sofile = "{{output_dir}}/$soname"rspfile = soname + ".rsp"
  1. 编译选项(command: so)
    command = "g++ -shared {{ldflags}} -o $sofile -Wl,-soname=$soname @$rspfile"rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}"

next:

    description = "SOLINK $soname"# Use this for {{output_extension}} expansions unless a target manually# overrides it (in which case {{output_extension}} will be what the target# specifies).default_output_extension = ".so"# Use this for {{output_dir}} expansions unless a target manually overrides# it (in which case {{output_dir}} will be what the target specifies).default_output_dir = "{{root_out_dir}}"outputs = [ sofile ]
  1. 连接依赖(link_depend)
    link_output = sofiledepend_output = sofile

next:

    output_prefix = "lib"}tool("link") {outfile = "{{target_output_name}}{{output_extension}}"rspfile = "$outfile.rsp"command = "g++ {{ldflags}} -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group {{libs}}"description = "LINK $outfile"default_output_dir = "{{root_out_dir}}"rspfile_content = "{{inputs}}"outputs = [ outfile ]}tool("stamp") {command = "touch {{output}}"description = "STAMP {{output}}"}tool("copy") {command = "cp -af {{source}} {{output}}"description = "COPY {{source}} {{output}}"}
}

Key Word

tool(“tool_name”)

gn reference

   Compiler tools:"cc": C compiler"cxx": C++ compiler"objc": Objective C compiler"objcxx": Objective C++ compiler"rc": Resource compiler (Windows .rc files)"asm": AssemblerLinker tools:"alink": Linker for static libraries (archives)"solink": Linker for shared libraries"link": Linker for executablesOther tools:"stamp": Tool for creating stamp files"copy": Tool to copy files."action": Defaults for actionsPlatform specific tools:"copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle."compile_xcassets": [iOS, macOS] Tool to compile asset catalogs.Rust tools:"rust_bin": Tool for compiling Rust binaries"rust_cdylib": Tool for compiling C-compatible dynamic libraries."rust_dylib": Tool for compiling Rust dynamic libraries."rust_macro": Tool for compiling Rust procedural macros."rust_rlib": Tool for compiling Rust libraries."rust_staticlib": Tool for compiling Rust static libraries.

depfile/depsformat

gn reference

    depfile  [string with substitutions]Valid for: compiler tools (optional)If the tool can write ".d" files, this specifies the name of theresulting file. These files are used to list header file dependencies(or other implicit input dependencies) that are discovered at buildtime. See also "depsformat".Example: depfile = "{{output}}.d"depsformat  [string]Valid for: compiler tools (when depfile is specified)Format for the deps outputs. This is either "gcc" or "msvc". See theninja documentation for "deps" for more information.Example: depsformat = "gcc"

gcc --help

-MD Write a depfile containing user and system headers
-MF Write depfile output from -MMD, -MD, -MM, or -M to
-MG Add missing headers to depfile
-MJ Write a compilation database entry per input
-MMD Write a depfile containing user headers
-MM Like -MMD, but also implies -E and writes to stdout by default
-MP Create phony target for each dependency (other than main file)
-MQ Specify name of main file output to quote in depfile
-MT Specify name of main file output in depfile
-MV Use NMake/Jom format for the depfile
-M Like -MD, but also implies -E and writes to stdout by default

紧随其后的command,包含编译选项-MMD -MF $depfile,即生成包含依赖用户头文件的{{output}}.d文件。

方舟编译器中的编译选项为-MD -MT {{output}} -MF $depfile,即生成包含依赖用户和系统头文件的{{output}}.d文件,同时-MT指定{{output}}.d中的主文件名为{{output}},取代{{output}}.o

command: cc/cxx

gn reference

  Compiler tools have the notion of a single input and a single output, alongwith a set of compiler-specific flags. The following expansions areavailable:{{asmflags}}{{cflags}}{{cflags_c}}{{cflags_cc}}{{cflags_objc}}{{cflags_objcc}}{{defines}}{{include_dirs}}Strings correspond that to the processed flags/defines/includedirectories specified for the target.Example: "--enable-foo --enable-bar"Defines will be prefixed by "-D" and include directories will beprefixed by "-I" (these work with Posix tools as well as Microsoftones).

gn reference

    {{output}}The relative path and name of the output(s) of the current build step.If there is more than one output, this will expand to a list of all ofthem. Example: "out/base/my_file.o"{{source}}The relative path and name of the current input file.Example: "../../base/my_file.cc"

gcc --help

-c Only run preprocess, compile, and assemble steps
-D = Define to (or 1 if omitted)
-I

Add directory to include search path

description

gn reference

    description  [string with substitutions, optional]Valid for: all toolsWhat to print when the command is run.Example: description = "Compiling {{source}}"

outputs

** gn reference**

   outputs  [list of strings with substitutions]Valid for: Linker and compiler tools (required)An array of names for the output files the tool produces. These arerelative to the build output directory. There must always be at leastone output file. There can be more than one output (a linker mightproduce a library and an import library, for example).This array just declares to GN what files the tool will produce. It isyour responsibility to specify the tool command that actually producesthese files.If you specify more than one output for shared library links, youshould consider setting link_output, depend_output, andruntime_outputs.Example for a compiler tool that produces .obj files:outputs = ["{{source_out_dir}}/{{source_name_part}}.obj"]Example for a linker tool that produces a .dll and a .lib. The use of{{target_output_name}}, {{output_extension}} and {{output_dir}} allowsthe target to override these values.outputs = ["{{output_dir}}/{{target_output_name}}""{{output_extension}}","{{output_dir}}/{{target_output_name}}.lib",]{{source_out_dir}}The directory in the generated file and output directories,respectively, for the current input file. If the source file is in thesame directory as the target is declared in, they will will be the sameas the "target" versions above. Example: "gen/base/test"{{source_name_part}}The filename part of the source file with no directory or extension. Thiswill generally be used for specifying a transformation from a source fileto a destination file with the same name but different extension."//foo/bar/baz.txt" => "baz"{{target_output_name}}The short name of the current target with no path information, or thevalue of the "output_name" variable if one is specified in the target.This will include the "output_prefix" if any. See also {{label_name}}.Example: "libfoo" for the target named "foo" and an output prefix forthe linker tool of "lib".{{output_extension}}The value of the "output_extension" variable in the target, or thevalue of the "default_output_extension" value in the tool if the targetdoes not specify an output extension.Example: ".so"

rsp

gn reference

    rspfile  [string with substitutions]Valid for: all tools except "action" (optional)Name of the response file. If empty, no response file will beused. See "rspfile_content".rspfile_content  [string with substitutions]Valid for: all tools except "action" (required when "rspfile" is used)The contents to be written to the response file. This may include allor part of the command to send to the tool which allows you to getaround OS command-line length limits.This example adds the inputs and libraries to a response file, butpasses the linker flags directly on the command line:tool("link") {command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"rspfile = "{{output}}.rsp"rspfile_content = "{{inputs}} {{solibs}} {{libs}}"}

command: a

man ar

NAMEar -- create and maintain library archives-c      Whenever an archive is created, an informational message to that effect is written to standard error. If the -c option is specified, ar creates the archive silently.-r      Replace or add the specified files to the archive.  If the archive does not exist a new archive file is created.  Files that replace existing files do not change the order of the files within the archive.  New files are appended to the archive unless one of the options -a, -b or -i is specified.-s      Write an object-file index into the archive, or update an existing one, even if no other change is made to the archive.  You may use this modifier flag either with any operation, or alone.  Running `ar s' on an archive is equivalent to running `ranlib' on it.

command: so

g++ --help

-shared Create a shared library.
-Wl, Pass comma-separated on to the linker.

-Wl,-soname=$soname可参考Linux下动态链接库文件的realname、soname和linkname。

-Wl,--whole-archive/--no-whole-archive可参考Linux C/C++ 链接选项之静态库–whole-archive,–no-whole-archive和–start-group, --end-group。

gn reference

    {{solibs}}Extra libraries from shared library dependencies not specified in the{{inputs}}. This is the list of link_output files from shared libraries(if the solink tool specifies a "link_output" variable separate fromthe "depend_output").These should generally be treated the same as libs by your tool.Example: "libfoo.so libbar.so"{{libs}}Expands to the list of system libraries to link to. Each will beprefixed by the "lib_switch".As a special case to support Mac, libraries with names ending in".framework" will be added to the {{libs}} with "-framework" precedingit, and the lib prefix will be ignored.Example: "-lfoo -lbar"

link depend

    link_output  [string with substitutions]depend_output  [string with substitutions]Valid for: "solink" only (optional)These two files specify which of the outputs from the solink toolshould be used for linking and dependency tracking. These should matchentries in the "outputs". If unspecified, the first item in the"outputs" array will be used for all. See "Separate linking anddependencies for shared libraries" below for more.On Windows, where the tools produce a .dll shared library and a .libimport library, you will want the first two to be the import libraryand the third one to be the .dll file. On Linux, if you're not doingthe separate linking/dependency optimization, all of these should bethe .so output.

@/$/${}/{{}}

@ gn reference

   - Response file: If the input starts with an "@", it will be interpreted asa path to a file containing a list of labels or file names, one per line.This allows us to handle long lists of inputs without worrying aboutcommand line limits.

$/${} gn language: Strings

{{}}猜测为表达式展开。

启用toolchain

build/BUILDCONFIG.gn中配置:

set_default_toolchain("//build/toolchain:gcc")

参考

[1] toolchain: Defines a toolchain.

https://gn.googlesource.com/gn/+/master/docs/reference.md#func_toolchain

[2] OpenArkCompiler

https://gitee.com/harmonyos/OpenArkCompiler

gn: toolchain相关推荐

  1. GN_1_在Ubuntu22.04安装GN

    安装依赖 sudo apt-get install clang sudo apt install ninja-build 安装GN sudo apt install git git clone htt ...

  2. [GN] 谷歌元构建系统GN,toolchain配置

    注:主要信息来自于GN的帮助文件 典型格式: tool(<tool type>) {     <tool variables...>   } tool type可选的范围: 编 ...

  3. WebRTC编译系统之GYP,gn和ninja

    GN(Generate Ninja)来生成构建脚本,使用 ninja 来构建. gn 的介绍在这里:https://www.chromium.org/developers/gn-build-confi ...

  4. CEF编译 执行gn args out\Release_GN_x86异常

    gn args out\Debug_GN_x86 用来配置编译参数,执行gn args out\Release_GN_x86时异常: Toolchain is out of date. Run &qu ...

  5. centos7.9使用gn+ninja编译程序

    1 编译ninja和gn 请参见上篇文章centos7.9编译安装构建系统gn+ninja 2 编译demo程序 当ninja和gn都编译安装好之后,就可以来感受一下它们的编译乐趣了.下边以gn目录e ...

  6. Fuchsia编译系统的GN结构

    在下载完成fuchsia的代码之后,编译代码之前,需要使用fx set命令指定要编译的目标(product.board).参见以下的fx set命令的帮助信息.其中的PRODUCT和BOARD参数可由 ...

  7. gn、ninja的安装-Ubuntu18.04

    版权声明:原创文章,欢迎转载,但请注明出处,谢谢.https://blog.csdn.net/qiuguolu1108/article/details/103842556 如果你不想编译gn.ninj ...

  8. 用 Ninja and GN 来加速 C++构建

    Ninja Ninja 原意是忍者的意思,它是一个专注于速度的小型构建工具.它是一个构建系统. 它将文件的相互依赖关系(通常是源代码和输出可执行文件)作为输入,并快速编排构建它们. 运行Ninja,默 ...

  9. GN_2_使用GN编译自己写的程序

    GN_1_在Ubuntu22.04安装GN 中已经介绍过GN的安装.下面我们使用GN来编译自己写的helloworld程序. 复制例子simple_build中的 .gn和build到你自己新建的目录 ...

  10. GYP,GN和Ninja

    chromium的编译过程中用到了GYP,GN和Ninja这三个构建工具,GYP是一个在不同平台构建项目的工具,GN是GYP的升级版,Ninja是一个小型追求速度的构建系统. GYP GYP是Gene ...

最新文章

  1. python并发编程方法_Python Futures并发编程详解
  2. python缩进在程序中长度统一且强制使用_Python习题纠错1
  3. 2021 最新版 Spring Boot 速记教程
  4. 【CUDA学习】GPU硬件结构
  5. Spring 中的内部bean 和集合
  6. Java将列表转换为数组,反之亦然
  7. 内聚的极限: 软件开发的不确定性原理
  8. FineReport:关于扩展行列求各种条件下的函数运用
  9. Qt总结之九:QMap中嵌套QList
  10. iwrite提交不了作业_痛点!为什么开发了那么多软件,还是解决不了教学问题!...
  11. c语言编程打印格式输出总结
  12. 学习记录:xmind2testcase测试用例模板
  13. 华为鸿蒙os logo,华为鸿蒙OS Logo曝光:Powered by HarmonyOS
  14. JAVA实现PDF合并、拆分代码工具类
  15. 美通社企业新闻汇总 | 2019.1.21 | 春节访日可享受更多免税优惠;勃林格殷格翰国产化猪疫苗上市...
  16. Golang中的并发:如何使用Goroutines?详细指南
  17. 你有多久没有看过星星
  18. Xcode13.3.1 upload ipa error:Invalid Provisioning Signature....STATE_ERROR.VALIDATION_ERROR.9016解决方案
  19. RPO和RTO是什么?
  20. 云与瘦客户机 未来IT数据安全延续

热门文章

  1. Windows下快速删除大量文件
  2. 最新CISP模拟考试题库及答案(一)
  3. 山峰Mac动态桌面壁纸
  4. 机器视觉中常用图像处理库都有哪些?
  5. mysql批量生成随机姓名、手机号等数据
  6. (droid分享)新浪微博开发系列【十一】之查看微博正文
  7. java编码解码工具类
  8. Hash表的时间复杂度为什么是O(1)?
  9. ie html保存为pdf文件,IE浏览器怎么将网页保存为pdf文档?将网页保存为pdf文档的方法说明...
  10. 伺服电机PID控制及增益调节