Move helper methods for accessing platform providers to a utility class.

Change-Id: I45d0cf8e5096ad4ab516af5ddaa98eea8d516c04
PiperOrigin-RevId: 157788771
diff --git a/src/main/java/com/google/devtools/build/lib/BUILD b/src/main/java/com/google/devtools/build/lib/BUILD
index 08f2703..ef5e0eb 100644
--- a/src/main/java/com/google/devtools/build/lib/BUILD
+++ b/src/main/java/com/google/devtools/build/lib/BUILD
@@ -544,6 +544,15 @@
 )
 
 java_library(
+    name = "skylark-provider-collection",
+    srcs = ["analysis/SkylarkProviderCollection.java"],
+    deps = [
+        ":packages-internal",
+        "//third_party:jsr305",
+    ],
+)
+
+java_library(
     name = "build-configuration-option-details",
     srcs = ["analysis/config/BuildConfigurationOptionDetails.java"],
     visibility = [
@@ -597,6 +606,7 @@
         ":os_util",
         ":packages-internal",
         ":shell",
+        ":skylark-provider-collection",
         ":skylarkinterface",
         ":syntax",
         ":transitive-info-provider",
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/SkylarkProviderCollection.java b/src/main/java/com/google/devtools/build/lib/analysis/SkylarkProviderCollection.java
new file mode 100644
index 0000000..c5c2130
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/analysis/SkylarkProviderCollection.java
@@ -0,0 +1,52 @@
+// Copyright 2017 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.analysis;
+
+import com.google.devtools.build.lib.packages.ClassObjectConstructor;
+import com.google.devtools.build.lib.packages.SkylarkClassObject;
+import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier;
+import javax.annotation.Nullable;
+
+/**
+ * Interface to mark classes that could contain transitive information added using the Skylark
+ * framework.
+ */
+public interface SkylarkProviderCollection {
+
+  /**
+   * Returns the transitive information requested or null, if the information is not found. The
+   * transitive information has to have been added using the Skylark framework.
+   */
+  @Nullable
+  Object get(String providerKey);
+
+  /**
+   * Returns the declared provider requested, or null, if the information is not found. The
+   * transitive information has to have been added using the Skylark framework.
+   */
+  @Nullable
+  SkylarkClassObject get(ClassObjectConstructor.Key providerKey);
+
+  /**
+   * Returns the provider defined in Skylark, or null, if the information is not found. The
+   * transitive information has to have been added using the Skylark framework.
+   *
+   * <p>This method dispatches to either {@link #get(ClassObjectConstructor.Key)} or {@link
+   * #get(String)} depending on whether {@link SkylarkProviderIdentifier} is for legacy or for
+   * declared provider.
+   */
+  @Nullable
+  Object get(SkylarkProviderIdentifier id);
+}
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/TransitiveInfoCollection.java b/src/main/java/com/google/devtools/build/lib/analysis/TransitiveInfoCollection.java
index 03f9d95..b174a47 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/TransitiveInfoCollection.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/TransitiveInfoCollection.java
@@ -16,9 +16,6 @@
 
 import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
 import com.google.devtools.build.lib.cmdline.Label;
-import com.google.devtools.build.lib.packages.ClassObjectConstructor;
-import com.google.devtools.build.lib.packages.SkylarkClassObject;
-import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
 import com.google.devtools.build.lib.syntax.SkylarkIndexable;
@@ -44,21 +41,25 @@
   category = SkylarkModuleCategory.BUILTIN,
   doc =
       "A BUILD target. It is essentially a <code>struct</code> with the following fields:"
-    + "<ul>"
-    + "<li><h3 id=\"modules.Target.label\">label</h3><code><a class=\"anchor\" "
-    + "href=\"Label.html\">Label</a> Target.label</code><br>The identifier of the target.</li>"
-    + "<li><h3 id=\"modules.Target.files\">files</h3><code><a class=\"anchor\" "
-    + "href=\"depset.html\">depset</a> Target.files </code><br>The set of <a class=\"anchor\" "
-    + "href=\"File.html\">File</a>s produced directly by this target.</li>"
-    + "<li><h3 id=\"modules.Target.aspect_ids\">aspect_ids</h3><code><a class=\"anchor\""
-    + "href=\"list.html\">list</a> Target.aspect_ids </code><br>The list of <a class=\"anchor\" "
-    + "href=\"ctx.html#aspect_id\">aspect_id</a>s applied to this target.</li>"
-    + "<li><h3 id=\"modules.Target.extraproviders\">Extra providers</h3>For rule targets all "
-    + "additional providers provided by this target are accessible as <code>struct</code> fields. "
-    + "These extra providers are defined in the <code>struct</code> returned by the rule "
-    + "implementation function.</li>"
-    + "</ul>")
-public interface TransitiveInfoCollection extends SkylarkIndexable {
+          + "<ul>"
+          + "<li><h3 id=\"modules.Target.label\">label</h3><code><a class=\"anchor\" "
+          + "href=\"Label.html\">Label</a> Target.label</code><br>The identifier of the "
+          + "target.</li>"
+          + "<li><h3 id=\"modules.Target.files\">files</h3><code><a class=\"anchor\" "
+          + "href=\"depset.html\">depset</a> Target.files </code><br>The set of "
+          + "<a class=\"anchor\" href=\"File.html\">File</a>s produced directly by this "
+          + "target.</li>"
+          + "<li><h3 id=\"modules.Target.aspect_ids\">aspect_ids</h3><code><a class=\"anchor\""
+          + "href=\"list.html\">list</a> Target.aspect_ids </code><br>The list of "
+          + "<a class=\"anchor\" href=\"ctx.html#aspect_id\">aspect_id</a>s applied to this "
+          + "target.</li>"
+          + "<li><h3 id=\"modules.Target.extraproviders\">Extra providers</h3>For rule targets all "
+          + "additional providers provided by this target are accessible as <code>struct</code> "
+          + "fields. These extra providers are defined in the <code>struct</code> returned by the "
+          + "rule implementation function.</li>"
+          + "</ul>"
+)
+public interface TransitiveInfoCollection extends SkylarkIndexable, SkylarkProviderCollection {
 
   /**
    * Returns the transitive information provider requested, or null if the provider is not found.
@@ -78,26 +79,4 @@
    * <b>null</b>.</p>
    */
   @Nullable BuildConfiguration getConfiguration();
-
-  /**
-   * Returns the transitive information requested or null, if the information is not found.
-   * The transitive information has to have been added using the Skylark framework.
-   */
-  @Nullable Object get(String providerKey);
-
-  /**
-   * Returns the declared provider requested, or null, if the information is not found.
-   * The transitive information has to have been added using the Skylark framework.
-   */
-  @Nullable SkylarkClassObject get(ClassObjectConstructor.Key providerKey);
-
-  /**
-   * Returns the provider defined in Skylark, or null, if the information is not found.
-   * The transitive information has to have been added using the Skylark framework.
-   *
-   * This method dispatches to either {@link #get(ClassObjectConstructor.Key)} or
-   * {@link #get(String)} depending on whether {@link SkylarkProviderIdentifier} is for
-   * legacy or for declared provider.
-   */
-  @Nullable Object get(SkylarkProviderIdentifier id);
 }
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/platform/BUILD b/src/main/java/com/google/devtools/build/lib/analysis/platform/BUILD
index ce3a69d..e5c4231 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/platform/BUILD
+++ b/src/main/java/com/google/devtools/build/lib/analysis/platform/BUILD
@@ -5,11 +5,16 @@
     default_visibility = ["//src:__subpackages__"],
 )
 
