diff options
Diffstat (limited to 'src/main/java/com/google')
4 files changed, 28 insertions, 28 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleClassFunctions.java b/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleClassFunctions.java index 80cbbb91d0..9aa6b261a5 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleClassFunctions.java +++ b/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleClassFunctions.java @@ -755,7 +755,7 @@ public class SkylarkRuleClassFunctions { throws EvalException { Label parentLabel = null; if (relativeToCallerRepository) { - parentLabel = SkylarkUtils.getCallerLabel(env); + parentLabel = env.getCallerLabel(); } else { parentLabel = env.getGlobals().label(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleConfiguredTargetBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleConfiguredTargetBuilder.java index b3c03aa4c9..f78e96d731 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleConfiguredTargetBuilder.java +++ b/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleConfiguredTargetBuilder.java @@ -47,7 +47,6 @@ import com.google.devtools.build.lib.syntax.Runtime; import com.google.devtools.build.lib.syntax.SkylarkList; import com.google.devtools.build.lib.syntax.SkylarkNestedSet; import com.google.devtools.build.lib.syntax.SkylarkType; -import com.google.devtools.build.lib.syntax.SkylarkUtils; import com.google.devtools.build.lib.syntax.Type; import com.google.devtools.build.lib.util.FileType; import com.google.devtools.build.lib.util.FileTypeSet; @@ -76,13 +75,12 @@ public final class SkylarkRuleConfiguredTargetBuilder { SkylarkRuleContext skylarkRuleContext = new SkylarkRuleContext(ruleContext, null); Environment env = Environment.builder(mutability) + .setCallerLabel(ruleContext.getLabel()) .setGlobals( ruleContext.getRule().getRuleClassObject().getRuleDefinitionEnvironment().getGlobals()) .setEventHandler(ruleContext.getAnalysisEnvironment().getEventHandler()) .build(); // NB: loading phase functions are not available: this is analysis already, // so we do *not* setLoadingPhase(). - SkylarkUtils.setCallerLabel(env, ruleContext.getLabel()); - Object target = ruleImplementation.call( ImmutableList.<Object>of(skylarkRuleContext), ImmutableMap.<String, Object>of(), 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 7029a59ce5..11515ef7d2 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 @@ -336,6 +336,13 @@ public final class Environment implements Freezable { @Nullable private Continuation continuation; /** + * Gets the label of the BUILD file that is using this environment. For example, if a target + * //foo has a dependency on //bar which is a Skylark rule defined in //rules:my_rule.bzl being + * evaluated in this environment, then this would return //foo. + */ + @Nullable private final Label callerLabel; + + /** * 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 @@ -456,6 +463,7 @@ public final class Environment implements Freezable { * @param importedExtensions Extension-s from which to import bindings with load() * @param fileContentHashCode a hash for the source file being evaluated, if any * @param phase the current phase + * @param callerLabel the label this environment came from */ private Environment( Frame globalFrame, @@ -463,7 +471,8 @@ public final class Environment implements Freezable { EventHandler eventHandler, Map<String, Extension> importedExtensions, @Nullable String fileContentHashCode, - Phase phase) { + Phase phase, + @Nullable Label callerLabel) { this.globalFrame = Preconditions.checkNotNull(globalFrame); this.dynamicFrame = Preconditions.checkNotNull(dynamicFrame); Preconditions.checkArgument(globalFrame.mutability().isMutable()); @@ -471,6 +480,7 @@ public final class Environment implements Freezable { this.eventHandler = eventHandler; this.importedExtensions = importedExtensions; this.phase = phase; + this.callerLabel = callerLabel; this.transitiveHashCode = computeTransitiveContentHashCode(fileContentHashCode, importedExtensions); } @@ -485,6 +495,7 @@ public final class Environment implements Freezable { @Nullable private EventHandler eventHandler; @Nullable private Map<String, Extension> importedExtensions; @Nullable private String fileContentHashCode; + private Label label; Builder(Mutability mutability) { this.mutability = mutability; @@ -549,7 +560,13 @@ public final class Environment implements Freezable { eventHandler, importedExtensions, fileContentHashCode, - phase); + phase, + label); + } + + public Builder setCallerLabel(Label label) { + this.label = label; + return this; } } @@ -558,6 +575,13 @@ public final class Environment implements Freezable { } /** + * Returns the caller's label. + */ + public Label getCallerLabel() { + return callerLabel; + } + + /** * Sets a binding for a special dynamic variable in this Environment. * This is not for end-users, and will throw an AssertionError in case of conflict. * @param varname the name of the dynamic variable to be bound diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkUtils.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkUtils.java index 3a17fe519c..970543f60a 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkUtils.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkUtils.java @@ -14,8 +14,6 @@ package com.google.devtools.build.lib.syntax; -import com.google.devtools.build.lib.cmdline.Label; - /** This class contains Bazel-specific functions to extend or interoperate with Skylark. */ public final class SkylarkUtils { @@ -25,7 +23,6 @@ public final class SkylarkUtils { } private static final String BAZEL_INFO_KEY = "$bazel"; - private static final String CALLER_LABEL_KEY = "$caller_label"; private static BazelInfo getInfo(Environment env) { Object info = env.lookup(BAZEL_INFO_KEY); @@ -49,23 +46,4 @@ public final class SkylarkUtils { public static String getToolsRepository(Environment env) { return getInfo(env).toolsRepository; } - - public static void setCallerLabel(Environment env, Label label) { - // We cannot store this information in BazelInfo, because we need to - // have it in the local environment (not the global environment). - try { - env.update(CALLER_LABEL_KEY, label); - } catch (EvalException e) { - throw new AssertionError(e); - } - } - - /** - * Gets the label of the BUILD file that is using this environment. For example, if a target - * //foo has a dependency on //bar which is a Skylark rule defined in //rules:my_rule.bzl being - * evaluated in this environment, then this would return //foo. - */ - public static Label getCallerLabel(Environment env) { - return (Label) env.lookup(CALLER_LABEL_KEY); - } } |