WebRTC编译系统之GYP,gn和ninja
生活随笔
收集整理的這篇文章主要介紹了
WebRTC编译系统之GYP,gn和ninja
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
GN(Generate Ninja)來生成構建腳本,使用 ninja 來構建。
gn 的介紹在這里:https://www.chromium.org/developers/gn-build-configuration
使用 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。
再執行ninja來build code
在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 的路徑。
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 |
| 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: Display or configure arguments declared by the build.
- gen: Generate ninja files.
總結
以上是生活随笔為你收集整理的WebRTC编译系统之GYP,gn和ninja的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Android]Linux下WebRT
- 下一篇: PKG_CONFIG_PATH错误提示解