赞
踩
gn
有大量的内置变量和库函数,熟悉这些库函数基本就知道gn能干什么,gn的官方文档很齐全,前往 gn参考手册翻看查找 。 gn
的语法简单,了解以下几点基本就能看懂gn
代码。重点了解下函数的调用方式,和其他高级语言不太一样。
字符串
a = "mypath"
b = "$a/foo.cc" # b -> "mypath/foo.cc"
c = "foo${a}bar.cc" # c -> "foomypathbar.cc"
列表
a = [ "first" ]
a += [ "second" ] # [ "first", "second" ]
a += [ "third", "fourth" ] # [ "first", "second", "third", "fourth" ]
b = a + [ "fifth" ] # [ "first", "second", "third", "fourth", "fifth" ]
条件语句
if (is_linux || (is_win && target_cpu == "x86")) {
sources -= [ "something.cc" ]
}else {
...
}
循环
foreach(i, mylist) {
print(i) # Note: i is a copy of each element, not a reference to it.
}
函数调用
print("hello, world")
assert(is_win, "This should only be executed on Windows") # 如果is_win为真,就打印后面的内容
static_library("mylibrary") {
sources = [ "a.cc" ]
}
模板 | Templates
#定义模板, 文件路径: //tools/idl_compiler.gni, 后缀.gni 代表这是一个 gn import file
template("idl") { #自定义一个名称为 "idl"的函数
source_set(target_name) { #调用内置函数 source_set
sources = invoker.sources #invoker为内置变量,含义为调用者内容 即:[ "a", "b" ]的内容
}
}
#如何使用模板, 用import,类似 C语言的 #include
import("//tools/idl_compiler.gni")
idl("my_interfaces") { #等同于调用 idl
sources = [ "a", "b" ] #给idl传参, 参数的接收方是 invoker.sources
}
目标项 | Targets
目标是构建图中的一个节点。它通常表示将生成某种可执行文件或库文件。整个构建是由一个个的目标组成。 目标包含:
action: 运行脚本以生成文件
executable: 生成可执行文件
group: 生成依赖关系组
shared_library: 生成.dll或.so动态链接库
static_library: 生成.lib或.a 静态链接库
...
配置项 | Configs
config("myconfig") {#创建一个标签为`myconfig`的配置项
include_dirs = [ "include/common" ]
defines = [ "ENABLE_DOOM_MELON" ]
}
executable("mything") {#生成可执行文件
configs = [ ":myconfig" ]#使用标签为`myconfig`的配置项来生成目标文件
}
gn`生成`ninja`的命令是 `gn gen ...
/home/tools/gn gen /home/openharmony/code-v1.1.1-LTS/out/hispark_aries/ipcamera_hispark_aries \
--root=/home/openharmony/code-v1.1.1-LTS \
--dotfile=/home/openharmony/code-v1.1.1-LTS/build/lite/.gn \
--script-executable=python3 \
'--args=ohos_build_type="debug" \
ohos_build_compiler_specified="clang" \
ohos_build_compiler_dir="/home/tools/llvm" \
product_path="/home/openharmony/code-v1.1.1-LTS/vendor/hisilicon/hispark_aries" \
device_path="/home/openharmony/code-v1.1.1-LTS/device/hisilicon/hispark_aries/sdk_liteos" \
ohos_kernel_type="liteos_a" \
enable_ohos_appexecfwk_feature_ability = false \
ohos_full_compile=true'
解读
root
,dotfile
,script-executable
是gn内置的固定参数,一切从dotfile
指向的文件开始。即build/lite/.gn
相当于main()
函数的作用,# The location of the build configuration file. #1.完成gn的配置工作
buildconfig = "//build/lite/config/BUILDCONFIG.gn"
# The source root location. #2.完成gn的编译工作
root = "//build/lite"
args
为用户自定义的参数,它们将会在解析BUILD.gn
,BUILDCONFIG.gn
过程中被使用
BUILDCONFIG.gn
为BUILD.gn
做准备,填充好编译所需的配置信息。即生成配置项 详细请查看 build/lite/config/BUILDCONFIG.gn 文件全部内容,本篇只贴出部分。
import("//build/lite/ohos_var.gni") import("${device_path}/config.gni") .... arch = "arm" if (ohos_kernel_type == "liteos_a") { target_triple = "$arch-liteos" } else if (ohos_kernel_type == "linux") { target_triple = "$arch-linux-ohosmusl" } ... template("executable") { #生成可执行文件 executable(target_name) { forward_variables_from(invoker, Variables_Executable) if (!defined(invoker.deps)) { deps = [ "//build/lite:prebuilts" ] } else { deps += [ "//build/lite:prebuilts" ] } if (defined(invoker.configs)) { configs = [] configs += invoker.configs } } } set_defaults("executable") {#设置目标类型的默认值 configs = default_executable_configs configs += [ "//build/lite/config:board_exe_ld_flags" ] } ...
解读
查看构建-配置内容,BUILDCONFIG
主要任务就是对其中的内置变量赋值。例如:
#目的是要得到项目各个模块的编译入口 group("ohos") { deps = [] if (ohos_build_target == "") { # Step 1: Read product configuration profile。 # 第一步:读取配置文件product_path的值来源于根目录的ohos_config.json,如下,内容由 hb set 命令生成 # { # "root_path": "/home/openharmony", # "board": "hispark_aries", # "kernel": "liteos_a", # "product": "ipcamera_hispark_aries", # "product_path": "/home/openharmony/vendor/hisilicon/hispark_aries", # "device_path": "/home/openharmony/device/hisilicon/hispark_aries/sdk_liteos", # "patch_cache": null #} product_cfg = read_file("${product_path}/config.json", "json") # Step 2: Loop subsystems configured by product. # 第二步:循环处理各自子系统,config.json中子系统部分格式如下hb #"subsystems": [ # { # "subsystem": "aafwk", # "components": [ # { "component": "ability", "features":[ "enable_ohos_appexecfwk_feature_ability = false" ] } # ] # }, # ... # { # "subsystem": "distributed_schedule", # "components": [ # { "component":
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。