在“WebRTC 构建系统介绍之gn和ninja”中,大概介绍了 gn 和 ninja 的简单用法,这次来看看 gn 用到的项目文件 .gn 、 .gni 和 DEPS ,它们指导了如何生成 ninja 构建文件。
借用 C++ 的概念,如果把 gn 看成一个编译系统, .gn 就是源文件, .gni 就是头文件。我们姑且这么理解就好了(其实 gni 里做的事情, gn 都可以做)。DEPS 主要用来设定包含路径。
gn 和 gni 文件都在源码树中,比如 src 目录。当执行 gn gen 时,gn 工具根据 gn 和 gni 生成 ninja 文件并将这些 ninja 文件放到指定的构建目录中。
.gn
.gn 文件是 GN build 的 “源文件”,在这里可以做各种条件判断和配置,gn 会根据这些配置生成特定的 ninja 文件。
.gn 文件中可以使用预定义的参数,比如 is_debug , target_os , rtc_use_h264 等。
.gn 中可以 import .gni 文件。
看一下 src/BUILD.gn :
import("webrtc/webrtc.gni")
group("default") {
testonly = true
deps = [
"//webrtc",
"//webrtc/examples",
"//webrtc/tools",
]
if (rtc_include_tests) {
deps += [ "//webrtc:webrtc_tests" ]
}
}
.gn 和 .gni 文件中用到各种指令,都在这里有说明:GN Reference。
这个 gn 文件中,导入了 webrtc/webrtc.gni 文件。
这个 gn 文件,用 group 指令声明了一个 default 目标,这个目标依赖 webrtc 、 webrtc/examples 和 webrtc/tools ,你可以在 webrtc 、 webrtc/examples 、 webrtc/tools 目录下找到对应的 BUILD.gn 。你可以把 group 当做 VS 的 solution ,或者 QtCreator 的 dir 项目。
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
gni 文件是 GN build 使用的头文件,它里面可以做各种事情,比如定义变量、宏、定义配置、定义模板等。
看下 webrtc/webrtc.gni 文件:
# Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS. All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.
import("//build/config/arm.gni")
import("//build/config/features.gni")
import("//build/config/mips.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//build_overrides/build.gni")
import("//testing/test.gni")
declare_args() {
# Disable this to avoid building the Opus audio codec.
rtc_include_opus = true
# Enable this if the Opus version upon which WebRTC is built supports direct
# encoding of 120 ms packets.
rtc_opus_suppo