Search in sources :

Example 6 with BaseFunction

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

the class RuleFactory method generatorAttributesForMacros.

/**
   * If the rule was created by a macro, this method sets the appropriate values for the
   * attributes generator_{name, function, location} and returns all attributes.
   *
   * <p>Otherwise, it returns the given attributes without any changes.
   */
private static AttributesAndLocation generatorAttributesForMacros(BuildLangTypedAttributeValuesMap args, @Nullable Environment env, Location location, Label label) {
    // trace (=> no macro) or b) the attributes have already been set by Python pre-processing.
    if (env == null) {
        return new AttributesAndLocation(args, location);
    }
    boolean hasName = args.containsAttributeNamed("generator_name");
    boolean hasFunc = args.containsAttributeNamed("generator_function");
    // TODO(bazel-team): resolve cases in our code where hasName && !hasFunc, or hasFunc && !hasName
    if (hasName || hasFunc) {
        return new AttributesAndLocation(args, location);
    }
    Pair<FuncallExpression, BaseFunction> topCall = env.getTopCall();
    if (topCall == null || !(topCall.second instanceof UserDefinedFunction)) {
        return new AttributesAndLocation(args, location);
    }
    FuncallExpression generator = topCall.first;
    BaseFunction function = topCall.second;
    String name = generator.getNameArg();
    ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
    for (String attributeName : args.getAttributeNames()) {
        builder.put(attributeName, args.getAttributeValue(attributeName));
    }
    builder.put("generator_name", (name == null) ? args.getAttributeValue("name") : name);
    builder.put("generator_function", function.getName());
    if (generator.getLocation() != null) {
        location = generator.getLocation();
    }
    String relativePath = maybeGetRelativeLocation(location, label);
    if (relativePath != null) {
        builder.put("generator_location", relativePath);
    }
    try {
        return new AttributesAndLocation(new BuildLangTypedAttributeValuesMap(builder.build()), location);
    } catch (IllegalArgumentException ex) {
        // We just fall back to the default case and swallow any messages.
        return new AttributesAndLocation(args, location);
    }
}
Also used : BaseFunction(com.google.devtools.build.lib.syntax.BaseFunction) UserDefinedFunction(com.google.devtools.build.lib.syntax.UserDefinedFunction) FuncallExpression(com.google.devtools.build.lib.syntax.FuncallExpression) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with BaseFunction

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

the class PackageFactory method newNativeModule.

/**
   * Returns a native module with the functions created using the {@link RuleClassProvider}
   * of this {@link PackageFactory}.
   */
private ClassObject newNativeModule() {
    ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
    for (String nativeFunction : Runtime.getFunctionNames(SkylarkNativeModule.class)) {
        builder.put(nativeFunction, Runtime.getFunction(SkylarkNativeModule.class, nativeFunction));
    }
    for (String ruleClass : ruleFactory.getRuleClassNames()) {
        builder.put(ruleClass, newRuleFunction(ruleFactory, ruleClass));
    }
    builder.put("package", newPackageFunction(packageArguments));
    for (EnvironmentExtension extension : environmentExtensions) {
        for (BaseFunction function : extension.nativeModuleFunctions()) {
            builder.put(function.getName(), function);
        }
    }
    return NativeClassObjectConstructor.STRUCT.create(builder.build(), "no native function or rule '%s'");
}
Also used : BaseFunction(com.google.devtools.build.lib.syntax.BaseFunction) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

BaseFunction (com.google.devtools.build.lib.syntax.BaseFunction)7 ImmutableMap (com.google.common.collect.ImmutableMap)5 EvalException (com.google.devtools.build.lib.syntax.EvalException)3 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)2 FuncallExpression (com.google.devtools.build.lib.syntax.FuncallExpression)2 Map (java.util.Map)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 EnvironmentExtension (com.google.devtools.build.lib.packages.PackageFactory.EnvironmentExtension)1 BuildLangTypedAttributeValuesMap (com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap)1 Environment (com.google.devtools.build.lib.syntax.Environment)1 Mutability (com.google.devtools.build.lib.syntax.Mutability)1 UserDefinedFunction (com.google.devtools.build.lib.syntax.UserDefinedFunction)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 Nullable (javax.annotation.Nullable)1