diff options
author | 2017-07-04 11:45:22 -0400 | |
---|---|---|
committer | 2017-07-05 10:58:19 -0400 | |
commit | 9c787fac7e556db7eae5c6ddea97eab46f825c87 (patch) | |
tree | 0ec9b1ea65b31703274ad7550b2cac43fae41a4d /src/main/java/com/google/devtools/build/lib/packages | |
parent | aff0c671cf0aaca4da2230abc69179bfd789dede (diff) |
Allow strings as default values of labels
Strings now can be passed to the default parameter of attr.label,
attr.label_list, and attr.label_keyed_string_dict.
PiperOrigin-RevId: 160896987
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/packages')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/packages/Attribute.java | 23 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/packages/BuildType.java | 3 |
2 files changed, 19 insertions, 7 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/packages/Attribute.java b/src/main/java/com/google/devtools/build/lib/packages/Attribute.java index ed8af8a6e7..ee2f8ac88f 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/Attribute.java +++ b/src/main/java/com/google/devtools/build/lib/packages/Attribute.java @@ -54,6 +54,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; +import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; /** @@ -199,7 +200,7 @@ public final class Attribute implements Comparable<Attribute> { **/ SPLIT(true); - private boolean defaultsToSelf; + private final boolean defaultsToSelf; ConfigurationTransition() { this(false); @@ -657,21 +658,29 @@ public final class Attribute implements Comparable<Attribute> { /** * See value(TYPE) above. This method is only meant for Skylark usage. * - * <p>The parameter {@code context} is relevant iff the default value is a Label string. - * In this case, {@code context} must point to the parent Label in order to be able to convert - * the default value string to a proper Label. + * <p>The parameter {@code context} is relevant iff the default value is a Label string. In this + * case, {@code context} must point to the parent Label in order to be able to convert the + * default value string to a proper Label. + * + * @param parameterName The name of the attribute to use in error messages */ - public Builder<TYPE> defaultValue(Object defaultValue, Object context) + public Builder<TYPE> defaultValue( + Object defaultValue, Object context, @Nullable String parameterName) throws ConversionException { Preconditions.checkState(!valueSet, "the default value is already set"); - value = type.convert(defaultValue, "attribute " + name, context); + value = + type.convert( + defaultValue, + ((parameterName == null) ? "" : String.format("parameter '%s' of ", parameterName)) + + String.format("attribute '%s'", name), + context); valueSet = true; return this; } /** See value(TYPE) above. This method is only meant for Skylark usage. */ public Builder<TYPE> defaultValue(Object defaultValue) throws ConversionException { - return defaultValue(defaultValue, null); + return defaultValue(defaultValue, null, null); } public boolean isValueSet() { diff --git a/src/main/java/com/google/devtools/build/lib/packages/BuildType.java b/src/main/java/com/google/devtools/build/lib/packages/BuildType.java index e0b1bbcf8c..418ac4d48f 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/BuildType.java +++ b/src/main/java/com/google/devtools/build/lib/packages/BuildType.java @@ -250,6 +250,9 @@ public final class BuildType { return (Label) x; } try { + if (x instanceof String && context == null) { + return Label.parseAbsolute((String) x, false); + } return ((Label) context).getRelative(STRING.convert(x, what, context)); } catch (LabelSyntaxException e) { throw new ConversionException("invalid label '" + x + "' in " |