How do i reference a specific file in a genrule

3,436 views
Skip to first unread message

kv

unread,
Mar 19, 2020, 6:15:56 PM3/19/20
to bazel-discuss
genrule(
     name = "abc",
     srcs =[ "@com_xyc//:spits2filesasoutput"], #<---- this rule gives me 2 files, fn.txt, otherfn.txt
     outs = ["newnamefn.log"],
     cmd = """
     mv $(locations @com_xyc//:spits2filesasoutput) $@
     """
)

I just want to rename otherfn.txt to newnamefn.log. How do I reference that specific file from the srcs?
Thanks
kv

Alex Garcia

unread,
Mar 19, 2020, 6:18:06 PM3/19/20
to bazel-discuss, kv
$(location otherfn.txt) should work

Alexandre Garcia Mayans
--
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/9d9f188f-c8ff-4fa0-b080-3cd503893820%40googlegroups.com.

kv

unread,
Mar 19, 2020, 6:40:28 PM3/19/20
to bazel-discuss

in cmd attribute of genrule rule //:abc: label '//:otherfn.txt' in $(location) expression is not a declared prerequisite of this rule

I'm getting this error. What am I missing?

On Thursday, March 19, 2020 at 3:18:06 PM UTC-7, Alex Garcia wrote:
$(location otherfn.txt) should work

Alexandre Garcia Mayans
On 19 Mar 2020, 23:15 +0100, kv <vijay....@gmail.com>, wrote:
genrule(
     name = "abc",
     srcs =[ "@com_xyc//:spits2filesasoutput"], #<---- this rule gives me 2 files, fn.txt, otherfn.txt
     outs = ["newnamefn.log"],
     cmd = """
     mv $(locations @com_xyc//:spits2filesasoutput) $@
     """
)

I just want to rename otherfn.txt to newnamefn.log. How do I reference that specific file from the srcs?
Thanks
kv

--
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-...@googlegroups.com.

Alex Garcia Mayans

unread,
Mar 19, 2020, 6:47:29 PM3/19/20
to kv, bazel-discuss
$(location @com_xyc//:otherfn.txt) maybe ?

Regards,
Alexandre Garcia Mayans

On 19 Mar 2020, at 23:40, kv <vijay.k...@gmail.com> wrote:


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/f69196da-5c31-46b9-b410-91736144715d%40googlegroups.com.

Alex Humesky

unread,
Mar 19, 2020, 6:57:21 PM3/19/20
to Alex Garcia Mayans, kv, bazel-discuss
$(location) requires a label, and only some outputs of a rule have labels (these are the "implicit outputs" of a rule, e.g. for a java_binary named "bar" in a package named "foo", there are the labels "//foo:bar" and "//foo:bar_deploy.jar"). So it depends on the implementation of the rule for "@com_xyc//:spits2filesasoutput"

Other times rules put their outputs in providers, which are available to other rules. So depending on the implementation of the rule for "@com_xyc//:spits2filesasoutput", you might need to create a Starlark rule instead of using a genrule so that you can access the outputs in the other rule's providers.

kv

unread,
Mar 19, 2020, 7:44:03 PM3/19/20
to bazel-discuss
Thanks Alex for the explanation. Makes sense. What can I do in the upstream rule to make this easy? The goal is to just rename a file which is an output of another rule. 


On Thursday, March 19, 2020 at 3:57:21 PM UTC-7, Alex Humesky wrote:
$(location) requires a label, and only some outputs of a rule have labels (these are the "implicit outputs" of a rule, e.g. for a java_binary named "bar" in a package named "foo", there are the labels "//foo:bar" and "//foo:bar_deploy.jar"). So it depends on the implementation of the rule for "@com_xyc//:spits2filesasoutput"

Other times rules put their outputs in providers, which are available to other rules. So depending on the implementation of the rule for "@com_xyc//:spits2filesasoutput", you might need to create a Starlark rule instead of using a genrule so that you can access the outputs in the other rule's providers.

On Thu, Mar 19, 2020 at 6:47 PM Alex Garcia Mayans <the...@gmail.com> wrote:
$(location @com_xyc//:otherfn.txt) maybe ?

Regards,
Alexandre Garcia Mayans

On 19 Mar 2020, at 23:40, kv <vijay....@gmail.com> wrote:



in cmd attribute of genrule rule //:abc: label '//:otherfn.txt' in $(location) expression is not a declared prerequisite of this rule

I'm getting this error. What am I missing?

On Thursday, March 19, 2020 at 3:18:06 PM UTC-7, Alex Garcia wrote:
$(location otherfn.txt) should work

Alexandre Garcia Mayans
On 19 Mar 2020, 23:15 +0100, kv <vijay....@gmail.com>, wrote:
genrule(
     name = "abc",
     srcs =[ "@com_xyc//:spits2filesasoutput"], #<---- this rule gives me 2 files, fn.txt, otherfn.txt
     outs = ["newnamefn.log"],
     cmd = """
     mv $(locations @com_xyc//:spits2filesasoutput) $@
     """
)

I just want to rename otherfn.txt to newnamefn.log. How do I reference that specific file from the srcs?
Thanks
kv

--
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-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/9d9f188f-c8ff-4fa0-b080-3cd503893820%40googlegroups.com.

--
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-...@googlegroups.com.

--
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-...@googlegroups.com.

Alex Humesky

unread,
Mar 19, 2020, 10:22:21 PM3/19/20
to kv, bazel-discuss
The easiest thing to do is to modify the genrule to find the file you're interested in inside "$(locations @com_xyc//:spits2filesasoutput)", e.g. like using grep. This requires that you put some information about how the file is named inside the genrule. That might be fine as a one-off, but if you need to do this a lot in a generic way, you can maybe use a macro to make it reusable.

(also, note that you'll want to use cp instead of mv, since that file might be an input to other actions)

The other options are
1. Have the other rule add the files to an output group in an OutputGroupInfo in the rule's providers, and then use a filegroup rule to give a label to that output group
See

2. Use attr.output to create an implicit output in the other rule



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/b85b6871-3f9d-4f4a-9cd2-c4defa330877%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages