Fix Java runtime toolchain resolution in cross-compilation scenarios#18262
Fix Java runtime toolchain resolution in cross-compilation scenarios#18262fmeum wants to merge 4 commits intobazelbuild:masterfrom
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
fa43c17 to
bfb7536
Compare
2c119b0 to
c8aa335
Compare
|
@cushon Could you review the first three commits of this PR? They implement what you described in #17085 (comment). @hvadehra Could you review the last commit as it touches the Starlark version of Please let me know if you prefer having this PR split into multiple ones. I tried to group independently reviewable changes into separate commits, but only the combination of these commits truly resolves the cross-compilation issues. FYI @illicitonion @jlaxson Please test with your setups if possible. |
|
I tested this out and it worked for me - I got a useful "no toolchain found" error when I didn't have one registered, and when I registered one, everything worked as I expected. Thanks so much! |
c8aa335 to
ee1b095
Compare
cushon
left a comment
There was a problem hiding this comment.
I'm on board with the overall approach here, thanks for tackling this.
I would like @hvadehra's input as well.
I think it probably makes sense to split some of the commits into separate PRs, maybe after everyone's taken a first pass.
|
LGTM in general. +1 to splitting this into separate commits. In particular, I think I'd like to do the changes in semantics.bzl slightly differently. But it's okay to leave as is on github, I'll handle that while importing. |
|
I will wait for bazelbuild/rules_java#110 to land before I continue working on this. |
Work towards bazelbuild#17085 Work towards bazelbuild/rules_java#64 Split off from bazelbuild#18262
The `java_binary` Starlark implementation now only directly depends on a Java runtime toolchain when it is used as an executable rule. This work will eventually allow `java_binary` with `create_executable = False` to be compiled for platforms that do not provide a regular JDK, such as Android. Work towards bazelbuild#17085 Work towards bazelbuild/rules_java#64 Split off from bazelbuild#18262
9ac40fd to
5c47637
Compare
5c47637 to
b0c94a1
Compare
I added the changes here, but I think that the failure is legitimate: According to https://github.com/bazelbuild/rules_java/blob/0c538b8720a8b8df5f50ce92debffc8b27b84af4/toolchains/BUILD#L260-L315, rules_java will use the following JDKs to compile for the different target Java versions: This is also what we are seeing in the test: @hvadehra Should we change the linked part of the I think that we can drop JDK 14, 15, and 16. We could also decide to consolidate the name to |
b0c94a1 to
e73a7cf
Compare
Good question, no idea yet, but luckily I can debug this locally :-) Edit: It is, I just don't know why yet. |
|
This is caused by the Without this line: This behavior is expected. But why does |
|
If we can't figure this out and can accept the warnings, I can revert the workaround. Unfortunately there doesn't seem to be a way to register a certain toolchain by default, but not with a particular I will look into why this doesn't behave as expected. |
|
Nvm, the RBE toolchains only include Java runtime toolchains, not Java compilation toolchains, resulting in the selection of the |
e73a7cf to
6daa568
Compare
|
@hvadehra Updated |
b0bc42b to
933da39
Compare
|
I forgot to update |
|
|
🤞 |
|
Great, I won't touch this anymore. |
|
@ahumesky Do you have any concerns about the change to |
thanks -- the dx parts are not used anymore and will be removed soon so this looks fine |
|
@bazel-io fork 7.0.0 |
The Java rules used toolchains of type
@bazel_tools//tools/jdk:runtime_toolchain_typefor 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:@local_jdkdid 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 buildingjava_binarytargets that wouldn't run on the target (see Bazel treats local_jdk as a "can run anywhere" toolchain #18265).This is resolved by adding a third toolchain type,
bootstrap_runtime_toolchain_type, that is only used by thebootclasspathrule (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:
android_libraryandjava_binarywithcreate_executable = False).bootclasspathrule have been replaced with dependencies on the bootstrap runtime.{local,remote}_java_repositorynow use the user-provided exec constraints of the Java toolchain as the target constraints of the associated runtime.@local_jdkuses 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_repositoryfrom@bazel_tools//tools/jdk:local_java_repository.bzl, which includeslocal_jdk, now havetarget_compatible_withset 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):java_binarytargets that aren't meant to be run withbazel runor as tools during the build withjava_single_jar(available in@rules_java//java:java_single_jar.bzl). Such targets do not require a Java runtime for the target configuration.--java_runtime_version=remotejdk_Nfor some Java versionNto let Bazel choose and download an appropriate remote JDK for the current target platform. This setting defaults tolocal_jdk, which means that Bazel can only use the local JDK, which isn't compatible with any other platform.local_java_runtimewith no value set forexec_compatible_with(defaults to[]) and select it by setting--java_runtime_versionto itsname. This fully restores the previous behavior, but can result in incorrect results when cross-compiling (see Bazel treats local_jdk as a "can run anywhere" toolchain #18265).