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;
}
};
}
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();
}
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;
}
};
}
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;
}
};
}
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;
}
Aggregations