WebRTC编译系统之gn files

Posted foruok

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebRTC编译系统之gn files相关的知识,希望对你有一定的参考价值。

在“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_support_120ms_ptime = false

  # Enable this to let the Opus audio codec change complexity on the fly.
  rtc_opus_variable_complexity = false

  # Disable to use absolute header paths for some libraries.
  rtc_relative_path = true

  # Used to specify an external Jsoncpp include path when not compiling the
  # library that comes with WebRTC (i.e. rtc_build_json == 0).
  rtc_jsoncpp_root = "//third_party/jsoncpp/source/include"

  # Used to specify an external OpenSSL include path when not compiling the
  # library that comes with WebRTC (i.e. rtc_build_ssl == 0).
  rtc_ssl_root = ""

  # Selects fixed-point code where possible.
  rtc_prefer_fixed_point = false

  # Enables the use of protocol buffers for debug recordings.
  rtc_enable_protobuf = true

  # Disable the code for the intelligibility enhancer by default.
  rtc_enable_intelligibility_enhancer = false

  # Enable when an external authentication mechanism is used for performing
  # packet authentication for RTP packets instead of libsrtp.
  rtc_enable_external_auth = build_with_chromium

  # Selects whether debug dumps for the audio processing module
  # should be generated.
  apm_debug_dump = false

  # Set this to true to enable BWE test logging.
  rtc_enable_bwe_test_logging = false

  # Set this to disable building with support for SCTP data channels.
  rtc_enable_sctp = true

  # Disable these to not build components which can be externally provided.
  rtc_build_expat = true
  rtc_build_json = true
  rtc_build_libjpeg = true
  rtc_build_libsrtp = true
  rtc_build_libvpx = true
  rtc_libvpx_build_vp9 = true
  rtc_build_libyuv = true
  rtc_build_openmax_dl = true
  rtc_build_opus = true
  rtc_build_ssl = true
  rtc_build_usrsctp = true

  # Enable to use the Mozilla internal settings.
  build_with_mozilla = false

  rtc_enable_android_opensl = false

  # Link-Time Optimizations.
  # Executes code generation at link-time instead of compile-time.
  # https://gcc.gnu.org/wiki/LinkTimeOptimization
  rtc_use_lto = false

  # Set to "func", "block", "edge" for coverage generation.
  # At unit test runtime set UBSAN_OPTIONS="coverage=1".
  # It is recommend to set include_examples=0.
  # Use llvm's sancov -html-report for human readable reports.
  # See http://clang.llvm.org/docs/SanitizerCoverage.html .
  rtc_sanitize_coverage = ""

  # Enable libevent task queues on platforms that support it.
  if (is_win || is_mac || is_ios || is_nacl) {
    rtc_enable_libevent = false
    rtc_build_libevent = false
  } else {
    rtc_enable_libevent = true
    rtc_build_libevent = true
  }

  if (current_cpu == "arm" || current_cpu == "arm64") {
    rtc_prefer_fixed_point = true
  }

  if (!is_ios && (current_cpu != "arm" || arm_version >= 7) &&
      current_cpu != "mips64el") {
    rtc_use_openmax_dl = true
  } else {
    rtc_use_openmax_dl = false
  }

  # Determines whether NEON code will be built.
  rtc_build_with_neon =
      (current_cpu == "arm" && arm_use_neon) || current_cpu == "arm64"

  # Enable this to build OpenH264 encoder/FFmpeg decoder. This is supported on
  # all platforms except Android and iOS. Because FFmpeg can be built
  # with/without H.264 support, |ffmpeg_branding| has to separately be set to a
  # value that includes H.264, for example "Chrome". If FFmpeg is built without
  # H.264, compilation succeeds but |H264DecoderImpl| fails to initialize. See
  # also: |rtc_initialize_ffmpeg|.
  # CHECK THE OPENH264, FFMPEG AND H.264 LICENSES/PATENTS BEFORE BUILDING.
  # http://www.openh264.org, https://www.ffmpeg.org/
  rtc_use_h264 = proprietary_codecs && !is_android && !is_ios

  # Determines whether QUIC code will be built.
  rtc_use_quic = false

  # By default, use normal platform audio support or dummy audio, but don't
  # use file-based audio playout and record.
  rtc_use_dummy_audio_file_devices = false

  # When set to true, test targets will declare the files needed to run memcheck
  # as data dependencies. This is to enable memcheck execution on swarming bots.
  rtc_use_memcheck = false

  # FFmpeg must be initialized for |H264DecoderImpl| to work. This can be done
  # by WebRTC during |H264DecoderImpl::InitDecode| or externally. FFmpeg must
  # only be initialized once. Projects that initialize FFmpeg externally, such
  # as Chromium, must turn this flag off so that WebRTC does not also
  # initialize.
  rtc_initialize_ffmpeg = !build_with_chromium

  # Build sources requiring GTK. NOTICE: This is not present in Chrome OS
  # build environments, even if available for Chromium builds.
  rtc_use_gtk = !build_with_chromium
}

# A second declare_args block, so that declarations within it can
# depend on the possibly overridden variables in the first
# declare_args block.
declare_args() {
  # Include the iLBC audio codec?
  rtc_include_ilbc = !(build_with_chromium || build_with_mozilla)

  rtc_restrict_logging = build_with_chromium

  # Excluded in Chromium since its prerequisites don't require Pulse Audio.
  rtc_include_pulse_audio = !build_with_chromium

  # Chromium uses its own IO handling, so the internal ADM is only built for
  # standalone WebRTC.
  rtc_include_internal_audio_device = !build_with_chromium

  # Include tests in standalone checkout.
  rtc_include_tests = !build_with_chromium
}

