Move default_java_toolchain definitions into java_tools repository

default_java_toolchain macro is handy to define toolchains in //tools/jvm/BUILD without making a release of java_tools.

However default_java_toolchain heavily relies on selects by operating system to pick the right parts from the right java_tools. This indirection is removed by putting definition directly into java_tools.

This is the first part. After java_tools release, default_java_toolchain and remote_java_tools_java_import can be removed.

Closes #12224.

PiperOrigin-RevId: 335923902
diff --git a/tools/jdk/BUILD.java_tools b/tools/jdk/BUILD.java_tools
index e996477..99f6342 100644
--- a/tools/jdk/BUILD.java_tools
+++ b/tools/jdk/BUILD.java_tools
@@ -1,4 +1,5 @@
 package(default_visibility = ["//visibility:public"])
+
 exports_files(glob(["**/*.jar"]))
 
 licenses(["notice"])  # Apache 2.0
@@ -6,6 +7,7 @@
 load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library")
 load("@rules_java//java:defs.bzl", "java_binary", "java_import", "java_toolchain")
 load("@rules_proto//proto:defs.bzl", "proto_library")
+load(":java_toolchain_default.bzl", "JDK9_JVM_OPTS", "java_toolchain_default")
 
 SUPRESSED_WARNINGS = select({
     ":windows": [],
@@ -15,49 +17,42 @@
     ],
 })
 
-java_toolchain(
+java_toolchain_default(
     name = "toolchain",
-    bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath"],
-    forcibly_disable_header_compilation = 0,
-    genclass = [":GenClass"],
-    header_compiler = [":Turbine"],
-    header_compiler_direct = [":TurbineDirect"],
-    ijar = [":ijar"],
-    jacocorunner = ":jacoco_coverage_runner_filegroup",
-    javabuilder = [":JavaBuilder"],
     javac = [":javac_jar"],
-    javac_supports_workers = 1,
     jvm_opts = [
         # In JDK9 we have seen a ~30% slow down in JavaBuilder performance when using
         # G1 collector and having compact strings enabled.
         "-XX:+UseParallelOldGC",
         "-XX:-CompactStrings",
-        # Allow JavaBuilder to access internal javac APIs.
-        "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
-        "--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
-
         # override the javac in the JDK.
         "--patch-module=java.compiler=$(location :java_compiler_jar)",
         "--patch-module=jdk.compiler=$(location :jdk_compiler_jar)",
+    ] + JDK9_JVM_OPTS,
+    tools = [
+        ":java_compiler_jar",
+        ":jdk_compiler_jar",
+    ],
+)
 
-        # quiet warnings from com.google.protobuf.UnsafeUtil,
-        # see: https://github.com/google/protobuf/issues/3781
-        # and: https://github.com/bazelbuild/bazel/issues/5599
-        "--add-opens=java.base/java.nio=ALL-UNNAMED",
-        "--add-opens=java.base/java.lang=ALL-UNNAMED",
-    ],
-    misc = [
-        "-XDskipDuplicateBridges=true",
-        "-g",
-        "-parameters",
-    ],
-    singlejar = [":singlejar"],
+java_toolchain_default(
+    name = "toolchain_hostjdk8",
+    javac = [":javac_jar"],
+    jvm_opts = ["-Xbootclasspath/p:$(location :javac_jar)"],
+    source_version = "8",
+    target_version = "8",
+)
+
+# Default to the Java 8 language level.
+# TODO(cushon): consider if/when we should increment this?
+java_toolchain_default(
+    name = "legacy_toolchain",
+    javac = [":javac_jar"],
+    jvm_opts = [
+        # override the javac in the JDK.
+        "--patch-module=java.compiler=$(location :java_compiler_jar)",
+        "--patch-module=jdk.compiler=$(location :jdk_compiler_jar)",
+    ] + JDK9_JVM_OPTS,
     source_version = "8",
     target_version = "8",
     tools = [
@@ -66,51 +61,84 @@
     ],
 )
 
