The .bazelrc
file can be utilized to offer a centralized source for specifying build configurations specific to operating system and build objectives.
In the .bazelrc
file, the specification of build
follow by :[name]
allows you to specify any build options following thereafter on the same line. The name can be anything. In my case, I specified the compiler used that corresponds to the operating system.
Consider the following:
#In the .bazelrc file
build:msvc --cxxopt='-std:c++latest' -c opt
--cxxopt=
is Bazel's flag to specify build options. -c
is the flag to utilize a set of flags pre-set by Google with opt
being optimal for speed in this case. All of this can be found in the documentation.
Now to use the same flags for testing without causing a complete rebuild, use the following:
test:msvc -c opt --test_output=all
There are nuances in this regard because Bazel does not like when flags are specified for a second time, even if it is the same flag. I cannot entirely remember my experiments with customizing on top of the -c opt
, so you will have to test if ordering matters for yourself. I do not think it does though.
Combined, it will look like the following:
build:msvc --cxxopt='-std:c++latest' -c opt
test:msvc -c opt --test_output=all
On the command line, you would do the following to choose one of the options:
bazel build --config=[name] //...
with the names used above:
bazel build --config=msvc //...
and to test swap out the word "build" with "test":
bazel test --config=msvc //...
A snippet from my own own project:
# To build|test Windows: bazel build|test --config=msvc
# To build|test Linux: bazel build|test --config=gcc
build:msvc --cxxopt='-std:c++latest' -c opt
test:msvc -c opt --test_output=all
build:gcc --cxxopt='-std=c++2a' -c opt
test:gcc -c opt --test_output=all
build:gccProfile --cxxopt='-std=c++2a' --cxxopt='-fno-omit-frame-pointer' -c dbg
Notice the subtle differences between the gcc
and msvc
specifications. The flags specified are specific to the compiler used, not generic specifications by Bazel. Also, Bazel pulls the first compiler it finds on your operating system. I tried some experiments of building with clang
on the same operating machine, but it requires a whole host of other specifications and so forth to make it work. gcc
was the first compiler installed by me and used by Bazel, and it kept it as the default compiler.