+UTIL_SRCS = ["PlatformProviderUtils.java"]
+
 java_library(
     name = "platform",
-    srcs = glob([
-        "*.java",
-    ]),
+    srcs = glob(
+        [
+            "*.java",
+        ],
+        exclude = UTIL_SRCS,
+    ),
     deps = [
         "//src/main/java/com/google/devtools/build/lib:packages",
         "//src/main/java/com/google/devtools/build/lib:skylarkinterface",
@@ -17,6 +22,17 @@
     ],
 )
 
+java_library(
+    name = "utils",
+    srcs = UTIL_SRCS,
+    deps = [
+        ":platform",
+        "//src/main/java/com/google/devtools/build/lib:preconditions",
+        "//src/main/java/com/google/devtools/build/lib:skylark-provider-collection",
+        "//third_party:guava",
+    ],
+)
+
 filegroup(
     name = "srcs",
     testonly = 0,  # All srcs should be not test only, overwrite package default.
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformProviderUtils.java b/src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformProviderUtils.java
new file mode 100644
index 0000000..7a5d06d
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformProviderUtils.java
@@ -0,0 +1,93 @@
+// Copyright 2017 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.analysis.platform;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
+import com.google.devtools.build.lib.analysis.SkylarkProviderCollection;
+import com.google.devtools.build.lib.util.Preconditions;
+
+/** Utility methods to help locate platform-related providers. */
+public class PlatformProviderUtils {
+
+  /** Retrieves and casts the {@link PlatformInfo} provider from the given target. */
+  public static PlatformInfo platform(SkylarkProviderCollection target) {
+    Object provider = target.get(PlatformInfo.SKYLARK_IDENTIFIER);
+    if (provider == null) {
+      return null;
+    }
+    Preconditions.checkState(provider instanceof PlatformInfo);
+    return (PlatformInfo) provider;
+  }
+
+  /** Retrieves and casts {@link PlatformInfo} providers from the given targets. */
+  public static Iterable<PlatformInfo> platforms(
+      Iterable<? extends SkylarkProviderCollection> targets) {
+    return Iterables.transform(
+        targets,
+        new Function<SkylarkProviderCollection, PlatformInfo>() {
+          @Override
+          public PlatformInfo apply(SkylarkProviderCollection target) {
+            return platform(target);
+          }
+        });
+  }
+
+  /** Retrieves and casts the {@link ConstraintSettingInfo} provider from the given target. */
+  public static ConstraintSettingInfo constraintSetting(SkylarkProviderCollection target) {
+    Object provider = target.get(ConstraintSettingInfo.SKYLARK_IDENTIFIER);
+    if (provider == null) {
+      return null;
+    }
+    Preconditions.checkState(provider instanceof ConstraintSettingInfo);
+    return (ConstraintSettingInfo) provider;
+  }
+
+  /** Retrieves and casts {@link ConstraintSettingInfo} providers from the given targets. */
+  public static Iterable<ConstraintSettingInfo> constraintSettings(
+      Iterable<? extends SkylarkProviderCollection> targets) {
+    return Iterables.transform(
+        targets,
+        new Function<SkylarkProviderCollection, ConstraintSettingInfo>() {
+          @Override
+          public ConstraintSettingInfo apply(SkylarkProviderCollection target) {
+            return constraintSetting(target);
+          }
+        });
+  }
+
+  /** Retrieves and casts the {@link ConstraintValueInfo} provider from the given target. */
+  public static ConstraintValueInfo constraintValue(SkylarkProviderCollection target) {
+    Object provider = target.get(ConstraintValueInfo.SKYLARK_IDENTIFIER);
+    if (provider == null) {
+      return null;
+    }
+    Preconditions.checkState(provider instanceof ConstraintValueInfo);
+    return (ConstraintValueInfo) provider;
+  }
+
+  /** Retrieves and casts {@link ConstraintValueInfo} providers from the given targets. */
+  public static Iterable<ConstraintValueInfo> constraintValues(
+      Iterable<? extends SkylarkProviderCollection> targets) {
+    return Iterables.transform(
+        targets,
+        new Function<SkylarkProviderCollection, ConstraintValueInfo>() {
+          @Override
+          public ConstraintValueInfo apply(SkylarkProviderCollection target) {
+            return constraintValue(target);
+          }
+        });
+  }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/BUILD b/src/main/java/com/google/devtools/build/lib/rules/platform/BUILD
index c3a8cc5..3aa6097 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/platform/BUILD
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/BUILD
@@ -15,6 +15,7 @@
         "//src/main/java/com/google/devtools/build/lib:packages",
         "//src/main/java/com/google/devtools/build/lib:skylarkinterface",
         "//src/main/java/com/google/devtools/build/lib/analysis/platform",
+        "//src/main/java/com/google/devtools/build/lib/analysis/platform:utils",
         "//third_party:guava",
     ],
 )
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSetting.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSetting.java
index 6c903fd..b7088d3c 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSetting.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSetting.java
@@ -14,18 +14,14 @@
 
 package com.google.devtools.build.lib.rules.platform;
 
