use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.
the class AspectDefinitionTest method testAspectWithImplicitOrLateboundAttribute_AddsToAttributeMap.
@Test
public void testAspectWithImplicitOrLateboundAttribute_AddsToAttributeMap() throws Exception {
Attribute implicit = attr("$runtime", BuildType.LABEL).value(Label.parseAbsoluteUnchecked("//run:time")).build();
LateBoundLabel<String> latebound = new LateBoundLabel<String>() {
@Override
public Label resolve(Rule rule, AttributeMap attributes, String configuration) {
return Label.parseAbsoluteUnchecked("//run:away");
}
};
AspectDefinition simple = new AspectDefinition.Builder(TEST_ASPECT_CLASS).add(implicit).add(attr(":latebound", BuildType.LABEL).value(latebound)).build();
assertThat(simple.getAttributes()).containsEntry("$runtime", implicit);
assertThat(simple.getAttributes()).containsKey(":latebound");
assertThat(simple.getAttributes().get(":latebound").getLateBoundDefault()).isEqualTo(latebound);
}
use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.
the class CompileOneDependencyTransformer method getOrderedRuleList.
/**
* Returns a list of rules in the given package sorted by BUILD file order. When
* multiple rules depend on a target, we choose the first match in this list (after
* filtering for preferred dependencies - see below).
*/
private Iterable<Rule> getOrderedRuleList(Package pkg) {
List<Rule> orderedList = Lists.newArrayList();
for (Rule rule : pkg.getTargets(Rule.class)) {
orderedList.add(rule);
}
Collections.sort(orderedList, new Comparator<Rule>() {
@Override
public int compare(Rule o1, Rule o2) {
return Integer.compare(o1.getLocation().getStartOffset(), o2.getLocation().getStartOffset());
}
});
return orderedList;
}
use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.
the class CompileOneDependencyTransformer method listContainsFile.
/**
* Returns true if a specific rule compiles a specific source. Looks through genrules and
* filegroups.
*/
private boolean listContainsFile(ExtendedEventHandler eventHandler, Collection<Label> srcLabels, Label source, Set<Label> visitedRuleLabels) throws TargetParsingException, InterruptedException {
if (srcLabels.contains(source)) {
return true;
}
for (Label label : srcLabels) {
if (!visitedRuleLabels.add(label)) {
continue;
}
Target target = null;
try {
target = targetProvider.getTarget(eventHandler, label);
} catch (NoSuchThingException e) {
// Just ignore failing sources/packages. We could report them here, but as long as we do
// early return, the presence of this error would then be determined by the order of items
// in the srcs attribute. A proper error will be created by the subsequent loading.
}
if (target == null || target instanceof FileTarget) {
continue;
}
Rule targetRule = target.getAssociatedRule();
if ("filegroup".equals(targetRule.getRuleClass())) {
RawAttributeMapper attributeMapper = RawAttributeMapper.of(targetRule);
Collection<Label> srcs = attributeMapper.getMergedValues("srcs", BuildType.LABEL_LIST);
if (listContainsFile(eventHandler, srcs, source, visitedRuleLabels)) {
return true;
}
} else if ("genrule".equals(targetRule.getRuleClass())) {
// TODO(djasper): Likely, it makes much more sense to look at the inputs of a genrule.
for (OutputFile file : targetRule.getOutputFiles()) {
if (file.getLabel().equals(source)) {
return true;
}
}
}
}
return false;
}
use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.
the class XmlOutputFormatter method createTargetElement.
/**
* Creates and returns a new DOM tree for the specified build target.
*
* XML structure:
* - element tag is <source-file>, <generated-file> or <rule
* class="cc_library">, following the terminology of
* {@link Target#getTargetKind()}.
* - 'name' attribute is target's label.
* - 'location' attribute is consistent with output of --output location.
* - rule attributes are represented in the DOM structure.
* @throws InterruptedException
*/
private Element createTargetElement(Document doc, Target target) throws InterruptedException {
Element elem;
if (target instanceof Rule) {
Rule rule = (Rule) target;
elem = doc.createElement("rule");
elem.setAttribute("class", rule.getRuleClass());
for (Attribute attr : rule.getAttributes()) {
PossibleAttributeValues values = getPossibleAttributeValues(rule, attr);
if (values.source == AttributeValueSource.RULE || options.xmlShowDefaultValues) {
Element attrElem = createValueElement(doc, attr.getType(), values);
attrElem.setAttribute("name", attr.getName());
elem.appendChild(attrElem);
}
}
// host-configuration outputs, and default values.
for (Label label : rule.getLabels(dependencyFilter)) {
Element inputElem = doc.createElement("rule-input");
inputElem.setAttribute("name", label.toString());
elem.appendChild(inputElem);
}
for (Label label : aspectResolver.computeAspectDependencies(target, dependencyFilter).values()) {
Element inputElem = doc.createElement("rule-input");
inputElem.setAttribute("name", label.toString());
elem.appendChild(inputElem);
}
for (OutputFile outputFile : rule.getOutputFiles()) {
Element outputElem = doc.createElement("rule-output");
outputElem.setAttribute("name", outputFile.getLabel().toString());
elem.appendChild(outputElem);
}
for (String feature : rule.getFeatures()) {
Element outputElem = doc.createElement("rule-default-setting");
outputElem.setAttribute("name", feature);
elem.appendChild(outputElem);
}
} else if (target instanceof PackageGroup) {
PackageGroup packageGroup = (PackageGroup) target;
elem = doc.createElement("package-group");
elem.setAttribute("name", packageGroup.getName());
Element includes = createValueElement(doc, BuildType.LABEL_LIST, packageGroup.getIncludes());
includes.setAttribute("name", "includes");
elem.appendChild(includes);
Element packages = createValueElement(doc, Type.STRING_LIST, packageGroup.getContainedPackages());
packages.setAttribute("name", "packages");
elem.appendChild(packages);
} else if (target instanceof OutputFile) {
OutputFile outputFile = (OutputFile) target;
elem = doc.createElement("generated-file");
elem.setAttribute("generating-rule", outputFile.getGeneratingRule().getLabel().toString());
} else if (target instanceof InputFile) {
elem = doc.createElement("source-file");
InputFile inputFile = (InputFile) target;
if (inputFile.getName().equals("BUILD")) {
addSubincludedFilesToElement(doc, elem, inputFile);
addSkylarkFilesToElement(doc, elem, inputFile);
addFeaturesToElement(doc, elem, inputFile);
elem.setAttribute("package_contains_errors", String.valueOf(inputFile.getPackage().containsErrors()));
}
addPackageGroupsToElement(doc, elem, inputFile);
} else if (target instanceof EnvironmentGroup) {
EnvironmentGroup envGroup = (EnvironmentGroup) target;
elem = doc.createElement("environment-group");
elem.setAttribute("name", envGroup.getName());
Element environments = createValueElement(doc, BuildType.LABEL_LIST, envGroup.getEnvironments());
environments.setAttribute("name", "environments");
elem.appendChild(environments);
Element defaults = createValueElement(doc, BuildType.LABEL_LIST, envGroup.getDefaults());
defaults.setAttribute("name", "defaults");
elem.appendChild(defaults);
} else if (target instanceof FakeSubincludeTarget) {
elem = doc.createElement("source-file");
} else {
throw new IllegalArgumentException(target.toString());
}
elem.setAttribute("name", target.getLabel().toString());
String location = getLocation(target, options.relativeLocations);
if (!options.xmlLineNumbers) {
int firstColon = location.indexOf(':');
if (firstColon != -1) {
location = location.substring(0, firstColon);
}
}
elem.setAttribute("location", location);
return elem;
}
use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.
the class BlazeQueryEnvironment method getTargetsMatchingPatternImpl.
private void getTargetsMatchingPatternImpl(String pattern, Callback<Target> callback) throws QueryException, InterruptedException {
// We can safely ignore the boolean error flag. The evaluateQuery() method above wraps the
// entire query computation in an error sensor.
Set<Target> targets = new LinkedHashSet<>(resolvedTargetPatterns.get(pattern));
// Sets.filter would be more convenient here, but can't deal with exceptions.
Iterator<Target> targetIterator = targets.iterator();
while (targetIterator.hasNext()) {
Target target = targetIterator.next();
if (!validateScope(target.getLabel(), strictScope)) {
targetIterator.remove();
}
}
Set<PathFragment> packages = new HashSet<>();
for (Target target : targets) {
packages.add(target.getLabel().getPackageFragment());
}
Set<Target> result = new LinkedHashSet<>();
for (Target target : targets) {
result.add(getOrCreate(target));
// targets in this set.
if (target instanceof OutputFile) {
OutputFile outputFile = (OutputFile) target;
if (targets.contains(outputFile.getGeneratingRule())) {
makeEdge(outputFile, outputFile.getGeneratingRule());
}
} else if (target instanceof Rule) {
Rule rule = (Rule) target;
for (Label label : rule.getLabels(dependencyFilter)) {
if (!packages.contains(label.getPackageFragment())) {
// don't cause additional package loading
continue;
}
try {
if (!validateScope(label, strictScope)) {
// Don't create edges to targets which are out of scope.
continue;
}
Target to = getTargetOrThrow(label);
if (targets.contains(to)) {
makeEdge(rule, to);
}
} catch (NoSuchThingException e) {
/* ignore */
}
}
}
}
callback.process(result);
}
Aggregations