aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax
diff options
context:
space:
mode:
authorGravatar Luis Fernando Pino Duque <lpino@google.com>2016-04-28 15:47:29 +0000
committerGravatar Yun Peng <pcloudy@google.com>2016-04-29 08:06:28 +0000
commit3fedf9e618cbce3dbdd00559b2de0bb8e2d43171 (patch)
treecbe0c132f0a815bbf0bc5a85560cf33cd7118484 /src/main/java/com/google/devtools/build/lib/syntax
parent60166c5dbef131b2d0104b514fe596bd5b572f7e (diff)
Inject the Constants.TOOLS_REPOSITORY in SkylarkRuleClassFunctions.testBaseRule
via the Skylark and delete the constant. Also, change the isLoadingPhase in the Skylark environment an enum Phase in order to: - Decide whether testRules are enabled or not and, - Check that the toolsRepository is set when in the LOADING phase. Finally, a few tests that were using ConfiguredRuleClassProvider directly had to be updated to set a tools repository, otherwise createGlobals() fails. -- MOS_MIGRATED_REVID=121022804
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax')
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/Environment.java63
1 files changed, 46 insertions, 17 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Environment.java b/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
index 8c1def42b2..8ef298a697 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
@@ -14,6 +14,8 @@
package com.google.devtools.build.lib.syntax;
+import static com.google.common.base.Preconditions.checkState;
+
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
@@ -75,6 +77,11 @@ import javax.annotation.Nullable;
public final class Environment implements Freezable {
/**
+ * A phase for enabling or disabling certain builtin functions
+ */
+ public enum Phase { WORKSPACE, LOADING, ANALYSIS }
+
+ /**
* A Frame is a Map of bindings, plus a {@link Mutability} and a parent Frame
* from which to inherit bindings.
*
@@ -331,7 +338,7 @@ public final class Environment implements Freezable {
* Is this Environment being executed during the loading phase?
* Many builtin functions are only enabled during the loading phase, and check this flag.
*/
- private final boolean isLoadingPhase;
+ private Phase phase;
/**
* When in a lexical (Skylark) Frame, this set contains the variable names that are global,
@@ -357,6 +364,11 @@ public final class Environment implements Freezable {
@Nullable private final Label callerLabel;
/**
+ * The path to the tools repository.
+ */
+ private final String toolsRepository;
+
+ /**
* Enters a scope by saving state to a new Continuation
* @param function the function whose scope to enter
* @param caller the source AST node for the caller
@@ -391,11 +403,10 @@ public final class Environment implements Freezable {
/**
* Is this Environment being evaluated during the loading phase?
* This is fixed during Environment setup, and enables various functions
- * that are not available during the analysis phase.
- * @return true if this Environment corresponds to code during the loading phase.
+ * that are not available during the analysis or workspace phase.
*/
- private boolean isLoadingPhase() {
- return isLoadingPhase;
+ public Phase getPhase() {
+ return phase;
}
/**
@@ -403,7 +414,7 @@ public final class Environment implements Freezable {
* @param symbol name of the function being only authorized thus.
*/
public void checkLoadingPhase(String symbol, Location loc) throws EvalException {
- if (!isLoadingPhase()) {
+ if (phase != Phase.LOADING) {
throw new EvalException(loc, symbol + "() can only be called during the loading phase");
}
}
@@ -481,7 +492,7 @@ public final class Environment implements Freezable {
* @param importedExtensions Extension-s from which to import bindings with load()
* @param isSkylark true if in Skylark context
* @param fileContentHashCode a hash for the source file being evaluated, if any
- * @param isLoadingPhase true if in loading phase
+ * @param phase the current phase
* @param callerLabel the label this environment came from
*/
private Environment(
@@ -491,8 +502,9 @@ public final class Environment implements Freezable {
Map<String, Extension> importedExtensions,
boolean isSkylark,
@Nullable String fileContentHashCode,
- boolean isLoadingPhase,
- @Nullable Label callerLabel) {
+ Phase phase,
+ @Nullable Label callerLabel,
+ String toolsRepository) {
this.globalFrame = Preconditions.checkNotNull(globalFrame);
this.dynamicFrame = Preconditions.checkNotNull(dynamicFrame);
Preconditions.checkArgument(globalFrame.mutability().isMutable());
@@ -501,8 +513,9 @@ public final class Environment implements Freezable {
this.importedExtensions = importedExtensions;
this.isSkylark = isSkylark;
this.fileContentHashCode = fileContentHashCode;
- this.isLoadingPhase = isLoadingPhase;
+ this.phase = phase;
this.callerLabel = callerLabel;
+ this.toolsRepository = toolsRepository;
}
/**
@@ -511,12 +524,13 @@ public final class Environment implements Freezable {
public static class Builder {
private final Mutability mutability;
private boolean isSkylark = false;
- private boolean isLoadingPhase = false;
+ private Phase phase = Phase.ANALYSIS;
@Nullable private Frame parent;
@Nullable private EventHandler eventHandler;
@Nullable private Map<String, Extension> importedExtensions;
@Nullable private String fileContentHashCode;
private Label label;
+ private String toolsRepository;
Builder(Mutability mutability) {
this.mutability = mutability;
@@ -529,10 +543,10 @@ public final class Environment implements Freezable {
return this;
}
- /** Enables loading phase only functions in this Environment. */
- public Builder setLoadingPhase() {
- Preconditions.checkState(!isLoadingPhase);
- isLoadingPhase = true;
+ /** Enables loading or workspace phase only functions in this Environment. */
+ public Builder setPhase(Phase phase) {
+ Preconditions.checkState(this.phase == Phase.ANALYSIS);
+ this.phase = phase;
return this;
}
@@ -563,6 +577,12 @@ public final class Environment implements Freezable {
return this;
}
+ /** Sets the path to the tools repository */
+ public Builder setToolsRepository(String toolsRepository) {
+ this.toolsRepository = toolsRepository;
+ return this;
+ }
+
/** Builds the Environment. */
public Environment build() {
Preconditions.checkArgument(mutability.isMutable());
@@ -574,6 +594,9 @@ public final class Environment implements Freezable {
if (importedExtensions == null) {
importedExtensions = ImmutableMap.of();
}
+ if (phase == Phase.LOADING) {
+ Preconditions.checkState(this.toolsRepository != null);
+ }
return new Environment(
globalFrame,
dynamicFrame,
@@ -581,8 +604,9 @@ public final class Environment implements Freezable {
importedExtensions,
isSkylark,
fileContentHashCode,
- isLoadingPhase,
- label);
+ phase,
+ label,
+ toolsRepository);
}
public Builder setCallerLabel(Label label) {
@@ -941,4 +965,9 @@ public final class Environment implements Freezable {
}
return last;
}
+
+ public String getToolsRepository() {
+ checkState(toolsRepository != null);
+ return toolsRepository;
+ }
}