You can do something like this in your .bazelrc
:
# GCC 9.3
build:gcc9 --cxxopt=-std=c++2a
build:gcc9 --cxxopt=-Wall
build:gcc9 --cxxopt=-Werror
##build:gcc9 --cxxopt=-Wextra
build:gcc9 --define compiler=gcc9_3_0
build:macos --cxxopt=-std=c++2a
build:macos --cxxopt=-Wall
#build:macos --cxxopt=-Werror
##build:macos --cxxopt=-Wextra
build:macos --define compiler=clang12
# Clang 13.0.0
build:clang13 --cxxopt=-std=c++17
#build:clang13 --cxxopt=-mavx
#build:clang13 --cxxopt=-mavx2
#build:clang13 --cxxopt=-msse4.2
#build:clang13 --cxxopt=-Werror
build:clang13 --define compiler=clang13
build:clang13 --incompatible_enable_cc_toolchain_resolution
build:clang13 --cxxopt=-mwaitpkg
# Mingw
build:mingw --cxxopt=-std=c++2a
#build:mingw --cxxopt=-Wall
#build:mingw --cxxopt=-Werror
#build:mingw --cxxopt=-Wextra
build:mingw --compiler=mingw-gcc
# Visual Studio 2019
build:vs2019 --cxxopt=/std:c++17
build:vs2019 --enable_runfiles # https://github.com/bazelbuild/bazel/issues/8843
build:vs2019 --define compiler=vs2019
build:vs2019 --copt=-DWIN32_LEAN_AND_MEAN
build:vs2019 --copt=-DNOGDI
build:vs2019 --host_copt=-DWIN32_LEAN_AND_MEAN
build:vs2019 --host_copt=-DNOGDI
# Visual Studio 2022
build:vs2022 --config=vs2019
And then select a config when you do a build e.g. bazel --config=vs2022 //...
When you are too lazy to type every time the config you can create in your home directory (e.g. ~/.bazelrc
a file that contains build --config=config_for_the_machine
.
Config switches are costly. Bazel needs to rerun a lot of stuff if you switch the config. If you switch on one machine often between different configs (e.g. GCC and Clang) then use a Bazel Remote Cache server. I use one on my local machine - this makes config switches cheap since the Bazel Remote Cache server is able to store artifacts for different configs whereas Bazel seems only to store artifacts for the latest config.