cc_library includes referencing generated sources directory

719 views
Skip to first unread message

Indigo Orton

unread,
Apr 15, 2015, 7:30:31 PM4/15/15
to bazel-...@googlegroups.com
Hi everyone,

First I'd like to thank Google, and the Bazel team specifically, for open-sourcing Bazel, it really is amazing.

Now to my problem: 

My project generates model files, using Java annotation processors, into C++.

I have genrules (in macros) that unzip the generated jars and "output" the .cc/.h files (they list the specific files as outputs, but all they do is the unzipping).
I then use a cc_library rule to compile the sources/headers. The problem is I need to add the containing directories into the cc_library's includes attribute. However, the includes attribute takes a list of strings, not labels. So I'm not sure how I can have my includes point at a generated source directory.

I thought about using custom skylark rules, however, rules (as I understand them) cannot output directories, so that wouldn't work.

TL;DR: How do I set a generate source directory as an include in a cc_library?

Thanks in advance
Indigo

Kristina Chodorow

unread,
Apr 16, 2015, 10:31:40 AM4/16/15
to Indigo Orton, bazel-...@googlegroups.com
Could you collect the outputs in a filegroup and then use $(location //your:filegroup) in the cc_library's includes?

--
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 post to this group, send email to bazel-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/61648bfe-e62b-47b4-9314-d1784ea7a6cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Indigo Orton

unread,
Apr 16, 2015, 10:33:24 AM4/16/15
to bazel-...@googlegroups.com, indigo...@gmail.com
Hi Kristina,

I tried using $(location xxx), but the cc_library includes field takes a list of strings, not labels. So it doesn't recognize $(location) (at least, that is my understanding from the docs and from the errors I'm given when I try).

Kristina Chodorow

unread,
Apr 16, 2015, 10:52:08 AM4/16/15
to Indigo Orton, bazel-...@googlegroups.com
Well, location should expand into a directory, but it looks like includes doesn't expand it.  

If you know the directory name, can you hard code it?  E.g.:

cc_library(
    name = "whatever",
    includes = ["my/unzipped/dir"],
    data = [":unzipper"]
    ...
)

Indigo Orton

unread,
Apr 16, 2015, 10:55:26 AM4/16/15
to bazel-...@googlegroups.com, indigo...@gmail.com
Unfortunately I don't know the directory, as it is in the generated sources directory. I suppose I could do something very hacky like: "../../../../../../../../../bazel-bin/src/main/java/com/company/model/cpp". But that is extremely hacky (especially when bazel so easily handles this with labels in other places).

Kristina Chodorow

unread,
Apr 16, 2015, 10:57:57 AM4/16/15
to Indigo Orton, bazel-...@googlegroups.com
You can use $(BINDIR) or $(GENDIR) from includes.

Indigo Orton

unread,
Apr 16, 2015, 11:16:36 AM4/16/15
to bazel-...@googlegroups.com, indigo...@gmail.com
Thank you Kristina!

So it is still a bit hacky because I have to get a path back to the root of my workspace, and then go into the bin. However, it works!

Because this is in a macro I've tried to make it reusable (i.e. not hard coding the "../../"). Final solution:

 1) Pass in the path from root to the current position (i.e. position of the BUILD file). (Stored in src_path)
 2) In the macro get the number of slashes (i.e. "/") in src_path
 3) Add a "../" for each "/" in the path (storing the output in relative_root)
 4) Set gen_root_path to relative_root + "$(BINDIR)" + src_path
 5) Set includes to [gen_root_path + "/includes", gen_root_path + "/otherincludes"]

Thanks again,
Indigo

Ulf Adams

unread,
Apr 16, 2015, 11:23:37 AM4/16/15
to Indigo Orton, bazel-...@googlegroups.com
Includes should automatically _also_ be applied to the genfiles directory, so GENDIR should never be necessary in includes. You can check the compiler command line by running blaze build -s //target.

Han-Wen Nienhuys

unread,
Apr 16, 2015, 11:26:12 AM4/16/15
to Ulf Adams, Indigo Orton, bazel-...@googlegroups.com
If you can post a toy example (maybe substitute the annotation
processor with a shell script)? We could tinker with that and offer a
solution.
> https://groups.google.com/d/msgid/bazel-discuss/CALS-RZ%2BAcquJvRj2tCBZFpfHEuN4QHH4KZ_1Re8DYOdh-6pX1Q%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Han-Wen Nienhuys
Google Munich
han...@google.com

Indigo Orton

unread,
Apr 16, 2015, 12:17:23 PM4/16/15
to bazel-...@googlegroups.com, ulf...@google.com, indigo...@gmail.com
Hi,

I'll make a toy example as soon as possible (hopefully be sometime today).

Thanks
Indigo
Reply all
Reply to author
Forward
0 new messages