Remove JavaCpuSupplier; use the actual --cpu value instead.

This is a semantic change for Bazel, which now observes the --cpu flag when
looking up a Jvm. Use "-default" as a fallback to keep the change backwards
compatible.

RELNOTES[INC]: Bazel now uses the --cpu flag to look up Jvms; it falls back
    to "default" if it can't find a Jvm matching the CPU value.

--
MOS_MIGRATED_REVID=135333759
diff --git a/src/main/java/com/google/devtools/build/lib/BUILD b/src/main/java/com/google/devtools/build/lib/BUILD
index 110b4c1..d2f503a 100644
--- a/src/main/java/com/google/devtools/build/lib/BUILD
+++ b/src/main/java/com/google/devtools/build/lib/BUILD
@@ -742,7 +742,6 @@
         "rules/java/JavaBinary.java",
         "rules/java/JavaCcLinkParamsProvider.java",
         "rules/java/JavaConfigurationLoader.java",
-        "rules/java/JavaCpuSupplier.java",
         "rules/java/JavaImport.java",
         "rules/java/JavaImportBaseRule.java",
         "rules/java/JavaLibrary.java",
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java
index cedecad..ad195b9 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java
@@ -25,10 +25,7 @@
 import com.google.devtools.build.lib.analysis.ConfiguredTarget;
 import com.google.devtools.build.lib.analysis.RuleContext;
 import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
-import com.google.devtools.build.lib.analysis.config.BuildOptions;
 import com.google.devtools.build.lib.analysis.config.ConfigRuleClasses;
-import com.google.devtools.build.lib.analysis.config.ConfigurationEnvironment;
-import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
 import com.google.devtools.build.lib.analysis.constraints.EnvironmentRule;
 import com.google.devtools.build.lib.bazel.rules.android.AarImportRule;
 import com.google.devtools.build.lib.bazel.rules.android.AndroidNdkRepositoryRule;
@@ -103,7 +100,6 @@
 import com.google.devtools.build.lib.rules.cpp.CppOptions;
 import com.google.devtools.build.lib.rules.genquery.GenQueryRule;
 import com.google.devtools.build.lib.rules.java.JavaConfigurationLoader;
-import com.google.devtools.build.lib.rules.java.JavaCpuSupplier;
 import com.google.devtools.build.lib.rules.java.JavaImportBaseRule;
 import com.google.devtools.build.lib.rules.java.JavaOptions;
 import com.google.devtools.build.lib.rules.java.JavaToolchainRule;
@@ -170,14 +166,6 @@
     return builder.build();
   }
 