-import com.google.common.base.Function;
-import com.google.common.collect.Iterables;
 import com.google.devtools.build.lib.analysis.ConfiguredTarget;
 import com.google.devtools.build.lib.analysis.FileProvider;
 import com.google.devtools.build.lib.analysis.FilesToRunProvider;
 import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
 import com.google.devtools.build.lib.analysis.RuleContext;
 import com.google.devtools.build.lib.analysis.RunfilesProvider;
-import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
 import com.google.devtools.build.lib.analysis.platform.ConstraintSettingInfo;
 import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
-import com.google.devtools.build.lib.util.Preconditions;
 
 /**
  * Defines a category of constraint that can be fulfilled by a constraint_value rule in a platform
@@ -44,27 +40,4 @@
         .addNativeDeclaredProvider(ConstraintSettingInfo.create(ruleContext.getLabel()))
         .build();
   }
-
-  /** Retrieves and casts the provider from the given target. */
-  public static ConstraintSettingInfo constraintSetting(TransitiveInfoCollection target) {
-    Object provider = target.get(ConstraintSettingInfo.SKYLARK_IDENTIFIER);
-    if (provider == null) {
-      return null;
-    }
-    Preconditions.checkState(provider instanceof ConstraintSettingInfo);
-    return (ConstraintSettingInfo) provider;
-  }
-
-  /** Retrieves and casts the providers from the given targets. */
-  public static Iterable<ConstraintSettingInfo> constraintSettings(
-      Iterable<? extends TransitiveInfoCollection> targets) {
-    return Iterables.transform(
-        targets,
-        new Function<TransitiveInfoCollection, ConstraintSettingInfo>() {
-          @Override
-          public ConstraintSettingInfo apply(TransitiveInfoCollection target) {
-            return constraintSetting(target);
-          }
-        });
-  }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValue.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValue.java
index 1eb3ba1..21e7588 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValue.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValue.java
@@ -14,8 +14,6 @@
 
 package com.google.devtools.build.lib.rules.platform;
 
