c++ - 请告诉我在 Bazel 中拥有多平台 WORKSPACE 的更好方法

标签 c++ bazel

这是我实际 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 symlinkd:/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/

相关文章:

c++ - Googletest 参数化测试崩溃

c++ - QThreads 中断槽和信号

C++ 结构初始化断言失败

c++ - 首次执行时无法运行已编译的C++程序

azure - 如何在Azure Pipelines中使用Bazel?

C++ shared_ptr 释放顺序

Erlang Hello World 使用 Bazel 作为构建系统

c++ - Bazel:为 cc_binary/cc_test 设置运行时环境变量和配置文件位置

go - 无法打开应该位于基本路径中的文件

Tensorflow 构建量化工具 - bazel 构建错误