bazelbuild / rules_cc

C++ Rules for Bazel
https://bazel.build
Apache License 2.0
186 stars 93 forks source link

find_cc_toolchain fails with --incompatible_enable_cc_toolchain_resolution #74

Closed sAlexander closed 10 months ago

sAlexander commented 4 years ago

Description of the problem / feature request:

Summary: find_cc_toolchain fails with --incompatible_enable_cc_toolchain_resolution

The find_cc_toolchain documentation indicates that if you're using --incompatible_enable_cc_toolchain_resolution, you must declare a dependency on the cc toolchain. It even provides a nice code snippet example (thank you!):

When https://github.com/bazelbuild/bazel/issues/7260 is flipped, current C++ toolchain is selected using the toolchain resolution mechanism (--platforms). For that to work the rule needs to declare a dependency on C++ toolchain type: foo = rule( implementation = _foo_impl, toolchains = [ "@rules_cc//cc:toolchain_type", # copybara-use-repo-external-label ], )

However, the provided code does not appear to actually work when using incompatible_enable_cc_toolchain_resolution. Instead, we receive the error In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

The simplest example is following the exact instructions in the documentation:

WORKSPACE #Empty file

foo.bzl

load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")

def _foo_impl(ctx):
    toolchain = find_cc_toolchain(ctx)
    print(toolchain)

foo = rule(
    implementation = _foo_impl,
    attrs = {
        "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
    },
    toolchains = [
        "@rules_cc//cc:toolchain_type",
    ],
)

BUILD

load("//:foo.bzl", "foo")

foo(
    name = "fails",
)

To reproduce the failure, you can use:

bazel build --incompatible_enable_cc_toolchain_resolution :fails

Instead of the expected print output, you get an error with In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.

You can confirm that thing work as expected when not using the incompatible_enable_cc_toolchain_resolution flag.

What operating system are you running Bazel on?

Linux, Ubuntu

What's the output of bazel info release?

release 3.1.0

What version of rules_cc do you use? Can you paste the workspace rule used to fetch rules_cc? What other relevant dependencies does your project have?

I have not depended on rules_cc in any special way; it appears to be unnecessary to include a dependency in my WORKSPACE files in order to use rules_cc.

Out of curiosity, I also imported the master branch as of this morning with the following in my WORKSPACE files, and the failure with the simple repro above still occurred:

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

http_archive(
    name = "rules_cc",
    strip_prefix="rules_cc-master",
    urls = ["https://github.com/bazelbuild/rules_cc/archive/master.zip"],
    sha256 = "a07fc8b90c22e42962142504438a84b31567976adcd9ee1a4c9f75d2e3396f8d",
)

What Bazel options do you use to trigger the issue? What C++ toolchain do you use?

bazel build --incompatible_enable_cc_toolchain_resolution :fails

Have you found anything relevant by searching the web?

I was not able to find any relevant info from the web. The issue for toolchain resolution (https://github.com/bazelbuild/bazel/issues/7260) mentions find_cc_toolchain as though it works, and the find_cc_toolchain even provides a nice code example. Looking at the code for find_cc_toolchain, everything looks normal, though there is "do not use" appended to "is_cc_toolchain_resolution_enabled_do_not_use", which is a bit suspicious.

sAlexander commented 4 years ago

As a workaround, using toolchain = ctx.toolchains["@rules_cc//cc:toolchain_type"] directly in _foo_impl works fine. But that won't work if incompatible_enable_cc_toolchain_resolution is omitted. And it's obviously not a proper fix.

nacl commented 3 years ago

It looks like this may have been fixed by https://github.com/bazelbuild/rules_cc/commit/88ef31b429631b787ceb5e4556d773b20ad797c8.

Tried out latest HEAD (https://github.com/bazelbuild/rules_cc/commit/c612c9581b9e740a49ed4c006edb93912c8ab205) locally, and I was able to getfind_cc_toolchain working with and without --incompatible_enable_cc_toolchain_resolution.

comius commented 10 months ago

Stale