Search in sources :

Example 6 with EvalException

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();
}
Also used : Frame(com.google.devtools.build.lib.syntax.Environment.Frame) HashMap(java.util.HashMap) EvalException(com.google.devtools.build.lib.syntax.EvalException) ImmutableMap(com.google.common.collect.ImmutableMap) EnvironmentExtension(com.google.devtools.build.lib.packages.PackageFactory.EnvironmentExtension) Extension(com.google.devtools.build.lib.syntax.Environment.Extension) Environment(com.google.devtools.build.lib.syntax.Environment) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with EvalException

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;
        }
    };
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) EvalException(com.google.devtools.build.lib.syntax.EvalException) NameConflictException(com.google.devtools.build.lib.packages.Package.NameConflictException) BuiltinFunction(com.google.devtools.build.lib.syntax.BuiltinFunction) Environment(com.google.devtools.build.lib.syntax.Environment) FuncallExpression(com.google.devtools.build.lib.syntax.FuncallExpression) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) InvalidRuleException(com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException)

Example 8 with EvalException

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);
    }
}
Also used : BaseFunction(com.google.devtools.build.lib.syntax.BaseFunction) EnvironmentExtension(com.google.devtools.build.lib.packages.PackageFactory.EnvironmentExtension) EvalException(com.google.devtools.build.lib.syntax.EvalException) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) File(java.io.File)

Example 9 with EvalException

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)));
    }
}
Also used : BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) ImmutableList(com.google.common.collect.ImmutableList) Label(com.google.devtools.build.lib.cmdline.Label) LateBoundDefault(com.google.devtools.build.lib.packages.Attribute.LateBoundDefault) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 10 with EvalException

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()));
}
Also used : ClassObjectConstructor(com.google.devtools.build.lib.packages.ClassObjectConstructor) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Aggregations

EvalException (com.google.devtools.build.lib.syntax.EvalException)47 IOException (java.io.IOException)15 WorkspaceAttributeMapper (com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper)9 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)9 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 Label (com.google.devtools.build.lib.cmdline.Label)8 Path (com.google.devtools.build.lib.vfs.Path)8 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)7 Environment (com.google.devtools.build.lib.syntax.Environment)7 Map (java.util.Map)7 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)6 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)6 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)6 SkyKey (com.google.devtools.build.skyframe.SkyKey)6 Nullable (javax.annotation.Nullable)6 FileValue (com.google.devtools.build.lib.skyframe.FileValue)5 ImmutableList (com.google.common.collect.ImmutableList)4 Artifact (com.google.devtools.build.lib.actions.Artifact)4 BaseFunction (com.google.devtools.build.lib.syntax.BaseFunction)3