Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to create a system header only library in bazel?

Tags:

c++

bazel

We are migrating a CMake project to Bazel. We have several header only libraries that are tagged SYSTEM in CMake project to suppress some warnings. When migrating these to Bazel, the way we are able to make this work is by using the below

cc_library(
    name = "lib",
    srcs = ["include/header1.h", ...],
    includes = ["include"],
)

This works but as per Bazel C++ documentation, it is not recommended to have interface/public headers in srcs. Those should be part of hdrs. Adding these to headers doesn't work because it uses the regular -I based inclusion instead of -isystem.

Is our way of doing this fine, although not recommended by bazel? If not, what would be the correct way of doing it?

EDIT: After some digging, found the textual_hdrs attribute on cc_library and using that it seems to work too. And this seems to be a cleaner approach than adding the public headers to srcs. Now the rule looks like this

cc_library(
    name = "lib",
    textual_hdrs = ["include/header1.h", ...],
    includes = ["include"],
)

This looks like a good solution for us, except that the documentation on textual_hdrs isn't clear enough to indicate that this is what it is meant for.

PS: It is really not possible for us to refactor the code to fix the warnings as there are numerous libraries like this and just completely outside the scope of this migration effort.

like image 242
Kanad Kanhere Avatar asked Sep 10 '25 09:09

Kanad Kanhere


1 Answers

It turns out adding it to hdrs does work if you make sure that strip_include_prefix is None (or not passed). We had a macro that was wrapping up the cc_library instance and it was defaulting strip_include_prefix to empty string. Interestingly this doesn't affect textual_hdrs or srcs, but does affect hdrs

In summary the below seems to work fine

cc_library(
    name = "lib",
    hdrs = ["include/header1.h", ...],
    includes = ["include"],
)
like image 146
Kanad Kanhere Avatar answered Sep 12 '25 23:09

Kanad Kanhere