执行组

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。
报告问题 查看源代码

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

背景

使用执行组,规则作者可以定义多组操作,每组操作可能具有不同的执行平台。多个执行平台可允许不同的操作执行,例如在远程 (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++ 链接操作。

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

有时,您想使用执行组为特定操作提供不同的执行属性,但实际上不希望使用与规则不同的工具链或约束条件。对于这些情况,您可以使用 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 优先于从执行平台继承的那些。