# Make it possible to provide custom locations for some libraries (move these
# up into declare_args should we need to actually use them for the GN build).
rtc_libvpx_dir = "//third_party/libvpx"
rtc_libyuv_dir = "//third_party/libyuv"
rtc_opus_dir = "//third_party/opus"

# Desktop capturer is supported only on Windows, OSX and Linux.
rtc_desktop_capture_supported = is_win || is_mac || is_linux

###############################################################################
# Templates
#

# Points to //webrtc/ in webrtc stand-alone or to //third_party/webrtc/ in
# chromium.
# We need absolute paths for all configs in templates as they are shared in
# different subdirectories.
webrtc_root = get_path_info(".", "abspath")

# Global configuration that should be applied to all WebRTC targets.
# You normally shouldn't need to include this in your target as it's
# automatically included when using the rtc_* templates.
# It sets defines, include paths and compilation warnings accordingly,
# both for WebRTC stand-alone builds and for the scenario when WebRTC
# native code is built as part of Chromium.
rtc_common_configs = [ webrtc_root + ":common_config" ]

# Global public configuration that should be applied to all WebRTC targets. You
# normally shouldn't need to include this in your target as it's automatically
# included when using the rtc_* templates. It set the defines, include paths and
# compilation warnings that should be propagated to dependents of the targets
# depending on the target having this config.
rtc_common_inherited_config = webrtc_root + ":common_inherited_config"

# Common configs to remove or add in all rtc targets.
rtc_remove_configs = []
rtc_add_configs = rtc_common_configs

set_defaults("rtc_test") {
  configs = rtc_add_configs
  suppressed_configs = []
}

set_defaults("rtc_source_set") {
  configs = rtc_add_configs
  suppressed_configs = []
}

set_defaults("rtc_executable") {
  configs = rtc_add_configs
  suppressed_configs = []
}

set_defaults("rtc_static_library") {
  configs = rtc_add_configs
  suppressed_configs = []
}

set_defaults("rtc_shared_library") {
  configs = rtc_add_configs
  suppressed_configs = []
}

template("rtc_test") {
  test(target_name) {
    forward_variables_from(invoker,
                           "*",
                           [
                             "configs",
                             "public_configs",
                             "suppressed_configs",
                           ])
    configs += invoker.configs
    configs -= rtc_remove_configs
    configs -= invoker.suppressed_configs
    public_configs = [ rtc_common_inherited_config ]
    if (defined(invoker.public_configs)) {
      public_configs += invoker.public_configs
    }
  }
}

template("rtc_source_set") {
  source_set(target_name) {
    forward_variables_from(invoker,
                           "*",
                           [
                             "configs",
                             "public_configs",
                             "suppressed_configs",
                           ])
    configs += invoker.configs
    configs -= rtc_remove_configs
    configs -= invoker.suppressed_configs
    public_configs = [ rtc_common_inherited_config ]
    if (defined(invoker.public_configs)) {
      public_configs += invoker.public_configs
    }
  }
}

template("rtc_executable") {
  executable(target_name) {
    forward_variables_from(invoker,
                           "*",
                           [
                             "deps",
                             "configs",
                             "public_configs",
                             "suppressed_configs",
                           ])
    configs += invoker.configs
    configs -= rtc_remove_configs
    configs -= invoker.suppressed_configs
    deps = [
      "//build/config/sanitizers:deps",
    ]
    deps += invoker.deps
    public_configs = [ rtc_common_inherited_config ]
    if (defined(invoker.public_configs)) {
      public_configs += invoker.public_configs
    }
  }
}

template("rtc_static_library") {
  static_library(target_name) {
    forward_variables_from(invoker,
                           "*",
                           [
                             "configs",
                             "public_configs",
                             "suppressed_configs",
                           ])
    configs += invoker.configs
    configs -= rtc_remove_configs
    configs -= invoker.suppressed_configs
    public_configs = [ rtc_common_inherited_config ]
    if (defined(invoker.public_configs)) {
      public_configs += invoker.public_configs
    }
  }
}

template("rtc_shared_library") {
  shared_library(target_name) {
    forward_variables_from(invoker,
                           "*",
                           [
                             "configs",
                             "public_configs",
                             "suppressed_configs",
                           ])
    configs += invoker.configs
    configs -= rtc_remove_configs
    configs -= invoker.suppressed_configs
    public_configs = [ rtc_common_inherited_config ]
    if (defined(invoker.public_configs)) {
      public_configs += invoker.public_configs
    }
  }
}

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

了解 .gn 和 .gni 文件的目的是修改它们。比如你想打开 WebRTC 对 H264 的支持,就可以修改 webrtc/webrtc.gni ,直接把 rtc_use_h264 设置为 true 。

比如你想为某个模块加一些文件,就可以修改 .gn 文件,修改 sources 变量,直接把你的源文件加进去。


好啦,到这儿吧。

相关阅读:

以上是关于WebRTC编译系统之gn files的主要内容,如果未能解决你的问题,请参考以下文章

WebRTC开发之webrtc/depot_tools/gn: line 8: exec: python: not found

WebRTC Native M96版本开篇之旅--一篇读懂代码下载编译(ninja gn depot_tools)

WebRTC Native M96版本开篇之旅--一篇读懂代码下载编译(ninja gn depot_tools)

使用ninja -C out/Release指令编译安卓Webrtc的SDK报错找不到android ndk处理方式

[openharmony]liteos-a系统编译之GN

webRTC编译windows 7 64位