如何在 Bazel 工作区中构建仅标头的 C++ 库?

Jem*_*ema 4 c++ bazel xtensor

我正在开发一个 C++ 项目,我需要 Numpy 之类的数组和 C++ 中的功能。我找到了一些替代方案,例如xtensorNumCpp等。这些是仅标头的库。问题是我是第一次尝试 Bazel,所以我不知道如何将仅标头库添加到 Bazel 工作区。在 Bazel 的其他问题上提出了一些建议,例如 genrule-environment、rules-foreign-cc。我已将 http_archive 添加到 WORKSPACE 文件中,但我不确定要在 BUILD 文件中添加什么。

工作空间文件

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

all_content = """filegroup(name = "all", srcs = glob(["**"]), visibility = ["//visibility:public"])"""

http_archive(
    name = "xtensor",
    build_file_content = all_content,
    strip_prefix = "xtensor-master",
    urls = ["https://github.com/xtensor-stack/xtensor/archive/refs/heads/master.zip"],
)

http_archive(
    name = "NumCpp",
    build_file_content = all_content,
    strip_prefix = "NumCpp-master",
    urls = ["https://github.com/dpilger26/NumCpp/archive/refs/heads/master.zip"],
)

http_archive(
    name = "rules_foreign_cc",
    sha256 = "c2cdcf55ffaf49366725639e45dedd449b8c3fe22b54e31625eb80ce3a240f1e",
    strip_prefix = "rules_foreign_cc-0.1.0",
    url = "https://github.com/bazelbuild/rules_foreign_cc/archive/0.1.0.zip",
)

load("@rules_foreign_cc//:workspace_definitions.bzl", "rules_foreign_cc_dependencies")

rules_foreign_cc_dependencies()
Run Code Online (Sandbox Code Playgroud)

我尝试遵循文档,但每次都失败。一些帮助将不胜感激。

Bri*_*man 5

对于诸如仅标头库之类的简单内容,我会自己编写 BUILD 文件,而不使用 Rules_foreign_cc。只需写一个cc_library不带srcs. 像这样的东西:

http_archive(
    name = "xtensor",
    build_file_content = all_content,
    strip_prefix = "xtensor-master",
    urls = ["https://github.com/xtensor-stack/xtensor/archive/refs/heads/master.zip"],
    build_file_content = """
cc_library(
    name = "xtensor",
    visibility = ["//visibility:public"],
    hdrs = glob(["xtensor/*.hpp"]),
    defines = [
        "XTENSOR_ENABLE_ASSERT",
    ],
    deps = [
        "@tbb",
    ],
)
""",
)
Run Code Online (Sandbox Code Playgroud)

@xtensor将是您的 Bazel 存储库。将用于在其中build_file_content创建一个文件。BUILD.bazel这意味着您的代码可以通过 依赖它@xtensor//:xtensor,它可以缩短为@xtensor. 您还可以将其放入单独的文件中(例如build/BUILD.xtensor.bazel),然后使用build_file = "@build//:BUILD.xtensor.bazel"而不是将其内联放入WORKSPACEvia中build_file_content

如果你设置了rules_foreign_cc,它会生成等价的东西,但这对我来说似乎比自己编写要麻烦得多。我设置definesdeps基于对 CMakeLists.txt 的扫描,您将需要根据您想要的配置方式进行自定义。

我看到 CMakeLists 所做的唯一一件事(除了查找依赖项和设置一些-D标志(如上所示进行翻译))是生成一个 #includes 所有其他文件的文件。如果你想这样做,我会做这样的事情(在 BUILD 文件中@xtensor):

genrule(
    name = "gen_single_include",
    outs = ["xtensor.hpp"],
    cmd = "\n".join([
        "cat > $@ <<'END'",
        "#ifndef XTENSOR",
        "#define XTENSOR",
    ] + ["#include \"%s\"" % h[len("xtensor/"):]
         for h in glob(["xtensor/*.hpp"])
         if h not in [
             "xtensor/xexpression_holder.hpp",
             "xtensor/xjson.hpp",
             "xtensor/xmime.hpp",
             "xtensor/xnpy.hpp",
          ]] + [
        "#endif",
        "END",
    ]),
Run Code Online (Sandbox Code Playgroud)

我可能没有在这里把一切都做到完美,但想法是使用将 stdin 复制到其中来构建文件cat,使用 bash here 文档来输入内容,并使用 Python 风格的列表理解来实际构建内容。

  • 是的,如果您想使用 tbb,您需要构建它。您也可以跳过该部分,从 CMakeLists.txt 来看,它看起来是可选的。我添加它作为如何添加依赖项的示例。如果你想使用它,有一些选项: * 为 tbb 编写一个 BUILD 文件(就像这个答案) * 在其上使用 Rules_foreign_cc 并使其工作 * 找到一个预编译的 tbb 并为其编写一个 BUILD 文件(只需将 .a 文件放入cc_library.srcs) * 找到别人的 BUILD 文件 (2认同)