0

I am trying to build a rust crate that uses 2 crates that rely on c libraries using Bazel. The crates are rdkafka and paho-mqttt. I am using Bazel rust_rules, especially the crate_universe rules to build the crate. This is the dependencies section of the Cargo.toml of my crate:

[dependencies]
serde = { version = "1.0.163", features = ["derive"] }
chrono = { version = "0.4.26", features = ["serde"] }
uuid = { version = "1.3.3", features = ["v7", "serde"] }
rdkafka = { version = "0.31", features = ["cmake-build"] }
paho-mqtt = { version = "0.11" }

And this is the relevant bazel WORKSPACE file (crate_repository definition) and the Bazel BUILD file for my crate:

crates_repository(
    name = "crate_index",
    cargo_lockfile = "//my-crate:Cargo.lock",
    lockfile = "//my-crate:Cargo.Bazel.lock",
    manifests = ["//my-crate:Cargo.toml"],
    annotations = {
        "uuid": [crate.annotation(
            rustc_flags = ["--cfg=uuid_unstable"],
        )],
    },
)

load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()
load("@crate_index//:defs.bzl", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_library")

rust_library(
    name = "my-crate",
    srcs = glob(["src/**/*.rs"]),
    deps = all_crate_deps(
        normal = True,
    ),
)

When I run bazel build //my-crate I get the following errors:

--stderr:
Building and linking librdkafka statically
CMake Error: Could not find CMAKE_ROOT !!!
CMake has most likely not been installed correctly.
Modules directory not found in

CMake Error: Error executing cmake::LoadCache(). Aborting.

thread 'main' panicked at '
command did not execute successfully, got: exit status: 1

This error is reported by both crates (rdkafka and paho-mqtt) as I have tried to remove the feature 'build-cmake' from rdkafka to see if that was the unique library that reported the issue, so I guess that the error would appear in all crates that relies on a C library and on cmake to build and link that library.

What I have tried

I have tried to build the crate by using cargo build and it works, so I guess that the error is not related to the installation of my system cmake. I tried to define the CMAKE_ROOT and CMAKE_MODULE_PATH using export CMAKE_ROOT=/path and also running bazel with --action_env=CMAKE_ROOT=/path and use_default_shell_env=True but with no luck, it gives the same error.

What may work

I got some suggestions in the Bazel slack (thanks Brian Myers) about using bazel to not only compile rdkafka but also for compiling librdkafka but I would like to understand why Bazel is reporting the cmake error and how could I fix it to simplify my bazel configuration until I feel more comfortable using Bazel.

Additional information

  • OS: Arch
  • Bazel version: 6.2.1
  • CMake version: 3.26.4
  • Rust version and the definition of the Bazel toolchain:
rust_register_toolchains(
    edition = "2021",
    versions = [
        "1.70.0"
    ],
)

As I mentioned, why does Bazel report the error? How could I solve the cmake error to avoid compiling librdkafka on Bazel?

David
  • 55
  • 6

0 Answers0