-  public static final JavaCpuSupplier JAVA_CPU_SUPPLIER = new JavaCpuSupplier() {
-    @Override
-    public String getJavaCpu(BuildOptions buildOptions, ConfigurationEnvironment env)
-        throws InvalidConfigurationException {
-      return "default";
-    }
-  };
-
   private static class BazelPrerequisiteValidator implements PrerequisiteValidator {
     @Override
     public void validate(RuleContext.Builder context,
@@ -309,7 +297,7 @@
 
   private static void initJava(ConfiguredRuleClassProvider.Builder builder) {
     builder.addConfigurationOptions(JavaOptions.class);
-    builder.addConfigurationFragment(new JvmConfigurationLoader(JAVA_CPU_SUPPLIER));
+    builder.addConfigurationFragment(new JvmConfigurationLoader());
     builder.addConfigurationFragment(new JavaConfigurationLoader());
 
     builder.addBuildInfoFactory(new BazelJavaBuildInfoFactory());
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE
index 6f85af1..3b78cac 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE
@@ -50,11 +50,24 @@
 )
 
 bind(
+    name = "jre",
+    actual = "@local_jdk//:jre",
+)
+
+bind(
+    name = "jdk",
+    actual = "@local_jdk//:jdk",
+)
+
+# TODO: Remove these two rules after we've migrated. In order to properly look
+# up Jdks/Jres for cross-platform builds, the lookup needs to happen in the Jdk
+# repository. For now, use an alias rule that redirects to //external:{jre,jdk}.
+bind(
     name = "jre-default",
-    actual = "@local_jdk//:jre-default",
+    actual = "@local_jdk//:jre",
 )
 
 bind(
     name = "jdk-default",
-    actual = "@local_jdk//:jdk-default",
+    actual = "@local_jdk//:jdk",
 )
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCpuSupplier.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCpuSupplier.java
deleted file mode 100644
index 6ca8e99..0000000
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCpuSupplier.java
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright 2014 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.
-
-package com.google.devtools.build.lib.rules.java;
-
-import com.google.devtools.build.lib.analysis.config.BuildOptions;
-import com.google.devtools.build.lib.analysis.config.ConfigurationEnvironment;
-import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
-
-/**
- * Determines the CPU to be used for Java compilation from the build options and the
- * configuration environment.
- */
-public interface JavaCpuSupplier {
-  /**
-   * Returns the Java CPU based on the buiold options and the configuration environment.
-   */
-  String getJavaCpu(BuildOptions buildOptions, ConfigurationEnvironment env)
-      throws InvalidConfigurationException;
-}
\ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JvmConfigurationLoader.java b/src/main/java/com/google/devtools/build/lib/rules/java/JvmConfigurationLoader.java
index fe97c33..6d9c42d 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JvmConfigurationLoader.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JvmConfigurationLoader.java
@@ -16,6 +16,7 @@
 
 import com.google.common.collect.ImmutableSet;
 import com.google.devtools.build.lib.analysis.RedirectChaser;
+import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
 import com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment;
 import com.google.devtools.build.lib.analysis.config.BuildOptions;
 import com.google.devtools.build.lib.analysis.config.ConfigurationEnvironment;
@@ -49,12 +50,6 @@
  * <p>The loader also supports legacy mode, where the JVM can be defined with an abolute path.
  */
 public final class JvmConfigurationLoader implements ConfigurationFragmentFactory {
-  private final JavaCpuSupplier cpuSupplier;
-
-  public JvmConfigurationLoader(JavaCpuSupplier cpuSupplier) {
-    this.cpuSupplier = cpuSupplier;
-  }
-
   @Override
   public Jvm create(ConfigurationEnvironment env, BuildOptions buildOptions)
       throws InvalidConfigurationException, InterruptedException {
@@ -64,10 +59,7 @@
       return null;
     }
     String javaHome = javaOptions.javaBase;
-    String cpu = cpuSupplier.getJavaCpu(buildOptions, env);
-    if (cpu == null) {
-      return null;
-    }
+    String cpu = buildOptions.get(BuildConfiguration.Options.class).cpu;
 
     try {
       return createDefault(env, javaHome, cpu);
@@ -109,40 +101,65 @@
               + " is configurable. JAVABASE targets don't support configurable attributes");
         }
         List<Label> labels = javaHomeAttributes.get("srcs", BuildType.LABEL_LIST);
+        Label selectedJvmLabel = null;
+        Label defaultJvmLabel = null;
         for (Label jvmLabel : labels) {
           if (jvmLabel.getName().endsWith("-" + cpu)) {
-            jvmLabel = RedirectChaser.followRedirects(
-                lookup, jvmLabel, "Architecture-specific JDK");
-            if (jvmLabel == null) {
-              return null;
-            }
-
-            Target jvmTarget = lookup.getTarget(jvmLabel);
-            if (jvmTarget == null) {
-              return null;
-            }
-
-            PathFragment javaHomePath;
-            if (jvmTarget.getLabel().getPackageIdentifier().getRepository().isDefault()) {
-              javaHomePath = jvmLabel.getPackageFragment();
-            } else {
-              javaHomePath = jvmTarget.getLabel().getPackageIdentifier().getSourceRoot();
-            }
-
-            if ((jvmTarget instanceof Rule) &&
-                "filegroup".equals(((Rule) jvmTarget).getRuleClass())) {
-              RawAttributeMapper jvmTargetAttributes = RawAttributeMapper.of((Rule) jvmTarget);
-              if (jvmTargetAttributes.isConfigurable("path", Type.STRING)) {
-                throw new InvalidConfigurationException("\"path\" in " + jvmTarget
-                    + " is configurable. JVM targets don't support configurable attributes");
-              }
-              String path = jvmTargetAttributes.get("path", Type.STRING);
-              if (path != null) {
-                javaHomePath = javaHomePath.getRelative(path);
-              }
-            }
-            return new Jvm(javaHomePath, jvmLabel);
+            selectedJvmLabel = jvmLabel;
+            break;
           }
+          // When we open sourced Bazel, we used the string "default" to look up the Jvm. This is
+          // incorrect for cross-platform builds, but works for purely local builds. Since we now
+          // need to support cross-platform builds, we need to look up by the CPU, rather than the
+          // hard-coded string "default". However, for local builds the Jvm is setup with a
+          // mechanism where we don't currently have access to the CPU value (this is different from
+          // C++, where we infer the CPU from the local machine). As such, looking up only by CPU
+          // breaks builds that currently work, unless we add alias rules for all possible CPU
+          // values (but this is problematic if Bazel is ported to more platforms). For now, we're
+          // working around this problem by falling back to -default if we can't find a Jvm ending
+          // in -<cpu>. This is backwards compatible, but still allows cross-platform builds. In the
+          // medium term, we should rewrite Jvm setup to use a Skylark remote repository, and also
+          // remove the necessity of having a Jvm defined for all platforms even if there's no Java
+          // code.
+          if (jvmLabel.getName().endsWith("-default")) {
+            defaultJvmLabel = jvmLabel;
+          }
+        }
+        if (selectedJvmLabel == null) {
+          selectedJvmLabel = defaultJvmLabel;
+        }
+        if (selectedJvmLabel != null) {
+          selectedJvmLabel = RedirectChaser.followRedirects(
+              lookup, selectedJvmLabel, "Architecture-specific JDK");
+          if (selectedJvmLabel == null) {
+            return null;
+          }
+
+          Target jvmTarget = lookup.getTarget(selectedJvmLabel);
+          if (jvmTarget == null) {
+            return null;
+          }
+
+          PathFragment javaHomePath;
+          if (jvmTarget.getLabel().getPackageIdentifier().getRepository().isDefault()) {
+            javaHomePath = selectedJvmLabel.getPackageFragment();
+          } else {
+            javaHomePath = jvmTarget.getLabel().getPackageIdentifier().getSourceRoot();
+          }
+
+          if ((jvmTarget instanceof Rule) &&
+              "filegroup".equals(((Rule) jvmTarget).getRuleClass())) {
+            RawAttributeMapper jvmTargetAttributes = RawAttributeMapper.of((Rule) jvmTarget);
+            if (jvmTargetAttributes.isConfigurable("path", Type.STRING)) {
+              throw new InvalidConfigurationException("\"path\" in " + jvmTarget
+                  + " is configurable. JVM targets don't support configurable attributes");
+            }
+            String path = jvmTargetAttributes.get("path", Type.STRING);
+            if (path != null) {
+              javaHomePath = javaHomePath.getRelative(path);
+            }
+          }
+          return new Jvm(javaHomePath, selectedJvmLabel);
         }
       }
       throw new InvalidConfigurationException("No JVM target found under " + javaHome
diff --git a/src/main/tools/jdk.BUILD b/src/main/tools/jdk.BUILD
index 57a2abb..6fcbcab 100644
--- a/src/main/tools/jdk.BUILD
+++ b/src/main/tools/jdk.BUILD
@@ -90,6 +90,11 @@
 )
 
 filegroup(
+    name = "jre",
+    srcs = [":jre-default"],
+)
+
+filegroup(
     name = "jre-default",
     srcs = [
         ":jre-bin",
@@ -121,6 +126,14 @@
         ]),
 )
 
