From 248581e8c72687c85b5c41edaea6780c8701aa55 Mon Sep 17 00:00:00 2001 From: John Cater Date: Mon, 1 May 2017 17:41:12 +0200 Subject: Move platform providers to a new package to break cyclic dependencies. Part of #2219. Change-Id: I87c7bc9fbfb38d3dbdf193b46247901d0f2a838d PiperOrigin-RevId: 154719063 --- src/main/java/com/google/devtools/build/lib/BUILD | 2 + .../devtools/build/lib/analysis/platform/BUILD | 24 +++ .../analysis/platform/ConstraintSettingInfo.java | 63 ++++++ .../lib/analysis/platform/ConstraintValueInfo.java | 73 +++++++ .../build/lib/analysis/platform/PlatformInfo.java | 207 ++++++++++++++++++ .../build/lib/analysis/platform/ToolchainInfo.java | 103 +++++++++ .../google/devtools/build/lib/rules/platform/BUILD | 1 + .../lib/rules/platform/ConstraintSetting.java | 28 +++ .../lib/rules/platform/ConstraintSettingInfo.java | 90 -------- .../build/lib/rules/platform/ConstraintValue.java | 31 ++- .../lib/rules/platform/ConstraintValueInfo.java | 100 --------- .../lib/rules/platform/ConstraintValueRule.java | 1 + .../build/lib/rules/platform/Platform.java | 38 +++- .../build/lib/rules/platform/PlatformCommon.java | 18 +- .../build/lib/rules/platform/PlatformInfo.java | 234 --------------------- .../build/lib/rules/platform/PlatformRule.java | 1 + .../build/lib/rules/platform/ToolchainInfo.java | 131 ------------ 17 files changed, 578 insertions(+), 567 deletions(-) create mode 100644 src/main/java/com/google/devtools/build/lib/analysis/platform/BUILD create mode 100644 src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfo.java create mode 100644 src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfo.java create mode 100644 src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformInfo.java create mode 100644 src/main/java/com/google/devtools/build/lib/analysis/platform/ToolchainInfo.java delete mode 100644 src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingInfo.java delete mode 100644 src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueInfo.java delete mode 100644 src/main/java/com/google/devtools/build/lib/rules/platform/PlatformInfo.java delete mode 100644 src/main/java/com/google/devtools/build/lib/rules/platform/ToolchainInfo.java (limited to 'src/main/java/com/google/devtools/build') diff --git a/src/main/java/com/google/devtools/build/lib/BUILD b/src/main/java/com/google/devtools/build/lib/BUILD index 6e0fb14e40..02086d9fae 100644 --- a/src/main/java/com/google/devtools/build/lib/BUILD +++ b/src/main/java/com/google/devtools/build/lib/BUILD @@ -35,6 +35,7 @@ filegroup( "//src/main/java/com/google/devtools/build/lib/rules/genquery:srcs", "//src/main/java/com/google/devtools/build/lib/rules/genrule:srcs", "//src/main/java/com/google/devtools/build/lib/rules/objc:srcs", + "//src/main/java/com/google/devtools/build/lib/analysis/platform:srcs", "//src/main/java/com/google/devtools/build/lib/rules/platform:srcs", "//src/main/java/com/google/devtools/build/lib/sandbox:srcs", "//src/main/java/com/google/devtools/build/lib/ssd:srcs", @@ -1221,6 +1222,7 @@ filegroup( "//src/main/java/com/google/devtools/build/lib/rules/genquery:srcs", "//src/main/java/com/google/devtools/build/lib/rules/genrule:srcs", "//src/main/java/com/google/devtools/build/lib/rules/objc:srcs", + "//src/main/java/com/google/devtools/build/lib/analysis/platform:srcs", "//src/main/java/com/google/devtools/build/lib/rules/platform:srcs", ], ) 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 new file mode 100644 index 0000000000..ce3a69d759 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/analysis/platform/BUILD @@ -0,0 +1,24 @@ +# Description: +# Providers defined for platforms, constraints, and toolchains. + +package( + default_visibility = ["//src:__subpackages__"], +) + +java_library( + name = "platform", + srcs = glob([ + "*.java", + ]), + deps = [ + "//src/main/java/com/google/devtools/build/lib:packages", + "//src/main/java/com/google/devtools/build/lib:skylarkinterface", + "//third_party:guava", + ], +) + +filegroup( + name = "srcs", + testonly = 0, # All srcs should be not test only, overwrite package default. + srcs = glob(["**"]), +) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfo.java b/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfo.java new file mode 100644 index 0000000000..817580e8fa --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfo.java @@ -0,0 +1,63 @@ +// 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.collect.ImmutableMap; +import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import com.google.devtools.build.lib.packages.ClassObjectConstructor; +import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; +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; + +/** Provider for a platform constraint setting that is available to be fulfilled. */ +@SkylarkModule( + name = "ConstraintSettingInfo", + doc = "A specific constraint setting that may be used to define a platform.", + category = SkylarkModuleCategory.PROVIDER +) +@Immutable +public class ConstraintSettingInfo extends SkylarkClassObject { + + /** Name used in Skylark for accessing this provider. */ + public static final String SKYLARK_NAME = "ConstraintSettingInfo"; + + /** Skylark constructor and identifier for this provider. */ + public static final ClassObjectConstructor SKYLARK_CONSTRUCTOR = + new NativeClassObjectConstructor(SKYLARK_NAME) {}; + + /** Identifier used to retrieve this provider from rules which export it. */ + public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER = + SkylarkProviderIdentifier.forKey(SKYLARK_CONSTRUCTOR.getKey()); + + private final Label label; + + private ConstraintSettingInfo(Label label) { + super(SKYLARK_CONSTRUCTOR, ImmutableMap.of("label", label)); + + this.label = label; + } + + public Label label() { + return label; + } + + /** Returns a new {@link ConstraintSettingInfo} with the given data. */ + public static ConstraintSettingInfo create(Label constraintSetting) { + return new ConstraintSettingInfo(constraintSetting); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfo.java b/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfo.java new file mode 100644 index 0000000000..9e58126264 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfo.java @@ -0,0 +1,73 @@ +// 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.collect.ImmutableMap; +import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import com.google.devtools.build.lib.packages.ClassObjectConstructor; +import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; +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; + +/** Provider for a platform constraint value that fulfills a {@link ConstraintSettingInfo}. */ +@SkylarkModule( + name = "ConstraintValueProvider", + doc = "A value for a constraint setting that can be used to define a platform.", + category = SkylarkModuleCategory.PROVIDER +) +@Immutable +public class ConstraintValueInfo extends SkylarkClassObject { + + /** Name used in Skylark for accessing this provider. */ + public static final String SKYLARK_NAME = "ConstraintValueInfo"; + + /** Skylark constructor and identifier for this provider. */ + public static final ClassObjectConstructor SKYLARK_CONSTRUCTOR = + new NativeClassObjectConstructor(SKYLARK_NAME) {}; + + /** Identifier used to retrieve this provider from rules which export it. */ + public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER = + SkylarkProviderIdentifier.forKey(SKYLARK_CONSTRUCTOR.getKey()); + + private final ConstraintSettingInfo constraint; + private final Label label; + + private ConstraintValueInfo(ConstraintSettingInfo constraint, Label label) { + super( + SKYLARK_CONSTRUCTOR, + ImmutableMap.of( + "constraint", constraint, + "label", label)); + + this.constraint = constraint; + this.label = label; + } + + public ConstraintSettingInfo constraint() { + return constraint; + } + + public Label label() { + return label; + } + + /** Returns a new {@link ConstraintValueInfo} with the given data. */ + public static ConstraintValueInfo create(ConstraintSettingInfo constraint, Label value) { + return new ConstraintValueInfo(constraint, value); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformInfo.java b/src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformInfo.java new file mode 100644 index 0000000000..64b9262adb --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformInfo.java @@ -0,0 +1,207 @@ +// 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.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Multimap; +import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import com.google.devtools.build.lib.packages.ClassObjectConstructor; +import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; +import com.google.devtools.build.lib.packages.SkylarkClassObject; +import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** Provider for a platform, which is a group of constraints and values. */ +@SkylarkModule( + name = "PlatformInfo", + doc = "Provides access to data about a specific platform.", + category = SkylarkModuleCategory.PROVIDER +) +@Immutable +public class PlatformInfo extends SkylarkClassObject { + + /** Name used in Skylark for accessing this provider. */ + public static final String SKYLARK_NAME = "PlatformInfo"; + + /** Skylark constructor and identifier for this provider. */ + public static final ClassObjectConstructor SKYLARK_CONSTRUCTOR = + new NativeClassObjectConstructor(SKYLARK_NAME) {}; + + /** Identifier used to retrieve this provider from rules which export it. */ + public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER = + SkylarkProviderIdentifier.forKey(SKYLARK_CONSTRUCTOR.getKey()); + + private final ImmutableList constraints; + private final ImmutableMap remoteExecutionProperties; + + private PlatformInfo( + ImmutableList constraints, + ImmutableMap remoteExecutionProperties) { + super(SKYLARK_CONSTRUCTOR, ImmutableMap.of("constraints", constraints)); + + this.constraints = constraints; + this.remoteExecutionProperties = remoteExecutionProperties; + } + + public ImmutableList constraints() { + return constraints; + } + + @SkylarkCallable( + name = "remoteExecutionProperties", + doc = "Properties that are available for the use of remote execution.", + structField = true + ) + public ImmutableMap remoteExecutionProperties() { + return remoteExecutionProperties; + } + + /** Returns a new {@link Builder} for creating a fresh {@link PlatformInfo} instance. */ + public static Builder builder() { + return new Builder(); + } + + /** Builder class to facilitate creating valid {@link PlatformInfo} instances. */ + public static class Builder { + private final List constraints = new ArrayList<>(); + private final Map remoteExecutionProperties = new HashMap<>(); + + /** + * Adds the given constraint value to the constraints that define this {@link PlatformInfo}. + * + * @param constraint the constraint to add + * @return the {@link Builder} instance for method chaining + */ + public Builder addConstraint(ConstraintValueInfo constraint) { + this.constraints.add(constraint); + return this; + } + + /** + * Adds the given constraint values to the constraints that define this {@link PlatformInfo}. + * + * @param constraints the constraints to add + * @return the {@link Builder} instance for method chaining + */ + public Builder addConstraints(Iterable constraints) { + for (ConstraintValueInfo constraint : constraints) { + this.addConstraint(constraint); + } + + return this; + } + + /** + * Adds the given key/value pair to the data being sent to a potential remote executor. If the + * key already exists in the map, the previous value will be overwritten. + * + * @param key the key to be used + * @param value the value to be used + * @return the {@link Builder} instance for method chaining + */ + public Builder addRemoteExecutionProperty(String key, String value) { + this.remoteExecutionProperties.put(key, value); + return this; + } + + /** + * Adds the given properties to the data being sent to a potential remote executor. If any key + * already exists in the map, the previous value will be overwritten. + * + * @param properties the properties to be added + * @return the {@link Builder} instance for method chaining + */ + public Builder addRemoteExecutionProperties(Map properties) { + for (Map.Entry entry : properties.entrySet()) { + this.addRemoteExecutionProperty(entry.getKey(), entry.getValue()); + } + return this; + } + + /** + * Returns the new {@link PlatformInfo} instance. + * + * @throws DuplicateConstraintException if more than one constraint value exists for the same + * constraint setting + */ + public PlatformInfo build() throws DuplicateConstraintException { + ImmutableList validatedConstraints = validateConstraints(constraints); + return new PlatformInfo(validatedConstraints, ImmutableMap.copyOf(remoteExecutionProperties)); + } + + private ImmutableList validateConstraints( + Iterable constraintValues) throws DuplicateConstraintException { + Multimap constraints = ArrayListMultimap.create(); + + for (ConstraintValueInfo constraintValue : constraintValues) { + constraints.put(constraintValue.constraint(), constraintValue); + } + + // Are there any settings with more than one value? + for (ConstraintSettingInfo constraintSetting : constraints.keySet()) { + if (constraints.get(constraintSetting).size() > 1) { + // Only reports the first case of this error. + throw new DuplicateConstraintException( + constraintSetting, constraints.get(constraintSetting)); + } + } + + return ImmutableList.copyOf(constraints.values()); + } + } + + /** + * Exception class used when more than one {@link ConstraintValueInfo} for the same {@link + * ConstraintSettingInfo} is added to a {@link Builder}. + */ + public static class DuplicateConstraintException extends Exception { + private final ImmutableSet duplicateConstraints; + + public DuplicateConstraintException( + ConstraintSettingInfo constraintSetting, + Iterable duplicateConstraintValues) { + super(formatError(constraintSetting, duplicateConstraintValues)); + this.duplicateConstraints = ImmutableSet.copyOf(duplicateConstraintValues); + } + + public ImmutableSet duplicateConstraints() { + return duplicateConstraints; + } + + private static String formatError( + ConstraintSettingInfo constraintSetting, + Iterable duplicateConstraints) { + StringBuilder constraintValuesDescription = new StringBuilder(); + for (ConstraintValueInfo constraintValue : duplicateConstraints) { + if (constraintValuesDescription.length() > 0) { + constraintValuesDescription.append(", "); + } + constraintValuesDescription.append(constraintValue.label()); + } + return String.format( + "Duplicate constraint_values for constraint_setting %s: %s", + constraintSetting.label(), constraintValuesDescription.toString()); + } + } +} diff --git a/src/main/java/com/google/devtools/build/lib/analysis/platform/ToolchainInfo.java b/src/main/java/com/google/devtools/build/lib/analysis/platform/ToolchainInfo.java new file mode 100644 index 0000000000..1e5e45f462 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/analysis/platform/ToolchainInfo.java @@ -0,0 +1,103 @@ +// 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.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import com.google.devtools.build.lib.events.Location; +import com.google.devtools.build.lib.packages.ClassObjectConstructor; +import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; +import com.google.devtools.build.lib.packages.SkylarkClassObject; +import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; +import java.util.Map; + +/** + * A provider that supplied information about a specific language toolchain, including what platform + * constraints are required for execution and for the target platform. + */ +@SkylarkModule( + name = "ToolchainInfo", + doc = "Provides access to data about a specific toolchain.", + category = SkylarkModuleCategory.PROVIDER +) +@Immutable +public class ToolchainInfo extends SkylarkClassObject { + + /** Name used in Skylark for accessing this provider. */ + public static final String SKYLARK_NAME = "ToolchainInfo"; + + /** Skylark constructor and identifier for this provider. */ + public static final ClassObjectConstructor SKYLARK_CONSTRUCTOR = + new NativeClassObjectConstructor(SKYLARK_NAME) {}; + + /** Identifier used to retrieve this provider from rules which export it. */ + public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER = + SkylarkProviderIdentifier.forKey(SKYLARK_CONSTRUCTOR.getKey()); + + private final ImmutableList execConstraints; + private final ImmutableList targetConstraints; + + public ToolchainInfo( + Iterable execConstraints, + Iterable targetConstraints, + Map toolchainData, + Location loc) { + this( + ImmutableList.copyOf(execConstraints), + ImmutableList.copyOf(targetConstraints), + toolchainData, + loc); + } + + public ToolchainInfo( + ImmutableList execConstraints, + ImmutableList targetConstraints, + Map toolchainData, + Location loc) { + super( + SKYLARK_CONSTRUCTOR, + ImmutableMap.builder() + .put("exec_compatible_with", execConstraints) + .put("target_compatible_with", targetConstraints) + .putAll(toolchainData) + .build(), + loc); + + this.execConstraints = execConstraints; + this.targetConstraints = targetConstraints; + } + + @SkylarkCallable( + name = "exec_compatible_with", + doc = "The constraints on the execution platforms this toolchain supports.", + structField = true + ) + public ImmutableList execConstraints() { + return execConstraints; + } + + @SkylarkCallable( + name = "target_compatible_with", + doc = "The constraints on the target platforms this toolchain supports.", + structField = true + ) + public ImmutableList targetConstraints() { + return targetConstraints; + } +} 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 8b3f0a0d57..c3a8cc5103 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 @@ -14,6 +14,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib:build-base", "//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", "//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 653ea2fe4f..6c903fd04a 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,13 +14,18 @@ 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 @@ -39,4 +44,27 @@ public class ConstraintSetting implements RuleConfiguredTargetFactory { .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 constraintSettings( + Iterable targets) { + return Iterables.transform( + targets, + new Function() { + @Override + public ConstraintSettingInfo apply(TransitiveInfoCollection target) { + return constraintSetting(target); + } + }); + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingInfo.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingInfo.java deleted file mode 100644 index 3b203e64ca..0000000000 --- a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingInfo.java +++ /dev/null @@ -1,90 +0,0 @@ -// 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.rules.platform; - -import com.google.common.base.Function; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; -import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; -import com.google.devtools.build.lib.cmdline.Label; -import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; -import com.google.devtools.build.lib.packages.ClassObjectConstructor; -import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; -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.util.Preconditions; - -/** Provider for a platform constraint setting that is available to be fulfilled. */ -@SkylarkModule( - name = "ConstraintSettingInfo", - doc = "A specific constraint setting that may be used to define a platform.", - category = SkylarkModuleCategory.PROVIDER -) -@Immutable -public class ConstraintSettingInfo extends SkylarkClassObject { - - /** Name used in Skylark for accessing this provider. */ - static final String SKYLARK_NAME = "ConstraintSettingInfo"; - - /** Skylark constructor and identifier for this provider. */ - static final ClassObjectConstructor SKYLARK_CONSTRUCTOR = - new NativeClassObjectConstructor(SKYLARK_NAME) {}; - - /** Identifier used to retrieve this provider from rules which export it. */ - public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER = - SkylarkProviderIdentifier.forKey(SKYLARK_CONSTRUCTOR.getKey()); - - private final Label label; - - private ConstraintSettingInfo(Label label) { - super(SKYLARK_CONSTRUCTOR, ImmutableMap.of("label", label)); - - this.label = label; - } - - public Label label() { - return label; - } - - /** Retrieves and casts the provider from the given target. */ - public static ConstraintSettingInfo fromTarget(TransitiveInfoCollection target) { - Object provider = target.get(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 fromTargets( - Iterable targets) { - return Iterables.transform( - targets, - new Function() { - @Override - public ConstraintSettingInfo apply(TransitiveInfoCollection target) { - return fromTarget(target); - } - }); - } - - /** Returns a new {@link ConstraintSettingInfo} with the given data. */ - public static ConstraintSettingInfo create(Label constraintSetting) { - return new ConstraintSettingInfo(constraintSetting); - } -} 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 e1aa6d933b..1eb3ba1802 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,6 +14,8 @@ 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; @@ -21,7 +23,11 @@ import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode; 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.rules.RuleConfiguredTargetFactory; +import com.google.devtools.build.lib.util.Preconditions; /** Defines a potential value of a constraint. */ public class ConstraintValue implements RuleConfiguredTargetFactory { @@ -31,7 +37,7 @@ public class ConstraintValue implements RuleConfiguredTargetFactory { throws InterruptedException, RuleErrorException { ConstraintSettingInfo constraint = - ConstraintSettingInfo.fromTarget( + ConstraintSetting.constraintSetting( ruleContext.getPrerequisite( ConstraintValueRule.CONSTRAINT_SETTING_ATTR, Mode.DONT_CHECK)); @@ -42,4 +48,27 @@ public class ConstraintValue implements RuleConfiguredTargetFactory { .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 constraintValues( + Iterable targets) { + return Iterables.transform( + targets, + new Function() { + @Override + public ConstraintValueInfo apply(TransitiveInfoCollection target) { + return constraintValue(target); + } + }); + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueInfo.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueInfo.java deleted file mode 100644 index 36854ffbfd..0000000000 --- a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueInfo.java +++ /dev/null @@ -1,100 +0,0 @@ -// 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.rules.platform; - -import com.google.common.base.Function; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; -import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; -import com.google.devtools.build.lib.cmdline.Label; -import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; -import com.google.devtools.build.lib.packages.ClassObjectConstructor; -import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; -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.util.Preconditions; - -/** Provider for a platform constraint value that fulfills a {@link ConstraintSettingInfo}. */ -@SkylarkModule( - name = "ConstraintValueProvider", - doc = "A value for a constraint setting that can be used to define a platform.", - category = SkylarkModuleCategory.PROVIDER -) -@Immutable -public class ConstraintValueInfo extends SkylarkClassObject { - - /** Name used in Skylark for accessing this provider. */ - static final String SKYLARK_NAME = "ConstraintValueInfo"; - - /** Skylark constructor and identifier for this provider. */ - static final ClassObjectConstructor SKYLARK_CONSTRUCTOR = - new NativeClassObjectConstructor(SKYLARK_NAME) {}; - - /** Identifier used to retrieve this provider from rules which export it. */ - public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER = - SkylarkProviderIdentifier.forKey(SKYLARK_CONSTRUCTOR.getKey()); - - private final ConstraintSettingInfo constraint; - private final Label label; - - private ConstraintValueInfo(ConstraintSettingInfo constraint, Label label) { - super( - SKYLARK_CONSTRUCTOR, - ImmutableMap.of( - "constraint", constraint, - "label", label)); - - this.constraint = constraint; - this.label = label; - } - - public ConstraintSettingInfo constraint() { - return constraint; - } - - public Label label() { - return label; - } - - /** Retrieves and casts the provider from the given target. */ - public static ConstraintValueInfo fromTarget(TransitiveInfoCollection target) { - Object provider = target.get(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 fromTargets( - Iterable targets) { - return Iterables.transform( - targets, - new Function() { - @Override - public ConstraintValueInfo apply(TransitiveInfoCollection target) { - return fromTarget(target); - } - }); - } - - /** Returns a new {@link ConstraintValueInfo} with the given data. */ - public static ConstraintValueInfo create(ConstraintSettingInfo constraint, Label value) { - return new ConstraintValueInfo(constraint, value); - } -} diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueRule.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueRule.java index 5593862754..ae8fd0495c 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueRule.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.analysis.BaseRuleClasses; import com.google.devtools.build.lib.analysis.RuleDefinition; import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; +import com.google.devtools.build.lib.analysis.platform.ConstraintSettingInfo; import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.packages.RuleClass; import com.google.devtools.build.lib.syntax.Type; 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 d1c48e9445..184f7a8a07 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,6 +14,8 @@ 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; @@ -21,11 +23,14 @@ import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode; 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.rules.RuleConfiguredTargetFactory; -import com.google.devtools.build.lib.rules.platform.PlatformInfo.DuplicateConstraintException; 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. */ @@ -41,7 +46,7 @@ public class Platform implements RuleConfiguredTargetFactory { autodetectHostConstraints(ruleContext, platformBuilder); } else { platformBuilder.addConstraints( - ConstraintValueInfo.fromTargets( + ConstraintValue.constraintValues( ruleContext.getPrerequisites(PlatformRule.CONSTRAINT_VALUES_ATTR, Mode.DONT_CHECK))); } @@ -52,7 +57,7 @@ public class Platform implements RuleConfiguredTargetFactory { PlatformInfo platformInfo; try { platformInfo = platformBuilder.build(); - } catch (DuplicateConstraintException e) { + } catch (PlatformInfo.DuplicateConstraintException e) { // Report the error and return null. ruleContext.attributeError(PlatformRule.CONSTRAINT_VALUES_ATTR, e.getMessage()); return null; @@ -72,7 +77,7 @@ public class Platform implements RuleConfiguredTargetFactory { // Add the CPU. CPU cpu = CPU.getCurrent(); Iterable cpuConstraintValues = - ConstraintValueInfo.fromTargets( + ConstraintValue.constraintValues( ruleContext.getPrerequisites(PlatformRule.HOST_CPU_CONSTRAINTS_ATTR, Mode.DONT_CHECK)); for (ConstraintValueInfo constraint : cpuConstraintValues) { if (cpu.getCanonicalName().equals(constraint.label().getName())) { @@ -84,7 +89,7 @@ public class Platform implements RuleConfiguredTargetFactory { // Add the OS. OS os = OS.getCurrent(); Iterable osConstraintValues = - ConstraintValueInfo.fromTargets( + ConstraintValue.constraintValues( ruleContext.getPrerequisites(PlatformRule.HOST_OS_CONSTRAINTS_ATTR, Mode.DONT_CHECK)); for (ConstraintValueInfo constraint : osConstraintValues) { if (os.getCanonicalName().equals(constraint.label().getName())) { @@ -93,4 +98,27 @@ public class Platform implements RuleConfiguredTargetFactory { } } } + + /** 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 platforms( + Iterable targets) { + return Iterables.transform( + targets, + new Function() { + @Override + public PlatformInfo apply(TransitiveInfoCollection target) { + return platform(target); + } + }); + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformCommon.java b/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformCommon.java index 3a67979bb8..09d31c15e5 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformCommon.java @@ -16,6 +16,10 @@ package com.google.devtools.build.lib.rules.platform; import com.google.common.collect.ImmutableMap; 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.PlatformInfo; +import com.google.devtools.build.lib.analysis.platform.ToolchainInfo; import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.packages.ClassObjectConstructor; import com.google.devtools.build.lib.skylarkinterface.Param; @@ -62,11 +66,11 @@ public class PlatformCommon { public ClassObjectConstructor getConstraintValueInfoConstructor() { return ConstraintValueInfo.SKYLARK_CONSTRUCTOR; } - + @SkylarkCallable( - name = ToolchainInfo.SKYLARK_NAME, - doc = "The key used to retrieve the provider containing toolchain data.", - structField = true + name = ToolchainInfo.SKYLARK_NAME, + doc = "The key used to retrieve the provider containing toolchain data.", + structField = true ) public ClassObjectConstructor getToolchainInfoConstructor() { return ToolchainInfo.SKYLARK_CONSTRUCTOR; @@ -85,6 +89,7 @@ public class PlatformCommon { @Param( name = "exec_compatible_with", type = SkylarkList.class, + generic1 = TransitiveInfoCollection.class, defaultValue = "[]", named = true, positional = false, @@ -93,6 +98,7 @@ public class PlatformCommon { @Param( name = "target_compatible_with", type = SkylarkList.class, + generic1 = TransitiveInfoCollection.class, defaultValue = "[]", named = true, positional = false, @@ -118,9 +124,9 @@ public class PlatformCommon { throws ConversionException, EvalException { Iterable execConstraints = - ConstraintValueInfo.fromTargets(execCompatibleWith); + ConstraintValue.constraintValues(execCompatibleWith); Iterable targetConstraints = - ConstraintValueInfo.fromTargets(targetCompatibleWith); + ConstraintValue.constraintValues(targetCompatibleWith); ImmutableMap toolchainData = ImmutableMap.copyOf( SkylarkDict.castSkylarkDictOrNoneToDict( diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformInfo.java b/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformInfo.java deleted file mode 100644 index 3f980733cf..0000000000 --- a/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformInfo.java +++ /dev/null @@ -1,234 +0,0 @@ -// 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.rules.platform; - -import com.google.common.base.Function; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; -import com.google.common.collect.Multimap; -import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; -import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; -import com.google.devtools.build.lib.packages.ClassObjectConstructor; -import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; -import com.google.devtools.build.lib.packages.SkylarkClassObject; -import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; -import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; -import com.google.devtools.build.lib.util.Preconditions; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** Provider for a platform, which is a group of constraints and values. */ -@SkylarkModule( - name = "PlatformInfo", - doc = "Provides access to data about a specific platform.", - category = SkylarkModuleCategory.PROVIDER -) -@Immutable -public class PlatformInfo extends SkylarkClassObject { - - /** Name used in Skylark for accessing this provider. */ - static final String SKYLARK_NAME = "PlatformInfo"; - - /** Skylark constructor and identifier for this provider. */ - static final ClassObjectConstructor SKYLARK_CONSTRUCTOR = - new NativeClassObjectConstructor(SKYLARK_NAME) {}; - - /** Identifier used to retrieve this provider from rules which export it. */ - public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER = - SkylarkProviderIdentifier.forKey(SKYLARK_CONSTRUCTOR.getKey()); - - private final ImmutableList constraints; - private final ImmutableMap remoteExecutionProperties; - - private PlatformInfo( - ImmutableList constraints, - ImmutableMap remoteExecutionProperties) { - super(SKYLARK_CONSTRUCTOR, ImmutableMap.of("constraints", constraints)); - - this.constraints = constraints; - this.remoteExecutionProperties = remoteExecutionProperties; - } - - public ImmutableList constraints() { - return constraints; - } - - @SkylarkCallable( - name = "remoteExecutionProperties", - doc = "Properties that are available for the use of remote execution.", - structField = true - ) - public ImmutableMap remoteExecutionProperties() { - return remoteExecutionProperties; - } - - /** Retrieves and casts the provider from the given target. */ - public static PlatformInfo fromTarget(TransitiveInfoCollection target) { - Object provider = target.get(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 fromTargets( - Iterable targets) { - return Iterables.transform( - targets, - new Function() { - @Override - public PlatformInfo apply(TransitiveInfoCollection target) { - return fromTarget(target); - } - }); - } - - /** Returns a new {@link Builder} for creating a fresh {@link PlatformInfo} instance. */ - public static Builder builder() { - return new Builder(); - } - - /** Builder class to facilitate creating valid {@link PlatformInfo} instances. */ - public static class Builder { - private final List constraints = new ArrayList<>(); - private final Map remoteExecutionProperties = new HashMap<>(); - - /** - * Adds the given constraint value to the constraints that define this {@link PlatformInfo}. - * - * @param constraint the constraint to add - * @return the {@link Builder} instance for method chaining - */ - public Builder addConstraint(ConstraintValueInfo constraint) { - this.constraints.add(constraint); - return this; - } - - /** - * Adds the given constraint values to the constraints that define this {@link PlatformInfo}. - * - * @param constraints the constraints to add - * @return the {@link Builder} instance for method chaining - */ - public Builder addConstraints(Iterable constraints) { - for (ConstraintValueInfo constraint : constraints) { - this.addConstraint(constraint); - } - - return this; - } - - /** - * Adds the given key/value pair to the data being sent to a potential remote executor. If the - * key already exists in the map, the previous value will be overwritten. - * - * @param key the key to be used - * @param value the value to be used - * @return the {@link Builder} instance for method chaining - */ - public Builder addRemoteExecutionProperty(String key, String value) { - this.remoteExecutionProperties.put(key, value); - return this; - } - - /** - * Adds the given properties to the data being sent to a potential remote executor. If any key - * already exists in the map, the previous value will be overwritten. - * - * @param properties the properties to be added - * @return the {@link Builder} instance for method chaining - */ - public Builder addRemoteExecutionProperties(Map properties) { - for (Map.Entry entry : properties.entrySet()) { - this.addRemoteExecutionProperty(entry.getKey(), entry.getValue()); - } - return this; - } - - /** - * Returns the new {@link PlatformInfo} instance. - * - * @throws DuplicateConstraintException if more than one constraint value exists for the same - * constraint setting - */ - public PlatformInfo build() throws DuplicateConstraintException { - ImmutableList validatedConstraints = validateConstraints(constraints); - return new PlatformInfo(validatedConstraints, ImmutableMap.copyOf(remoteExecutionProperties)); - } - - private ImmutableList validateConstraints( - Iterable constraintValues) throws DuplicateConstraintException { - Multimap constraints = ArrayListMultimap.create(); - - for (ConstraintValueInfo constraintValue : constraintValues) { - constraints.put(constraintValue.constraint(), constraintValue); - } - - // Are there any settings with more than one value? - for (ConstraintSettingInfo constraintSetting : constraints.keySet()) { - if (constraints.get(constraintSetting).size() > 1) { - // Only reports the first case of this error. - throw new DuplicateConstraintException( - constraintSetting, constraints.get(constraintSetting)); - } - } - - return ImmutableList.copyOf(constraints.values()); - } - } - - /** - * Exception class used when more than one {@link ConstraintValueInfo} for the same {@link - * ConstraintSettingInfo} is added to a {@link Builder}. - */ - public static class DuplicateConstraintException extends Exception { - private final ImmutableSet duplicateConstraints; - - public DuplicateConstraintException( - ConstraintSettingInfo constraintSetting, - Iterable duplicateConstraintValues) { - super(formatError(constraintSetting, duplicateConstraintValues)); - this.duplicateConstraints = ImmutableSet.copyOf(duplicateConstraintValues); - } - - public ImmutableSet duplicateConstraints() { - return duplicateConstraints; - } - - private static String formatError( - ConstraintSettingInfo constraintSetting, - Iterable duplicateConstraints) { - StringBuilder constraintValuesDescription = new StringBuilder(); - for (ConstraintValueInfo constraintValue : duplicateConstraints) { - if (constraintValuesDescription.length() > 0) { - constraintValuesDescription.append(", "); - } - constraintValuesDescription.append(constraintValue.label()); - } - return String.format( - "Duplicate constraint_values for constraint_setting %s: %s", - constraintSetting.label(), constraintValuesDescription.toString()); - } - } -} diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformRule.java b/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformRule.java index 126cd9d15d..3e4b0f2b9f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformRule.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.analysis.BaseRuleClasses; import com.google.devtools.build.lib.analysis.RuleDefinition; import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; +import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo; import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.packages.RuleClass; import com.google.devtools.build.lib.syntax.Type; diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ToolchainInfo.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ToolchainInfo.java deleted file mode 100644 index 320ee5e9df..0000000000 --- a/src/main/java/com/google/devtools/build/lib/rules/platform/ToolchainInfo.java +++ /dev/null @@ -1,131 +0,0 @@ -// 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.rules.platform; - -import com.google.common.base.Function; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; -import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; -import com.google.devtools.build.lib.analysis.TransitiveInfoProvider; -import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; -import com.google.devtools.build.lib.events.Location; -import com.google.devtools.build.lib.packages.ClassObjectConstructor; -import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; -import com.google.devtools.build.lib.packages.SkylarkClassObject; -import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; -import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; -import com.google.devtools.build.lib.util.Preconditions; -import java.util.Map; - -/** - * A provider that supplied information about a specific language toolchain, including what platform - * constraints are required for execution and for the target platform. - */ -@SkylarkModule( - name = "ToolchainInfo", - doc = "Provides access to data about a specific toolchain.", - category = SkylarkModuleCategory.PROVIDER -) -@Immutable -public class ToolchainInfo extends SkylarkClassObject implements TransitiveInfoProvider { - - /** Name used in Skylark for accessing this provider. */ - static final String SKYLARK_NAME = "ToolchainInfo"; - - /** Skylark constructor and identifier for this provider. */ - static final ClassObjectConstructor SKYLARK_CONSTRUCTOR = - new NativeClassObjectConstructor(SKYLARK_NAME) {}; - - /** Identifier used to retrieve this provider from rules which export it. */ - private static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER = - SkylarkProviderIdentifier.forKey(SKYLARK_CONSTRUCTOR.getKey()); - - private final ImmutableList execConstraints; - private final ImmutableList targetConstraints; - - public ToolchainInfo( - Iterable execConstraints, - Iterable targetConstraints, - Map toolchainData, - Location loc) { - this( - ImmutableList.copyOf(execConstraints), - ImmutableList.copyOf(targetConstraints), - toolchainData, - loc); - } - - public ToolchainInfo( - ImmutableList execConstraints, - ImmutableList targetConstraints, - Map toolchainData, - Location loc) { - super( - SKYLARK_CONSTRUCTOR, - ImmutableMap.builder() - .put("exec_compatible_with", execConstraints) - .put("target_compatible_with", targetConstraints) - .putAll(toolchainData) - .build(), - loc); - - this.execConstraints = execConstraints; - this.targetConstraints = targetConstraints; - } - - @SkylarkCallable( - name = "exec_compatible_with", - doc = "The constraints on the execution platforms this toolchain supports.", - structField = true - ) - public ImmutableList execConstraints() { - return execConstraints; - } - - @SkylarkCallable( - name = "target_compatible_with", - doc = "The constraints on the target platforms this toolchain supports.", - structField = true - ) - public ImmutableList targetConstraints() { - return targetConstraints; - } - - /** Retrieves and casts the provider from the given target. */ - public static ToolchainInfo fromTarget(TransitiveInfoCollection target) { - Object provider = target.get(SKYLARK_IDENTIFIER); - if (provider == null) { - return null; - } - Preconditions.checkState(provider instanceof ToolchainInfo); - return (ToolchainInfo) provider; - } - - /** Retrieves and casts the providers from the given targets. */ - public static Iterable fromTargets( - Iterable targets) { - return Iterables.transform( - targets, - new Function() { - @Override - public ToolchainInfo apply(TransitiveInfoCollection target) { - return fromTarget(target); - } - }); - } -} -- cgit v1.2.3