cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 1 | // Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 15 | import com.sun.tools.javac.api.JavacTool; |
| 16 | import com.sun.tools.javac.util.Context; |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 17 | import java.io.BufferedOutputStream; |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 18 | import java.io.ByteArrayOutputStream; |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 19 | import java.io.IOException; |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 20 | import java.io.InputStream; |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 21 | import java.io.OutputStream; |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 22 | import java.io.UncheckedIOException; |
Liam Miller-Cushon | 4566a42 | 2018-09-07 00:32:41 -0700 | [diff] [blame] | 23 | import java.lang.reflect.Method; |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 24 | import java.nio.file.Files; |
| 25 | import java.nio.file.Path; |
| 26 | import java.nio.file.Paths; |
lberki | 2834613 | 2018-09-11 11:55:39 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 28 | import java.util.Arrays; |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 29 | import java.util.Collection; |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 30 | import java.util.EnumSet; |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 31 | import java.util.GregorianCalendar; |
lberki | 2834613 | 2018-09-11 11:55:39 -0700 | [diff] [blame] | 32 | import java.util.List; |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 33 | import java.util.Map; |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 34 | import java.util.SortedMap; |
| 35 | import java.util.TreeMap; |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 36 | import java.util.jar.JarEntry; |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 37 | import java.util.jar.JarFile; |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 38 | import java.util.jar.JarOutputStream; |
| 39 | import java.util.zip.CRC32; |
| 40 | import java.util.zip.ZipEntry; |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 41 | import javax.tools.JavaFileManager; |
| 42 | import javax.tools.JavaFileObject; |
| 43 | import javax.tools.JavaFileObject.Kind; |
Liam Miller-Cushon | 4566a42 | 2018-09-07 00:32:41 -0700 | [diff] [blame] | 44 | import javax.tools.StandardJavaFileManager; |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 45 | import javax.tools.StandardLocation; |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
Liam Miller-Cushon | 3987300 | 2018-08-28 18:47:25 -0700 | [diff] [blame] | 48 | * Output a jar file containing all classes on the platform classpath of the given JDK release. |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 49 | * |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 50 | * <p>usage: DumpPlatformClassPath <release version> <output jar> <path to target JDK>? |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 51 | */ |
| 52 | public class DumpPlatformClassPath { |
| 53 | |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 54 | public static void main(String[] args) throws Exception { |
cushon | 917d0cd | 2018-10-24 00:27:35 -0700 | [diff] [blame] | 55 | if (args.length != 2) { |
| 56 | System.err.println("usage: DumpPlatformClassPath <output jar> <path to target JDK>"); |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 57 | System.exit(1); |
| 58 | } |
cushon | 917d0cd | 2018-10-24 00:27:35 -0700 | [diff] [blame] | 59 | Path output = Paths.get(args[0]); |
| 60 | Path targetJavabase = Paths.get(args[1]); |
lberki | 2834613 | 2018-09-11 11:55:39 -0700 | [diff] [blame] | 61 | |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 62 | int hostMajorVersion = hostMajorVersion(); |
| 63 | boolean ok; |
| 64 | if (hostMajorVersion == 8) { |
cushon | 917d0cd | 2018-10-24 00:27:35 -0700 | [diff] [blame] | 65 | ok = dumpJDK8BootClassPath(output, targetJavabase); |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 66 | } else { |
cushon | 917d0cd | 2018-10-24 00:27:35 -0700 | [diff] [blame] | 67 | ok = dumpJDK9AndNewerBootClassPath(hostMajorVersion, output, targetJavabase); |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 68 | } |
| 69 | System.exit(ok ? 0 : 1); |
| 70 | } |
lberki | 2834613 | 2018-09-11 11:55:39 -0700 | [diff] [blame] | 71 | |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 72 | // JDK 8 bootclasspath handling. |
| 73 | // * JDK 8 represents a bootclasspath as a search path of jars (rt.jar, etc.). |
| 74 | // * It does not support --release or --system. |
cushon | 917d0cd | 2018-10-24 00:27:35 -0700 | [diff] [blame] | 75 | static boolean dumpJDK8BootClassPath(Path output, Path targetJavabase) throws IOException { |
| 76 | List<Path> bootClassPathJars = getBootClassPathJars(targetJavabase); |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 77 | writeClassPathJars(output, bootClassPathJars); |
| 78 | return true; |
| 79 | } |
lberki | 2834613 | 2018-09-11 11:55:39 -0700 | [diff] [blame] | 80 | |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 81 | // JDK > 8 --host_javabase bootclasspath handling. |
| 82 | // (The default --host_javabase is currently JDK 9.) |
| 83 | static boolean dumpJDK9AndNewerBootClassPath( |
cushon | 917d0cd | 2018-10-24 00:27:35 -0700 | [diff] [blame] | 84 | int hostMajorVersion, Path output, Path targetJavabase) throws IOException { |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 85 | |
| 86 | // JDK 9 and newer support cross-compiling to older platform versions using the --system |
| 87 | // and --release flags. |
| 88 | // * --system takes the path to a JDK root for JDK 9 and up, and causes the compilation |
| 89 | // to target the APIs from that JDK. |
| 90 | // * --release takes a language level (e.g. '9') and uses the API information baked in to |
| 91 | // the host JDK (in lib/ct.sym). |
| 92 | |
| 93 | // Since --system only supports JDK >= 9, first check of the target JDK defines a JDK 8 |
| 94 | // bootclasspath. |
cushon | 917d0cd | 2018-10-24 00:27:35 -0700 | [diff] [blame] | 95 | List<Path> bootClassPathJars = getBootClassPathJars(targetJavabase); |
| 96 | if (!bootClassPathJars.isEmpty()) { |
| 97 | writeClassPathJars(output, bootClassPathJars); |
| 98 | return true; |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 99 | } |
| 100 | |
cushon | 917d0cd | 2018-10-24 00:27:35 -0700 | [diff] [blame] | 101 | // Initialize a FileManager to process the --system argument, and then read the |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 102 | // initialized bootclasspath data back out. |
| 103 | |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 104 | Context context = new Context(); |
| 105 | JavacTool.create() |
| 106 | .getTask( |
| 107 | /* out = */ null, |
| 108 | /* fileManager = */ null, |
| 109 | /* diagnosticListener = */ null, |
cushon | 917d0cd | 2018-10-24 00:27:35 -0700 | [diff] [blame] | 110 | /* options = */ Arrays.asList("--system", String.valueOf(targetJavabase)), |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 111 | /* classes = */ null, |
| 112 | /* compilationUnits = */ null, |
| 113 | context); |
| 114 | StandardJavaFileManager fileManager = |
| 115 | (StandardJavaFileManager) context.get(JavaFileManager.class); |
| 116 | |
| 117 | SortedMap<String, InputStream> entries = new TreeMap<>(); |
cushon | 917d0cd | 2018-10-24 00:27:35 -0700 | [diff] [blame] | 118 | for (JavaFileObject fileObject : |
| 119 | fileManager.list( |
| 120 | StandardLocation.PLATFORM_CLASS_PATH, |
| 121 | "", |
| 122 | EnumSet.of(Kind.CLASS), |
| 123 | /* recurse= */ true)) { |
| 124 | String binaryName = |
| 125 | fileManager.inferBinaryName(StandardLocation.PLATFORM_CLASS_PATH, fileObject); |
| 126 | entries.put(binaryName.replace('.', '/') + ".class", fileObject.openInputStream()); |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 127 | } |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 128 | writeEntries(output, entries); |
| 129 | return true; |
| 130 | } |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 131 | |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 132 | /** Writes the given entry names and data to a jar archive at the given path. */ |
| 133 | private static void writeEntries(Path output, Map<String, InputStream> entries) |
| 134 | throws IOException { |
cushon | 2579b79 | 2018-09-12 01:50:01 -0700 | [diff] [blame] | 135 | if (!entries.containsKey("java/lang/Object.class")) { |
| 136 | throw new AssertionError( |
| 137 | "\nCould not find java.lang.Object on bootclasspath; something has gone terribly wrong.\n" |
| 138 | + "Please file a bug: https://github.com/bazelbuild/bazel/issues"); |
| 139 | } |
cushon | c56699d | 2018-08-08 08:42:10 -0700 | [diff] [blame] | 140 | try (OutputStream os = Files.newOutputStream(output); |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 141 | BufferedOutputStream bos = new BufferedOutputStream(os, 65536); |
| 142 | JarOutputStream jos = new JarOutputStream(bos)) { |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 143 | entries.entrySet().stream() |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 144 | .forEachOrdered( |
| 145 | entry -> { |
| 146 | try { |
| 147 | addEntry(jos, entry.getKey(), entry.getValue()); |
| 148 | } catch (IOException e) { |
| 149 | throw new UncheckedIOException(e); |
cushon | c56699d | 2018-08-08 08:42:10 -0700 | [diff] [blame] | 150 | } |
| 151 | }); |
cushon | 444fc9f | 2018-01-24 02:52:19 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 155 | /** Collects the entries of the given jar files into a map from jar entry names to their data. */ |
| 156 | private static void writeClassPathJars(Path output, Collection<Path> paths) throws IOException { |
| 157 | List<JarFile> jars = new ArrayList<>(); |
| 158 | for (Path path : paths) { |
| 159 | jars.add(new JarFile(path.toFile())); |
| 160 | } |
| 161 | SortedMap<String, InputStream> entries = new TreeMap<>(); |
| 162 | for (JarFile jar : jars) { |
| 163 | jar.stream() |
| 164 | .filter(p -> p.getName().endsWith(".class")) |
| 165 | .forEachOrdered( |
| 166 | entry -> { |
| 167 | try { |
| 168 | entries.put(entry.getName(), jar.getInputStream(entry)); |
| 169 | } catch (IOException e) { |
| 170 | throw new UncheckedIOException(e); |
| 171 | } |
| 172 | }); |
| 173 | } |
| 174 | writeEntries(output, entries); |
| 175 | for (JarFile jar : jars) { |
| 176 | jar.close(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** Returns paths to the entries of a JDK 8-style bootclasspath. */ |
| 181 | private static List<Path> getBootClassPathJars(Path javaHome) throws IOException { |
| 182 | List<Path> jars = new ArrayList<>(); |
| 183 | Path extDir = javaHome.resolve("jre/lib/ext"); |
| 184 | if (Files.exists(extDir)) { |
| 185 | for (Path extJar : Files.newDirectoryStream(extDir, "*.jar")) { |
| 186 | jars.add(extJar); |
| 187 | } |
| 188 | } |
| 189 | for (String jar : |
| 190 | Arrays.asList("rt.jar", "resources.jar", "jsse.jar", "jce.jar", "charsets.jar")) { |
| 191 | Path path = javaHome.resolve("jre/lib").resolve(jar); |
| 192 | if (Files.exists(path)) { |
| 193 | jars.add(path); |
| 194 | } |
| 195 | } |
| 196 | return jars; |
| 197 | } |
| 198 | |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 199 | // Use a fixed timestamp for deterministic jar output. |
| 200 | private static final long FIXED_TIMESTAMP = |
| 201 | new GregorianCalendar(2010, 0, 1, 0, 0, 0).getTimeInMillis(); |
| 202 | |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 203 | /** |
| 204 | * Add a jar entry to the given {@link JarOutputStream}, normalizing the entry timestamps to |
| 205 | * ensure deterministic build output. |
| 206 | */ |
| 207 | private static void addEntry(JarOutputStream jos, String name, InputStream input) |
| 208 | throws IOException { |
cushon | c56699d | 2018-08-08 08:42:10 -0700 | [diff] [blame] | 209 | JarEntry je = new JarEntry(name); |
| 210 | je.setTime(FIXED_TIMESTAMP); |
| 211 | je.setMethod(ZipEntry.STORED); |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 212 | byte[] bytes = toByteArray(input); |
cushon | 148f714 | 2019-01-24 13:32:13 -0800 | [diff] [blame] | 213 | // When targeting JDK >= 10, patch the major version so it will be accepted by javac 9 |
| 214 | // TODO(cushon): remove this after updating javac |
| 215 | if (bytes[7] > 53) { |
| 216 | bytes[7] = 53; |
| 217 | } |
cushon | c56699d | 2018-08-08 08:42:10 -0700 | [diff] [blame] | 218 | je.setSize(bytes.length); |
| 219 | CRC32 crc = new CRC32(); |
| 220 | crc.update(bytes); |
| 221 | je.setCrc(crc.getValue()); |
| 222 | jos.putNextEntry(je); |
| 223 | jos.write(bytes); |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 224 | } |
Liam Miller-Cushon | 282b883 | 2018-08-16 08:02:08 -0700 | [diff] [blame] | 225 | |
| 226 | private static byte[] toByteArray(InputStream is) throws IOException { |
| 227 | byte[] buffer = new byte[8192]; |
| 228 | ByteArrayOutputStream boas = new ByteArrayOutputStream(); |
| 229 | while (true) { |
| 230 | int r = is.read(buffer); |
| 231 | if (r == -1) { |
| 232 | break; |
| 233 | } |
| 234 | boas.write(buffer, 0, r); |
| 235 | } |
| 236 | return boas.toByteArray(); |
| 237 | } |
Liam Miller-Cushon | 4566a42 | 2018-09-07 00:32:41 -0700 | [diff] [blame] | 238 | |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 239 | /** |
cushon | 999354b | 2018-09-21 00:31:42 -0700 | [diff] [blame] | 240 | * Returns the major version of the host Java runtime (e.g. '8' for JDK 8), using {@link |
| 241 | * Runtime#version} if it is available, and otherwise falling back to the {@code |
| 242 | * java.class.version} system. property. |
| 243 | */ |
| 244 | static int hostMajorVersion() { |
Liam Miller-Cushon | 4566a42 | 2018-09-07 00:32:41 -0700 | [diff] [blame] | 245 | try { |
| 246 | Method versionMethod = Runtime.class.getMethod("version"); |
| 247 | Object version = versionMethod.invoke(null); |
cushon | 2579b79 | 2018-09-12 01:50:01 -0700 | [diff] [blame] | 248 | return (int) version.getClass().getMethod("major").invoke(version); |
Liam Miller-Cushon | 4566a42 | 2018-09-07 00:32:41 -0700 | [diff] [blame] | 249 | } catch (ReflectiveOperationException e) { |
cushon | 2579b79 | 2018-09-12 01:50:01 -0700 | [diff] [blame] | 250 | // Runtime.version() isn't available on JDK 8; continue below |
Liam Miller-Cushon | 4566a42 | 2018-09-07 00:32:41 -0700 | [diff] [blame] | 251 | } |
cushon | 2579b79 | 2018-09-12 01:50:01 -0700 | [diff] [blame] | 252 | int version = (int) Double.parseDouble(System.getProperty("java.class.version")); |
| 253 | if (49 <= version && version <= 52) { |
| 254 | return version - (49 - 5); |
| 255 | } |
| 256 | throw new IllegalStateException( |
| 257 | "Unknown Java version: " + System.getProperty("java.specification.version")); |
Liam Miller-Cushon | 4566a42 | 2018-09-07 00:32:41 -0700 | [diff] [blame] | 258 | } |
cushon | 29a9682 | 2017-12-20 16:19:56 -0800 | [diff] [blame] | 259 | } |