Stop using --release in versioned java_toolchains
Using --release to target versions that support modules and are not the
latest supported version (e.g. --release 9 on JDK 10) doesn't work:
https://bugs.openjdk.java.net/browse/JDK-8209865
Related: #5723
Closes #5982.
PiperOrigin-RevId: 210646978
diff --git a/src/java_tools/buildjar/BUILD b/src/java_tools/buildjar/BUILD
index 2626c51..0f40201 100644
--- a/src/java_tools/buildjar/BUILD
+++ b/src/java_tools/buildjar/BUILD
@@ -77,7 +77,7 @@
# class path after -bootclasspath. For convenience, we currently have a
# single jar that contains the contents of both the bootclasspath and
# extdirs.
- bootclasspath = ["//tools/jdk:platformclasspath.jar"],
+ bootclasspath = ["//tools/jdk:platformclasspath8.jar"],
extclasspath = [],
genclass = ["bootstrap_genclass_deploy.jar"],
ijar = ["//third_party/ijar"],
diff --git a/tools/android/BUILD.tools b/tools/android/BUILD.tools
index 41961f4..bedb6cd 100644
--- a/tools/android/BUILD.tools
+++ b/tools/android/BUILD.tools
@@ -100,7 +100,7 @@
$(location %s) \
--exclude_build_data \
--dont_change_compression \
- --sources $(location @bazel_tools//tools/jdk:platformclasspath) \
+ --sources $(location @bazel_tools//tools/jdk:platformclasspath8) \
--include_prefixes "java/lang/invoke/" \
--include_prefixes "java/lang/annotation/" \
--output $@
@@ -111,7 +111,7 @@
genrule(
name = "gen_java_lang_extras_jar",
srcs = [
- "@bazel_tools//tools/jdk:platformclasspath"
+ "@bazel_tools//tools/jdk:platformclasspath8"
],
tools = select({
"//src/conditions:windows": [":singlejar_javabin"],
@@ -176,7 +176,7 @@
$(location :desugar_java8) \
--input $< \
--output $@ \
- --classpath_entry "$(location @bazel_tools//tools/jdk:platformclasspath)" \
+ --classpath_entry "$(location @bazel_tools//tools/jdk:platformclasspath8)" \
--core_library --allow_empty_bootclasspath \
--nobest_effort_tolerate_missing_deps \
--noemit_dependency_metadata_as_needed \
@@ -213,7 +213,7 @@
--dont_rewrite_core_library_invocation "java/util/Iterator#remove" """,
tools = [
":desugar_java8",
- "@bazel_tools//tools/jdk:platformclasspath",
+ "@bazel_tools//tools/jdk:platformclasspath8",
],
visibility = ["//visibility:private"],
)
diff --git a/tools/jdk/BUILD b/tools/jdk/BUILD
index 04eaee3..a42f165 100644
--- a/tools/jdk/BUILD
+++ b/tools/jdk/BUILD
@@ -181,11 +181,17 @@
actual = "@embedded_jdk//:jdk",
)
-genrule(
- name = "platformclasspath",
- srcs = ["DumpPlatformClassPath.java"],
- outs = ["platformclasspath.jar"],
- cmd = """
+RELEASES = (8, 9, 10)
+
+# Create jars containing compile-time bootclasspaths for each Java version
+# in RELEASES, using javac to read those APIs via the infrastructure added
+# for the --release flag (see http://openjdk.java.net/jeps/247).
+[
+ genrule(
+ name = "platformclasspath%d" % release,
+ srcs = ["DumpPlatformClassPath.java"],
+ outs = ["platformclasspath%d.jar" % release],
+ cmd = """
set -eu
TMPDIR=$$(mktemp -d -t tmp.XXXXXXXX)
$(JAVABASE)/bin/javac -source 8 -target 8 \
@@ -193,16 +199,18 @@
-d $$TMPDIR $<
$(JAVA) -XX:+IgnoreUnrecognizedVMOptions \
--add-exports=jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED \
- -cp $$TMPDIR DumpPlatformClassPath $@
+ -cp $$TMPDIR DumpPlatformClassPath %d $@
rm -rf $$TMPDIR
-""",
- toolchains = ["@bazel_tools//tools/jdk:current_host_java_runtime"],
- tools = ["@bazel_tools//tools/jdk:current_host_java_runtime"],
-)
+""" % release,
+ toolchains = ["@bazel_tools//tools/jdk:current_host_java_runtime"],
+ tools = ["@bazel_tools//tools/jdk:current_host_java_runtime"],
+ )
+ for release in RELEASES
+]
default_java_toolchain(
name = "toolchain_hostjdk8",
- bootclasspath = [":platformclasspath"],
+ bootclasspath = [":platformclasspath8"],
forcibly_disable_header_compilation = True,
javabuilder = [":vanillajavabuilder"],
jvm_opts = [],
@@ -210,28 +218,22 @@
target_version = "8",
)
-default_java_toolchain(
+# Default to the Java 8 language level.
+# TODO(cushon): consider if/when we should increment this?
+alias(
name = "toolchain",
- bootclasspath = [":platformclasspath"],
- source_version = "8",
- target_version = "8",
+ actual = "toolchain_java8",
)
-default_java_toolchain(
- name = "toolchain_java9",
- misc = DEFAULT_JAVACOPTS + [
- "--release",
- "9",
- ],
-)
-
-default_java_toolchain(
- name = "toolchain_java10",
- misc = DEFAULT_JAVACOPTS + [
- "--release",
- "10",
- ],
-)
+[
+ default_java_toolchain(
+ name = "toolchain_java%d" % release,
+ bootclasspath = [":platformclasspath%d" % release],
+ source_version = "%s" % release,
+ target_version = "%s" % release,
+ )
+ for release in RELEASES
+]
filegroup(
name = "srcs",
diff --git a/tools/jdk/DumpPlatformClassPath.java b/tools/jdk/DumpPlatformClassPath.java
index c81ab5d..dd33bba 100644
--- a/tools/jdk/DumpPlatformClassPath.java
+++ b/tools/jdk/DumpPlatformClassPath.java
@@ -46,22 +46,24 @@
import javax.tools.StandardLocation;
/**
- * Output a jar file containing all classes on the platform classpath of the current JDK.
+ * Output a jar file containing all classes on the platform classpath of the given JDK release.
*
- * <p>usage: DumpPlatformClassPath <output jar>
+ * <p>usage: DumpPlatformClassPath <release version> <output jar>
*/
public class DumpPlatformClassPath {
public static void main(String[] args) throws Exception {
- if (args.length != 1) {
- System.err.println("usage: DumpPlatformClassPath <output jar>");
+ if (args.length != 2) {
+ System.err.println("usage: DumpPlatformClassPath <release version> <output jar>");
System.exit(1);
}
- Path output = Paths.get(args[0]);
+ int release = Integer.parseInt(args[0]);
+ Path output = Paths.get(args[1]);
Map<String, byte[]> entries = new HashMap<>();
- // JDK 8 bootclasspath handling
+ // Legacy JDK 8 bootclasspath handling.
+ // TODO(cushon): make sure this has test coverage.
Path javaHome = Paths.get(System.getProperty("java.home"));
if (javaHome.endsWith("jre")) {
javaHome = javaHome.getParent();
@@ -85,13 +87,32 @@
}
}
- if (entries.isEmpty()) {
- // JDK > 8 bootclasspath handling
+ if (!entries.isEmpty()) {
+ // If we found a JDK 8 bootclasspath (rt.jar, etc.) then we're done.
+ //
+ // However JDK 8 only contains bootclasspath API information for the current release,
+ // so we're always going to get a JDK 8 API level regardless of what the user requested.
+ // Emit a warning if they wanted to target a different version.
+ if (release != 8) {
+ System.err.printf(
+ "warning: ignoring release %s on --host_javabase=%s\n",
+ release, System.getProperty("java.version"));
+ }
+ } else {
+ // JDK > 8 --host_javabase bootclasspath handling.
+ // The default --host_javabase is currently JDK 10.
- // Set up a compilation with --release 8 to initialize a filemanager
+ // Set up a compilation with --release to initialize a filemanager
Context context = new Context();
JavacTool.create()
- .getTask(null, null, null, Arrays.asList("--release", "8"), null, null, context);
+ .getTask(
+ /* out = */ null,
+ /* fileManager = */ null,
+ /* diagnosticListener = */ null,
+ /* options = */ Arrays.asList("--release", String.valueOf(release)),
+ /* classes = */ null,
+ /* compilationUnits = */ null,
+ context);
JavaFileManager fileManager = context.get(JavaFileManager.class);
for (JavaFileObject fileObject :