I am writing a sample C++ project that uses Bazel to serve as an example idiom for other collaborators to follow.
Here is the repository: https://github.com/thinlizz
Add a .bazelrc
to your project. Add the lines build:vs2019 --cxxopt=/std:c++14
and build:gcc --cxxopt=-std=c++14
. Build your code bazel build --config=msvc //...
or bazel build --config=gcc //....
@bazel_tools
contains predefined platform conditions:
$ bazel query @bazel_tools//src/conditions:all
@bazel_tools//src/conditions:windows_msys
@bazel_tools//src/conditions:windows_msvc
@bazel_tools//src/conditions:windows
@bazel_tools//src/conditions:remote
@bazel_tools//src/conditions:host_windows_msys
@bazel_tools//src/conditions:host_windows_msvc
@bazel_tools//src/conditions:host_windows
@bazel_tools//src/conditions:freebsd
@bazel_tools//src/conditions:darwin_x86_64
@bazel_tools//src/conditions:darwin
You can use them directly in the BUILD
file:
cc_library(
name = "impl",
srcs = ["Implementation.cpp"] + select({
"@bazel_tools//src/conditions:windows": ["ImplementationWin.cpp"],
"@bazel_tools//src/conditions:darwin": ["ImplementationMacOS.cpp"],
"//conditions:default": ["ImplementationLinux.cpp"],
}),
# .. same for hdrs and data
)
cc_binary(
name = "demo",
deps = [":impl"],
)
See the documentation for select for details on the syntax.