平台規則

規則

constraint_setting

constraint_setting(name, default_constraint_value, deprecation, distribs, exec_compatible_with, exec_properties, 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_setting 相同的套件中定義其指向的 constraint_value

如果限制設定設有預設值,則只要平台不包含該設定的任何限制值,就會與平台已指定預設值一樣。否則,如果沒有預設值,該平台會將限制設定視為未指定。在這種情況下,平台就不會與任何需要該設定特定值的限制清單 (例如 config_setting) 進行比對。

constraint_value

constraint_value(name, constraint_setting, deprecation, distribs, exec_compatible_with, exec_properties, 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_valueconstraint_setting 是可行的選項。

平台

platform(name, constraint_values, deprecation, distribs, exec_compatible_with, 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 並取消設定 k1exec_properties會是:{ "k2": "v2" }
  • child_d 會繼承父項的 exec_properties 並新增屬性。exec_properties會是:{ "k1": "v1", "k2": "v2", "k3": "v3" }

引數

屬性
name

Name; required

此目標的專屬名稱。

constraint_values

List of labels; optional

這個平台所含的限制選項組合。如要將平台套用至特定環境,該環境至少須在這份清單中含有值。

這份清單中的每個 constraint_value 都必須屬於不同的 constraint_setting。例如,您無法定義需要 CPU 架構同時為 @platforms//cpu:x86_64@platforms//cpu:arm 的平台。

exec_properties

Dictionary: String -> String; optional

會影響遠端動作執行方式的字串對應。Bazel 未嘗試解讀這個值,系統會將其視為透過遠端執行通訊協定的「平台」欄位轉送的不透明資料。包括來自父項平台 exec_properties 屬性的任何資料。如果子平台和父項平台定義了相同的鍵,系統會保留子項的值。任何與值為空白字串相關聯的鍵都會從字典中移除。這項屬性可以完全取代已淘汰的 remote_execution_properties
parents

List of labels; optional

這個平台應繼承的 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, exec_properties, 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

此目標的專屬名稱。