aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/config
diff options
context:
space:
mode:
authorGravatar mstaib <mstaib@google.com>2017-12-08 13:30:16 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2017-12-08 13:31:41 -0800
commitd899ad535b43b027112213b1ecd7bc0b18938b6e (patch)
tree6fde1faec4d91466ad4542e21a04bf55dac02900 /src/main/java/com/google/devtools/build/lib/rules/config
parent865b8887daca1477216ebe2527ad35f07081c778 (diff)
Make config_feature_flag's default_value optional.
With this change, if the default_value is not set, but the flag has been given a value by a configuration transition, that value is used and no error is produced. If the default_value is not set and the flag has not been given a value by a configuration transition, the rule produces an error (as it did before, but now the error is different). config_feature_flags with default_values behave as they did before. (use the default value if no configured value is set, otherwise use the configured value) Also transition the Optional used in feature flags from Guava to Java 8. RELNOTES: config_feature_flag's default_value is optional. It is only an error to have a config_feature_flag with no default_value if that config_feature_flag has not been set in the configuration it is being evaluated in. PiperOrigin-RevId: 178418907
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/config')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlag.java27
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagConfiguration.java4
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/config/ConfigRuleClasses.java1
3 files changed, 21 insertions, 11 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlag.java b/src/main/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlag.java
index 68bf177ce5..175588cc6d 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlag.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlag.java
@@ -40,6 +40,7 @@ import com.google.devtools.build.lib.packages.AttributeMap;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.syntax.Printer;
import java.util.List;
+import java.util.Optional;
/**
* The implementation of the config_feature_flag rule for defining custom flags for Android rules.
@@ -115,14 +116,17 @@ public class ConfigFeatureFlag implements RuleConfiguredTargetFactory {
+ Printer.repr(duplicates.build()));
}
- String defaultValue = ruleContext.attributes().get("default_value", STRING);
- if (!isValidValue.apply(defaultValue)) {
+ Optional<String> defaultValue =
+ ruleContext.attributes().isAttributeValueExplicitlySpecified("default_value")
+ ? Optional.of(ruleContext.attributes().get("default_value", STRING))
+ : Optional.empty();
+ if (defaultValue.isPresent() && !isValidValue.apply(defaultValue.get())) {
ruleContext.attributeError(
"default_value",
"must be one of "
+ Printer.repr(values.asList())
+ ", but was "
- + Printer.repr(defaultValue));
+ + Printer.repr(defaultValue.get()));
}
if (ruleContext.hasErrors()) {
@@ -131,22 +135,29 @@ public class ConfigFeatureFlag implements RuleConfiguredTargetFactory {
return null;
}
- String value =
+ Optional<String> configuredValue =
ruleContext
.getFragment(ConfigFeatureFlagConfiguration.class)
- .getFeatureFlagValue(ruleContext.getOwner())
- .or(defaultValue);
+ .getFeatureFlagValue(ruleContext.getOwner());
- if (!isValidValue.apply(value)) {
+ if (configuredValue.isPresent() && !isValidValue.apply(configuredValue.get())) {
// TODO(mstaib): When configurationError is available, use that instead.
ruleContext.ruleError(
"value must be one of "
+ Printer.repr(values.asList())
+ ", but was "
- + Printer.repr(value));
+ + Printer.repr(configuredValue.get()));
return null;
}
+ if (!configuredValue.isPresent() && !defaultValue.isPresent()) {
+ // TODO(mstaib): When configurationError is available, use that instead.
+ ruleContext.ruleError("flag has no default and must be set, but was not set");
+ return null;
+ }
+
+ String value = configuredValue.orElseGet(defaultValue::get);
+
ConfigFeatureFlagProvider provider = ConfigFeatureFlagProvider.create(value, isValidValue);
return new RuleConfiguredTargetBuilder(ruleContext)
diff --git a/src/main/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagConfiguration.java
index 9f34de2937..be93cb9353 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagConfiguration.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagConfiguration.java
@@ -14,7 +14,6 @@
package com.google.devtools.build.lib.rules.config;
-import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.hash.Hasher;
@@ -32,6 +31,7 @@ import com.google.devtools.common.options.OptionDocumentationCategory;
import com.google.devtools.common.options.OptionEffectTag;
import com.google.devtools.common.options.OptionMetadataTag;
import java.util.Map;
+import java.util.Optional;
import java.util.SortedMap;
import javax.annotation.Nullable;
@@ -141,7 +141,7 @@ public final class ConfigFeatureFlagConfiguration extends BuildConfiguration.Fra
* ArtifactOwner for a rule, call {@code ruleContext.getOwner()}.
*/
public Optional<String> getFeatureFlagValue(ArtifactOwner owner) {
- return Optional.fromNullable(flagValues.get(owner.getLabel()));
+ return Optional.ofNullable(flagValues.get(owner.getLabel()));
}
/**
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 7ba3611dad..fb8c973973 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
@@ -411,7 +411,6 @@ platform(
.nonconfigurable(NONCONFIGURABLE_ATTRIBUTE_REASON))
.add(
attr("default_value", STRING)
- .mandatory()
.nonconfigurable(NONCONFIGURABLE_ATTRIBUTE_REASON))
.add(ConfigFeatureFlag.getWhitelistAttribute(env))
.build();