执行组允许在单个目标内使用多个执行平台。每个执行组都有自己的工具链依赖项,并执行自己的工具链解析。
背景
执行组允许规则作者定义一组操作,其中每个操作都可能有不同的执行平台。多个执行平台可允许不同的操作执行,例如在远程 (Linux) 工作器上编译 iOS 应用,然后在本地 Mac 工作器上关联/代码签名。
能够定义操作组还有助于减少操作记忆作为指定操作的代理。助记符不能保证唯一,并且只能引用单个操作。这对于将特定资源分配给特定内存和处理密集型操作(如在 C++ 构建中进行链接,而不会过度分配到任务不太任务)尤其有用。
定义执行组
在规则定义期间,规则作者可以声明一组执行组。在每个执行组中,规则作者都可以指定为该执行组选择执行平台所需的所有内容,即通过 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_with
或 toolchains
会出错。
从默认执行组继承的执行组将复制默认约束条件、工具链和执行属性。这包括在目标级设置的约束和执行属性,而不仅仅是规则本身指定的属性和执行属性。换言之,假设存在以下情况:
# 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"],
)
已配置目标 demo
的 copied
执行组将包括:
- //fool_tools:toolchain_type
- @platforms//os:linux
- :local_constraint
访问执行组
在规则实现中,您可以声明操作应在执行组的执行平台上运行。为此,您可以使用操作参数 exec_group
参数生成方法,特别是 ctx.actions.run
和 ctx.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++ 关联操作。
创建执行组以设置执行属性
有时,您需要使用执行组为特定的操作指定不同的执行属性,但实际上并不需要与规则不同的工具链或约束条件。{0}{/1}对于这些情况,您可以使用 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
优先于从执行平台继承的那些。