Search in sources :

Example 1 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.

the class PackageFactory method callExportsFiles.

static Runtime.NoneType callExportsFiles(Object srcs, Object visibilityO, Object licensesO, FuncallExpression ast, Environment env) throws EvalException, ConversionException {
    Package.Builder pkgBuilder = getContext(env, ast).pkgBuilder;
    List<String> files = Type.STRING_LIST.convert(srcs, "'exports_files' operand");
    RuleVisibility visibility;
    try {
        visibility = EvalUtils.isNullOrNone(visibilityO) ? ConstantRuleVisibility.PUBLIC : getVisibility(pkgBuilder.getBuildFileLabel(), BuildType.LABEL_LIST.convert(visibilityO, "'exports_files' operand", pkgBuilder.getBuildFileLabel()));
    } catch (EvalException e) {
        throw new EvalException(ast.getLocation(), e.getMessage());
    }
    // TODO(bazel-team): is licenses plural or singular?
    License license = BuildType.LICENSE.convertOptional(licensesO, "'exports_files' operand");
    for (String file : files) {
        String errorMessage = LabelValidator.validateTargetName(file);
        if (errorMessage != null) {
            throw new EvalException(ast.getLocation(), errorMessage);
        }
        try {
            InputFile inputFile = pkgBuilder.createInputFile(file, ast.getLocation());
            if (inputFile.isVisibilitySpecified() && inputFile.getVisibility() != visibility) {
                throw new EvalException(ast.getLocation(), String.format("visibility for exported file '%s' declared twice", inputFile.getName()));
            }
            if (license != null && inputFile.isLicenseSpecified()) {
                throw new EvalException(ast.getLocation(), String.format("licenses for exported file '%s' declared twice", inputFile.getName()));
            }
            if (license == null && pkgBuilder.getDefaultLicense() == License.NO_LICENSE && RuleClass.isThirdPartyPackage(pkgBuilder.getPackageIdentifier())) {
                throw new EvalException(ast.getLocation(), "third-party file '" + inputFile.getName() + "' lacks a license declaration " + "with one of the following types: notice, reciprocal, permissive, " + "restricted, unencumbered, by_exception_only");
            }
            pkgBuilder.setVisibilityAndLicense(inputFile, visibility, license);
        } catch (Package.Builder.GeneratedLabelConflict e) {
            throw new EvalException(ast.getLocation(), e.getMessage());
        }
    }
    return Runtime.NONE;
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 2 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.

the class PackageFactory method handleGlob.

/**
   * Adds a glob to the package, reporting any errors it finds.
   *
   * @param includes the list of includes which must be non-null
   * @param excludes the list of excludes which must be non-null
   * @param context the package context
   * @param ast the AST
   * @return the list of matches
   * @throws EvalException if globbing failed
   */
private static GlobList<String> handleGlob(List<String> includes, List<String> excludes, boolean excludeDirs, PackageContext context, FuncallExpression ast) throws EvalException, InterruptedException {
    try {
        Globber.Token globToken = context.globber.runAsync(includes, excludes, excludeDirs);
        List<String> matches = context.globber.fetch(globToken);
        return GlobList.captureResults(includes, excludes, matches);
    } catch (IOException expected) {
        context.eventHandler.handle(Event.error(ast.getLocation(), "error globbing [" + Joiner.on(", ").join(includes) + "]: " + expected.getMessage()));
        context.pkgBuilder.setContainsErrors();
        return GlobList.captureResults(includes, excludes, ImmutableList.<String>of());
    } catch (BadGlobException e) {
        throw new EvalException(ast.getLocation(), e.getMessage());
    }
}
Also used : BadGlobException(com.google.devtools.build.lib.packages.Globber.BadGlobException) IOException(java.io.IOException) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 3 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.

the class PackageFactory method newPackageFunction.

/**
   * Returns a function-value implementing "package" in the specified package
   * context.
   */
private static BaseFunction newPackageFunction(final ImmutableMap<String, PackageArgument<?>> packageArguments) {
    // Flatten the map of argument name of PackageArgument specifier in two co-indexed arrays:
    // one for the argument names, to create a FunctionSignature when we create the function,
    // one of the PackageArgument specifiers, over which to iterate at every function invocation
    // at the same time that we iterate over the function arguments.
    final int numArgs = packageArguments.size();
    final String[] argumentNames = new String[numArgs];
    final PackageArgument<?>[] argumentSpecifiers = new PackageArgument<?>[numArgs];
    int i = 0;
    for (Map.Entry<String, PackageArgument<?>> entry : packageArguments.entrySet()) {
        argumentNames[i] = entry.getKey();
        argumentSpecifiers[i++] = entry.getValue();
    }
    return new BaseFunction("package", FunctionSignature.namedOnly(0, argumentNames)) {

        @Override
        public Object call(Object[] arguments, FuncallExpression ast, Environment env) throws EvalException {
            Package.Builder pkgBuilder = getContext(env, ast).pkgBuilder;
            // Validate parameter list
            if (pkgBuilder.isPackageFunctionUsed()) {
                throw new EvalException(ast.getLocation(), "'package' can only be used once per BUILD file");
            }
            pkgBuilder.setPackageFunctionUsed();
            // Parse params
            boolean foundParameter = false;
            for (int i = 0; i < numArgs; i++) {
                Object value = arguments[i];
                if (value != null) {
                    foundParameter = true;
                    argumentSpecifiers[i].convertAndProcess(pkgBuilder, ast.getLocation(), value);
                }
            }
            if (!foundParameter) {
                throw new EvalException(ast.getLocation(), "at least one argument must be given to the 'package' function");
            }
            return Runtime.NONE;
        }
    };
}
Also used : EvalException(com.google.devtools.build.lib.syntax.EvalException) BaseFunction(com.google.devtools.build.lib.syntax.BaseFunction) Environment(com.google.devtools.build.lib.syntax.Environment) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) FuncallExpression(com.google.devtools.build.lib.syntax.FuncallExpression) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap) TreeMap(java.util.TreeMap)

Example 4 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 5 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)

Aggregations

EvalException (com.google.devtools.build.lib.syntax.EvalException)54 IOException (java.io.IOException)15 ImmutableMap (com.google.common.collect.ImmutableMap)9 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 Label (com.google.devtools.build.lib.cmdline.Label)8 Path (com.google.devtools.build.lib.vfs.Path)8 Map (java.util.Map)8 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)7 Environment (com.google.devtools.build.lib.syntax.Environment)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 HashMap (java.util.HashMap)4