建立符號巨集

回報問題 查看來源 Nightly · 8.4 · 8.3 · 8.2 · 8.1 · 8.0 · 7.6

重要事項:本教學課程適用於符號巨集,也就是 Bazel 8 推出的新巨集系統。如需支援舊版 Bazel,建議改為編寫舊版巨集,請參閱「建立舊版巨集」。

假設您需要在建構過程中執行工具。舉例來說,您可能想產生或預先處理來源檔案,或是壓縮二進位檔。在本教學課程中,您將建立可調整圖片大小的符號巨集。

巨集適合用於簡單的工作。如要執行更複雜的操作 (例如新增對新程式設計語言的支援),請考慮建立規則。規則可讓您進一步控管及調整。

如要建立可調整圖片大小的巨集,最簡單的方法是使用 genrule

genrule(
    name = "logo_miniature",
    srcs = ["logo.png"],
    outs = ["small_logo.png"],
    cmd = "convert $< -resize 100x100 $@",
)

cc_binary(
    name = "my_app",
    srcs = ["my_app.cc"],
    data = [":logo_miniature"],
)

如果需要調整更多圖片的大小,建議重複使用程式碼。如要這麼做,請在個別的 .bzl 檔案中定義實作函式巨集宣告,並將該檔案命名為 miniature.bzl

# Implementation function
def _miniature_impl(name, visibility, src, size, **kwargs):
    native.genrule(
        name = name,
        visibility = visibility,
        srcs = [src],
        outs = [name + "_small_" + src.name],
        cmd = "convert $< -resize " + size + " $@",
        **kwargs,
    )

# Macro declaration
miniature = macro(
    doc = """Create a miniature of the src image.

    The generated file name will be prefixed with `name + "_small_"`.
    """,
    implementation = _miniature_impl,
    # Inherit most of genrule's attributes (such as tags and testonly)
    inherit_attrs = native.genrule,
    attrs = {
        "src": attr.label(
            doc = "Image file",
            allow_single_file = True,
            # Non-configurable because our genrule's output filename is
            # suffixed with src's name. (We want to suffix the output file with
            # srcs's name because some tools that operate on image files expect
            # the files to have the right file extension.)
            configurable = False,
        ),
        "size": attr.string(
            doc = "Output size in WxH format",
            default = "100x100",
        ),
        # Do not allow callers of miniature() to set srcs, cmd, or outs -
        # _miniature_impl overrides their values when calling native.genrule()
        "srcs": None,
        "cmd": None,
        "outs": None,
    },
)

注意事項:

  • 符號巨集實作函式必須有 namevisibility 參數。應做為巨集的主要目標。

  • 如要記錄符號巨集的行為,請使用 doc 參數來記錄 macro() 及其屬性。

  • 如要呼叫 genrule 或任何其他原生規則,請使用 native.

  • 使用 **kwargs 將額外繼承的引數轉送至基礎 genrule (運作方式與 Python 相同)。這項功能非常實用,使用者可以設定 tagstestonly 等標準屬性。

現在,請使用 BUILD 檔案中的巨集:

load("//path/to:miniature.bzl", "miniature")

miniature(
    name = "logo_miniature",
    src = "image.png",
)

cc_binary(
    name = "my_app",
    srcs = ["my_app.cc"],
    data = [":logo_miniature"],
)