在单个规则中有两个相关操作

Having two dependent actions in a single rule

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

有没有办法让规则中的操作生成一个文件,供同一规则中的后续操作使用?

例如:

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,但是 运行 动作要求生成的文件写在 srcs 和 deps 文件旁边execroot 让它工作。有没有办法将操作写入 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