Search in sources :

Example 16 with LabelSyntaxException

use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.

the class License method parseLicense.

/**
   * Computes a license which can be used to check if a package is compatible
   * with some kinds of distribution. The list of licenses is scanned for the
   * least restrictive, and the exceptions are added.
   *
   * @param licStrings the list of license strings declared for the package
   * @throws LicenseParsingException if there are any parsing problems
   */
public static License parseLicense(List<String> licStrings) throws LicenseParsingException {
    /*
     * The semantics of comparison for licenses depends on a stable iteration
     * order for both license types and exceptions. For licenseTypes, it will be
     * the comparison order from the enumerated types; for exceptions, it will
     * be lexicographic order achieved using TreeSets.
     */
    Set<LicenseType> licenseTypes = EnumSet.noneOf(LicenseType.class);
    Set<Label> exceptions = Sets.newTreeSet();
    for (String str : licStrings) {
        if (str.startsWith("exception=")) {
            try {
                Label label = Label.parseAbsolute(str.substring("exception=".length()));
                exceptions.add(label);
            } catch (LabelSyntaxException e) {
                throw new LicenseParsingException(e.getMessage());
            }
        } else {
            try {
                licenseTypes.add(LicenseType.valueOf(str.toUpperCase()));
            } catch (IllegalArgumentException e) {
                throw new LicenseParsingException("invalid license type: '" + str + "'");
            }
        }
    }
    return License.of(licenseTypes, exceptions);
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Label(com.google.devtools.build.lib.cmdline.Label)

Example 17 with LabelSyntaxException

use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.

the class WorkspaceFactory method newBindFunction.

private static BuiltinFunction newBindFunction(final RuleFactory ruleFactory) {
    return new BuiltinFunction("bind", FunctionSignature.namedOnly(1, "name", "actual"), BuiltinFunction.USE_AST_ENV) {

        public Object invoke(String name, String actual, FuncallExpression ast, Environment env) throws EvalException, InterruptedException {
            Label nameLabel;
            try {
                nameLabel = Label.parseAbsolute("//external:" + name);
                try {
                    Package.Builder builder = PackageFactory.getContext(env, ast).pkgBuilder;
                    RuleClass ruleClass = ruleFactory.getRuleClass("bind");
                    builder.externalPackageData().addBindRule(builder, ruleClass, nameLabel, actual == null ? null : Label.parseAbsolute(actual), ast.getLocation(), ruleFactory.getAttributeContainer(ruleClass));
                } catch (RuleFactory.InvalidRuleException | Package.NameConflictException | LabelSyntaxException e) {
                    throw new EvalException(ast.getLocation(), e.getMessage());
                }
            } catch (LabelSyntaxException e) {
                throw new EvalException(ast.getLocation(), e.getMessage());
            }
            return NONE;
        }
    };
}
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) 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) InvalidRuleException(com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException)

Example 18 with LabelSyntaxException

use of com.google.devtools.build.lib.cmdline.LabelSyntaxException 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)

Example 19 with LabelSyntaxException

use of com.google.devtools.build.lib.cmdline.LabelSyntaxException 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 20 with LabelSyntaxException

use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.

the class PreciseAspectResolver method computeBuildFileDependencies.

@Override
public Set<Label> computeBuildFileDependencies(Package pkg, BuildFileDependencyMode mode) throws InterruptedException {
    Set<Label> result = new LinkedHashSet<>();
    result.addAll(mode.getDependencies(pkg));
    Set<PackageIdentifier> dependentPackages = new LinkedHashSet<>();
    // Iterate over all rules...
    for (Target target : pkg.getTargets()) {
        if (!(target instanceof Rule)) {
            continue;
        }
        // ...figure out which direct dependencies can possibly have aspects attached to them...
        Multimap<Attribute, Label> depsWithPossibleAspects = ((Rule) target).getTransitions(new BinaryPredicate<Rule, Attribute>() {

            @Override
            public boolean apply(@Nullable Rule rule, Attribute attribute) {
                for (Aspect aspectWithParameters : attribute.getAspects(rule)) {
                    if (!aspectWithParameters.getDefinition().getAttributes().isEmpty()) {
                        return true;
                    }
                }
                return false;
            }
        });
        // ...and add the package of the aspect.
        for (Label depLabel : depsWithPossibleAspects.values()) {
            dependentPackages.add(depLabel.getPackageIdentifier());
        }
    }
    // Then add all the subinclude labels of the packages thus found to the result.
    for (PackageIdentifier packageIdentifier : dependentPackages) {
        try {
            result.add(Label.create(packageIdentifier, "BUILD"));
            Package dependentPackage = packageProvider.getPackage(eventHandler, packageIdentifier);
            result.addAll(mode.getDependencies(dependentPackage));
        } catch (NoSuchPackageException e) {
        // If the package is not found, just add its BUILD file, which is already done above.
        // Hopefully this error is not raised when there is a syntax error in a subincluded file
        // or something.
        } catch (LabelSyntaxException e) {
            throw new IllegalStateException(e);
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) Aspect(com.google.devtools.build.lib.packages.Aspect) Target(com.google.devtools.build.lib.packages.Target) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Rule(com.google.devtools.build.lib.packages.Rule) Package(com.google.devtools.build.lib.packages.Package)

Aggregations

LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)28 Label (com.google.devtools.build.lib.cmdline.Label)17 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)10 SkyKey (com.google.devtools.build.skyframe.SkyKey)8 Path (com.google.devtools.build.lib.vfs.Path)7 EvalException (com.google.devtools.build.lib.syntax.EvalException)6 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)6 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)5 Package (com.google.devtools.build.lib.packages.Package)5 Target (com.google.devtools.build.lib.packages.Target)4 LinkedHashSet (java.util.LinkedHashSet)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)3 NameConflictException (com.google.devtools.build.lib.packages.Package.NameConflictException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)2 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)2 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)2