-import com.google.common.base.Function;
-import com.google.common.collect.Iterables;
 import com.google.devtools.build.lib.analysis.ConfiguredTarget;
 import com.google.devtools.build.lib.analysis.FileProvider;
 import com.google.devtools.build.lib.analysis.FilesToRunProvider;
@@ -23,11 +21,10 @@
 import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
 import com.google.devtools.build.lib.analysis.RuleContext;
 import com.google.devtools.build.lib.analysis.RunfilesProvider;
-import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
 import com.google.devtools.build.lib.analysis.platform.ConstraintSettingInfo;
 import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
+import com.google.devtools.build.lib.analysis.platform.PlatformProviderUtils;
 import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
-import com.google.devtools.build.lib.util.Preconditions;
 
 /** Defines a potential value of a constraint. */
 public class ConstraintValue implements RuleConfiguredTargetFactory {
@@ -37,7 +34,7 @@
       throws InterruptedException, RuleErrorException {
 
     ConstraintSettingInfo constraint =
-        ConstraintSetting.constraintSetting(
+        PlatformProviderUtils.constraintSetting(
             ruleContext.getPrerequisite(
                 ConstraintValueRule.CONSTRAINT_SETTING_ATTR, Mode.DONT_CHECK));
 
@@ -48,27 +45,4 @@
         .addNativeDeclaredProvider(ConstraintValueInfo.create(constraint, ruleContext.getLabel()))
         .build();
   }
-
-  /** Retrieves and casts the provider from the given target. */
-  public static ConstraintValueInfo constraintValue(TransitiveInfoCollection target) {
-    Object provider = target.get(ConstraintValueInfo.SKYLARK_IDENTIFIER);
-    if (provider == null) {
-      return null;
-    }
-    Preconditions.checkState(provider instanceof ConstraintValueInfo);
-    return (ConstraintValueInfo) provider;
-  }
-
-  /** Retrieves and casts the providers from the given targets. */
-  public static Iterable<ConstraintValueInfo> constraintValues(
-      Iterable<? extends TransitiveInfoCollection> targets) {
-    return Iterables.transform(
-        targets,
-        new Function<TransitiveInfoCollection, ConstraintValueInfo>() {
-          @Override
-          public ConstraintValueInfo apply(TransitiveInfoCollection target) {
-            return constraintValue(target);
-          }
-        });
-  }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/Platform.java b/src/main/java/com/google/devtools/build/lib/rules/platform/Platform.java
index 184f7a8..87d4b6b 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/platform/Platform.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/Platform.java
@@ -14,8 +14,6 @@
 
 package com.google.devtools.build.lib.rules.platform;
 
-import com.google.common.base.Function;
-import com.google.common.collect.Iterables;
 import com.google.devtools.build.lib.analysis.ConfiguredTarget;
 import com.google.devtools.build.lib.analysis.FileProvider;
 import com.google.devtools.build.lib.analysis.FilesToRunProvider;
@@ -23,14 +21,13 @@
 import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
 import com.google.devtools.build.lib.analysis.RuleContext;
 import com.google.devtools.build.lib.analysis.RunfilesProvider;
-import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
 import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
 import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
+import com.google.devtools.build.lib.analysis.platform.PlatformProviderUtils;
 import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
 import com.google.devtools.build.lib.syntax.Type;
 import com.google.devtools.build.lib.util.CPU;
 import com.google.devtools.build.lib.util.OS;
-import com.google.devtools.build.lib.util.Preconditions;
 import java.util.Map;
 
 /** Defines a platform for execution contexts. */
@@ -46,7 +43,7 @@
       autodetectHostConstraints(ruleContext, platformBuilder);
     } else {
       platformBuilder.addConstraints(
-          ConstraintValue.constraintValues(
+          PlatformProviderUtils.constraintValues(
               ruleContext.getPrerequisites(PlatformRule.CONSTRAINT_VALUES_ATTR, Mode.DONT_CHECK)));
     }
 
@@ -77,7 +74,7 @@
     // Add the CPU.
     CPU cpu = CPU.getCurrent();
     Iterable<ConstraintValueInfo> cpuConstraintValues =
-        ConstraintValue.constraintValues(
+        PlatformProviderUtils.constraintValues(
             ruleContext.getPrerequisites(PlatformRule.HOST_CPU_CONSTRAINTS_ATTR, Mode.DONT_CHECK));
     for (ConstraintValueInfo constraint : cpuConstraintValues) {
       if (cpu.getCanonicalName().equals(constraint.label().getName())) {
@@ -89,7 +86,7 @@
     // Add the OS.
     OS os = OS.getCurrent();
     Iterable<ConstraintValueInfo> osConstraintValues =
-        ConstraintValue.constraintValues(
+        PlatformProviderUtils.constraintValues(
             ruleContext.getPrerequisites(PlatformRule.HOST_OS_CONSTRAINTS_ATTR, Mode.DONT_CHECK));
     for (ConstraintValueInfo constraint : osConstraintValues) {
       if (os.getCanonicalName().equals(constraint.label().getName())) {
@@ -98,27 +95,4 @@
       }
     }
   }
-
-  /** Retrieves and casts the provider from the given target. */
-  public static PlatformInfo platform(TransitiveInfoCollection target) {
-    Object provider = target.get(PlatformInfo.SKYLARK_IDENTIFIER);
-    if (provider == null) {
-      return null;
-    }
-    Preconditions.checkState(provider instanceof PlatformInfo);
-    return (PlatformInfo) provider;
-  }
-
-  /** Retrieves and casts the providers from the given targets. */
-  public static Iterable<PlatformInfo> platforms(
-      Iterable<? extends TransitiveInfoCollection> targets) {
-    return Iterables.transform(
-        targets,
-        new Function<TransitiveInfoCollection, PlatformInfo>() {
-          @Override
-          public PlatformInfo apply(TransitiveInfoCollection target) {
-            return platform(target);
-          }
-        });
-  }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/SkylarkToolchainConstructor.java b/src/main/java/com/google/devtools/build/lib/rules/platform/SkylarkToolchainConstructor.java
index 41d7cd1..47b2d8c 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/platform/SkylarkToolchainConstructor.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/SkylarkToolchainConstructor.java
@@ -14,9 +14,9 @@
 package com.google.devtools.build.lib.rules.platform;
 
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
 import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
 import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
+import com.google.devtools.build.lib.analysis.platform.PlatformProviderUtils;
 import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
 import com.google.devtools.build.lib.events.Location;
 import com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor;
@@ -28,8 +28,6 @@
 import com.google.devtools.build.lib.syntax.SkylarkDict;
 import com.google.devtools.build.lib.syntax.SkylarkList;
 import com.google.devtools.build.lib.syntax.SkylarkType;
-import java.util.Map;
-import java.util.Set;
 import javax.annotation.Nullable;
 
 /** Skylark value that can be used to create toolchains. */
@@ -74,27 +72,12 @@
 
     // Based on SIGNATURE above, the args are exec (list), target (list), data (map).
     Iterable<ConstraintValueInfo> execConstraints =
-        ConstraintValue.constraintValues((SkylarkList<TransitiveInfoCollection>) args[0]);
+        PlatformProviderUtils.constraintValues((SkylarkList<TransitiveInfoCollection>) args[0]);
     Iterable<ConstraintValueInfo> targetConstraints =
-        ConstraintValue.constraintValues((SkylarkList<TransitiveInfoCollection>) args[1]);
+        PlatformProviderUtils.constraintValues((SkylarkList<TransitiveInfoCollection>) args[1]);
     SkylarkDict<String, Object> toolchainData = (SkylarkDict<String, Object>) args[2];
     Location loc = ast != null ? ast.getLocation() : Location.BUILTIN;
 
     return new ToolchainInfo(getKey(), execConstraints, targetConstraints, toolchainData, loc);
   }
-
-  private Map<String, Object> collectData(
-      SkylarkDict<String, Object> receivedArguments, Set<String> ignoredKeys) {
-
-    ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
-    for (Map.Entry<String, Object> entry : receivedArguments.entrySet()) {
-      String key = entry.getKey();
-      Object value = entry.getValue();
-      if (!ignoredKeys.contains(key)) {
-        builder.put(key, value);
-      }
-    }
-
-    return builder.build();
-  }
 }
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/platform/BUILD b/src/test/java/com/google/devtools/build/lib/analysis/platform/BUILD
index 6bacfd1..0b9feba 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/platform/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/analysis/platform/BUILD
@@ -16,8 +16,8 @@
         "//src/main/java/com/google/devtools/build/lib:syntax",
         "//src/main/java/com/google/devtools/build/lib:util",
         "//src/main/java/com/google/devtools/build/lib/analysis/platform",
+        "//src/main/java/com/google/devtools/build/lib/analysis/platform:utils",
         "//src/main/java/com/google/devtools/build/lib/cmdline",
-        "//src/main/java/com/google/devtools/build/lib/rules/platform",
         "//src/test/java/com/google/devtools/build/lib:analysis_testutil",
         "//src/test/java/com/google/devtools/build/lib:packages_testutil",
         "//src/test/java/com/google/devtools/build/lib/skylark:testutil",
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfoTest.java b/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfoTest.java
index ddc66d7..ca34b45 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfoTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfoTest.java
@@ -20,7 +20,6 @@
 import com.google.devtools.build.lib.analysis.ConfiguredTarget;
 import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
 import com.google.devtools.build.lib.cmdline.Label;
-import com.google.devtools.build.lib.rules.platform.ConstraintSetting;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
@@ -58,8 +57,8 @@
 
     ConfiguredTarget setting = getConfiguredTarget("//test/platform:custom");
     assertThat(setting).isNotNull();
-    assertThat(ConstraintSetting.constraintSetting(setting)).isNotNull();
-    assertThat(ConstraintSetting.constraintSetting(setting).label())
+    assertThat(PlatformProviderUtils.constraintSetting(setting)).isNotNull();
+    assertThat(PlatformProviderUtils.constraintSetting(setting).label())
         .isEqualTo(Label.parseAbsolute("//test/platform:custom"));
   }
 }
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfoTest.java b/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfoTest.java
index 9a6e6d2..9684124 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfoTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfoTest.java
@@ -20,7 +20,6 @@
 import com.google.devtools.build.lib.analysis.ConfiguredTarget;
 import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
 import com.google.devtools.build.lib.cmdline.Label;
-import com.google.devtools.build.lib.rules.platform.ConstraintValue;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
@@ -73,10 +72,10 @@
 
     ConfiguredTarget value = getConfiguredTarget("//test/platform:custom");
     assertThat(value).isNotNull();
-    assertThat(ConstraintValue.constraintValue(value)).isNotNull();
-    assertThat(ConstraintValue.constraintValue(value).constraint().label())
+    assertThat(PlatformProviderUtils.constraintValue(value)).isNotNull();
+    assertThat(PlatformProviderUtils.constraintValue(value).constraint().label())
         .isEqualTo(Label.parseAbsolute("//test/platform:basic"));
-    assertThat(ConstraintValue.constraintValue(value).label())
+    assertThat(PlatformProviderUtils.constraintValue(value).label())
         .isEqualTo(Label.parseAbsolute("//test/platform:custom"));
   }
 }
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/platform/PlatformInfoTest.java b/src/test/java/com/google/devtools/build/lib/analysis/platform/PlatformInfoTest.java
index d8f0029..4553647 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/platform/PlatformInfoTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/platform/PlatformInfoTest.java
@@ -19,7 +19,6 @@
 import com.google.common.testing.EqualsTester;
 import com.google.devtools.build.lib.analysis.ConfiguredTarget;
 import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
