Search in sources :

Example 1 with CannotPrecomputeDefaultsException

use of com.google.devtools.build.lib.packages.Attribute.SkylarkComputedDefaultTemplate.CannotPrecomputeDefaultsException in project bazel by bazelbuild.

the class RuleFactory method createRule.

/**
   * Creates and returns a rule instance.
   *
   * <p>It is the caller's responsibility to add the rule to the package (the caller may choose not
   * to do so if, for example, the rule has errors).
   */
static Rule createRule(Package.Builder pkgBuilder, RuleClass ruleClass, BuildLangTypedAttributeValuesMap attributeValues, EventHandler eventHandler, @Nullable FuncallExpression ast, Location location, @Nullable Environment env, AttributeContainer attributeContainer) throws InvalidRuleException, InterruptedException {
    Preconditions.checkNotNull(ruleClass);
    String ruleClassName = ruleClass.getName();
    Object nameObject = attributeValues.getAttributeValue("name");
    if (nameObject == null) {
        throw new InvalidRuleException(ruleClassName + " rule has no 'name' attribute");
    } else if (!(nameObject instanceof String)) {
        throw new InvalidRuleException(ruleClassName + " 'name' attribute must be a string");
    }
    String name = (String) nameObject;
    Label label;
    try {
        // Test that this would form a valid label name -- in particular, this
        // catches cases where Makefile variables $(foo) appear in "name".
        label = pkgBuilder.createLabel(name);
    } catch (LabelSyntaxException e) {
        throw new InvalidRuleException("illegal rule name: " + name + ": " + e.getMessage());
    }
    boolean inWorkspaceFile = pkgBuilder.isWorkspace();
    if (ruleClass.getWorkspaceOnly() && !inWorkspaceFile) {
        throw new RuleFactory.InvalidRuleException(ruleClass + " must be in the WORKSPACE file " + "(used by " + label + ")");
    } else if (!ruleClass.getWorkspaceOnly() && inWorkspaceFile) {
        throw new RuleFactory.InvalidRuleException(ruleClass + " cannot be in the WORKSPACE file " + "(used by " + label + ")");
    }
    AttributesAndLocation generator = generatorAttributesForMacros(attributeValues, env, location, label);
    try {
        return ruleClass.createRule(pkgBuilder, label, generator.attributes, eventHandler, ast, generator.location, attributeContainer);
    } catch (LabelSyntaxException | CannotPrecomputeDefaultsException e) {
        throw new RuleFactory.InvalidRuleException(ruleClass + " " + e.getMessage());
    }
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) CannotPrecomputeDefaultsException(com.google.devtools.build.lib.packages.Attribute.SkylarkComputedDefaultTemplate.CannotPrecomputeDefaultsException) Label(com.google.devtools.build.lib.cmdline.Label)

Aggregations

Label (com.google.devtools.build.lib.cmdline.Label)1 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 CannotPrecomputeDefaultsException (com.google.devtools.build.lib.packages.Attribute.SkylarkComputedDefaultTemplate.CannotPrecomputeDefaultsException)1