Dig-Doug
Dig-Doug

Reputation: 111

Accessing runfiles during build in genrule

I have a cc_binary target that uses runfiles. I would like to zip the executable and all of the runfiles into a single archive using a genrule.

Something like:

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

However, this only includes the binary and not the binary.runfiles dir.

How can I get bazel to include the runfiles in the srcs?

Upvotes: 0

Views: 521

Answers (1)

Brian Silverman
Brian Silverman

Reputation: 3848

Genrules don't have access to enough information to do that. With a full custom rule it's pretty easy though. Something like this:

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),
    },
)

Upvotes: 1

Related Questions