平台和工具链规则

报告问题 查看源代码

通过这组规则,您可以对要针对其构建的特定硬件平台进行建模,并指定为这些平台编译代码所需的特定工具。 用户应熟悉此处所述的概念。

规则

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

Name; required

此目标的唯一名称。

default_constraint_value

Name; optional; nonconfigurable

此设置的默认值的标签,如果没有指定值,则使用该值。如果存在此属性,则它指向的 constraint_value 必须在此 constraint_setting 所在的软件包中进行定义。

如果限制条件设置具有默认值,则每当平台不包含此设置的任何限制条件值时,就好像平台已指定默认值。否则,如果没有默认值,该平台将认为未指定该限制条件设置。在这种情况下,平台不会与任何需要此设置特定值的约束列表(例如,config_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_64arm 等。

参数

属性
name

Name; required

此目标的唯一名称。

constraint_setting

Label; required; nonconfigurable

可针对此 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 的逻辑如下:

  1. 如果子平台上未设置 remote_execution_property,则系统将使用父级的 remote_execution_properties
  2. 如果在子平台上设置了 remote_execution_property,并且包含字面量字符串 {PARENT_REMOTE_EXECUTION_PROPERTIES},则该宏将替换为父项的 remote_execution_property 属性的内容。
  3. 如果在子平台上设置了 remote_execution_property,且不包含该宏,将使用子项的 remote_execution_property

由于 remote_execution_properties 已废弃并将逐步淘汰,因此不允许在同一继承链中混合使用 remote_execution_propertiesexec_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

Name; required

此目标的唯一名称。

constraint_values

List of labels; optional; nonconfigurable

此平台包含的约束选项组合。若要将平台应用于给定环境,该环境必须至少具有此列表中的值。

此列表中的每个 constraint_value 都必须对应不同的 constraint_setting。例如,您无法定义需要 CPU 架构为 @platforms//cpu:x86_64@platforms//cpu:arm 的平台。

exec_properties

Dictionary: String -> String; optional

影响操作远程执行方式的字符串映射。Bazel 不会尝试解释这一点,它被视为通过远程执行协议的 Platform 字段转发的不透明数据。这包括来自父级平台的 exec_properties 属性的所有数据。 如果子级和父级平台定义了相同的键,则系统会保留子级的值。任何与空字符串值关联的键都将从字典中移除。 此属性将完全取代已废弃的 remote_execution_properties
parents

List of labels; optional; nonconfigurable

此平台应继承的 platform 目标的标签。虽然该属性接受一个列表,但最多只能存在一个平台。未在此平台上直接设置的任何 constraint_settings 都可以在父级平台中找到。 如需了解详情,请参阅有关平台继承的部分。
remote_execution_properties

String; optional

已弃用。请改用 exec_properties 属性。 用于配置远程执行平台的字符串。实际构建不会尝试对此进行解释,系统会将其视为可供特定 SpawnRunner 使用的不透明数据。这可以使用宏“{PARENT_REMOTE_EXECUTION_PROPERTIES}”包含来自父级平台“remote_execution_properties”属性的数据。如需了解详情,请参阅有关平台继承的部分。

工具链

查看规则来源
toolchain(name, deprecation, distribs, exec_compatible_with, features, licenses, tags, target_compatible_with, target_settings, testonly, toolchain, toolchain_type, visibility)

此规则声明特定工具链的类型和约束条件,以便在工具链解析期间选择该类型。如需了解详情,请参阅工具链页面。

参数

属性
name

Name; required

此目标的唯一名称。

exec_compatible_with

List of labels; optional; nonconfigurable

必须由执行平台满足 constraint_value 列表,才能针对该平台上的目标构建选择此工具链。
target_compatible_with

List of labels; optional; nonconfigurable

为了针对该平台的目标构建选择此工具链,必须满足的一系列 constraint_value 列表。
target_settings

List of labels; optional

要在工具链解析期间选择此工具链,必须满足一系列 config_setting 目标配置要求。
toolchain

Name; required

代表选择此工具链时提供的实际工具或工具的目标。
toolchain_type

Label; required; nonconfigurable

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

Name; required

此目标的唯一名称。