use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.
the class WorkspaceFactory method execute.
private void execute(BuildFileAST ast, @Nullable Map<String, Extension> importedExtensions, StoredEventHandler localReporter) throws InterruptedException {
Environment.Builder environmentBuilder = Environment.builder(mutability).setGlobals(BazelLibrary.GLOBALS).setEventHandler(localReporter);
if (importedExtensions != null) {
Map<String, Extension> map = new HashMap<String, Extension>(parentImportMap);
map.putAll(importedExtensions);
importMap = ImmutableMap.<String, Extension>copyOf(importedExtensions);
} else {
importMap = parentImportMap;
}
environmentBuilder.setImportedExtensions(importMap);
Environment workspaceEnv = environmentBuilder.setPhase(Phase.WORKSPACE).build();
addWorkspaceFunctions(workspaceEnv, localReporter);
for (Map.Entry<String, Object> binding : parentVariableBindings.entrySet()) {
try {
workspaceEnv.update(binding.getKey(), binding.getValue());
} catch (EvalException e) {
// This should never happen because everything was already evaluated.
throw new IllegalStateException(e);
}
}
if (!ast.exec(workspaceEnv, localReporter)) {
localReporter.handle(Event.error("Error evaluating WORKSPACE file"));
}
// Save the list of variable bindings for the next part of the workspace file. The list of
// variable bindings of interest are the global variable bindings that are defined by the user,
// so not the workspace functions.
// Workspace functions are not serializable and should not be passed over sky values. They
// also have a package builder specific to the current part and should be reinitialized for
// each workspace file.
ImmutableMap.Builder<String, Object> bindingsBuilder = ImmutableMap.builder();
Frame globals = workspaceEnv.getGlobals();
for (String s : globals.getDirectVariableNames()) {
Object o = globals.get(s);
if (!isAWorkspaceFunction(s, o)) {
bindingsBuilder.put(s, o);
}
}
variableBindings = bindingsBuilder.build();
builder.addEvents(localReporter.getEvents());
if (localReporter.hasErrors()) {
builder.setContainsErrors();
}
localReporter.clear();
}
use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.
the class WorkspaceFactory method newRuleFunction.
/**
* Returns a function-value implementing the build rule "ruleClass" (e.g. cc_library) in the
* specified package context.
*/
private static BuiltinFunction newRuleFunction(final RuleFactory ruleFactory, final String ruleClassName, final boolean allowOverride) {
return new BuiltinFunction(ruleClassName, FunctionSignature.KWARGS, BuiltinFunction.USE_AST_ENV) {
public Object invoke(Map<String, Object> kwargs, FuncallExpression ast, Environment env) throws EvalException, InterruptedException {
try {
Package.Builder builder = PackageFactory.getContext(env, ast).pkgBuilder;
if (!allowOverride && kwargs.containsKey("name") && builder.targets.containsKey(kwargs.get("name"))) {
throw new EvalException(ast.getLocation(), "Cannot redefine repository after any load statement in the WORKSPACE file" + " (for repository '" + kwargs.get("name") + "')");
}
RuleClass ruleClass = ruleFactory.getRuleClass(ruleClassName);
RuleClass bindRuleClass = ruleFactory.getRuleClass("bind");
Rule rule = builder.externalPackageData().createAndAddRepositoryRule(builder, ruleClass, bindRuleClass, kwargs, ast);
if (!isLegalWorkspaceName(rule.getName())) {
throw new EvalException(ast.getLocation(), rule + "'s name field must be a legal workspace name");
}
} catch (RuleFactory.InvalidRuleException | Package.NameConflictException | LabelSyntaxException e) {
throw new EvalException(ast.getLocation(), e.getMessage());
}
return NONE;
}
};
}
use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.
the class WorkspaceFactory method addWorkspaceFunctions.
private void addWorkspaceFunctions(Environment workspaceEnv, StoredEventHandler localReporter) {
try {
workspaceEnv.setup("workspace", newWorkspaceFunction.apply(allowOverride, ruleFactory));
for (Map.Entry<String, BaseFunction> function : workspaceFunctions.entrySet()) {
workspaceEnv.update(function.getKey(), function.getValue());
}
if (installDir != null) {
workspaceEnv.update("__embedded_dir__", installDir.getPathString());
}
if (workspaceDir != null) {
workspaceEnv.update("__workspace_dir__", workspaceDir.getPathString());
}
File jreDirectory = new File(System.getProperty("java.home"));
workspaceEnv.update("DEFAULT_SERVER_JAVABASE", jreDirectory.getParentFile().toString());
for (EnvironmentExtension extension : environmentExtensions) {
extension.updateWorkspace(workspaceEnv);
}
workspaceEnv.setupDynamic(PackageFactory.PKG_CONTEXT, new PackageFactory.PackageContext(builder, null, localReporter, AttributeContainer.ATTRIBUTE_CONTAINER_FACTORY));
} catch (EvalException e) {
throw new AssertionError(e);
}
}
use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.
the class DependencyResolver method resolveLateBoundAttribute.
/**
* Returns the label dependencies for the given late-bound attribute in this rule.
*
* @param rule the rule being evaluated
* @param attribute the attribute to evaluate
* @param config the configuration to evaluate the attribute in
* @param attributeMap mapper to attribute values
*/
private Iterable<Label> resolveLateBoundAttribute(Rule rule, Attribute attribute, BuildConfiguration config, AttributeMap attributeMap) throws EvalException, InterruptedException {
Preconditions.checkArgument(attribute.isLateBound());
@SuppressWarnings("unchecked") LateBoundDefault<BuildConfiguration> lateBoundDefault = (LateBoundDefault<BuildConfiguration>) attribute.getLateBoundDefault();
// TODO(bazel-team): This might be too expensive - can we cache this somehow?
if (!lateBoundDefault.getRequiredConfigurationFragments().isEmpty()) {
if (!config.hasAllFragments(lateBoundDefault.getRequiredConfigurationFragments())) {
return ImmutableList.<Label>of();
}
}
// TODO(bazel-team): We should check if the implementation tries to access an undeclared
// fragment.
Object actualValue = lateBoundDefault.resolve(rule, attributeMap, config);
if (EvalUtils.isNullOrNone(actualValue)) {
return ImmutableList.<Label>of();
}
try {
ImmutableList.Builder<Label> deps = ImmutableList.builder();
if (attribute.getType() == BuildType.LABEL) {
deps.add(rule.getLabel().resolveRepositoryRelative(BuildType.LABEL.cast(actualValue)));
} else if (attribute.getType() == BuildType.LABEL_LIST) {
for (Label label : BuildType.LABEL_LIST.cast(actualValue)) {
deps.add(rule.getLabel().resolveRepositoryRelative(label));
}
} else {
throw new IllegalStateException(String.format("Late bound attribute '%s' is not a label or a label list", attribute.getName()));
}
return deps.build();
} catch (ClassCastException e) {
// From either of the cast calls above.
throw new EvalException(rule.getLocation(), String.format("When computing the default value of %s, expected '%s', got '%s'", attribute.getName(), attribute.getType(), EvalUtils.getDataTypeName(actualValue, true)));
}
}
use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.
the class AbstractConfiguredTarget method getIndex.
@Override
public Object getIndex(Object key, Location loc) throws EvalException {
if (!(key instanceof ClassObjectConstructor)) {
throw new EvalException(loc, String.format("Type Target only supports indexing by object constructors, got %s instead", EvalUtils.getDataTypeName(key)));
}
ClassObjectConstructor constructor = (ClassObjectConstructor) key;
SkylarkProviders provider = getProvider(SkylarkProviders.class);
if (provider != null) {
Object declaredProvider = provider.getDeclaredProvider(constructor.getKey());
if (declaredProvider != null) {
return declaredProvider;
}
}
// Either provider or declaredProvider is null
throw new EvalException(loc, String.format("Object of type Target doesn't contain declared provider %s", constructor.getPrintableName()));
}
Aggregations