How to use `include_prefix` so it doesn't affect external library itself?

1,346 views
Skip to first unread message

seee...@gmail.com

unread,
Jan 29, 2020, 9:20:19 PM1/29/20
to bazel-discuss
I want to use HdrHistogram_c in my project. However I don't want to include files like this:

#include <src/hdr_histogram.h>

but rather like:

#include <histogram/hdr_histogram.h>

So my hdr-histogram.BUILD is configured as follows:

cc_library(
    name
= "main",
    srcs
= glob([
       
"src/*.c",
   
]),
    hdrs
= glob([
       
"src/*.h",
   
]),
    includes
= ["."],
    strip_include_prefix
= "src",
    include_prefix
= "histogram",
    visibility
= ["//visibility:public"],
)

However, strip_include_prefix and include_prefix affects the compilation of library itself, and I get this error:

Use --sandbox_debug to see verbose messages from the sandbox
external
/hdrhistogram/src/hdr_histogram_log.c:22:26: fatal error: hdr_encoding.h: No such file or directory
compilation terminated
.

Ultimately, I'd like to encapsulate HdrHistogram_c.
My question is how do I do it, with or without (strip_)include_prefix?

seee...@gmail.com

unread,
Jan 29, 2020, 10:47:17 PM1/29/20
to bazel-discuss
I was able to achieve encapsulation by splitting the rule into two cc_library rules, as follows:

cc_library(
    name
= "base",

    srcs
= glob([
       
"src/*.c",

       
"src/*.h",
   
]),
)


cc_library
(
    name
= "main",

    hdrs
= glob([
       
"src/*.h",
   
]),
    includes
= ["."],
    strip_include_prefix
= "src",
    include_prefix
= "histogram",

    deps
= [":base"],

    visibility
= ["//visibility:public"],
)

Rule :base is for proper compilation of the external library, and rule :main is for proper encapsulation of it.

I can use the library like so:

#include <histogram/hdr_histogram.h>

Now I am wondering if this can be a general solution for encapsulation of external libraries. Or am I lucky this config is working (since project structure of HdrHistogram_c is simple)?

Tim Blakely

unread,
Nov 20, 2020, 9:34:50 AM11/20/20
to bazel-discuss
Just wanted to say thanks for the follow-up. Been wrestling with this for ~years, and this finally solved the prefix issue for me. Cheers!
Reply all
Reply to author
Forward
0 new messages