在 Bazel 项目中构建 CMake 库

Building a CMake library within a Bazel project

我在使用 nanomsg 的 TensorFlow 私有分支之上编写了一个模块。

对于我的本地开发服务器,我使用 cmake install 安装 nanomsg(到 /usr/local)并从安装位置访问头文件。该项目 运行 在当地没问题。

但是,我现在需要将 nanomsg 打包到我的 TensorFlow 工作区中。我尝试了以下两种方法,但都不满意:

  1. 类似于 for OpenCV, I precompiled nanomsg into a private repository, loaded it within my workspace (within tensorflow/workspace.bzl) using an http_archive directive然后在相关构建脚本中包含头文件和库。这 运行 很好,但不是可移植的解决方案。

  2. 一个更便携的解决方案,我创建了一个 genrule 到 运行 特定序列的 cmake 命令,可用于构建 nanomsg。这种方法更简洁,但 genrule 不能重复用于 cmake 其他项目。 (我提到了this discussion)。

显然 cmake 不支持作为 Bazel 构建中的第一个 class 公民。有没有人在你自己的项目中遇到过这个问题,创建了一种通用的、可移植的方式来将库包含在使用 cmake 构建的 Bazel 项目中?如果是,您是如何处理的?

正如 Ulf 所写,我认为您建议的选项 2 应该可以正常工作。

关于 "can I identify if the cmake fails",是的:cmake 失败时应该 return 带有错误退出代码 (!= 0)。这反过来会导致 Bazel 自动将 genrule 操作识别为失败,从而导致构建失败。因为 Bazel 在 运行 你的命令之前设置了 "set -e -o pipefail"(参见 https://docs.bazel.build/versions/master/be/general.html#genrule-environment),如果你在你的 genrule "cmd".

中链接多个 cmake 命令,它也应该工作

如果您在 "cmd" 属性中调用 shell 脚本,然后实际运行 cmake 命令,请确保自己将 "set -e -o pipefail" 放在脚本的第一行.否则当cmake失败时脚本不会失败。

如果我误解了你的问题 "Can I identify if the cmake fails",请告诉我。 :)

这个新项目:https://github.com/bazelbuild/rules_foreign_cc 似乎是一个解决方案(它为 cmake 构建规则以在 bazel 中构建您的项目)。