-import com.google.devtools.build.lib.rules.platform.Platform;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -116,7 +115,7 @@
     ConfiguredTarget platform = getConfiguredTarget("//test/platform:custom");
     assertThat(platform).isNotNull();
 
-    PlatformInfo provider = Platform.platform(platform);
+    PlatformInfo provider = PlatformProviderUtils.platform(platform);
     assertThat(provider).isNotNull();
     assertThat(provider.constraints()).hasSize(1);
     ConstraintSettingInfo constraintSetting =
diff --git a/src/test/java/com/google/devtools/build/lib/rules/platform/BUILD b/src/test/java/com/google/devtools/build/lib/rules/platform/BUILD
index 981b5b1..781420d 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/platform/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/rules/platform/BUILD
@@ -15,8 +15,8 @@
         "//src/main/java/com/google/devtools/build/lib:syntax",
         "//src/main/java/com/google/devtools/build/lib:util",
         "//src/main/java/com/google/devtools/build/lib/analysis/platform",
+        "//src/main/java/com/google/devtools/build/lib/analysis/platform:utils",
         "//src/main/java/com/google/devtools/build/lib/cmdline",
-        "//src/main/java/com/google/devtools/build/lib/rules/platform",
         "//src/test/java/com/google/devtools/build/lib:analysis_testutil",
         "//src/test/java/com/google/devtools/build/lib:packages_testutil",
         "//src/test/java/com/google/devtools/build/lib/skylark:testutil",
