WebRTC编译系统之gn files

本文详细介绍了WebRTC构建系统中的.gn和.gni文件,以及DEPS文件的作用。.gn文件作为编译的源文件,用于配置和条件判断,而.gni文件则作为头文件,用于定义变量和规则。DEPS文件主要设置包含路径。通过修改这些文件,可以定制WebRTC的编译选项,例如开启H264支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在“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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

foruok

你可以选择打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值