I'm working on a C++ project and I need Numpy like arrays and functionalities in C++. I found some alternatives like xtensor, NumCpp etc. These are header only libraries. The problem is I'm experimenting with Bazel for the first time so, I don't have any idea about how do I add header only library to Bazel workspace. There are some suggestions like genrule-environment, rules-foreign-cc suggested on other questions around Bazel. I've added http_archive to WORKSPACE file, but I'm not sure what to add in BUILD file.
WORKSPACE file
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()
I've tried to follow documentation, but failed every time. Some help would be appreciated.
For simple things like header-only libraries, I would write BUILD files yourself, without using rules_foreign_cc. Just write a
cc_librarywith nosrcs. Something like this:@xtensorwill be your Bazel repository. Thebuild_file_contentwill be used to create aBUILD.bazelfile in it. That means your code can depend on it via@xtensor//:xtensor, which can be shortened to just@xtensor. You can also put that in a separate file (saybuild/BUILD.xtensor.bazel) and then usebuild_file = "@build//:BUILD.xtensor.bazel"instead of putting it inline inWORKSPACEviabuild_file_content.rules_foreign_cc is going to generate something equivalent if you get it set up, but that seems like a lot more trouble to me than just writing it yourself. I set
definesanddepsbased on a scan through the CMakeLists.txt, you'll want to customize based on how you want it configured.The only other thing I see that CMakeLists doing (beyond finding dependencies and setting some
-Dflags, which translate as shown above) is generating a single file that #includes all the others. If you want to do that, I'd do something like this (in the BUILD file for@xtensor):I may not have gotten everything perfect here, but the idea is to build up the file using
catto copy stdin to it, a bash here document to feed the content in, and Python-style list comprehensions to actually build up the content.