diff options
author | John Cater <jcater@google.com> | 2018-08-03 06:59:36 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-08-03 07:00:58 -0700 |
commit | 8499426cd83b316a5f590fcfdbf10774deee7de4 (patch) | |
tree | 27f4ce51df9ef94faa2648790c3eb4f8aa83aeac | |
parent | 510aca0c993d89cccdf2da1b8bcafff35a0d030f (diff) |
Update config_setting to get the target platform from the ToolchainContext.
Closes #5746.
Change-Id: Ib5946d405bf22af131f9dbd7b93393958b0848b4
PiperOrigin-RevId: 207261030
4 files changed, 39 insertions, 48 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/config/BUILD b/src/main/java/com/google/devtools/build/lib/rules/config/BUILD index d9a0a81cd0..518df01831 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/config/BUILD +++ b/src/main/java/com/google/devtools/build/lib/rules/config/BUILD @@ -28,6 +28,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/collect", "//src/main/java/com/google/devtools/build/lib/collect/nestedset", "//src/main/java/com/google/devtools/build/lib/concurrent", + "//src/main/java/com/google/devtools/build/lib/rules/platform", "//src/main/java/com/google/devtools/build/lib/skyframe/serialization", "//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec", "//src/main/java/com/google/devtools/build/lib/skylarkbuildapi/config", diff --git a/src/main/java/com/google/devtools/build/lib/rules/config/ConfigRuleClasses.java b/src/main/java/com/google/devtools/build/lib/rules/config/ConfigRuleClasses.java index 8c5966bab4..e461ac0dc1 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/config/ConfigRuleClasses.java +++ b/src/main/java/com/google/devtools/build/lib/rules/config/ConfigRuleClasses.java @@ -27,13 +27,9 @@ import com.google.devtools.build.lib.analysis.PlatformConfiguration; import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.analysis.RuleDefinition; import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; -import com.google.devtools.build.lib.cmdline.Label; -import com.google.devtools.build.lib.packages.Attribute.LabelListLateBoundDefault; -import com.google.devtools.build.lib.packages.AttributeMap; import com.google.devtools.build.lib.packages.NonconfigurableAttributeMapper; import com.google.devtools.build.lib.packages.RuleClass; import com.google.devtools.build.lib.syntax.Type; -import java.util.List; /** * Definitions for rule classes that specify or manipulate configuration settings. @@ -74,10 +70,11 @@ public class ConfigRuleClasses { } /** - * A named "partial configuration setting" that specifies a set of command-line - * "flag=value" bindings. + * A named "partial configuration setting" that specifies a set of command-line "flag=value" + * bindings. * * <p>For example: + * * <pre> * config_setting( * name = 'foo', @@ -87,19 +84,37 @@ public class ConfigRuleClasses { * }) * </pre> * - * <p>declares a setting that binds command-line flag <pre>flag1</pre> to value - * <pre>aValue</pre> and <pre>flag2</pre> to <pre>bValue</pre>. + * <p>declares a setting that binds command-line flag + * + * <pre>flag1</pre> + * + * to value + * + * <pre>aValue</pre> + * + * and + * + * <pre>flag2</pre> + * + * to + * + * <pre>bValue</pre> * - * <p>This is used by configurable attributes to determine which branch to - * follow based on which <pre>config_setting</pre> instance matches all its - * flag values in the configurable attribute owner's configuration. + * . + * + * <p>This is used by configurable attributes to determine which branch to follow based on which + * + * <pre>config_setting</pre> + * + * instance matches all its flag values in the configurable attribute owner's configuration. * * <p>This rule isn't accessed through the standard {@link RuleContext#getPrerequisites} - * interface. This is because Bazel constructs a rule's configured attribute map *before* - * its {@link RuleContext} is created (in fact, the map is an input to the context's - * constructor). And the config_settings referenced by the rule's configurable attributes are - * themselves inputs to that map. So Bazel has special logic to read and properly apply - * config_setting instances. See {@link ConfiguredTargetFunction#getConfigConditions} for details. + * interface. This is because Bazel constructs a rule's configured attribute map *before* its + * {@link RuleContext} is created (in fact, the map is an input to the context's constructor). And + * the config_settings referenced by the rule's configurable attributes are themselves inputs to + * that map. So Bazel has special logic to read and properly apply config_setting instances. See + * {@link com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction#getConfigConditions} for + * details. */ public static final class ConfigSettingRule implements RuleDefinition { /** @@ -115,29 +130,12 @@ public class ConfigRuleClasses { public static final String FLAG_SETTINGS_ATTRIBUTE = "flag_values"; /** The name of the attribute that declares constraint_values. */ public static final String CONSTRAINT_VALUES_ATTRIBUTE = "constraint_values"; - /** The name of the late bound attribute that declares the target platforms list. */ - public static final String TARGET_PLATFORMS_ATTRIBUTE = ":target_platforms"; - - /** Implementation for the :target_platform attribute. */ - public static final LabelListLateBoundDefault<?> TARGET_PLATFORMS = - LabelListLateBoundDefault.fromTargetConfiguration( - PlatformConfiguration.class, - (rule, attributes, platformConfig) -> - ConfigSettingRule.getTargetPlatformsIfRelevant(attributes, platformConfig)); - - private static ImmutableList<Label> getTargetPlatformsIfRelevant( - AttributeMap attributes, PlatformConfiguration platformConfig) { - List<Label> constraintValues = attributes.get(CONSTRAINT_VALUES_ATTRIBUTE, LABEL_LIST); - if (constraintValues == null || constraintValues.isEmpty()) { - return ImmutableList.of(); - } else { - return platformConfig.getTargetPlatforms(); - } - } @Override public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) { return builder + .requiresConfigurationFragments(PlatformConfiguration.class) + /* <!-- #BLAZE_RULE(config_setting).ATTRIBUTE(values) --> The set of configuration values that match this rule (expressed as Bazel flags) @@ -279,10 +277,6 @@ public class ConfigRuleClasses { attr(CONSTRAINT_VALUES_ATTRIBUTE, LABEL_LIST) .nonconfigurable(NONCONFIGURABLE_ATTRIBUTE_REASON) .allowedFileTypes()) - .add( - attr(TARGET_PLATFORMS_ATTRIBUTE, LABEL_LIST) - .value(TARGET_PLATFORMS) - .nonconfigurable(NONCONFIGURABLE_ATTRIBUTE_REASON)) .setIsConfigMatcherForConfigSettingOnly() .setOptionReferenceFunctionForConfigSettingOnly( rule -> diff --git a/src/main/java/com/google/devtools/build/lib/rules/config/ConfigRules.java b/src/main/java/com/google/devtools/build/lib/rules/config/ConfigRules.java index 7531493e77..09622578e1 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/config/ConfigRules.java +++ b/src/main/java/com/google/devtools/build/lib/rules/config/ConfigRules.java @@ -18,6 +18,7 @@ import com.google.devtools.build.lib.analysis.BaseRuleClasses; import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider.RuleSet; import com.google.devtools.build.lib.rules.core.CoreRules; +import com.google.devtools.build.lib.rules.platform.PlatformRules; import com.google.devtools.build.lib.skylarkbuildapi.config.ConfigBootstrap; /** @@ -45,6 +46,6 @@ public final class ConfigRules implements RuleSet { @Override public ImmutableList<RuleSet> requires() { - return ImmutableList.of(CoreRules.INSTANCE); + return ImmutableList.of(CoreRules.INSTANCE, PlatformRules.INSTANCE); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/config/ConfigSetting.java b/src/main/java/com/google/devtools/build/lib/rules/config/ConfigSetting.java index ee57673d77..a3eedd13dd 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/config/ConfigSetting.java +++ b/src/main/java/com/google/devtools/build/lib/rules/config/ConfigSetting.java @@ -105,10 +105,7 @@ public class ConfigSetting implements RuleConfiguredTargetFactory { ConfigSettingRule.CONSTRAINT_VALUES_ATTRIBUTE, Mode.DONT_CHECK)); // Get the target platform - Iterable<PlatformInfo> targetPlatforms = - PlatformProviderUtils.platforms( - ruleContext.getPrerequisites( - ConfigSettingRule.TARGET_PLATFORMS_ATTRIBUTE, Mode.DONT_CHECK)); + PlatformInfo targetPlatform = ruleContext.getToolchainContext().targetPlatform(); // Check that this config_setting contains at least one of {values, define_values, // constraint_values} @@ -127,7 +124,7 @@ public class ConfigSetting implements RuleConfiguredTargetFactory { ConfigFeatureFlagMatch.fromAttributeValueAndPrerequisites( userDefinedFlagSettings, flagValues, ruleContext); - boolean constraintValuesMatch = matchesConstraints(constraintValues, targetPlatforms); + boolean constraintValuesMatch = matchesConstraints(constraintValues, targetPlatform); if (ruleContext.hasErrors()) { return null; @@ -309,14 +306,12 @@ public class ConfigSetting implements RuleConfiguredTargetFactory { } private boolean matchesConstraints( - Iterable<ConstraintValueInfo> expected, Iterable<PlatformInfo> targetPlatforms) { + Iterable<ConstraintValueInfo> expected, PlatformInfo targetPlatform) { // config_setting didn't specify any constraint values if (Iterables.isEmpty(expected)) { return true; } - // TODO(jcater): re-evaluate this for multiple target platforms. - PlatformInfo targetPlatform = Iterables.getOnlyElement(targetPlatforms); // config_setting DID specify constraint_value(s) but no target platforms are set // in the configuration. if (Iterables.isEmpty(targetPlatform.constraints())) { |