【问题标题】:Accessing runfiles during build in genrule在构建期间访问运行文件 genrule
【发布时间】:2021-11-10 14:03:58
【问题描述】:

我有一个使用运行文件的cc_binary 目标。我想使用 genrule 将可执行文件和所有运行文件压缩到一个存档中。

类似:

genrule(
  name = "zip_binary"
  srcs = [
    ":binary",
  ],
  outs = [
    "binary.zip",
  ],
  cmd = "zip -r $(OUTS) $(locations :binary)",
)

但是,这仅包括二进制文件,不包括 binary.runfiles 目录。

如何让 bazel 将运行文件包含在 srcs 中?

【问题讨论】:

    标签: bazel


    【解决方案1】:

    Genrules 无法获得足够的信息来做到这一点。有了完整的custom rule,这很容易。像这样的:

    def _zipper_impl(ctx):
        inputs = ctx.runfiles(files = ctx.files.srcs)
        inputs = inputs.merge_all([a[DefaultInfo].default_runfiles for a in ctx.attr.srcs])
        ctx.actions.run_shell(
            outputs = [ctx.output.out],
            inputs = inputs.files,
            command = " ".join(["zip", "-r", ctx.output.out.path] +
                [i.path for i in inputs.files.to_list()]),
        )
        return [DefaultInfo(files = depset(ctx.output.out))]
    
    zipper = rule(
        impl = _zipper_impl,
        attrs = {
            "out": attr.output(mandatory = True),
            "srcs": attr.label_list(allow_files = True),
        },
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-25
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多