genrule with generated "outs"

2,592 views
Skip to first unread message

Dan Fabulich

unread,
Feb 10, 2016, 5:09:42 PM2/10/16
to bazel-discuss
I'm working on a genrule that runs a custom compiler. It will take a collection of sources and compile them into .classes.

What would I list for my genrule "outs" parameter? The genrule's "srcs" is a glob; can "outs" be a glob? What would it say?

Alex Humesky

unread,
Feb 10, 2016, 5:13:58 PM2/10/16
to Dan Fabulich, bazel-discuss
The usual way to work with an unknown set of outputs in a genrule is to zip or tar up the files and put the tar/zip in the outs.

You might also consider writing sklyark rules for your compiler, which provide a much richer api to work with: http://bazel.io/docs/skylark/index.html

On Wed, Feb 10, 2016 at 5:09 PM Dan Fabulich <danfa...@gmail.com> wrote:
I'm working on a genrule that runs a custom compiler. It will take a collection of sources and compile them into .classes.

What would I list for my genrule "outs" parameter? The genrule's "srcs" is a glob; can "outs" be a glob? What would it say?

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/3e9e05f8-e044-4c48-a3d0-865acf265c96%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Humesky

unread,
Feb 10, 2016, 5:19:34 PM2/10/16
to Dan Fabulich, bazel-discuss
Also, if the sets of inputs and outputs are 1:1, or otherwise easy to calculate, you could use a skylark macro instead of writing your own skylark rule to create the outs for you, e.g. something like this:

def run_my_compiler(name, srcs):
  outs = [f.replace(".ext1", ".ext2") for f in srcs]
  native.genrule(
    name = name,
    srcs = srcs,
    outs = outs,
    cmd = """ ...... """)

Note though that you now have all those outputs to group up somehow in downstream rules. You might then use a filegroup to group these together in your macro:

native.filegroup(
  name = name + "_files",
  srcs = outs)

Dan Fabulich

unread,
Feb 15, 2016, 1:46:20 AM2/15/16
to bazel-discuss, danfa...@gmail.com
Am I right in thinking that an ordinary Skylark rule cannot modify its own outputs during the Analysis phase, and so only a Skylark Macro can compute generated outputs during the Loading phase?

This seems like a surprising result to me, because http://bazel.io/docs/skylark/macros.html says that "Macros don't give additional power, they are just used for encapsulation and code reuse." But modifying ctx.outputs certainly seems like a genuine addition of power to me!

In any case, I feel like it would be good to add an example of computed outputs to the cookbook. Right now the cookbook provides examples of default outputs and "custom" (hard-coded user-specified) outputs, but no examples of computed outputs.

Damien Martin-guillerez

unread,
Feb 15, 2016, 5:24:44 AM2/15/16
to Dan Fabulich, bazel-discuss
On Mon, Feb 15, 2016 at 7:46 AM Dan Fabulich <danfa...@gmail.com> wrote:
Am I right in thinking that an ordinary Skylark rule cannot modify its own outputs during the Analysis phase, and so only a Skylark Macro can compute generated outputs during the Loading phase?

I beg your pardon? Skylark rule cannot change its output list during the analysis phase. Skylark macro are analyzed during the loading phase so they can established their output list based on computed values like globs.
 

This seems like a surprising result to me, because http://bazel.io/docs/skylark/macros.html says that "Macros don't give additional power, they are just used for encapsulation and code reuse." But modifying ctx.outputs certainly seems like a genuine addition of power to me!

ctx.outputs is accessible only through skylark rules not in macros.
 

In any case, I feel like it would be good to add an example of computed outputs to the cookbook. Right now the cookbook provides examples of default outputs and "custom" (hard-coded user-specified) outputs, but no examples of computed outputs.

Reply all
Reply to author
Forward
0 new messages