这组规则旨在让您对要构建的特定硬件平台进行建模,并指定可能需要用于为这些平台编译代码的特定工具。用户应熟悉此处介绍的概念。
规则
constraint_setting
查看规则源代码constraint_setting(name, default_constraint_value, deprecation, distribs, features, licenses, tags, testonly, visibility)
此规则用于引入一种新的限制条件类型,平台可以为此类型指定值。
例如,您可以定义一个名为“glibc_version”的 constraint_setting,以表示平台安装不同版本的 glibc 库的功能。如需了解详情,请参阅
平台页面。
每个 constraint_setting 都有一组可扩展的关联 constraint_value。这些通常在同一软件包中定义,但有时不同的软件包会为现有设置引入新值。例如,预定义设置 @platforms//cpu:cpu 可以使用自定义值进行扩展,以便定义以模糊 CPU 架构为目标的平台。
实参
| 属性 | |
|---|---|
name |
名称;必需 此目标的唯一名称。 |
default_constraint_value
|
此设置的默认值的标签,如果未提供任何值,则使用此标签。如果存在此
属性,则它指向的 constraint_value 必须与此 constraint_setting 在同一软件包中定义。
如果限制条件设置具有默认值,那么每当平台不包含
任何限制条件值时,都相当于平台已指定
默认值。否则,如果没有默认值,则该平台会被视为未指定限制条件设置
。在这种情况下,平台将不会与任何
限制条件列表(例如 |
constraint_value
查看规则源代码constraint_value(name, constraint_setting, deprecation, distribs, features, licenses, tags, testonly, visibility)
示例
以下代码为表示 CPU 架构的预定义 constraint_value 创建了一个新的可能值。
constraint_value(
name = "mips",
constraint_setting = "@platforms//cpu:cpu",
)
mips 架构,作为 x86_64、arm 等的替代方案。
实参
| 属性 | |
|---|---|
name |
名称;必需 此目标的唯一名称。 |
constraint_setting
|
此 constraint_value 是其可能选择的 constraint_setting。 |
platform
查看规则源代码platform(name, constraint_values, deprecation, distribs, exec_properties, features, flags, licenses, missing_toolchain_error, parents, remote_execution_properties, required_settings, tags, testonly, visibility)
此规则定义了一个新平台,即限制条件选择(例如 CPU 架构或编译器版本)的命名集合,用于描述构建的某些部分可能运行的环境。 如需了解详情,请参阅平台页面。
示例
这定义了一个平台,用于描述在 ARM 上运行 Linux 的任何环境。
platform(
name = "linux_arm",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:arm",
],
)
平台标志
平台可以使用 flags 属性指定一个标志列表,每当平台用作目标平台(即作为 --platforms 标志的值)时,这些标志都会添加到配置中。
从平台设置的标志实际上具有最高优先级,并会覆盖命令行、rc 文件或转换中该标志的任何先前 值。
示例
platform(
name = "foo",
flags = [
"--dynamic_mode=fully",
"--//bool_flag",
"--no//package:other_bool_flag",
],
)
这定义了一个名为 foo 的平台。当此平台是目标平台时(无论是由于
用户指定了 --platforms//:foo,还是由于转换将
//command_line_option:platforms 标志设置为 ["//:foo"],还是由于
//:foo 用作执行平台),给定的标志都将在
配置中设置。
平台和可重复的标志
某些标志在重复时会累积值,例如 --features,
--copt,以及任何创建为 config.string(repeatable = True) 的 Starlark 标志。
这些标志与从平台设置标志不兼容:相反,所有先前
的值都将被移除,并被平台中的值覆盖。
例如,给定以下平台,调用 build --platforms=//:repeat_demo
--features feature_a --features feature_b 将导致
--feature 标志的值为 ["feature_c", "feature_d"],从而移除在命令行中设置的功能。
platform(
name = "repeat_demo",
flags = [
"--features=feature_c",
"--features=feature_d",
],
)
因此,不建议在 flags 属性中使用可重复的标志。
平台继承
平台可以使用 parents 属性指定它们将从中
继承限制条件值的另一个平台。虽然 parents 属性采用列表,但目前仅支持单个值,指定多个父级是错误的。
在平台中检查限制条件设置的值时,首先检查直接设置的值
(通过 constraint_values 属性)时,然后检查父级上的限制条件值
。此过程会以递归方式继续向上遍历父级平台链。这样,直接在平台上设置的任何
值都将覆盖在父级上设置的值。
平台从父级平台继承 exec_properties 属性。
父级和子级平台的 exec_properties 中的字典条目
将被合并。
如果父级和子级的 exec_properties 中都出现相同的键,则使用子级的值。如果子级平台将空字符串指定为值,则
相应属性将被取消设置。
平台还可以从父级平台继承(已废弃的)remote_execution_properties 属性
。注意:新代码应改用 exec_properties。下面介绍的
逻辑是为了与旧版行为兼容而保留的,但将来会被移除
。当存在父级平台时,设置 remote_execution_platform 的逻辑如下:
-
如果未在子级平台上设置
remote_execution_property,则使用父级的remote_execution_properties。 -
如果在子级平台上设置了
remote_execution_property,并且其中包含字面量字符串 {PARENT_REMOTE_EXECUTION_PROPERTIES},则该宏将被替换为父级的remote_execution_property属性的内容。 -
如果在子级平台上设置了
remote_execution_property,并且其中不包含 该宏,则子级的remote_execution_property将保持不变。
由于 remote_execution_properties 已废弃并将逐步淘汰,因此不允许在同一
继承链中混合使用
remote_execution_properties 和 exec_properties。
建议使用 exec_properties,而不是已废弃的
remote_execution_properties.
示例:限制条件值
platform(
name = "parent",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:arm",
],
)
platform(
name = "child_a",
parents = [":parent"],
constraint_values = [
"@platforms//cpu:x86_64",
],
)
platform(
name = "child_b",
parents = [":parent"],
)
在此示例中,子级平台具有以下属性:
-
child_a具有限制条件值@platforms//os:linux(继承自父级)和@platforms//cpu:x86_64(直接在平台上设置)。 -
child_b继承了父级的所有限制条件值,并且未设置任何自己的限制条件值。
示例:执行属性
platform(
name = "parent",
exec_properties = {
"k1": "v1",
"k2": "v2",
},
)
platform(
name = "child_a",
parents = [":parent"],
)
platform(
name = "child_b",
parents = [":parent"],
exec_properties = {
"k1": "child"
}
)
platform(
name = "child_c",
parents = [":parent"],
exec_properties = {
"k1": ""
}
)
platform(
name = "child_d",
parents = [":parent"],
exec_properties = {
"k3": "v3"
}
)
在此示例中,子级平台具有以下属性:
-
child_a继承了父级的“exec_properties”,并且未设置自己的“exec_properties”。 -
child_b继承了父级的exec_properties并覆盖了 的值。k1其exec_properties将为:{ "k1": "child", "k2": "v2" }. -
child_c继承了父级的exec_properties并取消设置了k1。其exec_properties将为:{ "k2": "v2" }. -
child_d继承了父级的exec_properties并添加了一个新 属性。其exec_properties将为:{ "k1": "v1", "k2": "v2", "k3": "v3" }.
实参
| 属性 | |
|---|---|
name |
名称;必需 此目标的唯一名称。 |
constraint_values
|
此平台包含的限制条件选择的组合。为了让平台
适用于给定的环境,该环境必须至少具有此列表中的值。
此列表中的每个 |
exec_properties
|
字典:字符串 -> 字符串;不可配置;默认值为 exec_properties 属性中的任何数据。
如果子级和父级平台定义了相同的键,则保留子级的值。与空字符串值关联的任何
键都将从字典中移除。
此属性完全取代了已废弃的
remote_execution_properties.
|
flags
|
字符串列表;不可配置;默认值为 |
missing_toolchain_error
|
字符串;不可配置;默认值为 |
parents
|
此平台应从中继承的 platform 目标的标签。虽然
该属性采用列表,但平台数量不应超过一个。任何未直接在此平台上设置的
constraint_settings 都将在父级平台中找到。
如需了解详情,请参阅平台继承部分。
|
remote_execution_properties
|
字符串;不可配置;默认值为 |
required_settings
|
一个 config_setting 列表,目标配置必须满足这些设置,才能在工具链解析期间将此平台用作执行平台。必需的设置不会从父级平台继承。
|
toolchain
查看规则源代码toolchain(name, deprecation, distribs, exec_compatible_with, features, licenses, tags, target_compatible_with, target_settings, testonly, toolchain, toolchain_type, use_target_platform_constraints, visibility)
此规则声明了特定工具链的类型和限制条件,以便在工具链解析期间可以选择该工具链。如需了解详情,请参阅 工具链页面。
实参
| 属性 | |
|---|---|
name |
名称;必需 此目标的唯一名称。 |
exec_compatible_with
|
一个 constraint_value 列表,执行平台必须满足这些值,才能为在该平台上构建的目标选择此工具链。 |
target_compatible_with
|
一个 constraint_value 列表,目标平台必须满足这些值,才能为在该平台上构建的目标选择此工具链。 |
target_settings
|
一个 config_setting 列表,目标配置必须满足这些设置
才能在工具链解析期间选择此工具链。 |
toolchain
|
名称;必需 表示在选择此 工具链时提供的实际工具或工具套件的目标。 |
toolchain_type
|
表示此
工具链所扮演角色的 toolchain_type 目标的标签。 |
use_target_platform_constraints
|
布尔值;不可配置;默认值为 True,则此工具链的行为就像其 exec_compatible_with 和
target_compatible_with 限制设置为当前目标
平台的限制一样。不得在这种情况下设置 exec_compatible_with 和 target_compatible_with。 |
toolchain_type
查看规则源代码toolchain_type(name, compatible_with, deprecation, features, no_match_error, restricted_to, tags, target_compatible_with, testonly, visibility)
此规则定义了一种新的工具链类型,即一个简单的目标,表示为不同平台提供相同角色的工具类。
如需了解详情,请参阅工具链页面。
示例
这为自定义规则定义了一个工具链类型。
toolchain_type(
name = "bar_toolchain_type",
)
这可以在 bzl 文件中使用。
bar_binary = rule(
implementation = _bar_binary_impl,
attrs = {
"srcs": attr.label_list(allow_files = True),
...
# No `_compiler` attribute anymore.
},
toolchains = ["//bar_tools:toolchain_type"]
)
实参
| 属性 | |
|---|---|
name |
名称;必需 此目标的唯一名称。 |
no_match_error
|
字符串;不可配置;默认值为 |