如何使用select正确检测我是在Windows还是Linux中构建C ++代码? [英] How to use select to properly detect whether I am building C++ code in Windows or Linux?
问题描述
我正在编写一个示例C ++项目,该项目使用Bazel作为其他合作者遵循的示例习语.
I am writing a sample C++ project that uses Bazel to serve as an example idiom for other collaborators to follow.
这里是存储库: https://github.com/thinlizzy/bazelexample
我有兴趣知道我是否在正确"地执行此操作,更具体地说是关于此文件: https://github.com/thinlizzy/bazelexample/blob/38cc07931e58ff5a888dd6a83456970f76d7e5b3/demo/BUILD 在选择特定的实现时.
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",
],
)
这是针对Bazel项目的正确/预期惯用法吗?通过将所有特定于平台的依赖项集中在单独的目标中,然后二进制文件仅依赖于它们,我已经在其他项目中采用这种方式.
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.
在bazel-discuss列表中的某人告诉我改用 select ,但是我的尝试未能检测"操作系统.我确定我做错了什么,但是缺少信息和示例并不能告诉我如何正确使用它.
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.
推荐答案
@bazel_tools
包含预定义的平台条件:
@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
您可以直接在BUILD
文件中使用它们:
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"],
)
有关详细信息,请参见 select
的文档语法.
See the documentation for select
for details on the syntax.
这篇关于如何使用select正确检测我是在Windows还是Linux中构建C ++代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!