Skip to content

Bazel Configuration

I need to configure Bazel with different configurations so I can build/run/test C++ applications in different settings. For example, I may want to compile the same source code with c++2a vs C++17, or Clang vs GCC. I want to have a convenient preset configuration triggered by some bazel build --config=XXX. Of cource, C++ toolchain configuration is itself a very complicated thing to do, and it is likely beyond the scope of the note.

Bazel Configurations are organzied by .bazelrc file in the workspace, the root of the workspace is where WORKSPACE file is created. You can directly write down all configuration in a single .bazelrc, or you can organize the configuration into different files and use import or try-import "declarative" to import them into the main .bazelrc.

Import path

import %workspace%/profile.d/bazelrc/clang.bazelrc # relative path from the WORKSPACE root.
import ./profile.d/bazelrc/clang.bazelrc  # currenct directory
import /profile.d/bazelrc/clang.bazelrc # absolute path

When import bazelrc fails, you may see Bazel logs like the following:

[INFO 21:24:54.581 src/main/cpp/option_processor.cc:407] Looking for the following rc files: /etc/bazel.bazelrc,/media/feitong/Data/Projects/monorepo/.bazelrc,/home/feitong/.bazelrc
[INFO 21:24:54.581 src/main/cpp/rc_file.cc:56] Parsing the RcFile /media/feitong/Data/Projects/monorepo/.bazelrc
[INFO 21:24:54.581 src/main/cpp/rc_file.cc:56] Parsing the RcFile profile.d/bazelrc/clang.bazelrc
[FATAL 21:24:54.581 src/main/cpp/blaze.cc:1302] Unexpected error reading .blazerc file 'profile.d/bazelrc/clang.bazelrc': (error: 2): No such file or directory

Bazel Configuration: different C++ preset

Different C++ configurations can be thought as different "presets" of running C++ Compilers. To properly configure Bazel C++ presets is the same as using anything build-system for C++ and pass the right flags to the compiler. So previous experiences with Make and CMake should be help, though the syntax is likely very different.

Compiler Flags and Environments

Sanitizers

...

C++ Toolchain Configuration

It is quite complicated to learn how to setup th C++ toolchain configuration. A few examples on the Github is helpful, but it is likely need significantly more work to extrapolate from the example to your own use case.

Todo

C++ toolchain configuration, e.g. official doc, involves lots of concepts around C/C++ that I don't fully understand at this moment, so I will not be able to set up the entire C++ toolchain configuration now. Leave it to future notes.

Query Dependency

# query bazel dependencies on //main:hello-world
bazel query --notool_deps --noimplicit_deps "deps(//main:hello-world)" --output graph
# to visualize the result, install GraphViz and Dot
sudo apt update && sudo apt install graphviz xdot
# visualize dependency
xdot <(bazel query --notool_deps --noimplicit_deps "deps(//main:hello-world)" --output graph)

Bazel Configuration: bazel's behavior

Reference