Search in sources :

Example 36 with EvalException

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

the class PackageFactory method prefetchGlobs.

/**
   * Visit all targets and expand the globs in parallel.
   */
private void prefetchGlobs(PackageIdentifier packageId, BuildFileAST buildFileAST, boolean wasPreprocessed, Path buildFilePath, Globber globber, RuleVisibility defaultVisibility, MakeEnvironment.Builder pkgMakeEnv) throws InterruptedException {
    if (wasPreprocessed && preprocessorFactory.considersGlobs()) {
        // point in prefetching them again.
        return;
    }
    // these can be executed and their impact on the resulting package can be saved.
    try (Mutability mutability = Mutability.create("prefetchGlobs for %s", packageId)) {
        Environment pkgEnv = Environment.builder(mutability).setGlobals(BazelLibrary.GLOBALS).setEventHandler(NullEventHandler.INSTANCE).setPhase(Phase.LOADING).build();
        SkylarkUtils.setToolsRepository(pkgEnv, ruleClassProvider.getToolsRepository());
        Package.Builder pkgBuilder = new Package.Builder(packageBuilderHelper.createFreshPackage(packageId, ruleClassProvider.getRunfilesPrefix()));
        pkgBuilder.setFilename(buildFilePath).setMakeEnv(pkgMakeEnv).setDefaultVisibility(defaultVisibility).setDefaultVisibilitySet(false);
        // Stuff that closes over the package context:
        PackageContext context = new PackageContext(pkgBuilder, globber, NullEventHandler.INSTANCE, ruleFactory.getAttributeContainerFactory());
        buildPkgEnv(pkgEnv, context, ruleFactory);
        try {
            pkgEnv.update("glob", newGlobFunction.apply(context, /*async=*/
            true));
            // The Fileset function is heavyweight in that it can run glob(). Avoid this during the
            // preloading phase.
            pkgEnv.update("FilesetEntry", Runtime.NONE);
        } catch (EvalException e) {
            throw new AssertionError(e);
        }
        buildFileAST.exec(pkgEnv, NullEventHandler.INSTANCE);
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Mutability(com.google.devtools.build.lib.syntax.Mutability) Environment(com.google.devtools.build.lib.syntax.Environment) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 37 with EvalException

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

the class PackageFactory method callPackageFunction.

static Runtime.NoneType callPackageFunction(String name, Object packagesO, Object includesO, FuncallExpression ast, Environment env) throws EvalException, ConversionException {
    PackageContext context = getContext(env, ast);
    List<String> packages = Type.STRING_LIST.convert(packagesO, "'package_group.packages argument'");
    List<Label> includes = BuildType.LABEL_LIST.convert(includesO, "'package_group.includes argument'", context.pkgBuilder.getBuildFileLabel());
    try {
        context.pkgBuilder.addPackageGroup(name, packages, includes, context.eventHandler, ast.getLocation());
        return Runtime.NONE;
    } catch (LabelSyntaxException e) {
        throw new EvalException(ast.getLocation(), "package group has invalid name: " + name + ": " + e.getMessage());
    } catch (Package.NameConflictException e) {
        throw new EvalException(ast.getLocation(), e.getMessage());
    }
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Label(com.google.devtools.build.lib.cmdline.Label) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 38 with EvalException

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

the class SkylarkRuleImplementationFunctions method checkLabelDict.

/**
   * Ensures the given {@link Map} has keys that have {@link Label} type and values that have either
   * {@link Iterable} or {@link SkylarkNestedSet} type, and raises {@link EvalException} otherwise.
   * Returns a corresponding map where any sets are replaced by iterables.
   */
// TODO(bazel-team): find a better way to typecheck this argument.
@SuppressWarnings("unchecked")
private static Map<Label, Iterable<Artifact>> checkLabelDict(Map<?, ?> labelDict, Location loc) throws EvalException {
    Map<Label, Iterable<Artifact>> convertedMap = new HashMap<>();
    for (Map.Entry<?, ?> entry : labelDict.entrySet()) {
        Object key = entry.getKey();
        if (!(key instanceof Label)) {
            throw new EvalException(loc, Printer.format("invalid key %r in 'label_dict'", key));
        }
        ImmutableList.Builder<Artifact> files = ImmutableList.builder();
        Object val = entry.getValue();
        Iterable<?> valIter;
        try {
            valIter = EvalUtils.toIterableStrict(val, loc);
        } catch (EvalException ex) {
            // EvalException is thrown only if the type is wrong.
            throw new EvalException(loc, Printer.format("invalid value %r in 'label_dict': " + ex, val));
        }
        for (Object file : valIter) {
            if (!(file instanceof Artifact)) {
                throw new EvalException(loc, Printer.format("invalid value %r in 'label_dict'", val));
            }
            files.add((Artifact) file);
        }
        convertedMap.put((Label) key, files.build());
    }
    return convertedMap;
}
Also used : HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) Label(com.google.devtools.build.lib.cmdline.Label) EvalException(com.google.devtools.build.lib.syntax.EvalException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 39 with EvalException

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

the class SkylarkAttr method createAttribute.

private static Attribute.Builder<?> createAttribute(Type<?> type, SkylarkDict<String, Object> arguments, FuncallExpression ast, Environment env) throws EvalException, ConversionException {
    // We use an empty name now so that we can set it later.
    // This trick makes sense only in the context of Skylark (builtin rules should not use it).
    Attribute.Builder<?> builder = Attribute.attr("", type);
    Object defaultValue = arguments.get(DEFAULT_ARG);
    if (!EvalUtils.isNullOrNone(defaultValue)) {
        if (defaultValue instanceof UserDefinedFunction) {
            // Computed attribute. Non label type attributes already caused a type check error.
            SkylarkCallbackFunction callback = new SkylarkCallbackFunction((UserDefinedFunction) defaultValue, ast, env);
            // SkylarkComputedDefaultTemplate needs to know the names of all attributes that it depends
            // on. However, this method does not know anything about other attributes.
            // We solve this problem by asking the SkylarkCallbackFunction for the parameter names used
            // in the function definition, which must be the names of attributes used by the callback.
            builder.value(new SkylarkComputedDefaultTemplate(type, callback.getParameterNames(), callback, ast.getLocation()));
        } else {
            builder.defaultValue(defaultValue, env.getGlobals().label());
        }
    }
    for (String flag : SkylarkList.castSkylarkListOrNoneToList(arguments.get(FLAGS_ARG), String.class, FLAGS_ARG)) {
        builder.setPropertyFlag(flag);
    }
    if (containsNonNoneKey(arguments, MANDATORY_ARG) && (Boolean) arguments.get(MANDATORY_ARG)) {
        builder.setPropertyFlag("MANDATORY");
    }
    // TODO(laurentlb): Deprecated, remove in August 2016 (use allow_empty instead).
    if (containsNonNoneKey(arguments, NON_EMPTY_ARG) && (Boolean) arguments.get(NON_EMPTY_ARG)) {
        builder.setPropertyFlag("NON_EMPTY");
    }
    if (containsNonNoneKey(arguments, ALLOW_EMPTY_ARG) && !(Boolean) arguments.get(ALLOW_EMPTY_ARG)) {
        builder.setPropertyFlag("NON_EMPTY");
    }
    if (containsNonNoneKey(arguments, EXECUTABLE_ARG) && (Boolean) arguments.get(EXECUTABLE_ARG)) {
        builder.setPropertyFlag("EXECUTABLE");
        if (!containsNonNoneKey(arguments, CONFIGURATION_ARG)) {
            throw new EvalException(ast.getLocation(), "cfg parameter is mandatory when executable=True is provided. Please see " + "https://www.bazel.build/versions/master/docs/skylark/rules.html#configurations " + "for more details.");
        }
    }
    // TODO(laurentlb): Deprecated, remove in August 2016 (use allow_single_file).
    if (containsNonNoneKey(arguments, SINGLE_FILE_ARG) && (Boolean) arguments.get(SINGLE_FILE_ARG)) {
        if (containsNonNoneKey(arguments, ALLOW_SINGLE_FILE_ARG)) {
            throw new EvalException(ast.getLocation(), "Cannot specify both single_file (deprecated) and allow_single_file");
        }
        builder.setPropertyFlag("SINGLE_ARTIFACT");
    }
    if (containsNonNoneKey(arguments, ALLOW_FILES_ARG) && containsNonNoneKey(arguments, ALLOW_SINGLE_FILE_ARG)) {
        throw new EvalException(ast.getLocation(), "Cannot specify both allow_files and allow_single_file");
    }
    if (containsNonNoneKey(arguments, ALLOW_FILES_ARG)) {
        Object fileTypesObj = arguments.get(ALLOW_FILES_ARG);
        setAllowedFileTypes(ALLOW_FILES_ARG, fileTypesObj, ast, builder);
    } else if (containsNonNoneKey(arguments, ALLOW_SINGLE_FILE_ARG)) {
        Object fileTypesObj = arguments.get(ALLOW_SINGLE_FILE_ARG);
        setAllowedFileTypes(ALLOW_SINGLE_FILE_ARG, fileTypesObj, ast, builder);
        builder.setPropertyFlag("SINGLE_ARTIFACT");
    } else if (type.getLabelClass() == LabelClass.DEPENDENCY) {
        builder.allowedFileTypes(FileTypeSet.NO_FILE);
    }
    Object ruleClassesObj = arguments.get(ALLOW_RULES_ARG);
    if (ruleClassesObj != null && ruleClassesObj != Runtime.NONE) {
        builder.allowedRuleClasses(SkylarkList.castSkylarkListOrNoneToList(ruleClassesObj, String.class, "allowed rule classes for attribute definition"));
    }
    List<Object> values = SkylarkList.castSkylarkListOrNoneToList(arguments.get(VALUES_ARG), Object.class, VALUES_ARG);
    if (!Iterables.isEmpty(values)) {
        builder.allowedValues(new AllowedValueSet(values));
    }
    if (containsNonNoneKey(arguments, PROVIDERS_ARG)) {
        Object obj = arguments.get(PROVIDERS_ARG);
        SkylarkType.checkType(obj, SkylarkList.class, PROVIDERS_ARG);
        ImmutableList<ImmutableSet<SkylarkProviderIdentifier>> providersList = buildProviderPredicate((SkylarkList<?>) obj, PROVIDERS_ARG, ast.getLocation());
        builder.mandatoryProvidersList(providersList);
    }
    if (containsNonNoneKey(arguments, CONFIGURATION_ARG)) {
        Object trans = arguments.get(CONFIGURATION_ARG);
        if (trans.equals("data")) {
            builder.cfg(ConfigurationTransition.DATA);
        } else if (trans.equals("host")) {
            builder.cfg(ConfigurationTransition.HOST);
        } else if (trans instanceof SplitTransition<?>) {
            builder.cfg((SplitTransition<?>) trans);
        } else if (!trans.equals("target")) {
            throw new EvalException(ast.getLocation(), "cfg must be either 'data', 'host', or 'target'.");
        }
    }
    if (containsNonNoneKey(arguments, ASPECTS_ARG)) {
        Object obj = arguments.get(ASPECTS_ARG);
        SkylarkType.checkType(obj, SkylarkList.class, ASPECTS_ARG);
        List<SkylarkAspect> aspects = ((SkylarkList<?>) obj).getContents(SkylarkAspect.class, "aspects");
        for (SkylarkAspect aspect : aspects) {
            builder.aspect(aspect, ast.getLocation());
        }
    }
    return builder;
}
Also used : SkylarkCallbackFunction(com.google.devtools.build.lib.syntax.SkylarkCallbackFunction) UserDefinedFunction(com.google.devtools.build.lib.syntax.UserDefinedFunction) Attribute(com.google.devtools.build.lib.packages.Attribute) EvalException(com.google.devtools.build.lib.syntax.EvalException) SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) SplitTransition(com.google.devtools.build.lib.packages.Attribute.SplitTransition) ImmutableSet(com.google.common.collect.ImmutableSet) SkylarkAspect(com.google.devtools.build.lib.packages.SkylarkAspect) SkylarkComputedDefaultTemplate(com.google.devtools.build.lib.packages.Attribute.SkylarkComputedDefaultTemplate) AllowedValueSet(com.google.devtools.build.lib.packages.Attribute.AllowedValueSet)

Example 40 with EvalException

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

the class SkylarkRuleConfiguredTargetBuilder method addStructFieldsAndBuild.

private static ConfiguredTarget addStructFieldsAndBuild(RuleContext ruleContext, RuleConfiguredTargetBuilder builder, Object target, Artifact executable, Map<String, Class<? extends TransitiveInfoProvider>> registeredProviderTypes) throws EvalException {
    Location loc = null;
    Boolean isParsed = false;
    if (target instanceof SkylarkClassObject) {
        SkylarkClassObject struct = (SkylarkClassObject) target;
        loc = struct.getCreationLoc();
        parseProviderKeys(struct, false, ruleContext, loc, executable, registeredProviderTypes, builder);
        isParsed = true;
    } else if (target instanceof Iterable) {
        loc = ruleContext.getRule().getRuleClassObject().getConfiguredTargetFunction().getLocation();
        for (Object o : (Iterable) target) {
            SkylarkClassObject declaredProvider = SkylarkType.cast(o, SkylarkClassObject.class, loc, "A return value of a rule implementation function should be " + "a sequence of declared providers");
            if (declaredProvider.getConstructor().getKey().equals(SkylarkRuleContext.getDefaultProvider().getKey())) {
                parseProviderKeys(declaredProvider, true, ruleContext, loc, executable, registeredProviderTypes, builder);
                isParsed = true;
            } else {
                Location creationLoc = declaredProvider.getCreationLocOrNull();
                builder.addSkylarkDeclaredProvider(declaredProvider, creationLoc != null ? creationLoc : loc);
            }
        }
    }
    if (!isParsed) {
        addSimpleProviders(builder, ruleContext, loc, executable, null, null, null, null);
    }
    try {
        return builder.build();
    } catch (IllegalArgumentException e) {
        throw new EvalException(loc, e.getMessage());
    }
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException) Location(com.google.devtools.build.lib.events.Location)

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