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