執行群組

回報問題 查看來源

執行群組允許在單一目標中使用多個執行平台。每個執行群組都有自己的工具鍊依附元件,且會執行自己的工具鍊解析

背景

執行群組可讓規則作者定義動作組合,每個動作組合的執行平台可能各有不同。多個執行平台可能會使動作執行不同執行,例如在遠端 (linux) 工作站上編譯 iOS 應用程式,然後在本機 Mac 工作站上連結/程式碼簽署。

能夠定義動作群組也有助於減少使用動作記憶法做為指定動作的 Proxy。魔術無法保證可以重複,且只能參照單一動作。這特別適合將額外資源分配至特定記憶體和處理密集型動作 (例如 C++ 建構作業中的連結),而不用過度分配資源來縮減資源需求。

定義執行群組

在規則定義期間,規則作者可以宣告一組執行群組。在每個執行群組中,規則作者可以指定為該執行群組選取執行平台所需的所有項目,也就是透過 exec_compatible_withtoolchain 工具鍊類型進行的任何限制。

# 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,而非從執行平台繼承的目標。