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/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 :