Bazel WORKSPACE中的有条件new_local_library

时间:2019-12-09 01:35:05

标签: bazel

我有一些预先构建的第三方库,必须在Bazel构建中包括这些库。所以我的WORKSPACE中有这样的内容:

new_local_repository(
   name = "thirdparty",
   path = ".install/OSX/DEBUG",
   build_file = "bazel/BUILD.third_party",
)

BUILD.third_party如下:

cc_library(
    name = "glog",
    srcs = ["glog/0.3.5.r1/lib/libglog.a"],
    hdrs = glob([
       "glog/0.3.5.r1/include/**"
    ]),
   visibility = ["//visibility:public"],
   includes = [
      "glog/0.3.5.r1/include"
   ]
)

问题是OSX/DEBUG规则中path的{​​{1}}部分需要取决于当前平台和new_local_repository(例如,我想链接到compilation_mode的发布版本(如果glog)。但是,据我了解,compilation_mode == opt规则必须在加载阶段进行评估,并且此时命令行选项和其他配置信息尚不可用。同样,new_local_repository文件中没有select表达式。有什么办法吗?

编辑

我找到了一种应该的工作方式,但是Bazel中似乎有一个错误。这是我的错误报告:https://github.com/bazelbuild/bazel/issues/10400

1 个答案:

答案 0 :(得分:1)

您可以通过将其翻转来做到这一点:编写单独的new_local_repository规则和使用它们的select

例如,WORKSPACE中的两个存储库具有相同的build_file

new_local_repository(
   name = "thirdparty_debug",
   path = ".install/OSX/DEBUG",
   build_file = "bazel/BUILD.third_party",
)
new_local_repository(
   name = "thirdparty_release",
   path = ".install/OSX/RELEASE",
   build_file = "bazel/BUILD.third_party",
)

然后,使用一个附加的“转发”库,该库取决于相应存储库中的相关库。像这样:

cc_library(
    name = "glog",
    deps = select({
        "a_config_setting_for_opt": ["@thirdparty_release//:glog"],
        "//conditions:default": ["@thirdparty_debug//:glog"],
    }),
    visibility = ["//visibility:public"],
)

BUILD.third_party可能与您的示例相同。也许值得将可见性限制为仅转发目标的程序包,以避免意外的直接依赖。