-# A toolchain that targets java 11.
-java_toolchain(
-    name = "toolchain_jdk_11",
-    bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath"],
-    forcibly_disable_header_compilation = 0,
-    genclass = [":GenClass"],
-    header_compiler = [":Turbine"],
-    header_compiler_direct = [":TurbineDirect"],
-    ijar = [":ijar"],
-    jacocorunner = ":jacoco_coverage_runner_filegroup",
-    javabuilder = [":JavaBuilder"],
+# Needed for openbsd / JVM8
+java_toolchain_default(
+    name = "legacy_toolchain_jvm8",
     javac = [":javac_jar"],
-    javac_supports_workers = 1,
+    jvm_opts = ["-Xbootclasspath/p:$(location :javac_jar)"],
+    source_version = "8",
+    target_version = "8",
+)
+
+# The 'vanilla' toolchain is an unsupported alternative to the default.
+#
+# It does not provide any of the following features:
+#   * Error Prone
+#   * Strict Java Deps
+#   * Header Compilation
+#   * Reduced Classpath Optimization
+#
+# It uses the version of internal javac from the `--host_javabase` JDK instead
+# of providing a javac. Internal javac may not be source- or bug-compatible with
+# the javac that is provided with other toolchains.
+#
+# However it does allow using a wider range of `--host_javabase`s, including
+# versions newer than the current JDK.
+java_toolchain_default(
+    name = "toolchain_vanilla",
+    forcibly_disable_header_compilation = True,
+    javabuilder = [":vanillajavabuilder"],
+    jvm_opts = [],
+    source_version = "",
+    target_version = "",
+)
+
+RELEASES = (8, 9, 10, 11)
+
+[
+    (
+        java_toolchain_default(
+            name = "toolchain_java%d" % release,
+            javac = [":javac_jar"],
+            jvm_opts = [
+                # override the javac in the JDK.
+                "--patch-module=java.compiler=$(location :java_compiler_jar)",
+                "--patch-module=jdk.compiler=$(location :jdk_compiler_jar)",
+            ] + JDK9_JVM_OPTS,
+            source_version = "%s" % release,
+            target_version = "%s" % release,
+            tools = [
+                ":java_compiler_jar",
+                ":jdk_compiler_jar",
+            ],
+        ),
+        # Needed for openbsd / JVM8
+        java_toolchain_default(
+            name = "toolchain_java%d_jvm8" % release,
+            javac = [":javac_jar"],
+            jvm_opts = [
+                "-Xbootclasspath/p:$(location :javac_jar)",
+            ],
+            source_version = "%s" % release,
+            target_version = "%s" % release,
+        ),
+    )
+    for release in RELEASES
+]
+
+# A toolchain that targets java 11.
+java_toolchain_default(
+    name = "toolchain_jdk_11",
+    javac = [":javac_jar"],
     jvm_opts = [
-        # In JDK9 we have seen a ~30% slow down in JavaBuilder performance
-        # when using G1 collector and having compact strings enabled.
+        # In JDK9 we have seen a ~30% slow down in JavaBuilder performance when using
+        # G1 collector and having compact strings enabled.
         "-XX:+UseParallelOldGC",
         "-XX:-CompactStrings",
-
-        # Allow JavaBuilder to access internal javac APIs.
-        "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
-        "--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
-
         # override the javac in the JDK.
         "--patch-module=java.compiler=$(location :java_compiler_jar)",
         "--patch-module=jdk.compiler=$(location :jdk_compiler_jar)",
-
-        # quiet warnings from com.google.protobuf.UnsafeUtil,
-        # see: https://github.com/google/protobuf/issues/3781
-        # and: https://github.com/bazelbuild/bazel/issues/5599
-        "--add-opens=java.base/java.nio=ALL-UNNAMED",
-        "--add-opens=java.base/java.lang=ALL-UNNAMED",
-    ],
-    misc = [
-        "-XDskipDuplicateBridges=true",
-        "-g",
-        "-parameters",
-    ],
-    singlejar = [":singlejar"],
+    ] + JDK9_JVM_OPTS,
     source_version = "11",
     target_version = "11",
     tools = [
@@ -120,94 +148,38 @@
 )
 
 # A toolchain that targets java 14.
-java_toolchain(
+java_toolchain_default(
     name = "toolchain_jdk_14",
-    bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath"],
-    forcibly_disable_header_compilation = 0,
-    genclass = [":GenClass"],
-    header_compiler = [":Turbine"],
-    header_compiler_direct = [":TurbineDirect"],
-    ijar = [":ijar"],
-    jacocorunner = ":jacoco_coverage_runner_filegroup",
-    javabuilder = [":JavaBuilder"],
-    javac_supports_workers = 1,
-    jvm_opts = [
-        # Allow JavaBuilder to access internal javac APIs.
-        "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
-        "--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
-
-        # quiet warnings from com.google.protobuf.UnsafeUtil,
-        # see: https://github.com/google/protobuf/issues/3781
-        # and: https://github.com/bazelbuild/bazel/issues/5599
-        "--add-opens=java.base/java.nio=ALL-UNNAMED",
-        "--add-opens=java.base/java.lang=ALL-UNNAMED",
-    ],
-    misc = [
-        "-XDskipDuplicateBridges=true",
-        "-g",
-        "-parameters",
-    ],
-    singlejar = [":singlejar"],
     source_version = "14",
     target_version = "14",
 )
 
+# A toolchain that targets java 15.
+java_toolchain_default(
+    name = "toolchain_jdk_15",
+    source_version = "15",
+    target_version = "15",
+)
+
 # The new toolchain is using all the pre-built tools, including
 # singlejar and ijar, even on remote execution. This toolchain
 # should be used only when host and execution platform are the
 # same, otherwise the binaries will not work on the execution
 # platform.
-java_toolchain(
+java_toolchain_default(
     name = "prebuilt_toolchain",
-    bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath"],
-    forcibly_disable_header_compilation = 0,
-    genclass = [":GenClass"],
-    header_compiler = [":Turbine"],
-    header_compiler_direct = [":TurbineDirect"],
     ijar = [":ijar_prebuilt_binary"],
-    jacocorunner = ":jacoco_coverage_runner_filegroup",
-    javabuilder = [":JavaBuilder"],
     javac = [":javac_jar"],
-    javac_supports_workers = 1,
     jvm_opts = [
         # In JDK9 we have seen a ~30% slow down in JavaBuilder performance when using
         # G1 collector and having compact strings enabled.
         "-XX:+UseParallelOldGC",
         "-XX:-CompactStrings",
-        # Allow JavaBuilder to access internal javac APIs.
-        "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
-        "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
-        "--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
-
         # override the javac in the JDK.
         "--patch-module=java.compiler=$(location :java_compiler_jar)",
         "--patch-module=jdk.compiler=$(location :jdk_compiler_jar)",
-
-        # quiet warnings from com.google.protobuf.UnsafeUtil,
-        # see: https://github.com/google/protobuf/issues/3781
-        # and: https://github.com/bazelbuild/bazel/issues/5599
-        "--add-opens=java.base/java.nio=ALL-UNNAMED",
-        "--add-opens=java.base/java.lang=ALL-UNNAMED",
-    ],
-    misc = [
-        "-XDskipDuplicateBridges=true",
-        "-g",
-        "-parameters",
-    ],
+    ] + JDK9_JVM_OPTS,
     singlejar = [":prebuilt_singlejar"],
-    source_version = "8",
-    target_version = "8",
     tools = [
         ":java_compiler_jar",
         ":jdk_compiler_jar",