Skip to content

Commit f79ca02

Browse files
fmeumcopybara-github
authored andcommitted
Fix Java runtime toolchain resolution in cross-compilation scenarios
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
1 parent 844c1d6 commit f79ca02

17 files changed

+785
-618
lines changed

.bazelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ build:remote_shared --tool_java_runtime_version=rbe_jdk
1414
build:remote_shared --noexperimental_check_desugar_deps
1515

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

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

3844
# Enable Bzlmod
3945
common:bzlmod --enable_bzlmod

BUILD

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Bazel - Google's Build System
22

33
load("@bazel_skylib//rules:write_file.bzl", "write_file")
4+
load("@rules_java//toolchains:default_java_toolchain.bzl", "default_java_toolchain")
45
load("@rules_license//rules:license.bzl", "license")
56
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
67
load("@rules_python//python:defs.bzl", "py_binary")
@@ -293,3 +294,19 @@ REMOTE_PLATFORMS = ("rbe_ubuntu2004_java11",)
293294
)
294295
for platform_name in REMOTE_PLATFORMS
295296
]
297+
298+
# Workaround for https://github.com/bazelbuild/bazel/issues/19837.
299+
# TODO(bazel-team): Remove these two targets when .bazelversion is 7.0.0rc2 or later.
300+
default_java_toolchain(
301+
name = "bazel_java_toolchain",
302+
bootclasspath = ["@rules_java//toolchains:platformclasspath"],
303+
source_version = "11",
304+
tags = ["manual"],
305+
target_version = "11",
306+
)
307+
308+
default_java_toolchain(
309+
name = "bazel_rbe_java_toolchain",
310+
source_version = "11",
311+
target_version = "11",
312+
)

MODULE.bazel

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ bazel_dep(name = "zstd-jni", version = "1.5.2-3.bcr.1")
2424
bazel_dep(name = "blake3", version = "1.3.3.bcr.1")
2525
bazel_dep(name = "zlib", version = "1.3")
2626
bazel_dep(name = "rules_cc", version = "0.0.9")
27-
bazel_dep(name = "rules_java", version = "6.3.1")
27+
bazel_dep(name = "rules_java", version = "7.0.6")
2828
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
2929
bazel_dep(name = "rules_jvm_external", version = "5.2")
3030
bazel_dep(name = "rules_python", version = "0.24.0")
@@ -231,10 +231,10 @@ use_repo(
231231
"remotejdk17_macos_aarch64",
232232
"remotejdk17_win",
233233
"remotejdk17_win_arm64",
234-
"remotejdk20_linux",
235-
"remotejdk20_macos",
236-
"remotejdk20_macos_aarch64",
237-
"remotejdk20_win",
234+
"remotejdk21_linux",
235+
"remotejdk21_macos",
236+
"remotejdk21_macos_aarch64",
237+
"remotejdk21_win",
238238
)
239239

240240
# =========================================
@@ -309,6 +309,10 @@ register_toolchains("@local_config_winsdk//:all")
309309

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

312+
# Workaround for https://github.com/bazelbuild/bazel/issues/19837.
313+
# TODO(bazel-team): Remove when .bazelversion is 7.0.0rc2 or later.
314+
register_toolchains("//:bazel_java_toolchain_definition")
315+
312316
# =========================================
313317
# Android tools dependencies
314318
# =========================================

0 commit comments

Comments
 (0)