สำคัญ: บทแนะนำนี้มีไว้สำหรับมาโครเชิงสัญลักษณ์ ซึ่งเป็นระบบมาโครใหม่ ที่เปิดตัวใน 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,
    },
)
ข้อควรทราบ
- ฟังก์ชันการใช้งานมาโครเชิงสัญลักษณ์ต้องมีพารามิเตอร์ - nameและ- visibilityควรใช้สำหรับเป้าหมายหลักของมาโคร
- หากต้องการบันทึกลักษณะการทำงานของมาโครสัญลักษณ์ ให้ใช้พารามิเตอร์ - docสำหรับ- macro()และแอตทริบิวต์ของมาโคร
- หากต้องการเรียกใช้ - genruleหรือกฎเนทีฟอื่นๆ ให้ใช้- native.
- ใช้ - **kwargsเพื่อส่งต่ออาร์กิวเมนต์ที่รับช่วงมาเพิ่มเติมไปยัง- genruleที่อยู่เบื้องหลัง (ทำงานเหมือนใน Python) ซึ่งจะเป็นประโยชน์เพื่อให้ผู้ใช้ตั้งค่าแอตทริบิวต์มาตรฐาน เช่น- tagsหรือ- testonlyได้
ตอนนี้ให้ใช้มาโครจากไฟล์ 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"],
)