通过这组规则,您可以对要针对其构建的特定硬件平台进行建模,并指定为这些平台编译代码所需的特定工具。 用户应熟悉此处所述的概念。
规则
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(name, constraint_values, deprecation, distribs, exec_properties, features, licenses, parents, remote_execution_properties, tags, testonly, visibility)
此规则定义了一个新平台,即约束条件选项(例如 CPU 架构或编译器版本)的命名集合,其中描述了可以运行构建的部分环境。如需了解详情,请参阅平台页面。
示例
它定义了一个描述任何在 ARM 上运行 Linux 的环境的平台。
platform( name = "linux_arm", constraint_values = [ "@platforms//os:linux", "@platforms//cpu:arm", ], )
平台继承
平台可以使用 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”,不会设置自己的父级。 -
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 。
|
parents
|
platform 目标的标签。虽然该属性接受一个列表,但最多只能存在一个平台。未在此平台上直接设置的任何 constraint_settings 都可以在父级平台中找到。
如需了解详情,请参阅有关平台继承的部分。
|
remote_execution_properties
|
|
工具链
查看规则来源toolchain(name, deprecation, distribs, exec_compatible_with, features, licenses, tags, target_compatible_with, target_settings, testonly, toolchain, toolchain_type, visibility)
此规则声明特定工具链的类型和约束条件,以便在工具链解析期间选择该类型。如需了解详情,请参阅工具链页面。
参数
属性 | |
---|---|
name |
此目标的唯一名称。 |
exec_compatible_with
|
constraint_value 列表,才能针对该平台上的目标构建选择此工具链。
|
target_compatible_with
|
constraint_value 列表。 |
target_settings
|
config_setting 目标配置要求。 |
toolchain
|
|
toolchain_type
|
toolchain_type 目标的标签,表示此工具链提供的角色。
|
toolchain_type
查看规则来源toolchain_type(name, compatible_with, deprecation, features, 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 |
此目标的唯一名称。 |