Automated rollback of commit 8f8bfb4291216ef3e58e2ec6716569953274297a.
*** Reason for rollback ***
This uses the host/exec javabase, not the java_toolchain.javabase, and the former needs to support JDK 8 for now
*** Original change description ***
Remove some JDK 8-only logic in DumpPlatformClassPath
PiperOrigin-RevId: 449749072
Change-Id: Ia2b5e4b3e222927f1e08510b6b5edfb8f8532221
diff --git a/toolchains/DumpPlatformClassPath.java b/toolchains/DumpPlatformClassPath.java
index b6d7249..e23ca53 100644
--- a/toolchains/DumpPlatformClassPath.java
+++ b/toolchains/DumpPlatformClassPath.java
@@ -20,14 +20,15 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
+import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
+import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
@@ -58,12 +59,29 @@
Path output = Paths.get(args[0]);
Path targetJavabase = Paths.get(args[1]);
- boolean ok = dumpJDK9AndNewerBootClassPath(output, targetJavabase);
+ int hostMajorVersion = hostMajorVersion();
+ boolean ok;
+ if (hostMajorVersion == 8) {
+ ok = dumpJDK8BootClassPath(output, targetJavabase);
+ } else {
+ ok = dumpJDK9AndNewerBootClassPath(hostMajorVersion, output, targetJavabase);
+ }
System.exit(ok ? 0 : 1);
}
- static boolean dumpJDK9AndNewerBootClassPath(Path output, Path targetJavabase)
- throws IOException {
+ // JDK 8 bootclasspath handling.
+ // * JDK 8 represents a bootclasspath as a search path of jars (rt.jar, etc.).
+ // * It does not support --release or --system.
+ static boolean dumpJDK8BootClassPath(Path output, Path targetJavabase) throws IOException {
+ List<Path> bootClassPathJars = getBootClassPathJars(targetJavabase);
+ writeClassPathJars(output, bootClassPathJars);
+ return true;
+ }
+
+ // JDK > 8 --host_javabase bootclasspath handling.
+ // (The default --host_javabase is currently JDK 9.)
+ static boolean dumpJDK9AndNewerBootClassPath(
+ int hostMajorVersion, Path output, Path targetJavabase) throws IOException {
// JDK 9 and newer support cross-compiling to older platform versions using the --system
// and --release flags.
@@ -179,7 +197,8 @@
}
// Use a fixed timestamp for deterministic jar output.
- static final LocalDateTime DEFAULT_TIMESTAMP = LocalDateTime.of(2010, 1, 1, 0, 0, 0);
+ private static final long FIXED_TIMESTAMP =
+ new GregorianCalendar(2010, 0, 1, 0, 0, 0).getTimeInMillis();
/**
* Add a jar entry to the given {@link JarOutputStream}, normalizing the entry timestamps to
@@ -188,13 +207,13 @@
private static void addEntry(JarOutputStream jos, String name, InputStream input)
throws IOException {
JarEntry je = new JarEntry(name);
- je.setTimeLocal(DEFAULT_TIMESTAMP);
+ je.setTime(FIXED_TIMESTAMP);
je.setMethod(ZipEntry.STORED);
byte[] bytes = toByteArray(input);
- // When targeting JDK > 11, patch the major version so it will be accepted by javac 9
+ // When targeting JDK >= 10, patch the major version so it will be accepted by javac 9
// TODO(cushon): remove this after updating javac
- if (bytes[7] > 55) {
- bytes[7] = 55;
+ if (bytes[7] > 53) {
+ bytes[7] = 53;
}
je.setSize(bytes.length);
CRC32 crc = new CRC32();
@@ -216,4 +235,25 @@
}
return boas.toByteArray();
}
+
+ /**
+ * Returns the major version of the host Java runtime (e.g. '8' for JDK 8), using {@link
+ * Runtime#version} if it is available, and otherwise falling back to the {@code
+ * java.class.version} system. property.
+ */
+ static int hostMajorVersion() {
+ try {
+ Method versionMethod = Runtime.class.getMethod("version");
+ Object version = versionMethod.invoke(null);
+ return (int) version.getClass().getMethod("major").invoke(version);
+ } catch (ReflectiveOperationException e) {
+ // Runtime.version() isn't available on JDK 8; continue below
+ }
+ int version = (int) Double.parseDouble(System.getProperty("java.class.version"));
+ if (49 <= version && version <= 52) {
+ return version - (49 - 5);
+ }
+ throw new IllegalStateException(
+ "Unknown Java version: " + System.getProperty("java.specification.version"));
+ }
}
diff --git a/toolchains/default_java_toolchain.bzl b/toolchains/default_java_toolchain.bzl
index bbb3f12..8558aa9 100644
--- a/toolchains/default_java_toolchain.bzl
+++ b/toolchains/default_java_toolchain.bzl
@@ -229,13 +229,12 @@
args = ctx.actions.args()
args.add("-source")
- args.add("11")
+ args.add("8")
args.add("-target")
- args.add("11")
+ args.add("8")
args.add("-Xlint:-options")
- args.add("--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED")
- args.add("--add-exports=jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED")
- args.add("--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED")
+ args.add("-cp")
+ args.add("%s/lib/tools.jar" % host_javabase.java_home)
args.add("-d")
args.add(class_outputs[0].dirname)
args.add(ctx.file.src)
@@ -257,7 +256,11 @@
args.add("--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED")
args.add("--add-exports=jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED")
args.add("--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED")
- args.add("-cp", class_outputs[0].dirname)
+ args.add_joined(
+ "-cp",
+ [class_outputs[0].dirname, "%s/lib/tools.jar" % host_javabase.java_home],
+ join_with = ctx.configuration.host_path_separator,
+ )
args.add("DumpPlatformClassPath")
args.add(bootclasspath)