aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/google')
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/ConfigurationMakeVariableContext.java5
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/Expander.java53
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java4
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/LocationTemplateContext.java111
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java5
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleImplementationFunctions.java31
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/ExpansionException.java4
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/TemplateContext.java25
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/TemplateExpander.java34
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java5
10 files changed, 61 insertions, 216 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ConfigurationMakeVariableContext.java b/src/main/java/com/google/devtools/build/lib/analysis/ConfigurationMakeVariableContext.java
index f9c170a0e2..a5c7cbfb99 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/ConfigurationMakeVariableContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/ConfigurationMakeVariableContext.java
@@ -105,9 +105,4 @@ public class ConfigurationMakeVariableContext implements TemplateContext {
}
return SkylarkDict.<String, String>copyOf(null, map);
}
-
- @Override
- public String lookupFunction(String name, String param) throws ExpansionException {
- throw new ExpansionException(String.format("$(%s) not defined", name));
- }
}
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/Expander.java b/src/main/java/com/google/devtools/build/lib/analysis/Expander.java
index 9dc224a29c..c4eb867427 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/Expander.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/Expander.java
@@ -19,7 +19,6 @@ import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.LocationExpander.Options;
import com.google.devtools.build.lib.analysis.stringtemplate.ExpansionException;
-import com.google.devtools.build.lib.analysis.stringtemplate.TemplateContext;
import com.google.devtools.build.lib.analysis.stringtemplate.TemplateExpander;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.shell.ShellUtils;
@@ -39,26 +38,37 @@ public final class Expander {
}
private final RuleContext ruleContext;
- private final TemplateContext templateContext;
+ private final ConfigurationMakeVariableContext makeVariableContext;
+ @Nullable private final LocationExpander locationExpander;
- Expander(RuleContext ruleContext, TemplateContext templateContext) {
+ private Expander(
+ RuleContext ruleContext,
+ ConfigurationMakeVariableContext makeVariableContext,
+ @Nullable LocationExpander locationExpander) {
this.ruleContext = ruleContext;
- this.templateContext = templateContext;
+ this.makeVariableContext = makeVariableContext;
+ this.locationExpander = locationExpander;
+ }
+
+ Expander(
+ RuleContext ruleContext,
+ ConfigurationMakeVariableContext makeVariableContext) {
+ this(ruleContext, makeVariableContext, null);
}
/**
* Returns a new instance that also expands locations using the default configuration of
- * {@link LocationTemplateContext}.
+ * {@link LocationExpander}.
*/
public Expander withLocations(Options... options) {
- TemplateContext newTemplateContext =
- new LocationTemplateContext(templateContext, ruleContext, options);
- return new Expander(ruleContext, newTemplateContext);
+ LocationExpander newLocationExpander =
+ new LocationExpander(ruleContext, options);
+ return new Expander(ruleContext, makeVariableContext, newLocationExpander);
}
/**
* Returns a new instance that also expands locations, passing {@link Options#ALLOW_DATA} to the
- * underlying {@link LocationTemplateContext}.
+ * underlying {@link LocationExpander}.
*/
public Expander withDataLocations() {
return withLocations(Options.ALLOW_DATA);
@@ -66,7 +76,7 @@ public final class Expander {
/**
* Returns a new instance that also expands locations, passing {@link Options#ALLOW_DATA} and
- * {@link Options#EXEC_PATHS} to the underlying {@link LocationTemplateContext}.
+ * {@link Options#EXEC_PATHS} to the underlying {@link LocationExpander}.
*/
public Expander withDataExecLocations() {
return withLocations(Options.ALLOW_DATA, Options.EXEC_PATHS);
@@ -74,12 +84,12 @@ public final class Expander {
/**
* Returns a new instance that also expands locations, passing the given location map, as well as
- * {@link Options#EXEC_PATHS} to the underlying {@link LocationTemplateContext}.
+ * {@link Options#EXEC_PATHS} to the underlying {@link LocationExpander}.
*/
public Expander withExecLocations(ImmutableMap<Label, ImmutableCollection<Artifact>> locations) {
- TemplateContext newTemplateContext =
- new LocationTemplateContext(templateContext, ruleContext, locations, Options.EXEC_PATHS);
- return new Expander(ruleContext, newTemplateContext);
+ LocationExpander newLocationExpander =
+ new LocationExpander(ruleContext, locations, Options.EXEC_PATHS);
+ return new Expander(ruleContext, makeVariableContext, newLocationExpander);
}
/**
@@ -135,15 +145,14 @@ public final class Expander {
* @param expression the string to expand.
* @return the expansion of "expression".
*/
- public String expand(@Nullable String attributeName, String expression) {
+ public String expand(String attributeName, String expression) {
+ if (locationExpander != null) {
+ expression = locationExpander.expandAttribute(attributeName, expression);
+ }
try {
- return TemplateExpander.expand(expression, templateContext);
+ return TemplateExpander.expand(expression, makeVariableContext);
} catch (ExpansionException e) {
- if (attributeName == null) {
- ruleContext.ruleError(e.getMessage());
- } else {
- ruleContext.attributeError(attributeName, e.getMessage());
- }
+ ruleContext.attributeError(attributeName, e.getMessage());
return expression;
}
}
@@ -205,7 +214,7 @@ public final class Expander {
@Nullable
public String expandSingleMakeVariable(String attrName, String expression) {
try {
- return TemplateExpander.expandSingleVariable(expression, templateContext);
+ return TemplateExpander.expandSingleVariable(expression, makeVariableContext);
} catch (ExpansionException e) {
ruleContext.attributeError(attrName, e.getMessage());
return expression;
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java b/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java
index 7bba08b03f..e43aa71bf4 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java
@@ -60,7 +60,7 @@ import javax.annotation.Nullable;
*
* <p>DO NOT USE DIRECTLY! Use RuleContext.getExpander() instead.
*/
-final class LocationExpander {
+public class LocationExpander {
/**
* List of options to tweak the LocationExpander.
@@ -329,7 +329,7 @@ final class LocationExpander {
* @param labelMap map of labels to build artifacts
* @return map of all possible target locations
*/
- static Map<Label, Collection<Artifact>> buildLocationMap(
+ private static Map<Label, Collection<Artifact>> buildLocationMap(
RuleContext ruleContext,
Map<Label, ? extends Collection<Artifact>> labelMap,
boolean allowDataAttributeEntriesInLabel) {
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/LocationTemplateContext.java b/src/main/java/com/google/devtools/build/lib/analysis/LocationTemplateContext.java
deleted file mode 100644
index d2888200e0..0000000000
--- a/src/main/java/com/google/devtools/build/lib/analysis/LocationTemplateContext.java
+++ /dev/null
@@ -1,111 +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.analysis;
-
-import com.google.common.base.Supplier;
-import com.google.common.base.Suppliers;
-import com.google.common.collect.ImmutableCollection;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import com.google.devtools.build.lib.actions.Artifact;
-import com.google.devtools.build.lib.analysis.LocationExpander.LocationFunction;
-import com.google.devtools.build.lib.analysis.LocationExpander.Options;
-import com.google.devtools.build.lib.analysis.stringtemplate.ExpansionException;
-import com.google.devtools.build.lib.analysis.stringtemplate.TemplateContext;
-import com.google.devtools.build.lib.cmdline.Label;
-import java.util.Collection;
-import java.util.Map;
-import java.util.function.Function;
-import javax.annotation.Nullable;
-
-/**
- * Expands $(location) and $(locations) tags inside target attributes. You can specify something
- * like this in the BUILD file:
- *
- * <pre>
- * somerule(name='some name',
- * someopt = [ '$(location //mypackage:myhelper)' ],
- * ...)
- * </pre>
- *
- * and location will be substituted with //mypackage:myhelper executable output.
- *
- * <p>Note that this expander will always expand labels in srcs, deps, and tools attributes, with
- * data being optional.
- *
- * <p>DO NOT USE DIRECTLY! Use RuleContext.getExpander() instead.
- */
-final class LocationTemplateContext implements TemplateContext {
- private final TemplateContext delegate;
- private final Function<String, String> locationFunction;
- private final Function<String, String> locationsFunction;
-
- private LocationTemplateContext(
- TemplateContext delegate,
- Label root,
- Supplier<Map<Label, Collection<Artifact>>> locationMap,
- boolean execPaths) {
- this.delegate = delegate;
- this.locationFunction = new LocationFunction(root, locationMap, execPaths, false);
- this.locationsFunction = new LocationFunction(root, locationMap, execPaths, true);
- }
-
- private LocationTemplateContext(
- TemplateContext delegate,
- RuleContext ruleContext,
- @Nullable ImmutableMap<Label, ImmutableCollection<Artifact>> labelMap,
- ImmutableSet<Options> options) {
- this(
- delegate,
- ruleContext.getLabel(),
- // Use a memoizing supplier to avoid eagerly building the location map.
- Suppliers.memoize(
- () -> LocationExpander.buildLocationMap(
- ruleContext, labelMap, options.contains(Options.ALLOW_DATA))),
- options.contains(Options.EXEC_PATHS));
- }
-
- public LocationTemplateContext(
- TemplateContext delegate,
- RuleContext ruleContext,
- @Nullable ImmutableMap<Label, ImmutableCollection<Artifact>> labelMap,
- LocationExpander.Options... options) {
- this(delegate, ruleContext, labelMap, ImmutableSet.copyOf(options));
- }
-
- public LocationTemplateContext(
- TemplateContext delegate, RuleContext ruleContext, LocationExpander.Options... options) {
- this(delegate, ruleContext, null, ImmutableSet.copyOf(options));
- }
-
- @Override
- public String lookupVariable(String name) throws ExpansionException {
- return delegate.lookupVariable(name);
- }
-
- @Override
- public String lookupFunction(String name, String param) throws ExpansionException {
- try {
- if ("location".equals(name)) {
- return locationFunction.apply(param);
- } else if ("locations".equals(name)) {
- return locationsFunction.apply(param);
- }
- } catch (IllegalStateException e) {
- throw new ExpansionException(e.getMessage(), e);
- }
- return delegate.lookupFunction(name, param);
- }
-}
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
index 15337837fc..94423b6482 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
@@ -48,7 +48,6 @@ import com.google.devtools.build.lib.analysis.config.FragmentCollection;
import com.google.devtools.build.lib.analysis.config.PatchTransition;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.fileset.FilesetProvider;
-import com.google.devtools.build.lib.analysis.stringtemplate.TemplateContext;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.collect.ImmutableSortedKeyListMultimap;
@@ -955,8 +954,8 @@ public final class RuleContext extends TargetContext
initConfigurationMakeVariableContext(ImmutableList.copyOf(makeVariableSuppliers));
}
- public Expander getExpander(TemplateContext templateContext) {
- return new Expander(this, templateContext);
+ public Expander getExpander(ConfigurationMakeVariableContext makeVariableContext) {
+ return new Expander(this, makeVariableContext);
}
public Expander getExpander() {
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleImplementationFunctions.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleImplementationFunctions.java
index 53aaf47e79..f71730b10a 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleImplementationFunctions.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleImplementationFunctions.java
@@ -20,12 +20,11 @@ import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.AliasProvider;
import com.google.devtools.build.lib.analysis.CommandHelper;
import com.google.devtools.build.lib.analysis.FileProvider;
+import com.google.devtools.build.lib.analysis.LocationExpander;
import com.google.devtools.build.lib.analysis.Runfiles;
import com.google.devtools.build.lib.analysis.RunfilesProvider;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.configuredtargets.AbstractConfiguredTarget;
-import com.google.devtools.build.lib.analysis.stringtemplate.ExpansionException;
-import com.google.devtools.build.lib.analysis.stringtemplate.TemplateContext;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.skylarkinterface.Param;
@@ -319,12 +318,11 @@ public class SkylarkRuleImplementationFunctions {
throws EvalException {
ctx.checkMutable("expand_location");
try {
- return ctx
- .getRuleContext()
- .getExpander(new FakeTemplateContext())
- .withExecLocations(
- makeLabelMap(targets.getContents(TransitiveInfoCollection.class, "targets")))
- .expand(/*attributeName=*/null, input);
+ return new LocationExpander(
+ ctx.getRuleContext(),
+ makeLabelMap(targets.getContents(TransitiveInfoCollection.class, "targets")),
+ LocationExpander.Options.EXEC_PATHS)
+ .expand(input);
} catch (IllegalStateException ise) {
throw new EvalException(loc, ise);
}
@@ -332,23 +330,6 @@ public class SkylarkRuleImplementationFunctions {
};
/**
- * This class only exists for backwards compatibility. Previously, we were using LocationExpander
- * directly, which passes through all unrecognized $() expressions.
- */
- private static final class FakeTemplateContext implements TemplateContext {
- @Override
- public String lookupVariable(String name) throws ExpansionException {
- return String.format("$$(%s)", name);
- }
-
- @Override
- public String lookupFunction(String name, String param) throws ExpansionException {
- // Variables get recursively expanded, functions don't.
- return String.format("$(%s %s)", name, param);
- }
- }
-
- /**
* Builds a map: Label -> List of files from the given labels
*
* @param knownLabels List of known labels
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/ExpansionException.java b/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/ExpansionException.java
index 10542e770c..66f5437667 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/ExpansionException.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/ExpansionException.java
@@ -21,8 +21,4 @@ public class ExpansionException extends Exception {
public ExpansionException(String message) {
super(message);
}
-
- public ExpansionException(String message, Throwable cause) {
- super(message, cause);
- }
} \ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/TemplateContext.java b/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/TemplateContext.java
index dc6d27598d..d4aa40dcf0 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/TemplateContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/TemplateContext.java
@@ -14,29 +14,18 @@
package com.google.devtools.build.lib.analysis.stringtemplate;
/**
- * Interface to be implemented by callers of {@link TemplateExpander} which defines the expansion of
- * each template variable and function.
+ * Interface to be implemented by callers of MakeVariableExpander which defines the expansion of
+ * each "Make" variable.
*/
public interface TemplateContext {
/**
- * Returns the expansion of the specified template variable.
+ * Returns the expansion of the specified "Make" variable.
*
- * @param name the variable to expand
- * @return the expansion of the variable
- * @throws ExpansionException if the given variable was not defined or there was any other error
- * during expansion
+ * @param name the variable to expand.
+ * @return the expansion of the variable.
+ * @throws ExpansionException if the variable "var" was not defined or
+ * there was any other error while expanding "var".
*/
String lookupVariable(String name) throws ExpansionException;
-
- /**
- * Returns the expansion of the specified template function with the given parameter.
- *
- * @param name the function name
- * @param param the function parameter
- * @return the expansion of the function applied to the parameter
- * @throws ExpansionException if the function was not defined, or if the function application
- * failed for some reason
- */
- String lookupFunction(String name, String param) throws ExpansionException;
} \ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/TemplateExpander.java b/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/TemplateExpander.java
index 692c4e0eec..9807a7c3cc 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/TemplateExpander.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/stringtemplate/TemplateExpander.java
@@ -15,10 +15,12 @@ package com.google.devtools.build.lib.analysis.stringtemplate;
/**
* Simple string template expansion. String templates consist of text interspersed with
- * <code>$(variable)</code> or <code>$(function value)</code> references, which are replaced by
- * strings.
+ * <code>$(variable)</code> references, which are replaced by strings.
+ *
+ * <p>Note that neither <code>$(location x)</code> nor Make-isms are treated specially in any way by
+ * this class.
*/
-public final class TemplateExpander {
+public class TemplateExpander {
private final char[] buffer;
private final int length;
private int offset;
@@ -79,22 +81,13 @@ public final class TemplateExpander {
result.append('$');
} else {
String var = scanVariable();
- int spaceIndex = var.indexOf(' ');
- if (spaceIndex < 0) {
- String value = context.lookupVariable(var);
- // To prevent infinite recursion for the ignored shell variables
- if (!value.equals(var)) {
- // recursively expand using Make's ":=" semantics:
- value = expand(value, context, depth + 1);
- }
- result.append(value);
- } else {
- String name = var.substring(0, spaceIndex);
- // Trim the string to remove leading and trailing whitespace.
- String param = var.substring(spaceIndex + 1).trim();
- String value = context.lookupFunction(name, param);
- result.append(value);
+ String value = context.lookupVariable(var);
+ // To prevent infinite recursion for the ignored shell variables
+ if (!value.equals(var)) {
+ // recursively expand using Make's ":=" semantics:
+ value = expand(value, context, depth + 1);
}
+ result.append(value);
}
} else {
result.append(c);
@@ -116,7 +109,7 @@ public final class TemplateExpander {
private String scanVariable() throws ExpansionException {
char c = buffer[offset];
switch (c) {
- case '(': { // looks like $(SRCS)
+ case '(': { // $(SRCS)
offset++;
int start = offset;
while (offset < length && buffer[offset] != ')') {
@@ -127,8 +120,7 @@ public final class TemplateExpander {
}
return new String(buffer, start, offset - start);
}
- // We only parse ${variable} syntax to provide a better error message.
- case '{': { // looks like ${SRCS}
+ case '{': { // ${SRCS}
offset++;
int start = offset;
while (offset < length && buffer[offset] != '}') {
diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java
index b91e57f14b..e0410dbb4d 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java
@@ -205,11 +205,6 @@ public class ProtoCompileActionBuilder {
}
return value.toString();
}
-
- @Override
- public String lookupFunction(String name, String param) throws ExpansionException {
- throw new ExpansionException(String.format("$(%s) not defined", name));
- }
});
} catch (ExpansionException e) {
// Squeelch. We don't throw this exception in the lookupMakeVariable implementation above,