Skip to content

Commit

Permalink
bazel syntax: delete SkylarkUtils
Browse files Browse the repository at this point in the history
The Phase is now recorded in lib.packages.BazelStarlarkContext.
It is now always explicit whereas, before, failure to set it
caused it to assume a default of ANALYSIS.

In SkylarkRepositoryFunction, the "fetch" thread now uses
phase=LOADING where before it has none (=ANALYSIS), which
I suspect was an oversight.
PiperOrigin-RevId: 285236359
  • Loading branch information
adonovan authored and copybara-github committed Dec 12, 2019
1 parent 26fbb9d commit b23b8c7
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
import com.google.devtools.build.lib.syntax.ClassObject;
import com.google.devtools.build.lib.syntax.Module;
import com.google.devtools.build.lib.syntax.Mutability;
import com.google.devtools.build.lib.syntax.SkylarkUtils;
import com.google.devtools.build.lib.syntax.SkylarkUtils.Phase;
import com.google.devtools.build.lib.syntax.StarlarkSemantics;
import com.google.devtools.build.lib.syntax.StarlarkThread;
import com.google.devtools.build.lib.syntax.StarlarkThread.Extension;
Expand Down Expand Up @@ -819,14 +817,14 @@ public StarlarkThread createRuleClassStarlarkThread(
.build();

new BazelStarlarkContext(
BazelStarlarkContext.Phase.LOADING,
toolsRepository,
configurationFragmentMap,
repoMapping,
new SymbolGenerator<>(fileLabel),
/* analysisRuleLabel= */ null)
.storeInThread(thread);

SkylarkUtils.setPhase(thread, Phase.LOADING);
return thread;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import com.google.devtools.build.lib.syntax.Printer;
import com.google.devtools.build.lib.syntax.Sequence;
import com.google.devtools.build.lib.syntax.SkylarkType;
import com.google.devtools.build.lib.syntax.SkylarkUtils;
import com.google.devtools.build.lib.syntax.Starlark;
import com.google.devtools.build.lib.syntax.StarlarkFunction;
import com.google.devtools.build.lib.syntax.StarlarkThread;
Expand Down Expand Up @@ -458,7 +457,7 @@ public Descriptor intAttribute(
StarlarkThread thread)
throws EvalException {
// TODO(bazel-team): Replace literal strings with constants.
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.int", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.int");
return createAttrDescriptor(
"int",
EvalUtils.<String, Object>optionMap(
Expand All @@ -477,7 +476,7 @@ public Descriptor stringAttribute(
FuncallExpression ast,
StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.string", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.string");
return createAttrDescriptor(
"string",
EvalUtils.<String, Object>optionMap(
Expand All @@ -503,7 +502,7 @@ public Descriptor labelAttribute(
FuncallExpression ast,
StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.label", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.label");
try {
ImmutableAttributeFactory attribute =
createAttributeFactory(
Expand Down Expand Up @@ -550,7 +549,7 @@ public Descriptor stringListAttribute(
FuncallExpression ast,
StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.string_list", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.string_list");
return createAttrDescriptor(
"string_list",
EvalUtils.<String, Object>optionMap(
Expand Down Expand Up @@ -578,7 +577,7 @@ public Descriptor intListAttribute(
FuncallExpression ast,
StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.int_list", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.int_list");
return createAttrDescriptor(
"int_list",
EvalUtils.<String, Object>optionMap(
Expand Down Expand Up @@ -612,7 +611,7 @@ public Descriptor labelListAttribute(
FuncallExpression ast,
StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.label_list", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.label_list");
Dict<String, Object> kwargs =
EvalUtils.<String, Object>optionMap(
thread,
Expand Down Expand Up @@ -661,8 +660,7 @@ public Descriptor labelKeyedStringDictAttribute(
FuncallExpression ast,
StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(
thread, "attr.label_keyed_string_dict", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.label_keyed_string_dict");
Dict<String, Object> kwargs =
EvalUtils.<String, Object>optionMap(
thread,
Expand Down Expand Up @@ -705,7 +703,7 @@ public Descriptor labelKeyedStringDictAttribute(
public Descriptor boolAttribute(
Boolean defaultO, String doc, Boolean mandatory, FuncallExpression ast, StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.bool", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.bool");
return createAttrDescriptor(
"bool",
EvalUtils.<String, Object>optionMap(
Expand All @@ -719,7 +717,7 @@ public Descriptor boolAttribute(
public Descriptor outputAttribute(
Object defaultO, String doc, Boolean mandatory, FuncallExpression ast, StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.output", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.output");

return createNonconfigurableAttrDescriptor(
"output",
Expand All @@ -740,7 +738,7 @@ public Descriptor outputListAttribute(
FuncallExpression ast,
StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.output_list", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.output_list");

return createAttrDescriptor(
"output_list",
Expand Down Expand Up @@ -769,7 +767,7 @@ public Descriptor stringDictAttribute(
FuncallExpression ast,
StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.string_dict", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.string_dict");
return createAttrDescriptor(
"string_dict",
EvalUtils.<String, Object>optionMap(
Expand Down Expand Up @@ -797,7 +795,7 @@ public Descriptor stringListDictAttribute(
FuncallExpression ast,
StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.string_list_dict", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.string_list_dict");
return createAttrDescriptor(
"string_list_dict",
EvalUtils.<String, Object>optionMap(
Expand All @@ -819,7 +817,7 @@ public Descriptor stringListDictAttribute(
public Descriptor licenseAttribute(
Object defaultO, String doc, Boolean mandatory, FuncallExpression ast, StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "attr.license", ast.getLocation());
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("attr.license");
return createNonconfigurableAttrDescriptor(
"license",
EvalUtils.<String, Object>optionMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
import com.google.devtools.build.lib.syntax.Printer;
import com.google.devtools.build.lib.syntax.Sequence;
import com.google.devtools.build.lib.syntax.SkylarkType;
import com.google.devtools.build.lib.syntax.SkylarkUtils;
import com.google.devtools.build.lib.syntax.Starlark;
import com.google.devtools.build.lib.syntax.StarlarkFunction;
import com.google.devtools.build.lib.syntax.StarlarkThread;
Expand Down Expand Up @@ -297,9 +296,9 @@ public BaseFunction rule(
FuncallExpression ast,
StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, "rule", ast.getLocation());

BazelStarlarkContext bazelContext = BazelStarlarkContext.from(thread);
bazelContext.checkLoadingOrWorkspacePhase("rule");

// analysis_test=true implies test=true.
test |= Boolean.TRUE.equals(analysisTest);

Expand Down Expand Up @@ -644,7 +643,7 @@ public String getName() {
public Object call(Object[] args, FuncallExpression astForLocation, StarlarkThread thread)
throws EvalException, InterruptedException, ConversionException {
Location loc = astForLocation.getLocation();
SkylarkUtils.checkLoadingPhase(thread, getName(), loc);
BazelStarlarkContext.from(thread).checkLoadingPhase(getName());
if (ruleClass == null) {
throw new EvalException(loc, "Invalid rule class hasn't been exported by a bzl file");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ public static ConfiguredTarget buildRule(
StarlarkThread.builder(mutability)
.setSemantics(starlarkSemantics)
.setEventHandler(ruleContext.getAnalysisEnvironment().getEventHandler())
.build(); // NB: loading phase functions are not available: this is analysis already,
// so we do *not* setLoadingPhase().
.build();

new BazelStarlarkContext(
BazelStarlarkContext.Phase.ANALYSIS,
toolsRepository,
/* fragmentNameToClass= */ null,
/*fragmentNameToClass=*/ null,
ruleContext.getTarget().getPackage().getRepositoryMapping(),
ruleContext.getSymbolGenerator(),
ruleContext.getLabel())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ public RepositoryDirectoryValue.Builder fetch(
// The fetch phase does not need the tools repository
// or the fragment map because it happens before analysis.
new BazelStarlarkContext(
/* toolsRepository = */ null,
/* fragmentNameToClass = */ null,
BazelStarlarkContext.Phase.LOADING, // ("fetch")
/*toolsRepository=*/ null,
/*fragmentNameToClass=*/ null,
rule.getPackage().getRepositoryMapping(),
new SymbolGenerator<>(key),
/* analysisRuleLabel= */ null)
/*analysisRuleLabel=*/ null)
.storeInThread(thread);

SkylarkRepositoryContext skylarkRepositoryContext =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.packages.AttributeValueSource;
import com.google.devtools.build.lib.packages.BazelStarlarkContext;
import com.google.devtools.build.lib.packages.Package;
import com.google.devtools.build.lib.packages.Package.NameConflictException;
import com.google.devtools.build.lib.packages.PackageFactory;
Expand All @@ -47,10 +48,8 @@
import com.google.devtools.build.lib.syntax.Identifier;
import com.google.devtools.build.lib.syntax.Printer;
import com.google.devtools.build.lib.syntax.Sequence;
import com.google.devtools.build.lib.syntax.SkylarkUtils;
import com.google.devtools.build.lib.syntax.Starlark;
import com.google.devtools.build.lib.syntax.StarlarkFunction;
import com.google.devtools.build.lib.syntax.StarlarkSemantics;
import com.google.devtools.build.lib.syntax.StarlarkThread;
import java.util.Map;

Expand All @@ -70,16 +69,15 @@ public BaseFunction repositoryRule(
Boolean remotable,
String doc,
FuncallExpression ast,
StarlarkThread funcallThread)
StarlarkThread thread)
throws EvalException {
SkylarkUtils.checkLoadingOrWorkspacePhase(funcallThread, "repository_rule", ast.getLocation());
StarlarkSemantics semantics = funcallThread.getSemantics();
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("repository_rule");
// We'll set the name later, pass the empty string for now.
RuleClass.Builder builder = new RuleClass.Builder("", RuleClassType.WORKSPACE, true);

builder.addOrOverrideAttribute(attr("$local", BOOLEAN).defaultValue(local).build());
builder.addOrOverrideAttribute(attr("$configure", BOOLEAN).defaultValue(configure).build());
if (semantics.experimentalRepoRemoteExec()) {
if (thread.getSemantics().experimentalRepoRemoteExec()) {
builder.addOrOverrideAttribute(attr("$remotable", BOOLEAN).defaultValue(remotable).build());
BaseRuleClasses.execPropertiesAttribute(builder);
}
Expand All @@ -99,8 +97,7 @@ public BaseFunction repositoryRule(
}
builder.setConfiguredTargetFunction(implementation);
builder.setRuleDefinitionEnvironmentLabelAndHashCode(
(Label) funcallThread.getGlobals().getLabel(),
funcallThread.getTransitiveContentHashCode());
(Label) thread.getGlobals().getLabel(), thread.getTransitiveContentHashCode());
builder.setWorkspaceOnly();
return new RepositoryRuleFunction(builder, ast.getLocation());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@
import com.google.devtools.build.lib.analysis.RuleDefinitionContext;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.StarlarkThread;
import javax.annotation.Nullable;

/** Contextual information associated with each Starlark thread created by Bazel. */
public class BazelStarlarkContext implements RuleDefinitionContext, Label.HasRepoMapping {
public final class BazelStarlarkContext implements RuleDefinitionContext, Label.HasRepoMapping {

/** The phase to which this Starlark thread belongs. */
public enum Phase {
WORKSPACE,
LOADING,
ANALYSIS
}

/** Return the Bazel information associated with the specified Starlark thread. */
public static BazelStarlarkContext from(StarlarkThread thread) {
Expand All @@ -36,13 +44,15 @@ public void storeInThread(StarlarkThread thread) {
thread.setThreadLocal(Label.HasRepoMapping.class, this);
}

private final Phase phase;
private final String toolsRepository;
@Nullable private final ImmutableMap<String, Class<?>> fragmentNameToClass;
private final ImmutableMap<RepositoryName, RepositoryName> repoMapping;
private final SymbolGenerator<?> symbolGenerator;
@Nullable private final Label analysisRuleLabel;

/**
* @param phase the phase to which this Starlark thread belongs
* @param toolsRepository the name of the tools repository, such as "@bazel_tools"
* @param fragmentNameToClass a map from configuration fragment name to configuration fragment
* class, such as "apple" to AppleConfiguration.class
Expand All @@ -56,22 +66,30 @@ public void storeInThread(StarlarkThread thread) {
// analysis, workspace, implicit outputs, computed defaults, etc), perhaps by splitting these into
// separate structs, exactly one of which is populated (plus the common fields). And eliminate
// SkylarkUtils.Phase.
// TODO(adonovan): move PackageFactory.PackageContext in here, for loading-phase threads.
// TODO(adonovan): add a PackageIdentifier here, for use by the Starlark Label function.
// TODO(adonovan): is there any reason not to put the entire RuleContext in this thread, for
// analysis threads?
public BazelStarlarkContext(
Phase phase,
String toolsRepository,
@Nullable ImmutableMap<String, Class<?>> fragmentNameToClass,
ImmutableMap<RepositoryName, RepositoryName> repoMapping,
SymbolGenerator<?> symbolGenerator,
@Nullable Label analysisRuleLabel) {
this.phase = phase;
this.toolsRepository = toolsRepository;
this.fragmentNameToClass = fragmentNameToClass;
this.repoMapping = repoMapping;
this.symbolGenerator = Preconditions.checkNotNull(symbolGenerator);
this.analysisRuleLabel = analysisRuleLabel;
}

/** Returns the phase to which this Starlark thread belongs. */
public Phase getPhase() {
return phase;
}

/** Returns the name of the tools repository, such as "@bazel_tools". */
@Override
public String getToolsRepository() {
Expand Down Expand Up @@ -105,4 +123,28 @@ public SymbolGenerator<?> getSymbolGenerator() {
public Label getAnalysisRuleLabel() {
return analysisRuleLabel;
}

/**
* Checks that the Starlark thread is in the loading or the workspace phase.
*
* @param function name of a function that requires this check
*/
public void checkLoadingOrWorkspacePhase(String function) throws EvalException {
if (phase == Phase.ANALYSIS) {
throw new EvalException(
null, "'" + function + "' cannot be called during the analysis phase");
}
}

/**
* Checks that the current StarlarkThread is in the loading phase.
*
* @param function name of a function that requires this check
*/
public void checkLoadingPhase(String function) throws EvalException {
if (phase != Phase.LOADING) {
throw new EvalException(
null, "'" + function + "' can only be called during the loading phase");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
import com.google.devtools.build.lib.syntax.NoneType;
import com.google.devtools.build.lib.syntax.ParserInput;
import com.google.devtools.build.lib.syntax.Printer;
import com.google.devtools.build.lib.syntax.SkylarkUtils;
import com.google.devtools.build.lib.syntax.SkylarkUtils.Phase;
import com.google.devtools.build.lib.syntax.Starlark;
import com.google.devtools.build.lib.syntax.StarlarkFile;
import com.google.devtools.build.lib.syntax.StarlarkSemantics;
Expand Down Expand Up @@ -605,7 +603,7 @@ public NoneType callImpl(
throw new EvalException(null, "unexpected positional arguments");
}
Location loc = call != null ? call.getLocation() : Location.BUILTIN;
SkylarkUtils.checkLoadingOrWorkspacePhase(thread, ruleClass.getName(), loc);
BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase(ruleClass.getName());
try {
addRule(getContext(thread, loc), kwargs, loc, thread);
} catch (RuleFactory.InvalidRuleException | Package.NameConflictException e) {
Expand Down Expand Up @@ -1004,14 +1002,14 @@ public Package.Builder evaluateBuildFile(
.setEventHandler(eventHandler)
.setImportedExtensions(imports)
.build();
SkylarkUtils.setPhase(thread, Phase.LOADING);

// TODO(adonovan): save this as a field in BazelSkylarkContext.
// It needn't be a third thread-local.
// It needn't be a second thread-local.
thread.setThreadLocal(
PackageContext.class, new PackageContext(pkgBuilder, globber, eventHandler));

new BazelStarlarkContext(
BazelStarlarkContext.Phase.LOADING,
ruleClassProvider.getToolsRepository(),
/*fragmentNameToClass=*/ null,
repositoryMapping,
Expand Down
Loading

0 comments on commit b23b8c7

Please sign in to comment.