Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bazel platform-specific C++ defines

Tags:

c++

bazel

How should one include a platform-specific flag in bazel?

Say we have a cross-platform library that we want to change defines on.

Can we do something like:

defines = ["FEATURE_A"]
if somehow_detect_the_platform == 'ios':
    defines.extend("FEATURE_B")

cc_library(
    name = "foo",
    srcs = ["foo.cc"],
    defines = defines,
    hdrs = ["foo.h"],
)

Also note that this concept can apply to other things like includes, cflags, sources, headers, etc.

like image 428
syvex Avatar asked Dec 02 '25 17:12

syvex


1 Answers

There are 2 options. First is using select function. With select you can conditionally set the value of an attribute depending on the current configuration.

Second option is to tweak the CROSSTOOL configuration file, put a specific feature that would emit different flags depending on the toolchain. Then you can enable that feature in your cc_library in features attribute.

First option is simpler, the second one is more robust and more encapsulated.

like image 91
hlopko Avatar answered Dec 04 '25 07:12

hlopko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!