+# Bazel looks for a label ending in -<cpu>, or -default if it can't find one.
+filegroup(
+    name = "jdk",
+    srcs = [
+        ":jdk-default",
+    ],
+)
+
 filegroup(
     name = "jdk-default",
     srcs = [
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
index 5c79699..f3a7d25 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
@@ -22,7 +22,6 @@
 import com.google.devtools.build.lib.analysis.util.AnalysisMock;
 import com.google.devtools.build.lib.bazel.rules.BazelConfiguration;
 import com.google.devtools.build.lib.bazel.rules.BazelConfigurationCollection;
-import com.google.devtools.build.lib.bazel.rules.BazelRuleClassProvider;
 import com.google.devtools.build.lib.bazel.rules.python.BazelPythonConfiguration;
 import com.google.devtools.build.lib.packages.util.BazelMockCcSupport;
 import com.google.devtools.build.lib.packages.util.MockCcSupport;
@@ -212,7 +211,7 @@
         new CppConfigurationLoader(Functions.<String>identity()),
         new PythonConfigurationLoader(),
         new BazelPythonConfiguration.Loader(),
-        new JvmConfigurationLoader(BazelRuleClassProvider.JAVA_CPU_SUPPLIER),
+        new JvmConfigurationLoader(),
         new JavaConfigurationLoader(),
         new ObjcConfigurationLoader(),
         new AppleConfiguration.Loader(),
diff --git a/tools/jdk/BUILD b/tools/jdk/BUILD
index 5ec7ec6..22d26eb 100644
--- a/tools/jdk/BUILD
+++ b/tools/jdk/BUILD
@@ -20,39 +20,39 @@
     }),
 )
 
