1

[gn+ninja学习 0x04] gn最小化构建项目

 1 year ago
source link: https://ost.51cto.com/posts/18332
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[gn+ninja学习 0x04] gn最小化构建项目

OpenHarmony使用gn+ninja来维护开源项目的构建。之前没有接触过gn+ninja,是时候系统性的来学习下了。边学边记录下学习过程,希望对同样需要学习gn+ninja的朋友有所帮助。

这一篇,我们来学习如何准备一个最小化的GN构建项目,看看都需要哪些文件。也可以阅读官方的原版内容docs/standalone.md

1、Introduction简介

本文介绍如何设计一个项目,该项目可以使用GN独立构建,也可以引入Chrome构建。—注:如何引入Chrome不是我们需要关注的。

GN原则上与GYP没有什么不同,因为独立版本和Chrome版本之间的一些核心配置必须相同。但是,GN 在命名和配置方面更加明确,因此两个构建之间的相似性也更加明确,并且在配置方式方面的灵活性也较小。

2、gn最小化构建项目

gn最小化构建项目主要包含如下几种。

2.1、 主生成配置文件BUILDCONFIG.gn

Chrome项目是//build/config/BUILDCONFIG.gn。对于OpenHarmony,为https://gitee.com/openharmony/build/blob/master/config/BUILDCONFIG.gn。

该文件的注释部分如下,可以看到该文件在构建目录下构建参数文件args.gn和顶级的".gn"文件之后进行加载。".gn"会指向BUILDCONFIG.gn文件作为构建配置。该文件执行后,产生的上下文被用来执行构建的其他文件。定义的变量为全局的,下划线开头的除外。

# =============================================================================
# WHAT IS THIS FILE?
# =============================================================================
#
# This is the master GN build configuration. This file is loaded after the
# build args (args.gn) for the build directory and after the toplevel ".gn"
# file (which points to this file as the build configuration).
#
# This file will be executed and the resulting context will be used to execute
# every other file in the build. So variables declared here (that don't start
# with an underscore) will be implicitly global.

2.2、工具链定义的单独构建文件

出于复杂原因,将这些工具链定义放在与任何target目标定义共享的BUILD.gn文件中不是一个好主意。Chrome项目该文件在.//build/toolchain/<platform>/BUILD.gn。对于OpenHarmony项目,可以查看https://gitee.com/openharmony/build/blob/master/toolchain/ohos/BUILD.gn、https://gitee.com/openharmony/build_lite/blob/master/toolchain/BUILD.gn。

关于如何定义工具链,可以参阅官方教程https://gitee.com/openharmony/third_party_gn/blob/master/docs/reference.md#func_toolchain。

2.3、根目录中的文件

根目录下有个文件BUILD.gn,该文件将在构建配置文件之后加载以开始构建。在每一个GN构建项目的根目录,都可以找到该文件,如https://gitee.com/openharmony/third_party_gn/blob/master/examples/simple_build/BUILD.gn。

根目录中还可能有个.gn文件。当您运行 GN 时,它会递归地查找目录树,直到找到此文件,并将包含目录视为“源根目录”。此文件还定义了主构建配置文件的位置:

  • 请参阅Chrome项目src/.gn文件。
  • 与 Chrome 不同,您可能不需要定义辅助根目录。
  • 通过命令gn help dotfile查看更多。

如果您想要一个与 Chrome 无关且不使用 Chrome 文件的完全独立的版本,则可以查看//tools/gn/example

本篇,我们学习了GN最小化构建项目的组成,了解从头开始使用GN构建,需要准备哪些文件。可以查看提供的OpenHarmony项目的例子,也可以查看GN中自带的例子。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK