在一条规则中有两个相互依赖的动作



Edit:下面的例子确实有效,我误解了编译器给我的输出。答案可能对一些人仍然有帮助。

规则中的操作是否有方法生成一个文件,该文件由同一规则中的后续操作使用?

例如:

def _example_rule_impl(ctx):
thefile = ctx.actions.declare_file("required_file.json")
ctx.actions.write(
output = thefile,
content = "CONTENT",
)
args = ctx.actions.args()
args.add("--config", thefile)
ctx.actions.run(
inputs = ctx.files.srcs + ctx.files.deps + [thefile],
outputs = outs,
arguments = [args],
progress_message = "Compiling...",
executable = ctx.executable._compiler,
)

这方面的主要问题似乎是,所有操作输出似乎都被写入bazel-out,但运行操作需要将生成的文件写入execroot中的srcs和deps文件旁边才能工作。有没有一种方法可以将操作写入execroot,或者这不是正确的方法?

将其他操作的输出作为输入的操作是一件非常典型的事情,基本上应该按照设置的方式工作。Bazel负责处理输入和输出文件,例如,在linux上使用大量符号链接,在沙盒中安装东西,上传和下载文件以进行远程执行等。

请参阅https://docs.bazel.build/versions/main/output_directories.html

此外,关于这条线:

inputs = ctx.files.srcs + ctx.files.deps + [thefile],

根据需要执行的操作,出于性能原因,您可能需要使用depsets。看见https://docs.bazel.build/versions/main/skylark/depsets.html尤其是在最后https://docs.bazel.build/versions/main/skylark/depsets.html#performance

最新更新