Bazel can't find include file

2,039 views
Skip to first unread message

Shareef Jalloq

unread,
Nov 25, 2022, 5:17:00 PM11/25/22
to bazel-discuss
So I've spent the best part of a day bashing my head against the wall and wishing I hadn't left the comfort of Makefiles.

Anyway, I'm trying to compile a cc_binary that depends on some files within my directory tree.  But for the life of me cannot work out how to set up the include paths such that I can compile my app.

I have a tree that looks something like:

+- WORKSPACE
+- BUILD
    |
    +--some/path/to/my_app.cc
    |
    +- my/lib/path/*.{cc,h}

I've tried a few things including trying to use filegroups, various versions of cc_library's and other things.  What I have currently as a last resort is a BUILD file that looks like:

cc_binary(
  name = "my_app",
  srcs = ["some/path/to/my_app.cc"],
  deps = [":my_lib"],
)

cc_library(
  name = "my_lib",
  srcs = glob(["my/lib/path/*.cc"]),
  hdrs = glob(["my/lib/path/*.h"]),
  includes = ["my/lib/path"]
)

My source files do a #include "my_lib.h" and I've been reading that unless you use cc_library.includes, cc_library.strip_include_prefix or pass in an include path to cc_library.copts, then the path won't be found.  I managed to prove this for myself using a simple helloworld app but can't get it to work with the above.

What trick am I missing?

I've addition to the includes above I've tried both the strip_include_prefix arg and trying to pass in a -Imy/lib/path to copts.

When using cc_library.includes, I can see the full command line has a "-isystem my/lib/path" option which is what the documentation states.  Shouldn't this be turned into the relevant -I path?

Lost.

Shareef Jalloq

unread,
Nov 25, 2022, 5:49:49 PM11/25/22
to bazel-discuss
Ergh, I think I just fell foul of the Bazel silent failure when trying to include files beneath a directory containing another BUILD file.  So my next issue is how to pass include files from the output of a rule to a dependency...

I have a rule that generates an archive library along with a couple of headers.  In my rule I'm using output_groups to group the archive and the headers.  I then have a filegroup that depends on that rule and uses the output_group to select the correct files.  I then pass that to a cc_library that I'm trying to depend on in my cc_binary.  Crystal.  So how do I point at that include file that is sat somewhere under bazel-out/k8-fastbuild/bin/build.toolflow/*.h?

Austin Schuh

unread,
Nov 25, 2022, 7:10:57 PM11/25/22
to Shareef Jalloq, bazel-discuss
Pretend it isn't under bazel-out/k8-fastbuild/bin and Bazel will do the rest :)

If you look under the covers a bit, Bazel adds both
./bazel-bin/include_path and ./include_path to the include path when
you add an include path to includes. It doesn't like making a
distinction between the two folders since a file is a file to Bazel,
and if it is generated or not isn't a distinction made elsewhere.

Austin
> --
> 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/96febd38-547a-46e5-88d4-b78ac653f669n%40googlegroups.com.

Shareef Jalloq

unread,
Nov 25, 2022, 7:11:52 PM11/25/22
to bazel-discuss
I think I understand more about providers and CcInfo now so may have solved this issue.

Philipp Schrader

unread,
Nov 25, 2022, 7:13:13 PM11/25/22
to bazel-discuss
Bazel should automatically point the compiler at bazel-out/k8-fastbuild/bin if one of its dependencies is in there.
I.e. it should work if you do something like:
#include "build.toolflow/foo.h"
(as long as your "deps" are specified correctly)

Shareef Jalloq

unread,
Nov 28, 2022, 5:58:58 AM11/28/22
to bazel-discuss
Thanks, I can't modify the source code based on the build tool.

What I've just tried is to return a CcInfo provider from my custom rule but perhaps I'm falling into the same trap.  The files that I care about are output to the following directory structure:

    bazel-out/k8-fastbuild/bin/build.verilator_build/archive-verilator/<name>.{a,h}

and I'm creating the CompilationContext and LinkingContext from those.

If I have a cc_library that depends on that output and expects to #include "<name>.h", how do I do that?  I did just try adding the following copts to the cc_library but that hasn't worked:

copts = ["-Ibuild.verilator_build/archive-verilator"],

Shareef Jalloq

unread,
Nov 29, 2022, 6:04:47 PM11/29/22
to bazel-discuss
Not sure where my email replies are going on this thread? I'll repeat a question I asked yesterday, this time via the web interface...

So the issue with the flow I'm using is that I'm generating an archive library and some header files using an external tool flow, in this case Verilator.  Verilator spits out the archive and headers to the bazel-out directory mentioned previously and I need to use those in a cc_binary.

So what is the correct method for generating a dependency to feed into a cc_binary where I can just #include "<filename>.h"?

Gregg Reynolds

unread,
Nov 29, 2022, 9:15:43 PM11/29/22
to Shareef Jalloq, bazel-discuss
On Tue, Nov 29, 2022 at 5:04 PM Shareef Jalloq <sha...@jalloq.co.uk> wrote:
Not sure where my email replies are going on this thread? I'll repeat a question I asked yesterday, this time via the web interface...

So the issue with the flow I'm using is that I'm generating an archive library and some header files using an external tool flow, in this case Verilator.  Verilator spits out the archive and headers to the bazel-out directory mentioned previously and I need to use those in a cc_binary.

Are you using Bazel to build Verilator?  If not, having it write to bazel-out is definitely not the way to go.  Just build it wherever you want and try using cc_import.  Writing your own CcInfo producer is also probably not the way to go if all you are trying to do is depend on your lib and headers.
 
So what is the correct method for generating a dependency to feed into a cc_binary where I can just #include "<filename>.h"?

I'd have to see more details to give you any more concrete advice.

HTH

Gregg

Shareef Jalloq

unread,
Nov 30, 2022, 4:49:41 AM11/30/22
to bazel-discuss
I'm using Bazel to run Verilator.  I'm trying to put together a testcase at the moment and will post it later.  Should be a bit easier to visualise then.

Shareef Jalloq

unread,
Nov 30, 2022, 6:56:16 AM11/30/22
to bazel-discuss
OK, here's a testcase that hopefully clarifies the flow:  https://github.com/shareefj/bazel_fusesoc_example

What I'm trying to do is compile a Verilator binary using target //cpp:verilator_top.  This depends on the output files generated by running Verilator using FuseSoC.  There is a fusesoc_build rule that outputs two files, an archive library and a header file named Vtop__ALL.a and Vtop.h respectively.  It is those two files that I'm trying to pass as input dependencies to cc_binary.

When I run the //cpp:verilator_top target I get an error that Vtop.h cannot be found:

cpp/top.cc:2:10: fatal error: Vtop.h: No such file or directory
    2 | #include "Vtop.h"

Yet the command line has the relevant include path -Ibazel-bin/build.verilator_build/archive-verilator.

So what is the correct way of passing the output of a build rule as a cc_library that can be used?

Shareef.

Shareef Jalloq

unread,
Nov 30, 2022, 8:24:03 AM11/30/22
to bazel-discuss
So I finally think I found the way to specify the include path.  I've updated my testcase and the main change was adding a cc_library.includes field pointing to the directory under bazel-out/k8-fastbuild/bin/.

filegroup(
    name = "verilator_archive",
    srcs = [":verilator_build"],
    output_group = "archive"
)

filegroup(
    name = "verilator_headers",
    srcs = [":verilator_build"],
    output_group = "headers"
)

cc_library(
    name = "verilator_cc_lib",
    srcs = [":verilator_archive"],
    hdrs = [":verilator_headers"],
    linkstatic=1,
    includes = ["build.verilator_build/archive-verilator"]
)

Gregg Reynolds

unread,
Nov 30, 2022, 8:28:09 AM11/30/22
to Shareef Jalloq, bazel-discuss
On Wed, Nov 30, 2022 at 7:24 AM Shareef Jalloq <sha...@jalloq.co.uk> wrote:
So I finally think I found the way to specify the include path.  I've updated my testcase and the main change was adding a cc_library.includes field pointing to the directory under bazel-out/k8-fastbuild/bin/.

filegroup(
    name = "verilator_archive",
    srcs = [":verilator_build"],
    output_group = "archive"
)

filegroup(
    name = "verilator_headers",
    srcs = [":verilator_build"],
    output_group = "headers"
)

cc_library(
    name = "verilator_cc_lib",
    srcs = [":verilator_archive"],
    hdrs = [":verilator_headers"],
    linkstatic=1,
    includes = ["build.verilator_build/archive-verilator"]
)


Might be better to  make that " copts = ["-I", "build.verilator_build/archive-verilator"].  See the docs on the "includes" attribute for why.

Shareef Jalloq

unread,
Nov 30, 2022, 8:44:47 AM11/30/22
to bazel-discuss
I've specifically used includes so that every other rule that depends on it can find Vtop.h.  Or are you saying it's better to add that copts to every rule that depends on :verilator_cc_lib?
Reply all
Reply to author
Forward
0 new messages