如果你想覆盖(默认禁用)Doom Melon:
gn --args="enable_doom_melon=true enable_teleporter=true"
这也设置传送器,但它已经默认为打开,所以它会没有效果。
```
## gn help defined
返回ture如果所给的参数被定义了。
例子:
template("mytemplate") {
# To help users call this template properly...
assert(defined(invoker.sources), "Sources must be defined")
```
# If we want to accept an optional "values" argument, we don't
# want to dereference something that may not be defined.
if (defined(invoker.values)) {
values = invoker.values
} else {
values = "some default value"
}
```
Return value
返回值的类型将与输入值相同(可以是字符串或字符串列表)。所有的相对源,绝对文件名系统绝对路径将被转换为相对于请求的输出
输出路径是否以斜杠结尾将匹配对应的输入路径以斜杠结尾。它将返回"."或"./"(取决于输入是否以斜杠结尾)以避免返回空
字符串。这意味着如果您希望根路径("//"或"/")不是以斜杠,你可以添加一个点("//.")。
```
Example
#将当前目录中的文件转换为与build相关的文件
#目录(执行编译器和脚本时的当前目录)。
foo = rebase_path("myfile.txt", root_build_dir)
可能会生成“../../project/myfile.txt”。
将文件转换为系统绝对文件:
foo = rebase_path("myfile.txt")
# Might produce "D:\\source\\project\\myfile.txt" on Windows or
# "/home/you/source/project/myfile.txt" on Linux.
# Typical usage for converting to the build directory for a script.
action("myscript") {
#可能在Windows上生成“D:\\source\\project\\myfile.txt”
# "/home/you/source/project/myfile.txt"
转换到脚本的构建目录的典型用法。
sources = [ "foo.txt", "bar.txt" ]
The following will be written to "$root_build_dir/my_files.json" (less the
comments):
[
"baz.cpp", // from //base:c via //base:b
"missing.cpp" // from //base:d via //base:b
"bar.cpp", // from //base:b via //base:a
"foo.cpp", // from //base:a
]
Alternatively, as an example of using walk_keys, if the following
generated_file target is defined:
The following will be written to "$root_build_dir/my_files.json" (again less
the comments):
[
"baz.cpp", // from //base:c via //base:b
"bar.cpp", // from //base:b via //base:a
"foo.cpp", // from //base:a
]
If `rebase` is used in the following generated_file target:
The following will be written to "$root_build_dir/my_files.json" (again less
the comments) (assuming root_build_dir = "//out"):
[
"../base/baz.cpp", // from //base:c via //base:b
"../base/bar.cpp", // from //base:b via //base:a
"../base/foo.cpp", // from //base:a
]
变量:
```
# Our script reads this file each time, so we need to list it as a
# dependency so we can rebuild if it changes.
inputs = [ "my_configuration.txt" ]
# Transformation from source file name to output file names.
outputs = [ "$target_gen_dir/{{source_name_part}}.h",
"$target_gen_dir/{{source_name_part}}.cc" ]
# Note that since "args" is opaque to GN, if you specify paths here, you
# will need to convert it to be relative to the build directory using
# rebase_path().
args = [
"{{source}}",
"-o",
rebase_path(relative_target_gen_dir, root_build_dir) +
"/{{source_name_part}}.h" ]
```
static_library("mylib") {
# The configs will be auto-populated as above. You can remove it if
# you don't want the default for a particular default:
configs -= [ "//tools/mything:settings" ]
}
```
```
toolchain_args = {
# use_doom_melon is not overridden here, it will take the default.
current_cpu = "x64"
}
```
}
交叉工具链依赖实例:
If a 64-bit target wants to depend on a 32-bit binary, it would specify a
dependency using data_deps (data deps are like deps that are only needed at
runtime and aren't linked, since you can't link a 32-bit and a 64-bit
library).
```
executable("my_program") {
...
if (target_cpu == "x64") {
# The 64-bit build needs this 32-bit helper.
data_deps = [ ":helper(//toolchains:32)" ]
}
}
if (target_cpu == "x86") {
# Our helper library is only compiled in 32-bits.
shared_library("helper") {
...
}
}
```
```
# Locate the depfile in the output directory named like the
# inputs but with a ".d" appended.
depfile = "$relative_target_output_dir/{{source_name}}.d"
# Say our script uses "-o <d file>" to indicate the depfile.
args = [ "{{source}}", "-o", depfile ]
```
}
## gn help deps
私有依赖
不能从依赖中#include头文件,公开配置也无法被传递。
Details of dependency propagation 依赖性传递详解
Source sets, shared libraries, and non-complete static libraries will be
propagated up the dependency tree across groups, non-complete static
libraries and source sets.
Executables, shared libraries, and complete static libraries will link all
propagated targets and stop propagation. Actions and copy steps also stop
propagation, allowing them to take a library as an input but not force
dependents to link to it.
Propagation of all_dependent_configs and public_configs happens independently
of target type. all_dependent_configs are always propagated across all types
of targets, and public_configs are always propagated across public deps of
all types of targets.
Data dependencies are propagated differently. See "gn help data_deps" and
"gn help runtime_deps".
```
public = [
"public_api.h", # Normal public API for dependent targets.
]
# Private API and sources.
sources = [
"a_source_file.cc",
# Normal targets that depend on this one won't be able to include this
# because this target defines a list of "public" headers. Without the
# "public" list, all headers are implicitly public.
"private_api.h",
]
```
}
executable("unit_tests") {
sources = [
# This can include "private_api.h" from the :lib target because it
# depends on that target and because of the friend annotation.
"my_test.cc",
]
```
deps = [
":lib", # Required for the include to be allowed.
]
```
These exact files are public:
public = [ "foo.h", "bar.h" ]
```
No files are public (no targets may include headers from this one):
// This allows starting compilation in dependent targets earlier.
public = []
```
```
static_library("toplevel") {
# This target will get "my_config" applied to it. However, since this
# target uses "deps" and not "public_deps", targets that depend on this
# one won't get it.
deps = [ ":intermediate" ]
}
static_library("intermediate") {
# Depending on "lower" in any way will apply "my_config" to this target.
# Additionall, since this target depends on "lower" via public_deps,
# targets that depend on this one will also get "my_config".
public_deps = [ ":lower" ]
}
static_library("lower") {
# This will get applied to all targets that depend on this one.
public_configs = [ ":my_config" ]
}
```
Avoiding applying public configs to this target:
如果你想把"public_configs"应用到依赖当前目标的目标上,但并不应用到当前目标上,使用组(<group>)定义额外的间接层:
```
# External targets depend on this group.
group("my_target") {
# Config to apply to all targets that depend on this one.
public_configs = [ ":external_settings" ]
deps = [ ":internal_target" ]
}
# Internal target to actually compile the sources.
static_library("internal_target") {
# Force all external targets to depend on the group instead of directly
# on this so the "external_settings" config will get applied.
visibility = [ ":my_target" ]
...
}
```
## gn hellp public_deps
声明公共依赖。
公共依赖类似于私有依赖(参见“gn help deps”),但是附加地表示当前目标将列出的deps作为其公共API的一部分公开。
如果有三个目标: A -> B -> C,C的可见性(<visibility>)允许B依赖它但不允许A,这样A就无法包括任何来自于A的头文件,而C的"public_configs"也只能用于B。
对于二进制目标(如source sets, executables, and libraries),这些已知文件的类型会用相关的工具来编译。而未知的文件类型和头文件会被跳过。但是,你仍然应该列出C/C++的头文件,所以GN知道这些文件的存在就是是为了文件引入检查(include check,可以查看 gn help check,检查头文件的引入合法性)的目的。