From b14e7a36fff32df7db30a9e9b83c43d1d8ba4c96 Mon Sep 17 00:00:00 2001 From: cushon Date: Wed, 12 Sep 2018 01:50:01 -0700 Subject: [PATCH] Fix toolchain_java9 on --host_javabase= after 7eb9ea150fb889a93908d96896db77d5658e5005 See #6127 PiperOrigin-RevId: 212600423 --- tools/jdk/BUILD | 1 + tools/jdk/DumpPlatformClassPath.java | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/tools/jdk/BUILD b/tools/jdk/BUILD index 0e798d84a6a402..9f8c50f3d6ad71 100644 --- a/tools/jdk/BUILD +++ b/tools/jdk/BUILD @@ -185,6 +185,7 @@ RELEASES = (8, 9) set -eu TMPDIR=$$(mktemp -d -t tmp.XXXXXXXX) $(JAVABASE)/bin/javac -source 8 -target 8 \ + -Xlint:-options \ -cp $(JAVABASE)/lib/tools.jar \ -d $$TMPDIR $< $(JAVA) -XX:+IgnoreUnrecognizedVMOptions \ diff --git a/tools/jdk/DumpPlatformClassPath.java b/tools/jdk/DumpPlatformClassPath.java index 0ec566fa4ec119..62b541827cadbf 100644 --- a/tools/jdk/DumpPlatformClassPath.java +++ b/tools/jdk/DumpPlatformClassPath.java @@ -119,7 +119,10 @@ public static void main(String[] args) throws Exception { StandardJavaFileManager fileManager = (StandardJavaFileManager) context.get(JavaFileManager.class); - if (isJdk9OrEarlier()) { + int majorVersion = majorVersion(); + if (majorVersion == 9 && release == 8) { + // Work-around: when running on a JDK 9 host_javabase with --release 8, the ct.sym + // handling isn't compatible with the FileManager#list code path in the branch below. for (Path path : getLocationAsPaths(fileManager)) { Files.walkFileTree( path, @@ -171,6 +174,12 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) }); } + if (!entries.containsKey("java/lang/Object.class")) { + throw new AssertionError( + "\nCould not find java.lang.Object on bootclasspath; something has gone terribly wrong.\n" + + "Please file a bug: https://github.com/bazelbuild/bazel/issues"); + } + try (OutputStream os = Files.newOutputStream(output); BufferedOutputStream bos = new BufferedOutputStream(os, 65536); JarOutputStream jos = new JarOutputStream(bos)) { @@ -228,14 +237,19 @@ private static Iterable getLocationAsPaths(StandardJavaFileManager fileMan } } - static boolean isJdk9OrEarlier() { + static int majorVersion() { try { Method versionMethod = Runtime.class.getMethod("version"); Object version = versionMethod.invoke(null); - int majorVersion = (int) version.getClass().getMethod("major").invoke(version); - return majorVersion <= 9; + return (int) version.getClass().getMethod("major").invoke(version); } catch (ReflectiveOperationException e) { - return true; + // 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")); } }