这是我实际 WORKSPACE 中的一个示例,使我的项目能够看到 TUT(模板单元测试),无论我是在 Windows 还是 Linux 上测试它们。
new_local_repository(
name = "win32_tut",
path = "/d/diego/progs/c++/lib/tut/",
build_file_content = """
cc_library(
name = "tut",
srcs = glob([
"tut/*.hpp",
]),
hdrs = glob([
"*.h",
]),
visibility = ["//visibility:public"],
)
""",
)
new_local_repository(
name = "linux_tut",
path = "/usr/include/",
build_file_content = """
cc_library(
name = "tut",
srcs = glob([
"tut/*.hpp",
]),
hdrs = glob([
"tut.h",
]),
visibility = ["//visibility:public"],
)
""",
)
new_local_repository(
name = "tut",
path = ".",
build_file_content = """
cc_library(
name = "tut",
deps = select({
"@bazel_tools//src/conditions:windows": ["@win32_tut//:tut"],
"//conditions:default": ["@linux_tut//:tut"],
}),
visibility = ["//visibility:public"],
)
""",
)
我所有的 cc_test 规则都成功取决于 "@tut//:tut"
。
这对我有用,但看起来我在滥用 WORKSPACE 文件。有没有更好的方法来实现这一点?
最佳答案
我觉得你的解决方案不错。
it looks like I am abusing the WORKSPACE file
或者,创建一个自定义存储库规则 detects the OS [1], creates a symlink到 d:/diego/progs/c++/lib/tut
或 /usr/include
目录,和 creates a BUILD file只有一个 cc_library,将所有文件放在符号链接(symbolic link)下。
查看 example for a custom repository .
[1] 例如repository_ctx.os.name.lower().startswith("windows")
关于c++ - 请告诉我在 Bazel 中拥有多平台 WORKSPACE 的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49722244/