diff --git a/src/test/java/com/google/devtools/build/lib/rules/platform/ConstraintTest.java b/src/test/java/com/google/devtools/build/lib/rules/platform/ConstraintTest.java
index fbf1e19..23ff526 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/platform/ConstraintTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/platform/ConstraintTest.java
@@ -17,6 +17,7 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import com.google.devtools.build.lib.analysis.ConfiguredTarget;
+import com.google.devtools.build.lib.analysis.platform.PlatformProviderUtils;
 import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
 import com.google.devtools.build.lib.cmdline.Label;
 import org.junit.Before;
@@ -45,22 +46,22 @@
   public void testConstraint() throws Exception {
     ConfiguredTarget setting = getConfiguredTarget("//constraint:basic");
     assertThat(setting).isNotNull();
-    assertThat(ConstraintSetting.constraintSetting(setting)).isNotNull();
-    assertThat(ConstraintSetting.constraintSetting(setting)).isNotNull();
-    assertThat(ConstraintSetting.constraintSetting(setting).label())
+    assertThat(PlatformProviderUtils.constraintSetting(setting)).isNotNull();
+    assertThat(PlatformProviderUtils.constraintSetting(setting)).isNotNull();
+    assertThat(PlatformProviderUtils.constraintSetting(setting).label())
         .isEqualTo(Label.parseAbsolute("//constraint:basic"));
     ConfiguredTarget fooValue = getConfiguredTarget("//constraint:foo");
     assertThat(fooValue).isNotNull();
-    assertThat(ConstraintValue.constraintValue(fooValue)).isNotNull();
-    assertThat(ConstraintValue.constraintValue(fooValue).constraint().label())
+    assertThat(PlatformProviderUtils.constraintValue(fooValue)).isNotNull();
+    assertThat(PlatformProviderUtils.constraintValue(fooValue).constraint().label())
         .isEqualTo(Label.parseAbsolute("//constraint:basic"));
-    assertThat(ConstraintValue.constraintValue(fooValue).label())
+    assertThat(PlatformProviderUtils.constraintValue(fooValue).label())
         .isEqualTo(Label.parseAbsolute("//constraint:foo"));
     ConfiguredTarget barValue = getConfiguredTarget("//constraint:bar");
     assertThat(barValue).isNotNull();
-    assertThat(ConstraintValue.constraintValue(barValue).constraint().label())
+    assertThat(PlatformProviderUtils.constraintValue(barValue).constraint().label())
         .isEqualTo(Label.parseAbsolute("//constraint:basic"));
-    assertThat(ConstraintValue.constraintValue(barValue).label())
+    assertThat(PlatformProviderUtils.constraintValue(barValue).label())
         .isEqualTo(Label.parseAbsolute("//constraint:bar"));
   }
 
