Skip to content

Commit

Permalink
Fix Java runtime toolchain resolution in cross-compilation scenarios
Browse files Browse the repository at this point in the history
The Java rules used toolchains of type `@bazel_tools//tools/jdk:runtime_toolchain_type` for two different purposes requiring different exec/target platform constraints, which led to issues when cross-compiling: The runtime added to the runfiles of an executable Java target has to have constraints on the target platform, whereas the runtime used to extract the bootclasspath should not have constraints on the target platform. As a result:
1. `@local_jdk` did not define any target constraints for its runtime, which allowed the runtime to provide the bootclasspath required by Android rules, but could also end up building `java_binary` targets that wouldn't run on the target (see #18265).
2. Remote JDKs defined a target constraint for the their runtimes, which prevented them from being added to the runfiles of targets they couldn't run on, but broke Android compilation due to the failure to resolve a runtime for bootclasspath extraction (see #17085).

This is resolved by adding a third toolchain type, `bootstrap_runtime_toolchain_type`, that is only used by the `bootclasspath` rule (realizes #17085 (comment)). Detailed explanations of the different types and their intended constraints have been added to `@bazel_tools//tools/jdk:BUILD`.

Addressing the cross-compilation errors then required the following changes:
1. Direct dependencies on the Java runtime have been removed from rules that do not need them (e.g. `android_library` and `java_binary` with `create_executable = False`).
3. Implicit dependencies on the Java runtime through the `bootclasspath` rule have been replaced with dependencies on the bootstrap runtime.
4. `{local,remote}_java_repository` now use the user-provided exec constraints of the Java toolchain as the target constraints of the associated runtime.
5. `@local_jdk` uses the auto-detected host constraints as exec constraints for the local Java toolchain.

Fixes #17085
Fixes #18265
Fixes bazelbuild/rules_java#64

RELNOTES[INC]: Java runtime toolchains created via `local_java_repository` from `@bazel_tools//tools/jdk:local_java_repository.bzl`, which includes `local_jdk`, now have `target_compatible_with` set to the auto-detected host constraints. This can result in errors about toolchain resolution failures for `@bazel_tools//tools/jdk:runtime_toolchain_type`, especially when cross-compiling. These failures can be fixed in the following ways (listed in decreasing order of preference):
* Replace `java_binary` targets that aren't meant to be run with `bazel run` or as tools during the build with `java_single_jar` (available in `@rules_java//java:java_single_jar.bzl`). Such targets do not require a Java runtime for the target configuration.
* Set `--java_runtime_version=remotejdk_N` for some Java version `N` to let Bazel choose and download an appropriate remote JDK for the current target platform. This setting defaults to `local_jdk`, which means that Bazel can only use the local JDK, which isn't compatible with any other platform.
* Manually define and register a `local_java_runtime` with no value set for `exec_compatible_with` (defaults to `[]`) and select it by setting `--java_runtime_version` to its `name`. This fully restores the previous behavior, but can result in incorrect results when cross-compiling (see #18265).

Closes #18262.

PiperOrigin-RevId: 574914446
Change-Id: I6cbfb7ffa2fbfd62e5f6fb49532b36be658dfa40
  • Loading branch information
fmeum authored and copybara-github committed Oct 19, 2023
1 parent 844c1d6 commit f79ca02
Show file tree
Hide file tree
Showing 17 changed files with 785 additions and 618 deletions.
6 changes: 6 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ build:remote_shared --tool_java_runtime_version=rbe_jdk
build:remote_shared --noexperimental_check_desugar_deps

# Configuration to build and test Bazel on RBE on Ubuntu 18.04 with Java 11
# Workaround for https://github.com/bazelbuild/bazel/issues/19837.
# TODO(bazel-team): Remove this toolchain when .bazelversion is 7.0.0rc2 or later.
build:ubuntu2004_java11 --extra_toolchains=//:bazel_rbe_java_toolchain_definition
build:ubuntu2004_java11 --extra_toolchains=@rbe_ubuntu2004_java11//java:all
build:ubuntu2004_java11 --crosstool_top=@rbe_ubuntu2004_java11//cc:toolchain
build:ubuntu2004_java11 --extra_toolchains=@rbe_ubuntu2004_java11//config:cc-toolchain
Expand All @@ -34,6 +37,9 @@ build:windows --extra_toolchains=@bazel_tools//tools/python:autodetecting_toolch

build:windows_arm64 --platforms=//:windows_arm64
build:windows_arm64 --extra_toolchains=@local_config_cc//:cc-toolchain-arm64_windows
# When cross-compiling, the local Java runtime, which is used by default, will not be compatible
# with the target.
build:windows_arm64 --java_runtime_version=remotejdk_11

# Enable Bzlmod
common:bzlmod --enable_bzlmod
Expand Down
17 changes: 17 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Bazel - Google's Build System

load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_java//toolchains:default_java_toolchain.bzl", "default_java_toolchain")
load("@rules_license//rules:license.bzl", "license")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("@rules_python//python:defs.bzl", "py_binary")
Expand Down Expand Up @@ -293,3 +294,19 @@ REMOTE_PLATFORMS = ("rbe_ubuntu2004_java11",)
)
for platform_name in REMOTE_PLATFORMS
]

# Workaround for https://github.com/bazelbuild/bazel/issues/19837.
# TODO(bazel-team): Remove these two targets when .bazelversion is 7.0.0rc2 or later.
default_java_toolchain(
name = "bazel_java_toolchain",
bootclasspath = ["@rules_java//toolchains:platformclasspath"],
source_version = "11",
tags = ["manual"],
target_version = "11",
)

default_java_toolchain(
name = "bazel_rbe_java_toolchain",
source_version = "11",
target_version = "11",
)
14 changes: 9 additions & 5 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bazel_dep(name = "zstd-jni", version = "1.5.2-3.bcr.1")
bazel_dep(name = "blake3", version = "1.3.3.bcr.1")
bazel_dep(name = "zlib", version = "1.3")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_java", version = "6.3.1")
bazel_dep(name = "rules_java", version = "7.0.6")
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
bazel_dep(name = "rules_jvm_external", version = "5.2")
bazel_dep(name = "rules_python", version = "0.24.0")
Expand Down Expand Up @@ -231,10 +231,10 @@ use_repo(
"remotejdk17_macos_aarch64",
"remotejdk17_win",
"remotejdk17_win_arm64",
"remotejdk20_linux",
"remotejdk20_macos",
"remotejdk20_macos_aarch64",
"remotejdk20_win",
"remotejdk21_linux",
"remotejdk21_macos",
"remotejdk21_macos_aarch64",
"remotejdk21_win",
)

# =========================================
Expand Down Expand Up @@ -309,6 +309,10 @@ register_toolchains("@local_config_winsdk//:all")

register_toolchains("//src/main/res:empty_rc_toolchain")

# Workaround for https://github.com/bazelbuild/bazel/issues/19837.
# TODO(bazel-team): Remove when .bazelversion is 7.0.0rc2 or later.
register_toolchains("//:bazel_java_toolchain_definition")

# =========================================
# Android tools dependencies
# =========================================
Expand Down
Loading

0 comments on commit f79ca02

Please sign in to comment.