Search in sources :

Example 1 with Environment

use of com.google.devtools.build.lib.syntax.Environment 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 2 with Environment

use of com.google.devtools.build.lib.syntax.Environment 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 3 with Environment

use of com.google.devtools.build.lib.syntax.Environment 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 4 with Environment

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

the class SkylarkTestCase method createEvaluationTestCase.

protected EvaluationTestCase createEvaluationTestCase() {
    return new EvaluationTestCase() {

        @Override
        public Environment newEnvironment() throws Exception {
            Environment env = Environment.builder(mutability).setEventHandler(getEventHandler()).setGlobals(SkylarkModules.getGlobals(SkylarkModules.MODULES)).setPhase(Phase.LOADING).build().setupDynamic(PackageFactory.PKG_CONTEXT, // create rules. Creating actual rules is tested in SkylarkIntegrationTest.
            new PackageContext(null, null, getEventHandler(), null));
            SkylarkUtils.setToolsRepository(env, TestConstants.TOOLS_REPOSITORY);
            return env;
        }
    };
}
Also used : EvaluationTestCase(com.google.devtools.build.lib.syntax.util.EvaluationTestCase) PackageContext(com.google.devtools.build.lib.packages.PackageFactory.PackageContext) Environment(com.google.devtools.build.lib.syntax.Environment)

Example 5 with Environment

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

the class EvaluationTestCase method newBuildEnvironment.

/**
   * Creates a standard Environment for tests in the BUILD language.
   * No PythonPreprocessing, mostly empty mutable Environment.
   */
public Environment newBuildEnvironment() {
    Environment env = Environment.builder(mutability).setGlobals(BazelLibrary.GLOBALS).setEventHandler(getEventHandler()).setPhase(Phase.LOADING).build();
    SkylarkUtils.setToolsRepository(env, TestConstants.TOOLS_REPOSITORY);
    return env;
}
Also used : Environment(com.google.devtools.build.lib.syntax.Environment) ValidationEnvironment(com.google.devtools.build.lib.syntax.ValidationEnvironment)

Aggregations

Environment (com.google.devtools.build.lib.syntax.Environment)17 EvalException (com.google.devtools.build.lib.syntax.EvalException)7 Mutability (com.google.devtools.build.lib.syntax.Mutability)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)3 BuiltinFunction (com.google.devtools.build.lib.syntax.BuiltinFunction)3 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)3 FuncallExpression (com.google.devtools.build.lib.syntax.FuncallExpression)3 Map (java.util.Map)3 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)2 Label (com.google.devtools.build.lib.cmdline.Label)2 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)2 NameConflictException (com.google.devtools.build.lib.packages.Package.NameConflictException)2 InvalidRuleException (com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException)2 HashMap (java.util.HashMap)2 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)1 StoredEventHandler (com.google.devtools.build.lib.events.StoredEventHandler)1 AspectDescriptor (com.google.devtools.build.lib.packages.AspectDescriptor)1