重要提示:本教程适用于符号宏,这是 Bazel 8 中引入的新宏系统。如果您需要支持旧版 Bazel,则需要编写旧版宏;请参阅创建旧版宏。
假设您需要在 build 过程中运行某个工具。例如,您可能需要生成或预处理源文件,或者压缩二进制文件。在本教程中,您将创建一个用于调整图片大小的符号宏。
宏适合简单的任务。如果您想执行更复杂的操作,例如添加对新编程语言的支持,请考虑创建规则。规则可让您获得更高的控制权和灵活性。
创建可调整图片大小的宏的最简单方法是使用 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
参数。它们应用于宏的主要目标。如需记录符号宏的行为,请为
macro()
及其属性使用doc
参数。如需调用
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"],
)