Bazel提供了靈活的擴(kuò)展機(jī)制曲管,用于自定義宏函數(shù)顽素。例如,
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"],
)
為了復(fù)用代碼嚎于,可以應(yīng)用「參數(shù)化」設(shè)計(jì)击孩,提取公共的宏函數(shù)迫悠。
def miniature(name, src, size="100x100", **kwargs):
"""Create a miniature of the src image.
The generated file is prefixed with 'small_'.
"""
native.genrule(
name = name,
srcs = [src],
outs = ["small_" + src],
cmd = "convert $< -resize " + size + " $@",
**kwargs
)
為了應(yīng)用新定義的宏函數(shù),使用內(nèi)置的load
函數(shù)導(dǎo)入miniature
函數(shù)巩梢,這個(gè)機(jī)制類似于Python
的import行為创泄。
load("http://path/to:miniature.bzl", "miniature")
miniature(
name = "logo_miniature",
src = "image.png",
)
cc_binary(
name = "my_app",
srcs = ["my_app.cc"],
data = [":logo_miniature"],
)