use of com.google.devtools.build.lib.packages.OutputFile 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.OutputFile 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.OutputFile 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);
}
use of com.google.devtools.build.lib.packages.OutputFile in project bazel by bazelbuild.
the class LocationExpander method buildLocationMap.
/**
* Extracts all possible target locations from target specification.
*
* @param ruleContext BUILD target object
* @param labelMap map of labels to build artifacts
* @return map of all possible target locations
*/
private static Map<Label, Collection<Artifact>> buildLocationMap(RuleContext ruleContext, Map<Label, ? extends Collection<Artifact>> labelMap, boolean allowDataAttributeEntriesInLabel) {
Map<Label, Collection<Artifact>> locationMap = Maps.newHashMap();
if (labelMap != null) {
for (Map.Entry<Label, ? extends Collection<Artifact>> entry : labelMap.entrySet()) {
mapGet(locationMap, entry.getKey()).addAll(entry.getValue());
}
}
// Add all destination locations.
for (OutputFile out : ruleContext.getRule().getOutputFiles()) {
mapGet(locationMap, out.getLabel()).add(ruleContext.createOutputArtifact(out));
}
if (ruleContext.getRule().isAttrDefined("srcs", BuildType.LABEL_LIST)) {
for (TransitiveInfoCollection src : ruleContext.getPrerequisitesIf("srcs", Mode.TARGET, FileProvider.class)) {
Iterables.addAll(mapGet(locationMap, AliasProvider.getDependencyLabel(src)), src.getProvider(FileProvider.class).getFilesToBuild());
}
}
// Add all locations associated with dependencies and tools
List<TransitiveInfoCollection> depsDataAndTools = new ArrayList<>();
if (ruleContext.getRule().isAttrDefined("deps", BuildType.LABEL_LIST)) {
Iterables.addAll(depsDataAndTools, ruleContext.getPrerequisitesIf("deps", Mode.DONT_CHECK, FilesToRunProvider.class));
}
if (allowDataAttributeEntriesInLabel && ruleContext.getRule().isAttrDefined("data", BuildType.LABEL_LIST)) {
Iterables.addAll(depsDataAndTools, ruleContext.getPrerequisitesIf("data", Mode.DATA, FilesToRunProvider.class));
}
if (ruleContext.getRule().isAttrDefined("tools", BuildType.LABEL_LIST)) {
Iterables.addAll(depsDataAndTools, ruleContext.getPrerequisitesIf("tools", Mode.HOST, FilesToRunProvider.class));
}
for (TransitiveInfoCollection dep : depsDataAndTools) {
Label label = AliasProvider.getDependencyLabel(dep);
FilesToRunProvider filesToRun = dep.getProvider(FilesToRunProvider.class);
Artifact executableArtifact = filesToRun.getExecutable();
// If the label has an executable artifact add that to the multimaps.
if (executableArtifact != null) {
mapGet(locationMap, label).add(executableArtifact);
} else {
mapGet(locationMap, label).addAll(filesToRun.getFilesToRun());
}
}
return locationMap;
}
use of com.google.devtools.build.lib.packages.OutputFile in project bazel by bazelbuild.
the class TransitiveBaseTraversalFunction method getLabelDeps.
// TODO(bazel-team): Unify this logic with that in LabelVisitor, and possibly DependencyResolver.
private static Iterable<Label> getLabelDeps(Target target) throws InterruptedException {
final Set<Label> labels = new HashSet<>();
if (target instanceof OutputFile) {
Rule rule = ((OutputFile) target).getGeneratingRule();
labels.add(rule.getLabel());
visitTargetVisibility(target, labels);
} else if (target instanceof InputFile) {
visitTargetVisibility(target, labels);
} else if (target instanceof Rule) {
visitTargetVisibility(target, labels);
visitRule(target, labels);
} else if (target instanceof PackageGroup) {
visitPackageGroup((PackageGroup) target, labels);
}
return labels;
}
Aggregations