-filegroup(
+alias(
     name = "jni_header",
-    srcs = ["//external:jni_header"],
+    actual = "//external:jni_header",
 )
 
-filegroup(
+alias(
     name = "jni_md_header-darwin",
-    srcs = ["//external:jni_md_header-darwin"],
+    actual = "//external:jni_md_header-darwin",
 )
 
-filegroup(
+alias(
     name = "jni_md_header-linux",
-    srcs = ["//external:jni_md_header-linux"],
+    actual = "//external:jni_md_header-linux",
 )
 
-filegroup(
+alias(
     name = "jni_md_header-freebsd",
-    srcs = ["//external:jni_md_header-freebsd"],
+    actual = "//external:jni_md_header-freebsd",
 )
 
-filegroup(
+alias(
     name = "java",
-    srcs = ["//external:java"],
+    actual = "//external:java",
 )
 
-filegroup(
+alias(
     name = "jar",
-    srcs = ["//external:jar"],
+    actual = "//external:jar",
 )
 
-filegroup(
+alias(
     name = "javac",
-    srcs = ["//external:javac"],
+    actual = "//external:javac",
 )
 
 # On Windows, executables end in ".exe", but the label we reach it through
@@ -71,14 +71,14 @@
     "charsets.jar",
 ]
 
-filegroup(
+alias(
     name = "bootclasspath",
-    srcs = ["//external:bootclasspath"],
+    actual = "//external:bootclasspath",
 )
 
-filegroup(
+alias(
     name = "extdir",
-    srcs = ["//external:extdir"],
+    actual = "//external:extdir",
 )
 
 filegroup(
@@ -92,26 +92,14 @@
     neverlink = 1,
 )
 
-# This one is just needed because of how filegroup redirection works.
-filegroup(name = "jre-null")
-
-filegroup(
+alias(
     name = "jre",
-    srcs = [
-        ":jre-null",
-        "//external:jre-default",
-    ],
+    actual = "//external:jre",
 )
 
-# This one is just needed because of how filegroup redirection works.
-filegroup(name = "jdk-null")
-
-filegroup(
+alias(
     name = "jdk",
-    srcs = [
-        ":jdk-null",
-        "//external:jdk-default",
-    ],
+    actual = "//external:jdk",
 )
 
 java_toolchain(