'How to use select to properly detect whether I am building C++ code in Windows or Linux?
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/thinlizzy/bazelexample
I am interested to know if I am doing it 'right', more specifically about this file: https://github.com/thinlizzy/bazelexample/blob/38cc07931e58ff5a888dd6a83456970f76d7e5b3/demo/BUILD when regarding to pick particular implementations.
cc_library(
name = "demo",
srcs = ["demo.cpp"],
deps = [
"//example:frontend",
],
)
cc_binary(
name = "main_win",
deps = [
":demo",
"//example:impl_win",
],
)
cc_binary(
name = "main_linux",
deps = [
":demo",
"//example:impl_linux",
],
)
Is this following a correct/expected idiom for Bazel projects? I am doing this way already for other projects, by concentrating all the platform-specific dependencies in separate targets and then the binaries just depend on them.
Someone in bazel-discuss list told me to use select, instead, but my attempts failed to 'detect' the operating system. I'm sure I did something wrong, but the lack of info and examples don't tell me much how to use it properly.
Solution 1:[1]
@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.
Solution 2:[2]
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 //....
Solution 3:[3]
@Vertexwahn's answer caused some confusion on my end, so I hope this answer helps clarify a bit. While his answer does not directly tie into the question, it may be of use to others trying to build on entirely different platforms without file specific inclusions.
Here is a link to where I answered that particular question: How do I specify portable build configurations for different operating systems for Bazel?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Vertexwahn |
Solution 3 |