执行组

执行组允许在单个目标中使用多个执行平台。每个执行组都有自己的工具链依赖项,并执行自己的工具链解析

背景

执行组允许规则作者定义操作集,每组操作可能具有不同的执行平台。多个执行平台可能允许以不同方式执行操作,例如在远程 (linux) 工作器上编译 iOS 应用,然后在本地 Mac 工作器上进行链接/代码签名。

能够定义操作组还有助于减少将操作助记符用作指定操作的代理。助记符不一定是唯一的,并且只能引用一项操作。这对于将额外的资源分配给特定内存和处理密集型操作(如在 C++ build 中建立关联)特别有帮助,而且避免过度分配给要求较低的任务。

定义执行组

在规则定义期间,规则作者可以声明一组执行组。在每个执行组上,规则作者可以指定为该执行组选择执行平台所需的一切内容,即通过 exec_compatible_with 指定任何限制条件,通过 toolchain 指定工具链类型。

# foo.bzl
my_rule = rule(
    _impl,
    exec_groups = {
        “link”: exec_group(
            exec_compatible_with = [ "@platforms//os:linux" ]
            toolchains = ["//foo:toolchain_type"],
        ),
        “test”: exec_group(
            toolchains = ["//foo_tools:toolchain_type"],
        ),
    },
    attrs = {
        "_compiler": attr.label(cfg = config.exec("link"))
    },
)

在上面的代码段中,您可以看到工具依赖项还可以使用 cfg 属性参数和 config 模块为执行组指定转换。该模块公开一个 exec 函数,该函数接受单个字符串参数,该参数是应为其构建依赖项的执行组的名称。

与原生规则一样,test 执行组默认存在于 Starlark 测试规则中。

执行组继承

除了定义自己的限制条件和工具链之外,新执行组还可以传递 copy_from_rule = True 参数,声明要从规则的默认执行组继承。将 copy_from_rule 设置为 true 并同时传递 exec_compatible_withtoolchains 是错误的。

从默认执行组继承的执行组会从默认执行组复制限制条件、工具链和执行属性。这包括在目标级别设置的约束条件和执行属性,而不仅仅是规则本身指定的限制条件和执行属性。换句话说,假设存在以下情况:

# foo.bzl
my_rule = rule(
    _impl,
    exec_groups = {
        “copied”: exec_group(
            copy_from_rule = True,
            # This will inherit exec_compatible_with and toolchains.
            # Setting them here directly would be an error, however.
        ),
    },
    toolchains = ["//foo_tools:toolchain_type"],
    exec_compatible_with = ["@platforms//os:linux"],
)

# BUILD

my_rule(
    name = "demo",
    exec_compatible_with = [":local_constraint"],
)

已配置的目标 democopied 执行组将包含以下全部: - //fool_tools:toolchain_type - @platforms//os:linux - :local_constraint

访问执行组

在规则实现中,您可以声明操作应在执行组的执行平台上运行。为此,您可以使用操作生成方法的 exec_group 参数,具体来说就是 ctx.actions.runctx.actions.run_shell

# foo.bzl
def _impl(ctx):
  ctx.actions.run(
     inputs = [ctx.attr._some_tool, ctx.srcs[0]]
     exec_group = "compile",
     # ...
  )

规则作者还可以访问执行组的已解析工具链,方法与访问目标的已解析工具链类似:

# foo.bzl
def _impl(ctx):
  foo_info = ctx.exec_groups["link"].toolchains["//foo:toolchain_type"].fooinfo
  ctx.actions.run(
     inputs = [foo_info, ctx.srcs[0]]
     exec_group = "link",
     # ...
  )

使用执行组设置执行属性

执行组与每条规则都存在的 exec_properties 属性集成,并允许目标写入者指定属性的字符串字典,然后将其传递给执行机器。例如,如果要为目标设置某个属性(例如内存),并为某些操作分配更高的内存分配,可以使用执行组增强键编写 exec_properties 条目,例如:

# BUILD
my_rule(
    name = 'my_target',
    exec_properties = {
        'mem': '12g',
        'link.mem': '16g'
    }
    …
)

具有 exec_group = "link" 的所有操作都将看到作为 {"mem": "16g"} 的执行属性字典。如您所见,执行组级别的设置会覆盖目标级别的设置。

原生规则的执行组

以下执行组适用于由原生规则定义的操作:

  • test:测试运行程序操作。
  • cpp_link:C++ 链接操作。

创建执行组以设置执行属性

有时,您需要使用 exec 组为特定操作提供不同的 exec 属性,但实际上并不需要与规则不同的工具链或限制条件。对于这些情况,您可以使用 copy_from_rule 参数创建执行组:

# foo.bzl

# Creating an exec group with `copy_from_rule=True` is the same as explicitly
# setting the exec group's toolchains and constraints to the same values as the
# rule's respective parameters.
my_rule = rule(
    _impl,
    exec_compatible_with = ["@platforms//os:linux"],
    toolchains = ["//foo:toolchain_type"],
    exec_groups = {
        # The following two groups have the same toolchains and constraints:
        “foo”: exec_group(copy_from_rule = True),
        "bar": exec_group(
            exec_compatible_with = ["@platforms//os:linux"],
            toolchains = ["//foo:toolchain_type"],
        ),
    },
)

#

执行组和平台执行属性

您可以为平台目标上的任意执行组定义 exec_properties(不同于直接在目标上设置的 exec_properties,未知执行组的属性会被拒绝)。然后,目标会继承执行平台的 exec_properties,这会影响默认执行组和任何其他相关执行组。

例如,假设运行 C++ 测试需要一些资源可用,但编译和链接不需要该资源;可以按如下方式建模:

constraint_setting(name = "resource")
constraint_value(name = "has_resource", constraint_setting = ":resource")

platform(
    name = "platform_with_resource",
    constraint_values = [":has_resource"],
    exec_properties = {
        "test.resource": "...",
    },
)

cc_test(
    name = "my_test",
    srcs = ["my_test.cc"],
    exec_compatible_with = [":has_resource"],
)

直接在目标上定义的 exec_properties 优先于从执行平台继承的 。