diff --git a/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java b/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java
index ee430b5..152aca3 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java
@@ -21,6 +21,7 @@
 import com.google.devtools.build.lib.analysis.platform.ConstraintSettingInfo;
 import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
 import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
+import com.google.devtools.build.lib.analysis.platform.PlatformProviderUtils;
 import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
 import com.google.devtools.build.lib.cmdline.Label;
 import com.google.devtools.build.lib.util.CPU;
@@ -57,7 +58,7 @@
     ConfiguredTarget platform = getConfiguredTarget("//constraint:plat1");
     assertThat(platform).isNotNull();
 
-    PlatformInfo provider = Platform.platform(platform);
+    PlatformInfo provider = PlatformProviderUtils.platform(platform);
     assertThat(provider).isNotNull();
     assertThat(provider.constraints()).hasSize(1);
     ConstraintSettingInfo constraintSetting =
@@ -89,7 +90,7 @@
     ConfiguredTarget platform = getConfiguredTarget("//host:host_platform");
     assertThat(platform).isNotNull();
 
-    PlatformInfo provider = Platform.platform(platform);
+    PlatformInfo provider = PlatformProviderUtils.platform(platform);
     assertThat(provider).isNotNull();
 
     // Check the CPU and OS.
@@ -135,7 +136,7 @@
     ConfiguredTarget platform = getConfiguredTarget("//constraint/remote:plat_remote");
     assertThat(platform).isNotNull();
 
-    PlatformInfo provider = Platform.platform(platform);
+    PlatformInfo provider = PlatformProviderUtils.platform(platform);
     assertThat(provider).isNotNull();
     assertThat(provider.remoteExecutionProperties())
         .containsExactlyEntriesIn(ImmutableMap.of("foo", "val1", "bar", "val2"));