Skip to content

incompatible_use_toolchain_resolution_for_java_rules: use toolchain resolution for Java rules #7849

@lberki

Description

@lberki

Flag: --incompatible_use_toolchain_resolution_for_java_rules
Available since: 5.0.0
Will be flipped in: 5.0.0
Tracking issue: #4592

Motivation

Currently, Java rules find their Java toolchain and JDK using the --javabase / --java_toolchain / --host_javabase / --host_java_toolchain command line options. This will be changed to use platform-based toolchain resolution so as to be consistent with the rest of Bazel and to support multiple platforms more easily.

Migration notes

For --javabase with old values:

  • @local_jdk://jdk
  • @remotejdk11_{linux,window,darwin}_{cpu}//:jdk
  • @remotejdk14_{linux,window,darwin}//:jdk
  • @remotejdk15_{linux,window,darwin}.*//:jdk

Replace the flag with --java_runtime_version={local_jdk,remotejdk_14,remotejdk_15}.

For --java_toolchain with old values:

  • @bazel_tools//tools/jdk:toolchain,
  • @bazel_tools_hostjdk8,
  • @bazel_tools//jdk:legacy_toolchain,
  • @bazel_tools//tools/jdk:remote_toolchain,
  • @bazel_tools//tools/jdk:toolchain_java_{ver},
  • @remote_java_tools_xxx//:toolchain,
  • @remote_java_tools_xxx//:toolchain_jdk_11,
  • @remote_java_tools_xxx//:toolchain_jdk_14,
  • @remote_java_tools_xxx//:toolchain_jdk_15

Replace the flag with --java_language_version={8,...,15}

Migration of more advanced cases

For --javabase=@bazel_tools//tools/jdk:​absolute_javabase, use local_java_repositoy in the WORKSPACE file.

For custom --javabase labels, do the following:

  1. replace http_archive with remote_java_repository:
remote_java_repository(
   name = ...
   sha256 = ...
   strip_prefix = ...
   urls = ...
   prefix = "myjdk",
   version = "11",
   exec_compatible_with = ["@platforms//cpu:arm", "@platforms//os:linux"]
)
  1. replace the old flag with --java_runtime_version with the specified version or prefix_version value (for example --java_runtime_version=myjdk_11).

For old --java_toolchain values:

  • @bazel_tools//tools/jdk:toolchain_vanilla
  • @remote_java_tools_xxx//:prebuilt_toolchain
  • custom label
  1. add custom toolchain definition to a BUILD file (or replace custom target):
default_java_toolchain(
  name = "mytoolchain",
  configuration = "PREBUILT_TOOLCHAIN_CONFIGURATION"
     #or "VANILLA_TOOLCHAIN_CONFIGURATION"
  ...
)
  1. register custom toolchain in the WORKSPACE or use configuration flag --extra_toolchains.

RBE migration

  1. Update the version of bazel_toolchains.
  2. Add following flags to .bazelrc affecting remote configuration:
build:remote --java_runtime_version=rbe_jdk     # Uses JDK installed on docker, configured by bazel_toolchains
build:remote --tool_java_runtime_version=rbe_jdk
build:remote --extra_toolchains=@rbe_ubuntu1804//java:all   # Optional: uses JDK installed on docker to compile 
  1. In case the sources are not Java 8, also add:
build --java_language_version=11
build --tool_java_language_version=11
  1. Once Bazel 4.1.0 is released and used on RBE Remove --{,host}javabase and --{,host}_javatoolchain flags.

Metadata

Metadata

Assignees

Labels

P1I'll work on this now. (Assignee required)area-java-toolchainsjavabase, java_toolchain flags, JDK selection, java_toolchain rules, java_tools repositoryincompatible-changeIncompatible/breaking changeteam-Rules-JavaIssues for Java rulestype: process

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions