Search code examples
bazel

Bazel failes with "your rule has to depend on C++ toolchain" when including supplier files


We are currently moving our code into a Monorepo and I am trying to integrate a component that heavily builds on source files we get from a supplier. We used to build this component with CMake, but in the Monorepo we want to use Bazel of course (version 7.0.0, using WORKSPACE).

The supplier provides us Bazel files to build the project, and I managed to make the component build. However now other components don't build with this error message:

    INFO: Invocation ID: 5a9b30d2-8184-4571-a1f2-23a549e47de1
ERROR: ~/.cache/bazel/_bazel_my-name-redacted/d94aa72c6ec7790234ff351e3e09c8d7/external/ros2_rcl_interfaces/BUILD.bazel:25:23: in @@com_github_mvukov_rules_ros2//ros2:interfaces.bzl%idl_adapter_aspect,@@com_github_mvukov_rules_ros2//ros2:interfaces.bzl%idl_generator_type_description_aspect,@@com_github_mvukov_rules_ros2//ros2:interfaces.bzl%c_generator_aspect aspect on _ros2_interface_library rule @@ros2_rcl_interfaces//:builtin_interfaces: 
Traceback (most recent call last):
        File "~/.cache/bazel/_bazel_my-name-redacted/d94aa72c6ec7790234ff351e3e09c8d7/external/com_github_mvukov_rules_ros2/ros2/interfaces.bzl", line 514, column 50, in _c_generator_aspect_impl
                compilation_info = _compile_cc_generated_code(
        File "~/.cache/bazel/_bazel_my-name-redacted/d94aa72c6ec7790234ff351e3e09c8d7/external/com_github_mvukov_rules_ros2/ros2/interfaces.bzl", line 392, column 38, in _compile_cc_generated_code
                cc_toolchain = find_cpp_toolchain(ctx)
        File "~/.cache/bazel/_bazel_my-name-redacted/d94aa72c6ec7790234ff351e3e09c8d7/external/rules_cc/cc/toolchain_utils.bzl", line 31, column 29, in find_cpp_toolchain
                return find_cc_toolchain(ctx)
        File "~/.cache/bazel/_bazel_my-name-redacted/d94aa72c6ec7790234ff351e3e09c8d7/external/rules_cc/cc/find_cc_toolchain.bzl", line 68, column 13, in find_cc_toolchain
                fail("In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.")
Error in fail: In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.
ERROR: Analysis of target '@@ros2_rcl_interfaces//:builtin_interfaces' failed
ERROR: Analysis of target '//components/examples/model-component/src/model_component:model_component' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.164s, Critical Path: 0.01s
INFO: 1 process: 1 internal.

I could track the error down so far to this specific .bzl file from the supplier. If I comment the line protobuf_deps() the other components build again, but of course my component does not build anymore:

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
load("@rules_proto_grpc//:repositories.bzl", "rules_proto_grpc_repos", "rules_proto_grpc_toolchains")
load("@rules_proto_grpc//cpp:repositories.bzl", rules_proto_grpc_cpp_repos = "cpp_repos")

def supplier_extra_deps():
    protobuf_deps()

    rules_proto_grpc_toolchains()
    rules_proto_grpc_repos()
    rules_proto_grpc_cpp_repos()

The commit we are referencing from the Protobuf repo is b2b7a51. I am stuck and need help.


Solution

  • I suspect you're using one of the old rules_cc versions that half-supported the new-style toolchain resolution. Try using the newest release instead by adding this at the top of your WORKSPACE (from the latest release's notes:

    load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
    
    http_archive(
        name = "rules_cc",
        urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.9/rules_cc-0.0.9.tar.gz"],
        sha256 = "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf",
        strip_prefix = "rules_cc-0.0.9",
    )
    

    Note that it needs to be at the top. The convention with WORKSPACE repository rules is each one skips adding a repository if it already exists, so effectively the first one wins (which is not always what you want, but it is easy to override). bzlmod has a more helpful system, but if you're seeing this error then I'm guessing you're not using that yet.