Add initial starlark implementations of Java toolchain alias rules to Bazel

PiperOrigin-RevId: 233692159
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java
index 38765c1..81a7bbe 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java
@@ -137,8 +137,8 @@
             .add(
                 attr(JavaRuleClasses.JAVA_TOOLCHAIN_ATTRIBUTE_NAME, LABEL)
                     .useOutputLicenses()
-                    .allowedRuleClasses("java_toolchain")
-                    .value(javaToolchainAttribute))
+                    .value(javaToolchainAttribute)
+                    .mandatoryProviders(ToolchainInfo.PROVIDER.id()))
             .addRequiredToolchains(javaRuntimeToolchainType, javaToolchainType);
 
     Attribute.Builder<Label> jacocoAttr =
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java
index 4ce5764..52036f8 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java
@@ -33,6 +33,7 @@
 import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
 import com.google.devtools.build.lib.analysis.config.HostTransition;
 import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
+import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
 import com.google.devtools.build.lib.cmdline.Label;
 import com.google.devtools.build.lib.collect.nestedset.NestedSet;
 import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
@@ -140,12 +141,13 @@
             .add(
                 attr(HOST_JAVA_RUNTIME_ATTRIBUTE_NAME, LABEL)
                     .cfg(HostTransition.INSTANCE)
-                    .value(hostJdkAttribute))
+                    .value(hostJdkAttribute)
+                    .mandatoryProviders(ToolchainInfo.PROVIDER.id()))
             .add(
                 attr(JavaRuleClasses.JAVA_TOOLCHAIN_ATTRIBUTE_NAME, LABEL)
                     .useOutputLicenses()
-                    .allowedRuleClasses("java_toolchain")
-                    .value(javaToolchainAttribute))
+                    .value(javaToolchainAttribute)
+                    .mandatoryProviders(ToolchainInfo.PROVIDER.id()))
             .addRequiredToolchains(javaRuntimeToolchainType, javaToolchainType);
 
     rpcSupport.mutateAspectDefinition(result, aspectParameters);
diff --git a/tools/jdk/BUILD b/tools/jdk/BUILD
index 9e15088..37c9e84 100644
--- a/tools/jdk/BUILD
+++ b/tools/jdk/BUILD
@@ -7,6 +7,14 @@
     "JDK8_JVM_OPTS_REMOTE_JAVAC",
     "bootclasspath",
 )
+load(
+    "//tools/jdk:java_toolchain_alias.bzl",
+    "java_toolchain_alias",
+    "java_runtime_alias",
+    "java_host_runtime_alias",
+    "legacy_java_toolchain_alias",
+    "legacy_java_runtime_alias",
+)
 
 package(default_visibility = ["//visibility:public"])
 
@@ -47,6 +55,12 @@
 
 java_toolchain_alias(name = "current_java_toolchain")
 
+# This exists to support the migration to toolchain resolution.
+# TODO(cushon): delete once the migration is complete.
+legacy_java_runtime_alias(name = "legacy_current_java_runtime")
+
+legacy_java_toolchain_alias(name = "legacy_current_java_toolchain")
+
 # Used to set --host_javabase or --javabase to a local JDK without having to define
 # a custom java_runtime rule.
 # E.g.:
@@ -334,6 +348,7 @@
         "BUILD-jdk",  # Tools are build from the workspace for tests.
         "DumpPlatformClassPath.java",
         "default_java_toolchain.bzl",
+        "java_toolchain_alias.bzl",
         "proguard_whitelister.py",
         "proguard_whitelister_test.py",
         "proguard_whitelister_test_input.cfg",
diff --git a/tools/jdk/java_toolchain_alias.bzl b/tools/jdk/java_toolchain_alias.bzl
new file mode 100644
index 0000000..759d6b2
--- /dev/null
+++ b/tools/jdk/java_toolchain_alias.bzl
@@ -0,0 +1,112 @@
+# Copyright 2019 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Experimental re-implementations of Java toolchain aliases using toolchain resolution."""
+
+load(":toolchain_utils.bzl", "find_java_runtime_toolchain", "find_java_toolchain")
+
+def _copy_default_info(info):
+    # TODO(b/123999008): should copying DefaultInfo be necessary?
+    return DefaultInfo(
+        default_runfiles = info.default_runfiles,
+        data_runfiles = info.data_runfiles,
+        files = info.files,
+    )
+
+def _java_runtime_alias(ctx):
+    """An experimental implementation of java_runtime_alias using toolchain resolution."""
+    toolchain = find_java_runtime_toolchain(ctx, target = ctx.attr._java_runtime)
+
+    # find_java_runtime_toolchain returns a configured target if toolchain resolution is disabled
+    # TODO(cushon): consolidate this with the branch below once JavaRuntimeInfo.files is released
+    if type(toolchain) == "Target":
+        return [
+            toolchain[java_common.JavaRuntimeInfo],
+            toolchain[platform_common.TemplateVariableInfo],
+            _copy_default_info(toolchain[DefaultInfo]),
+        ]
+
+    return [
+        toolchain,
+        platform_common.TemplateVariableInfo({
+            "JAVA": str(toolchain.java_executable_exec_path),
+            "JAVABASE": str(toolchain.java_home),
+        }),
+        # See b/65239471 for related discussion of handling toolchain runfiles/data.
+        DefaultInfo(
+            runfiles = ctx.runfiles(transitive_files = toolchain.files),
+            files = toolchain.files,
+        ),
+    ]
+
+java_runtime_alias = rule(
+    implementation = _java_runtime_alias,
+    toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"],
+    attrs = {
+        "_java_runtime": attr.label(
+            default = Label("@bazel_tools//tools/jdk:legacy_current_java_runtime"),
+        ),
+    },
+)
+
+def _java_host_runtime_alias(ctx):
+    """An experimental implementation of java_host_runtime_alias using toolchain resolution."""
+    runtime = ctx.attr._runtime
+    return [
+        runtime[java_common.JavaRuntimeInfo],
+        runtime[platform_common.TemplateVariableInfo],
+        _copy_default_info(runtime[DefaultInfo]),
+    ]
+
+java_host_runtime_alias = rule(
+    implementation = _java_host_runtime_alias,
+    attrs = {
+        "_runtime": attr.label(
+            default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
+            providers = [
+                java_common.JavaRuntimeInfo,
+                platform_common.TemplateVariableInfo,
+            ],
+            cfg = "host",
+        ),
+    },
+)
+
+def _java_toolchain_alias(ctx):
+    """An experimental implementation of java_toolchain_alias using toolchain resolution."""
+    toolchain = find_java_toolchain(ctx, target = ctx.attr._java_toolchain)
+
+    # find_java_runtime_toolchain returns a configured target if toolchain resolution is disabled
+    if type(toolchain) == "Target":
+        return struct(
+            providers = [toolchain[java_common.JavaToolchainInfo]],
+            # Use the legacy provider syntax for compatibility with the native rules.
+            java_toolchain = toolchain[java_common.JavaToolchainInfo],
+        )
+
+    return toolchain
+
+java_toolchain_alias = rule(
+    implementation = _java_toolchain_alias,
+    toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
+    attrs = {
+        "_java_toolchain": attr.label(
+            default = Label("@bazel_tools//tools/jdk:legacy_current_java_toolchain"),
+        ),
+    },
+)
+
+# Add aliases for the legacy native rules to allow referring to both versions in @bazel_tools//tools/jdk
+legacy_java_toolchain_alias = native.java_toolchain_alias
+legacy_java_runtime_alias = native.java_runtime_alias