WebRTC編譯系統之GYP,gn和ninja


GN(Generate Ninja) 來生成構建腳本,使用 ninja 來構建。
使用 gn 生成 ninja 構建文件 的常用命令:
// 生成 debug 版本的構建文件,默認配置 gn gen out/Debug // 生成 release 版本的構建文件 gn gen out/Release --args="is_debug=false"
注意,通過  --args  可以傳遞參數給 gn ,具體參數的含義,由 WebRTC 的構建系統來解釋。比如 is_debug 選項,決定構建 debug 還是 release 版本。
如果你已經使用 gn gen 生成過構建文件,想看看這個版本的構建文件都指定了什么參數,可以使用下面命令:
gn args out/Release --list
它會列出所有的 build arguments 和對應的文檔,以及當前值。

ninja 的官網在這里: https://ninja-build.org/
后綴為 ninja(*.ninja) 的文件是 ninja 的 構建文件。對 WebRTC 來講,執行完 gn gen 之后,會在 out/Release 下生成 build.ninja 文件,可以把這個文件看做是整個 WebRTC 的“ Makefile ”。它里面調用了各個模塊的 ninja 文件。
要完整編譯 WebRTC ,只要在 src 目錄執行下列命令:
ninja -C out/Release
-C 選項告訴 ninja ,進入 out/Release 目錄來編譯。所以,它等同於:
cd out/Release ninja
要編譯某個模塊,可以在 ninja 命令后跟模塊名字(build.ninja文件中定義的構建目標,就像 Makefile 中的構建目標一樣)。比如:
// 構建 webrtc/pc ninja pc // 構建 webrtc/media ninja media
看看 gn 用到的項目文件 .gn 、 .gni 和 DEPS ,它們指導了如何生成 ninja 構建文件。
如果把 gn 看成一個編譯系統, .gn 就是源文件, .gni 就是頭文件。我們姑且這么理解就好了(其實 gni 里做的事情, gn 都可以做)。DEPS 主要用來設定包含路徑。
gn 和 gni 文件都在源碼樹中,比如 src 目錄。當執行 gn gen 時,gn 工具根據 gn 和 gni 生成 ninja 文件並將這些 ninja 文件放到指定的構建目錄中。
.gn 文件是 GN build 的 “源文件”,在這里可以做各種條件判斷和配置,gn 會根據這些配置生成特定的 ninja 文件。
.gn 文件中可以使用預定義的參數,比如 is_debug , target_os , rtc_use_h264 等。
.gn 中可以 import .gni 文件。
.gn 和 .gni 文件中用到各種指令,都在這里有說明: GN Reference
import("webrtc/webrtc.gni") group("default") { testonly = true deps = [ "//webrtc", "//webrtc/examples", "//webrtc/tools", ] if (rtc_include_tests) { deps += [ "//webrtc:webrtc_tests" ] } }
group 指令聲明了一個 default 目標,這個目標依賴 webrtc 、 webrtc/examples 和 webrtc/tools ,你可以在 webrtc 、 webrtc/examples 、 webrtc/tools 目錄下找到對應的 BUILD.gn 。你可以把 group 當做 VS 的 solution
gn 文件中也可以通過 defines 來定義宏,通過 cflags 來指定傳遞給編譯器的標記,通過 ldflags 指定傳遞給鏈接器的標記,還可以使用 sources 指定源文件。下面是 webrtc/BUILD.gn 文件的部分內容:
if (is_win) { defines += [ "WEBRTC_WIN", "_CRT_SECURE_NO_WARNINGS", # Suppress warnings about _vsnprinf ] } if (is_android) { defines += [ "WEBRTC_LINUX", "WEBRTC_ANDROID", ] } if (is_chromeos) { defines += [ "CHROMEOS" ] } if (rtc_sanitize_coverage != "") { assert(is_clang, "sanitizer coverage requires clang") cflags += [ "-fsanitize-coverage=${rtc_sanitize_coverage}" ] ldflags += [ "-fsanitize-coverage=${rtc_sanitize_coverage}" ] }
gni 文件是 GN build 使用的頭文件,它里面可以做各種事情,比如定義變量、宏、定義配置、定義模板等。
webrtc.gni 是一個比較特殊的 gni 文件,你可以把它看做 全局配置文件
webrtc.gni 定義了 WebRTC 項目用到的一些標記,比如 rtc_build_libvpx、rtc_build_ssl、rtc_use_h264 等。
還使用 template 語句定義了幾個模板,比如 rtc_executable 、 rtc_static_library 、 rtc_shared_library ,這幾個模板定義了生成可執行文件、靜態庫、動態庫的規則。在 webrtc/examples/BUILD.gn 中就有用到這些模板 ,用它們來指導如何生成可執行文件、靜態庫等。
你也可以直接使用 gn 內置的 shared_library 和 static_library 來聲明目標, 比如 third_party/ffmpeg/BUILD.gn 就使用 shared_library 來生成動態庫。
DEPS 文件
webrtc/examples/DEPS :
include_rules = [ "+WebRTC", "+webrtc/api", "+webrtc/base", "+webrtc/media", "+webrtc/modules/audio_device", "+webrtc/modules/video_capture", "+webrtc/p2p", "+webrtc/pc", ]
include_rules 定義了包含路徑。
了解 .gn 和 .gni 文件的目的是修改它們。比如你想打開 WebRTC 對 H264 的支持,就可以修改 webrtc/webrtc.gni ,直接把 rtc_use_h264 設置為 true 。
比如你想為某個模塊加一些文件,就可以修改 .gn 文件,修改 sources 變量,直接把你的源文件加進去。
GYP 是一個在不同平台構建項目的工具, GN GYP 的升級版
GYP Generate Your Projects 的縮寫, GYP 的目的是為了支持更大的項目編譯在不同的平台,比如 Mac Windows Linux ,它可以生成Xcode工程,Visual Studio工程,Ninja編譯文件和Mackefiles。
GYP 的輸入是 .gyp .gypi 文件, .gypi 文件是用於 .gyp 文件include使用的。 .gyp 文件就是符合特定格式的 json 文件。
gn gen 指定在 out/ 目錄里面生成ninja。
1 gn gen out
再執行ninja來build code
1 ninja -C out

在src目錄有一個.gn的隱藏文件
import("//build/dotfile_settings.gni")
# The location of the build configuration file.
buildconfig = "//build/config/BUILDCONFIG.gn"
# The secondary source root is a parallel directory tree where
# GN build files are placed when they can not be placed directly
# in the source tree, e.g. for third party source trees.
secondary_source = "//build/secondary/"
# These are the targets to check headers for by default. The files in targets
# matching these patterns (see "gn help label_pattern" for format) will have
# their includes checked for proper dependencies when you run either
# "gn check" or "gn gen --check".
check_targets = [ "//webrtc/*" ]
# These are the list of GN files that run exec_script. This whitelist exists
# to force additional review for new uses of exec_script, which is strongly
# discouraged except for gypi_to_gn calls.
exec_script_whitelist = build_dotfile_settings.exec_script_whitelist
default_args = {
# Webrtc does not support component builds because we are not using the
# template "component" but we rely directly on "rtc_static_library" and
# "rtc_shared_library". This means that we cannot use the chromium default
# value for this argument.
# This also means that the user can override this value using --args or
# the args.gn file but this setting will be ignored because we don't support
# component builds.
is_component_build = false
}
.gn  檔所在的目錄會被 GN 工具認定是 project 的  source root .gn  的內容 最基本就是用  buildconfig  來指定 build config 檔的位置 ,其中  //  開頭的字串就是用來指定相對於 source root 的路徑。
args命令應該是產生args.gn文件的。
gen命令應該是根據args.gn生成ninja文件的。
If specified, arguments from the --args command line flag are used. If that flag is not specified, args from previous builds in the build directory will be used (this is in the file args.gn in the build directory).
gn gen out/FooBar --args="enable_doom_melon=true os=\"android\"" This will overwrite the build directory with the given arguments.
build flow流程
1. Look for ".gn" file (see "gn help dotfile") in the current directory and walk up the directory tree until one is found. Set this directory to be the "source root" and interpret this file to find the name of the build config file. 2. Execute the build config file identified by .gn to set up the global variables and default toolchain name. Any arguments, variables, defaults, etc. set up in this file will be visible to all files in the build. 3. Load the //BUILD.gn (in the source root directory). 4. Recursively evaluate rules and load BUILD.gn in other directories as necessary to resolve dependencies. If a BUILD file isn't found in the specified location, GN will look in the corresponding location inside the secondary_source defined in the dotfile (see "gn help dotfile"). 5. When a target's dependencies are resolved, write out the `.ninja` file to disk. 6. When all targets are resolved, write out the root build.ninja file.
ninja -t
-t flag on the Ninja command line runs some tools that we have found useful during Ninja’s development. The current tools are:
ninja -t clean
remove built files. By default it removes all built files except for those created by the generator.
Adding the -g flag also removes built files created by the generator
更多ninja -h
參考:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM