Ubuntu22.04中搭建GN编译环境
Ubuntu22.04中搭建GN编译环境
GN
是一种元构建系统(Meta Build System),而ninja
是一个注重速度的构建系统。ninja
通常与元构建系统一并使用。常见的有cmake + make
cmake + ninja
gn + ninja
。本文要介绍的是gn + ninja
的构建方式,另外OpenHarmony
编译子系统也是以GN
和Ninja
构建为基座,对构建和配置粒度进行部件化抽象、对内建模块进行功能增强、对业务模块进行功能扩展的系统,具体可以参见:OpenHarmony编译构建指导
1. ninja安装
ninja安装有两种途径,一是通过ubuntu自带的软件库安装;另一个是从GitHub上下载ninja源码自己编译。本文是直接在Ubuntu中下载,如想自己编译,可以根据GitHub上的README.md进行操作,如:
$ git clone https://github.com/ninja-build/ninja.git && cd ninja
$ git checkout release
$ cat README.md
我们直接通过ubuntu22.04中的sudo apt install ninja-build
直接安装ninja
软件:
sudo apt install ninja-build
安装完毕后,查询版本,显示出版本号后,说明安装成功。
$ ninja --version
1.11.1
说明我的Ubuntu22.04系统的ninja
版本号是:1.11.1
2. 安装clang
编译gn需要编译器支持C++17的特性,所以编译器的版本不能太低,经过测试clang至少需要8.0的版本。我在我的系统上使用sudo apt search clang
命令发现可以安装比较新的版本: clang-16
,当然也可以安装其他的clang
版本:
ccf@ccf-virtual-machine:~/WorkSpace/GithubProjects/ninja$ sudo apt search clang
Sorting... Done
Full Text Search... Done
bear/noble 3.1.3-1build4 amd64generate compilation database for Clang toolingc2go/noble-updates,noble-security 0.26.10-1ubuntu0.24.04.3 amd64tool for transpiling C to Goccls/noble 0.20240202-1 amd64C/C++/ObjC language serverclang/noble 1:18.0-59~exp2 amd64C, C++ and Objective-C compiler (LLVM based), clang binaryclang-14/noble 1:14.0.6-19build4 amd64C, C++ and Objective-C compilerclang-14-doc/noble,noble 1:14.0.6-19build4 allC, C++ and Objective-C compiler - Documentationclang-14-examples/noble 1:14.0.6-19build4 amd64Clang examplesclang-15/noble 1:15.0.7-14build3 amd64C, C++ and Objective-C compilerclang-15-doc/noble,noble 1:15.0.7-14build3 allC, C++ and Objective-C compiler - Documentationclang-15-examples/noble 1:15.0.7-14build3 amd64Clang examplesclang-16/noble,now 1:16.0.6-23ubuntu4 amd64 [installed]C, C++ and Objective-C compilerclang-16-doc/noble,noble 1:16.0.6-23ubuntu4 allC, C++ and Objective-C compiler - Documentationclang-16-examples/noble 1:16.0.6-23ubuntu4 amd64Clang examplesclang-17/noble 1:17.0.6-9ubuntu1 amd64C, C++ and Objective-C compilerclang-17-doc/noble,noble 1:17.0.6-9ubuntu1 allC, C++ and Objective-C compiler - Documentationclang-17-examples/noble,noble 1:17.0.6-9ubuntu1 allClang examplesclang-18/noble-updates 1:18.1.3-1ubuntu1 amd64C, C++ and Objective-C compilerclang-18-doc/noble-updates,noble-updates 1:18.1.3-1ubuntu1 allC, C++ and Objective-C compiler - Documentationclang-18-examples/noble-updates,noble-updates 1:18.1.3-1ubuntu1 allClang examplesclang-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64C, C++ and Objective-C compilerclang-19-doc/noble-updates,noble-updates 1:19.1.1-1ubuntu1~24.04.2 allC, C++ and Objective-C compiler - Documentationclang-19-examples/noble-updates,noble-updates 1:19.1.1-1ubuntu1~24.04.2 allClang examplesclang-format/noble 1:18.0-59~exp2 amd64Tool to format C/C++/Obj-C codeclang-format-14/noble 1:14.0.6-19build4 amd64Tool to format C/C++/Obj-C codeclang-format-15/noble 1:15.0.7-14build3 amd64Tool to format C/C++/Obj-C codeclang-format-16/noble 1:16.0.6-23ubuntu4 amd64Tool to format C/C++/Obj-C codeclang-format-17/noble 1:17.0.6-9ubuntu1 amd64Tool to format C/C++/Obj-C codeclang-format-18/noble-updates 1:18.1.3-1ubuntu1 amd64Tool to format C/C++/Obj-C codeclang-format-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64Tool to format C/C++/Obj-C codeclang-tidy/noble 1:18.0-59~exp2 amd64clang-based C++ linter toolclang-tidy-14/noble 1:14.0.6-19build4 amd64clang-based C++ linter toolclang-tidy-15/noble 1:15.0.7-14build3 amd64clang-based C++ linter toolclang-tidy-16/noble 1:16.0.6-23ubuntu4 amd64clang-based C++ linter toolclang-tidy-17/noble 1:17.0.6-9ubuntu1 amd64clang-based C++ linter toolclang-tidy-18/noble-updates 1:18.1.3-1ubuntu1 amd64clang-based C++ linter toolclang-tidy-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64clang-based C++ linter toolclang-tools/noble 1:18.0-59~exp2 amd64clang-based toolsclang-tools-14/noble 1:14.0.6-19build4 amd64clang-based tools for C/C++ developmentsclang-tools-15/noble 1:15.0.7-14build3 amd64clang-based tools for C/C++ developmentsclang-tools-16/noble 1:16.0.6-23ubuntu4 amd64clang-based tools for C/C++ developmentsclang-tools-17/noble 1:17.0.6-9ubuntu1 amd64clang-based tools for C/C++ developmentsclang-tools-18/noble-updates 1:18.1.3-1ubuntu1 amd64clang-based tools for C/C++ developmentsclang-tools-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64clang-based tools for C/C++ developmentsclangd/noble 1:18.0-59~exp2 amd64Language server that provides IDE-like features to editorsclangd-14/noble 1:14.0.6-19build4 amd64Language server that provides IDE-like features to editorsclangd-15/noble 1:15.0.7-14build3 amd64Language server that provides IDE-like features to editorsclangd-16/noble 1:16.0.6-23ubuntu4 amd64Language server that provides IDE-like features to editorsclangd-17/noble 1:17.0.6-9ubuntu1 amd64Language server that provides IDE-like features to editorsclangd-18/noble-updates 1:18.1.3-1ubuntu1 amd64Language server that provides IDE-like features to editorsclangd-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64Language server that provides IDE-like features to editorsclazy/noble 1.11-4ubuntu2 amd64Clang plugin for additional warningsdebugbreak/noble 1.0-1 amd64Put breakpoints in C/C++ codeelpa-format-all/noble,noble 0.5.0-2 allAuto-format C, C++, JS, Python, Ruby and 50 other languageselpa-irony/noble,noble 1.6.0-2build2 allEmacs C/C++ minor mode powered by libclangemscripten/noble,noble 3.1.6~dfsg-7 allLLVM-to-JavaScript Compileremscripten-doc/noble,noble 3.1.6~dfsg-7 allLLVM-to-JavaScript Compilerglslc/noble 2023.8-1build1 amd64Command line compiler for GLSL/HLSL to SPIR-Vgolang-github-bmatsuo-lmdb-go-dev/noble-updates,noble-updates,noble-security,noble-security 1.8.0+git20170215.a14b5a3-4ubuntu0.24.04.3 allBindings for the LMDB C libraryhtslib-test/noble,noble 1.19+ds-1.1build3 allTest data for HTSlibirony-server/noble 1.6.0-2build2 amd64Emacs C/C++ minor mode powered by libclang (server)iwyu/noble 8.21-1build2 amd64Analyze #includes in C and C++ source fileslibbio-db-hts-perl/noble 3.01-4build3 amd64Perl interface to the HTS librarylibclang-14-dev/noble 1:14.0.6-19build4 amd64Clang library - Development packagelibclang-15-dev/noble 1:15.0.7-14build3 amd64Clang library - Development packagelibclang-16-dev/noble 1:16.0.6-23ubuntu4 amd64Clang library - Development packagelibclang-17-dev/noble 1:17.0.6-9ubuntu1 amd64Clang library - Development packagelibclang-18-dev/noble-updates 1:18.1.3-1ubuntu1 amd64Clang library - Development packagelibclang-19-dev/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64Clang library - Development packagelibclang-common-14-dev/noble 1:14.0.6-19build4 amd64Clang library - Common development packagelibclang-common-15-dev/noble 1:15.0.7-14build3 amd64Clang library - Common development packagelibclang-common-16-dev/noble,now 1:16.0.6-23ubuntu4 amd64 [installed,automatic]Clang library - Common development packagelibclang-common-17-dev/noble 1:17.0.6-9ubuntu1 amd64Clang library - Common development packagelibclang-common-18-dev/noble-updates 1:18.1.3-1ubuntu1 amd64Clang library - Common development packagelibclang-common-19-dev/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64Clang library - Common development packagelibclang-cpp-dev/noble 1:18.0-59~exp2 amd64C++ interface to the clang librarylibclang-cpp14-dev/noble 1:14.0.6-19build4 amd64C++ interface to the Clang librarylibclang-cpp14t64/noble 1:14.0.6-19build4 amd64C++ interface to the Clang librarylibclang-cpp15-dev/noble 1:15.0.7-14build3 amd64C++ interface to the Clang librarylibclang-cpp15t64/noble 1:15.0.7-14build3 amd64C++ interface to the Clang librarylibclang-cpp16-dev/noble 1:16.0.6-23ubuntu4 amd64C++ interface to the Clang librarylibclang-cpp16t64/noble,now 1:16.0.6-23ubuntu4 amd64 [installed,automatic]C++ interface to the Clang librarylibclang-cpp17-dev/noble 1:17.0.6-9ubuntu1 amd64C++ interface to the Clang librarylibclang-cpp17t64/noble 1:17.0.6-9ubuntu1 amd64C++ interface to the Clang librarylibclang-cpp18/noble-updates,now 1:18.1.3-1ubuntu1 amd64 [installed,automatic]C++ interface to the Clang librarylibclang-cpp18-dev/noble-updates 1:18.1.3-1ubuntu1 amd64C++ interface to the Clang librarylibclang-cpp19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64C++ interface to the Clang librarylibclang-cpp19-dev/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64C++ interface to the Clang librarylibclang-dev/noble 1:18.0-59~exp2 amd64clang library - Development packagelibclang-perl/noble 0.09-6build4 amd64Perl bindings to the Clang compiler's indexing interfacelibclang-rt-14-dev/noble 1:14.0.6-19build4 amd64Compiler-rt - development packagelibclang-rt-14-dev-wasm32/noble,noble 1:14.0.6-19build4 allCompiler-rt - wasm32 builtinslibclang-rt-14-dev-wasm64/noble,noble 1:14.0.6-19build4 allCompiler-rt - wasm64 builtinslibclang-rt-15-dev/noble 1:15.0.7-14build3 amd64Compiler-rt - development packagelibclang-rt-15-dev-wasm32/noble,noble 1:15.0.7-14build3 allCompiler-rt - wasm32 builtinslibclang-rt-15-dev-wasm64/noble,noble 1:15.0.7-14build3 allCompiler-rt - wasm64 builtinslibclang-rt-16-dev/noble,now 1:16.0.6-23ubuntu4 amd64 [installed,automatic]Compiler-rt - development packagelibclang-rt-16-dev-wasm32/noble,noble 1:16.0.6-23ubuntu4 allCompiler-rt - wasm32 builtinslibclang-rt-16-dev-wasm64/noble,noble 1:16.0.6-23ubuntu4 allCompiler-rt - wasm64 builtinslibclang-rt-17-dev/noble 1:17.0.6-9ubuntu1 amd64Compiler-rt - development packagelibclang-rt-17-dev-wasm32/noble,noble 1:17.0.6-9ubuntu1 allCompiler-rt - wasm32 builtinslibclang-rt-17-dev-wasm64/noble,noble 1:17.0.6-9ubuntu1 allCompiler-rt - wasm64 builtinslibclang-rt-18-dev/noble-updates 1:18.1.3-1ubuntu1 amd64Compiler-rt - development packagelibclang-rt-18-dev-wasm32/noble-updates,noble-updates 1:18.1.3-1ubuntu1 allCompiler-rt - wasm32 builtinslibclang-rt-18-dev-wasm64/noble-updates,noble-updates 1:18.1.3-1ubuntu1 allCompiler-rt - wasm64 builtinslibclang-rt-19-dev/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64Compiler-rt - development packagelibclang-rt-19-dev-wasm32/noble-updates,noble-updates 1:19.1.1-1ubuntu1~24.04.2 allCompiler-rt - wasm32 builtinslibclang-rt-19-dev-wasm64/noble-updates,noble-updates 1:19.1.1-1ubuntu1~24.04.2 allCompiler-rt - wasm64 builtinslibclang-rt-dev/noble 1:18.0-59~exp2 amd64Compiler-rt - Development packagelibclang-rt-dev-wasm32/noble 1:18.0-59~exp2 amd64Compiler-rt - wasm32 builtinslibclang-rt-dev-wasm64/noble 1:18.0-59~exp2 amd64Compiler-rt - wasm64 builtinslibclang1/noble 1:18.0-59~exp2 amd64C, C++ and Objective-C compiler (LLVM based)libclang1-14t64/noble 1:14.0.6-19build4 amd64C interface to the Clang librarylibclang1-15t64/noble 1:15.0.7-14build3 amd64C interface to the Clang librarylibclang1-16t64/noble,now 1:16.0.6-23ubuntu4 amd64 [installed,automatic]C interface to the Clang librarylibclang1-17t64/noble 1:17.0.6-9ubuntu1 amd64C interface to the Clang librarylibclang1-18/noble-updates,now 1:18.1.3-1ubuntu1 amd64 [installed,automatic]C interface to the Clang librarylibclang1-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64C interface to the Clang librarylibcode-tidyall-plugin-clangformat-perl/noble,noble 0.0.2-2 allmodule to run clang-format using Code::TidyAlllibear/noble 3.1.3-1build4 amd64generate compilation database for Clang tooling (wrapper library)libghc-trifecta-dev/noble 2.1.3-1 amd64modern parser combinator library with convenient diagnosticslibghc-trifecta-doc/noble,noble 2.1.3-1 allmodern parser combinator library with convenient diagnostics; documentationlibghc-trifecta-prof/noble 2.1.3-1 amd64modern parser combinator library with convenient diagnostics; profiling librarieslibhts-dev/noble 1.19+ds-1.1build3 amd64development files for the HTSliblibhts3t64/noble 1.19+ds-1.1build3 amd64C library for high-throughput sequencing data formatsliblldb-14-dev/noble 1:14.0.6-19build4 amd64Next generation, high-performance debugger, header filesliblldb-14t64/noble 1:14.0.6-19build4 amd64Next generation, high-performance debugger, libraryliblldb-15-dev/noble 1:15.0.7-14build3 amd64Next generation, high-performance debugger, header filesliblldb-15t64/noble 1:15.0.7-14build3 amd64Next generation, high-performance debugger, libraryliblldb-16-dev/noble 1:16.0.6-23ubuntu4 amd64Next generation, high-performance debugger, header filesliblldb-16t64/noble 1:16.0.6-23ubuntu4 amd64Next generation, high-performance debugger, libraryliblldb-17-dev/noble 1:17.0.6-9ubuntu1 amd64Next generation, high-performance debugger, header filesliblldb-17t64/noble 1:17.0.6-9ubuntu1 amd64Next generation, high-performance debugger, libraryliblldb-18/noble-updates 1:18.1.3-1ubuntu1 amd64Next generation, high-performance debugger, libraryliblldb-18-dev/noble-updates 1:18.1.3-1ubuntu1 amd64Next generation, high-performance debugger, header filesliblldb-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64Next generation, high-performance debugger, libraryliblldb-19-dev/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64Next generation, high-performance debugger, header filesliblldb-dev/noble 1:18.0-59~exp2 amd64Next generation, high-performance debugger, header fileslibopencl-clang-14-dev/noble 14.0.0-4build2 amd64thin wrapper for clang -- development fileslibopencl-clang-15-dev/noble 15.0.0-3build2 amd64thin wrapper for clang -- development fileslibopencl-clang-16-dev/noble 16.0.0-3.1build3 amd64thin wrapper for clang -- development fileslibopencl-clang-17-dev/noble 17.0.0-2.1build2 amd64thin wrapper for clang -- development fileslibopencl-clang14/noble 14.0.0-4build2 amd64thin wrapper for clanglibopencl-clang15/noble 15.0.0-3build2 amd64thin wrapper for clanglibopencl-clang16t64/noble 16.0.0-3.1build3 amd64thin wrapper for clanglibopencl-clang17t64/noble 17.0.0-2.1build2 amd64thin wrapper for clanglibparallel-hashmap-dev/noble,noble 1.3.12+ds-1 allheader-only hash map implementationlibrust-clang-sys+libloading-dev/noble 1.3.0-2 amd64Rust bindings for libclang - feature "libloading" and 1 morelibrust-clang-sys-dev/noble 1.3.0-2 amd64Rust bindings for libclang - Rust source codelibshaderc-dev/noble 2023.8-1build1 amd64Library API for accessing glslc functionality - static libraries and headerslibshaderc1/noble 2023.8-1build1 amd64Library API for accessing glslc functionality - shared librarieslibsimde-dev/noble,noble 0.7.2-6 allImplementations of SIMD instructions for all systemslldb/noble 1:18.0-59~exp2 amd64Next generation, high-performance debuggerlldb-14/noble 1:14.0.6-19build4 amd64Next generation, high-performance debuggerlldb-15/noble 1:15.0.7-14build3 amd64Next generation, high-performance debuggerlldb-16/noble 1:16.0.6-23ubuntu4 amd64Next generation, high-performance debuggerlldb-17/noble 1:17.0.6-9ubuntu1 amd64Next generation, high-performance debuggerlldb-18/noble-updates 1:18.1.3-1ubuntu1 amd64Next generation, high-performance debuggerlldb-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64Next generation, high-performance debuggerpython3-clang/noble 1:18.0-59~exp2 amd64Clang Python Bindingspython3-clang-14/noble 1:14.0.6-19build4 amd64Clang Python Bindingspython3-clang-15/noble 1:15.0.7-14build3 amd64Clang Python Bindingspython3-clang-16/noble 1:16.0.6-23ubuntu4 amd64Clang Python Bindingspython3-clang-17/noble 1:17.0.6-9ubuntu1 amd64Clang Python Bindingspython3-clang-18/noble-updates 1:18.1.3-1ubuntu1 amd64Clang Python Bindingspython3-clang-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64Clang Python Bindingspython3-lldb/noble 1:18.0-59~exp2 amd64Next generation, high-performance debugger, python libpython3-lldb-14/noble 1:14.0.6-19build4 amd64Next generation, high-performance debugger, python3 libpython3-lldb-15/noble 1:15.0.7-14build3 amd64Next generation, high-performance debugger, python3 libpython3-lldb-16/noble 1:16.0.6-23ubuntu4 amd64Next generation, high-performance debugger, python3 libpython3-lldb-17/noble 1:17.0.6-9ubuntu1 amd64Next generation, high-performance debugger, python3 libpython3-lldb-18/noble-updates 1:18.1.3-1ubuntu1 amd64Next generation, high-performance debugger, python3 libpython3-lldb-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64Next generation, high-performance debugger, python3 librtags/noble 2.38-10 amd64C/C++ client/server indexer with integration for Emacsruby-whitequark-parser/noble,noble 3.1.3.0-1 allRuby parser written in pure Rubysc3-plugins-language/noble,noble 3.9.1~repack-4build1 allsclang class files of UGen plugins for SuperCollidertexlive-science/noble,noble 2023.20240207-1 allTeX Live: Mathematics, natural sciences, computer science packagesvim-youcompleteme/noble,noble 0+20231230+git71166ea+ds-2 allfast, as-you-type, fuzzy-search code completion engine for Vimwasi-libc/noble,noble 0.0~git20230113.4362b18-3 allWASI libc implementation for WebAssemblyzmk/noble,noble 0.5.1-2 allcollection of reusable Makefileszmk-doc/noble,noble 0.5.1-2 allcollection of reusable Makefiles (manual pages)
于是可以使用``命令安装clang-16
sudo apt install clang-16
同样的,安装完之后,查询下版本号
john@john-virtual-machine:~/WorkSpace/GithubProjects/ninja$ clang-16 --version
Ubuntu clang version 16.0.6 (23ubuntu4)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
注意到,此时我们输入的命令是clang-16
而不是clang
,要直接使用clang
需要去/usr/bin
目录下加入软链接:
$ sudo ln -s ../lib/llvm-16/bin/clang clang
$ sudo ln -s ../lib/llvm-16/bin/clang++ clang++
注意
,如果系统本身安装过旧版本的clang,可能会提示failed to create symbolic link ‘clang’: File exists,此时要么rm -rf clang
删除, 要么把上述命令改为ln -snf ../lib/llvm-16/bin/clang clang
修改之后,如下所示,此时再用clang-16
就只需要输入clang
即可
john@john-virtual-machine:/usr/bin$ ll|grep clang
lrwxrwxrwx 1 root root 24 7月 30 20:21 clang -> ../lib/llvm-16/bin/clang*
lrwxrwxrwx 1 root root 26 7月 30 20:22 clang++ -> ../lib/llvm-16/bin/clang++*
lrwxrwxrwx 1 root root 26 4月 14 2024 clang++-16 -> ../lib/llvm-16/bin/clang++*
lrwxrwxrwx 1 root root 24 4月 14 2024 clang-16 -> ../lib/llvm-16/bin/clang*
lrwxrwxrwx 1 root root 28 4月 14 2024 clang-cpp-16 -> ../lib/llvm-16/bin/clang-cpp*
查看clang
版本号:
ccf@ccf-virtual-machine:~$ clang --version
Ubuntu clang version 16.0.6 (23ubuntu4)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
3. 安装 GN 构建系统
gn的源码可以从两个地方下载,google官方的需要科学上网,另一个就是GitHub上有fork的仓库,GitHub也上不去的话,就用鸿蒙提供的Gitee的仓库
- google官方源: https://gn.googlesource.com/gn
- Github: https://github.com/timniederhausen/gn
- Gitee: https://gitee.com/openharmony/third_party_gn
我选择的是github上面的gn代码仓:
git clone https://github.com/timniederhausen/gn.git
cd gn
python build/gen.py # --allow-warning if you want to build with warnings.
ninja -C out
# To run tests:
out/gn_unittests
等待编译结束,跑一下gn的测试代码,显示passed说明gn功能没问题
john@john-virtual-machine:~/WorkSpace/GithubProjects/gn$ out/gn_unittests
[721/721] ImportantFileWriterTest.Basic
PASSED
编译成功后,将gn
复制到/usr/bin
目录下
john@john-virtual-machine:~/WorkSpace/GithubProjects/gn$ ls
AUTHORS build docs examples infra LICENSE misc out OWNERS README.md src tools
ccf@ccf-virtual-machine:~/WorkSpace/GithubProjects/gn$ sudo cp out/gn /usr/bin/
接着在任意终端中,输入gn help
检测gn
是否安装成功
john@john-virtual-machine:~/WorkSpace/GithubProjects/gn$ gn helpCommands (type "gn help <command>" for more help):analyze: Analyze which targets are affected by a list of files.args: Display or configure arguments declared by the build.check: Check header dependencies.clean: Cleans the output directory.clean_stale: Cleans the stale output files from the output directory.desc: Show lots of insightful information about a target or config.format: Format .gn files.gen: Generate ninja files.help: Does what you think.ls: List matching targets.meta: List target metadata collection results.outputs: Which files a source/target make.path: Find paths between two targets.refs: Find stuff referencing a target or file.Target declarations (type "gn help <function>" for more help):action: Declare a target that runs a script a single time.action_foreach: Declare a target that runs a script over a set of files.bundle_data: [iOS/macOS] Declare a target without output.copy: Declare a target that copies files.create_bundle: [iOS/macOS] Build an iOS or macOS bundle.executable: Declare an executable target.generated_file: Declare a generated_file target.group: Declare a named group of targets.loadable_module: Declare a loadable module target.rust_library: Declare a Rust library target.rust_proc_macro: Declare a Rust procedural macro target.shared_library: Declare a shared library target.source_set: Declare a source set target.static_library: Declare a static library target.target: Declare a target with the given programmatic type.Buildfile functions (type "gn help <function>" for more help):assert: Assert an expression is true at generation time.config: Defines a configuration object.declare_args: Declare build arguments.defined: Returns whether an identifier is defined.exec_script: Synchronously run a script and return the output.filter_exclude: Remove values that match a set of patterns.filter_include: Remove values that do not match a set of patterns.filter_labels_exclude: Remove labels that match a set of patterns.filter_labels_include: Remove labels that do not match a set of patterns.foreach: Iterate over a list.forward_variables_from: Copies variables from a different scope.get_label_info: Get an attribute from a target's label.get_path_info: Extract parts of a file or directory name.get_target_outputs: [file list] Get the list of outputs from a target.getenv: Get an environment variable.import: Import a file into the current scope.label_matches: Returns whether a label matches any of a list of patterns.not_needed: Mark variables from scope as not needed.pool: Defines a pool object.print: Prints to the console.print_stack_trace: Prints a stack trace.process_file_template: Do template expansion over a list of files.read_file: Read a file into a variable.rebase_path: Rebase a file or directory to another location.set_default_toolchain: Sets the default toolchain name.set_defaults: Set default values for a target type.split_list: Splits a list into N different sub-lists.string_join: Concatenates a list of strings with a separator.string_replace: Replaces substring in the given string.string_split: Split string into a list of strings.template: Define a template rule.tool: Specify arguments to a toolchain tool.toolchain: Defines a toolchain.write_file: Write a file to disk.Built-in predefined variables (type "gn help <variable>" for more help):current_cpu: [string] The processor architecture of the current toolchain.current_os: [string] The operating system of the current toolchain.current_toolchain: [string] Label of the current toolchain.default_toolchain: [string] Label of the default toolchain.gn_version: [number] The version of gn.host_cpu: [string] The processor architecture that GN is running on.host_os: [string] The operating system that GN is running on.invoker: [string] The invoking scope inside a template.python_path: [string] Absolute path of Python.root_build_dir: [string] Directory where build commands are run.root_gen_dir: [string] Directory for the toolchain's generated files.root_out_dir: [string] Root directory for toolchain output files.target_cpu: [string] The desired cpu architecture for the build.target_gen_dir: [string] Directory for a target's generated files.target_name: [string] The name of the current target.target_os: [string] The desired operating system for the build.target_out_dir: [string] Directory for target output files.Variables you set in targets (type "gn help <variable>" for more help):aliased_deps: [scope] Set of crate-dependency pairs.all_dependent_configs: [label list] Configs to be forced on dependents.allow_circular_includes_from: [label list] Permit includes from deps.arflags: [string list] Arguments passed to static_library archiver.args: [string list] Arguments passed to an action.asmflags: [string list] Flags passed to the assembler.assert_no_deps: [label pattern list] Ensure no deps on these targets.bridge_header: [string] Path to C/Objective-C compatibility header.bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.bundle_deps_filter: [label list] A list of labels that are filtered out.bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundlebundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.cflags: [string list] Flags passed to all C compiler variants.cflags_c: [string list] Flags passed to the C compiler.cflags_cc: [string list] Flags passed to the C++ compiler.cflags_objc: [string list] Flags passed to the Objective C compiler.cflags_objcc: [string list] Flags passed to the Objective C++ compiler.check_includes: [boolean] Controls whether a target's files are checked.code_signing_args: [string list] [deprecated] Args for the post-processing script.code_signing_outputs: [file list] [deprecated] Outputs of the post-processing step.code_signing_script: [file name] [deprecated] Script for the post-processing step.code_signing_sources: [file list] [deprecated] Sources for the post-processing step.complete_static_lib: [boolean] Links all deps into a static library.configs: [label list] Configs applying to this target or config.contents: Contents to write to file.crate_name: [string] The name for the compiled crate.crate_root: [string] The root source file for a binary or library.crate_type: [string] The type of linkage to use on a shared_library.data: [file list] Runtime data file dependencies.data_deps: [label list] Non-linked dependencies.data_keys: [string list] Keys from which to collect metadata.defines: [string list] C preprocessor defines.depfile: [string] File name for input dependencies for actions.deps: [label list] Private linked dependencies.description: [string] Command description for actions.externs: [scope] Set of Rust crate-dependency pairs.framework_dirs: [directory list] Additional framework search directories.frameworks: [name list] Name of frameworks that must be linked.friend: [label pattern list] Allow targets to include private headers.gen_deps: [label list] Declares targets that should generate when this one does.include_dirs: [directory list] Additional include directories.inputs: [file list] Additional compile-time dependencies.ldflags: [string list] Flags passed to the linker.lib_dirs: [directory list] Additional library directories.libs: [string list] Additional libraries to link.metadata: [scope] Metadata of this target.mnemonic: [string] Prefix displayed when ninja runs this action.module_name: [string] The name for the compiled module.output_conversion: Data format for generated_file targets.output_dir: [directory] Directory to put output file in.output_extension: [string] Value to use for the output's file extension.output_name: [string] Name for the output file other than the default.output_prefix_override: [boolean] Don't use prefix for output name.outputs: [file list] Output files for actions and copy targets.partial_info_plist: [filename] Path plist from asset catalog compiler.pool: [string] Label of the pool used by binary targets and actions.post_processing_args: [string list] Args for the post-processing script.post_processing_outputs: [file list] Outputs of the post-processing step.post_processing_script: [file name] Script for the post-processing step.post_processing_sources: [file list] Sources for the post-processing step.precompiled_header: [string] Header file to precompile.precompiled_header_type: [string] "gcc" or "msvc".precompiled_source: [file name] Source file to precompile.product_type: [string] Product type for the bundle.public: [file list] Declare public header files for a target.public_configs: [label list] Configs applied to dependents.public_deps: [label list] Declare public dependencies.rebase: [boolean] Rebase collected metadata as files.response_file_contents: [string list] Contents of .rsp file for actions.rustflags: [string list] Flags passed to the Rust compiler.script: [file name] Script file for actions.sources: [file list] Source files for a target.swiftflags: [string list] Flags passed to the swift compiler.testonly: [boolean] Declares a target must only be used for testing.transparent: [bool] True if the bundle is transparent.visibility: [label list] A list of labels that can depend on a target.walk_keys: [string list] Key(s) for managing the metadata collection walk.weak_frameworks: [name list] Name of frameworks that must be weak linked.write_runtime_deps: Writes the target's runtime_deps to the given path.xcasset_compiler_flags: [string list] Flags passed to xcassets compilerxcode_extra_attributes: [scope] Extra attributes for Xcode projects.xcode_test_application_name: [string] Name for Xcode test target.Other help topics:all: Print all the help at oncebuildargs: How build arguments work.dotfile: Info about the toplevel .gn file.execution: Build graph and execution overview.grammar: Language and grammar for GN build files.input_conversion: Processing input from exec_script and read_file.file_pattern: Matching more than one file.label_pattern: Matching more than one label.labels: About labels.metadata_collection: About metadata and its collection.ninja_rules: How Ninja build rules are named.nogncheck: Annotating includes for checking.output_conversion: Specifies how to transform a value to output.runtime_deps: How runtime dependency computation works.source_expansion: Map sources to outputs for scripts.switches: Show available command-line switches.
用gn
和ninja
编译示例程序
在gn仓库中,自带一个例程,路径为gn/example/simple_build
john@john-virtual-machine:~/WorkSpace/GithubProjects/gn$ ls
AUTHORS build docs examples infra LICENSE misc out OWNERS README.md src tools
ccf@ccf-virtual-machine:~/WorkSpace/GithubProjects/gn$ tree -L 2
.
├── AUTHORS
├── build
│ ├── build_aix.ninja.template
│ ├── build_haiku.ninja.template
│ ├── build_linux.ninja.template
│ ├── build_mac.ninja.template
│ ├── build_openbsd.ninja.template
│ ├── build_win.ninja.template
│ ├── build_zos.ninja.template
│ ├── full_test.py
│ ├── gen.py
│ └── windows.manifest.xml
├── docs
│ ├── cross_compiles.md
│ ├── faq.md
│ ├── language.md
│ ├── mingw.md
│ ├── quick_start.md
│ ├── reference.md
│ ├── standalone.md
│ └── style_guide.md
├── examples
│ ├── ios
│ ├── rust_example
│ └── simple_build
├── infra
│ ├── config
│ ├── README.recipes.md
│ ├── recipe_modules
│ ├── recipes
│ └── recipes.py
├── LICENSE
├── misc
│ ├── emacs
│ ├── help_as_html.py
│ ├── tm
│ └── vim
├── out
│ ├── base.a
│ ├── build.ninja
│ ├── build.ninja.d
│ ├── Debug
│ ├── gn
│ ├── gn_lib.a
│ ├── gn_unittests
│ ├── last_commit_position.h
│ └── src
├── OWNERS
├── README.md
├── src
│ ├── base
│ ├── gn
│ └── util
└── tools├── find_unreachable.py├── run_formatter.sh└── update_reference.sh23 directories, 35 files
$ cd ./example/simple_build #已在gn仓库目录下
$ gn gen out
Done. Made 3 targets from 4 files in 61ms
$ ninja -C out
ninja: Entering directory `out'
[6/6] LINK hello
最后运行我们通过gn
和ninja
编译的程序
john@john-virtual-machine:~/WorkSpace/GithubProjects/gn/examples/simple_build$ ./out/hello
Hello, world
至此,我们可以愉快地使用gn来编译代码啦。
我的第一个简单的GN C++项目
使用GN构建的一个C++小项目,
该项目包含一个可执行程序,依赖于静态库 A 和动态库 B。
首先,让我们设计项目结构:
project/
├── BUILD.gn # 主构建文件
├── main.cpp # 主程序
├── libA/ # 静态库A
│ ├── BUILD.gn
│ ├── a.cpp
│ └── a.h
└── libB/ # 动态库B├── BUILD.gn├── b.cpp└── b.h
代码已上传到gitee码云和github上,对应地址为:
- gitee源码地址:https://gitee.com/havealex/my-first-gn-project
- github源代码地址:https://github.com/ccf19881030/MyFirstGnProject
1. 构建项目
# 创建输出目录
mkdir -p out/Default# 生成Ninja构建文件
gn gen out/Default# 执行构建
ninja -C out/Default
2. 运行生成的可执行程序:
# 设置动态库路径
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)/out/Default/libB# 运行程序
./out/Default/my_app
运行成功后,您应该看到以下输出:
Starting main function...
This is functionA from static library A
This is functionB from dynamic library B
Ending main function.
参考资料
- https://ninja-build.org/
- https://github.com/ninja-build/ninja
- https://gn.googlesource.com/gn
- https://github.com/timniederhausen/gn
- OpenHarmony编译构建指导