bazelbuild / examples

Examples for Bazel
http://bazel.build
Apache License 2.0
800 stars 497 forks source link

Error No matching toolchains found for types @@bazel_tools//tools/jdk:runtime_toolchain_type. #400

Open sgowroji opened 5 months ago

sgowroji commented 5 months ago

CI: https://buildkite.com/bazel/bazel-at-head-plus-downstream/builds/3599#018d1aba-719c-4797-b00a-1d83dfafff2b https://buildkite.com/bazel/bazel-at-head-plus-downstream/builds/3608#018d2ef4-1d2c-446b-a314-c133272f58d9

Platform: Ubuntu

Logs:

To debug, rerun with --toolchain_resolution_debug='@@bazel_tools//tools/jdk:runtime_toolchain_type'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
(04:01:26) ERROR: Analysis of target '//app/src/main:app' failed; build aborted: Analysis failed
(04:01:26) INFO: Elapsed time: 3.047s, Critical Path: 0.02s
(04:01:26) INFO: 1 process: 1 internal.
(04:01:26) ERROR: Build did NOT complete successfully
(04:01:26) FAILED:`
Traceback (most recent call last):
    File "/virtual_builtins_bzl/common/cc/cc_toolchain_alias.bzl", line 26, column 48, in _impl
    File "/virtual_builtins_bzl/common/cc/cc_helper.bzl", line 217, column 13, in _find_cpp_toolchain
Error in fail: Unable to find a CC toolchain using toolchain resolution. Target: @@bazel_tools//tools/cpp:current_cc_toolchain, Platform: @@//:arm64-v8a, Exec platform: @@local_config_platform//:host
(02:20:31) ERROR: /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/051ad6a69d7a84f28cf16b88c532f996/external/bazel_tools/tools/cpp/BUILD:58:19: Analysis of target '@@bazel_tools//tools/cpp:current_cc_toolchain' failed
(02:20:31) ERROR: Analysis of target '//app/src/main:app' failed; build aborted: Analysis failed

Culprit: https://github.com/bazelbuild/bazel/commit/03490b9f72dc479c979afff6b1731a72787cba8b, https://github.com/bazelbuild/bazel/commit/8ba66188e378fe3df5db9549b3b9661d62cf93bb Steps:

git clone -v https://github.com/bazelbuild/examples.git
git reset d18ce42623f136616e9512d713a47c33f9ad6a58 --hard
export USE_BAZEL_VERSION=3e7052824b54158d72e4b07d6330aa41a9857707
cd /android/jetpack-compose
bazel build --android_platforms=//:arm64-v8a --remote_download_outputs=all --enable_bzlmod //app/src/main:app 

CC Greenteam @meteorcloudy

meteorcloudy commented 5 months ago

@katre Is https://github.com/bazelbuild/examples/pull/398 related to this?

katre commented 5 months ago

I'll take a look today

katre commented 5 months ago

There are two problems, lack of a CC toolchain and lack of a runtime Java toolchain

  1. There's no android_ndk_repository, which means there are no Android CC toolchains available. Possibly this worked in the past but currently every Android project needs CC toolchains
    1. The version of Android rules in use is v0.1.1, which is from 2018, and so is slightly out of date with Bazel at head.

I can create a PR to fix these issues.

I'm not sure when the requirement for a runtime Java toolchain was added, and I'm not sure where that should even come from. (pinging @ahumesky for advice).

ahumesky commented 5 months ago

v0.1.1 of rules_android is just thin wrappers around the native android rules, so it's possible that something elsewhere within bazel changed to require this --

This is where we get the java toolchains in the starlark rules: https://github.com/bazelbuild/rules_android/blob/5f6cc061249f8ea750f221da1ee0916cba61563f/MODULE.bazel#L8-L11

katre commented 5 months ago

Okay, debugged a bit further: rules_java adds runtime toolchains for various processors, including aarch64, but not for the Android OS constraint, so those aren't used. (These are created from the remote JDKs defined in rules_java/MODULE.bazel, where the target constraints are determined based on the actual name. There are no JDKs loaded for "android_aarch64", so none exist).

The JDK toolchain (not runtime) is fine, because it doesn't define any target constraints (cpu or os) (see https://github.com/bazelbuild/rules_java/blob/9cd25f0f7edc5184c4f1a10c4772ac4127f7322e/toolchains/default_java_toolchain.bzl#L181).

@ahumesky: Does the Android SDK contain a JDK for the different Android cpu flavors (in which case we just need to add the relevant toolchain configs), or do we need to download and configure a new one?

cushon commented 5 months ago

Does the Android SDK contain a JDK for the different Android cpu flavors

I'm not sure that's what we want? There aren't JDKs for Android cpu flavors. I wonder if that what we want here is to be able to run things like robolectric tests, which exercise code built targeting a particular Android flavour on a JVM.

The approach we take for that in google3 is:

toolchain(
    name = "jdk-runtime-fake-android",
    target_compatible_with = [
        "//third_party/bazel_platforms/os:android",
    ],
    toolchain = ... # A JDK for the platform that the robolectric test executes on, e.g. linux k8
    toolchain_type = ":runtime_toolchain_type",
)
katre commented 5 months ago

That's a great question: all I know is "bazel thinks it wants a runtime JDK that targets android_aarch64", I haven't dug into why it wants that.