基于ct.actions.run
1.在bzl中创建自定义rule
test.bzl
dh_binary = rule(
implementation = _dh_impl,
attrs = {
"_lib_hash": attr.label(
default = Label("//universal:dh_test"),
executable = True,
/*设置label是否是可执行的, default = False
True if the dependency has to be executable. This means the label must refer to an executable file, or to a rule that outputs an executable file. Access the label with ctx.executable.<attribute_name>.*/
cfg = "exec",/*如果excutable这个就必须写, host和exec没发现有啥区别*/
),
},
outputs = {"out": "%{name}.out"},// 输出名字
)
def _dh_impl(ctx):
tool = ctx.executable._lib_hash
out = ctx.outputs.out
ctx.actions.run(
executable = tool,
arguments = [out.path,"bbb"],
inputs = [],
tools = [tool],
outputs = [out],
)
2.在BUILD中实例化规则
load(":bl_dump.bzl", "bl_dump_binary")
bl_dump_binary(
name = "bl_test",
)
命令行:
bazel build //universal:bl_test
3.遇到的问题
1.outputs如果写了就要生成对应文件 否则报错,不管是ctx.actions.write 还是ctx.actions.run 都要生成文件
2.default = Label("//universal:dh_test"),需要写对应脚本路径
网友评论