use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.
the class RepositoryFunctionTest method testGenerateWorkspaceFile.
@Test
public void testGenerateWorkspaceFile() throws Exception {
Rule rule = scratchRule("external", "abc", "local_repository(", " name = 'abc',", " path = '/a/b/c',", ")");
RepositoryFunction.createWorkspaceFile(rootDirectory, rule.getTargetKind(), rule.getName());
String workspaceContent = new String(FileSystemUtils.readContentAsLatin1(rootDirectory.getRelative("WORKSPACE")));
assertThat(workspaceContent).contains("workspace(name = \"abc\")");
}
use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.
the class RepositoryFunctionTest method testGetTargetPathRelative.
@Test
public void testGetTargetPathRelative() throws Exception {
Rule rule = scratchRule("external", "z", "local_repository(", " name = 'z',", " path = 'a/b/c',", ")");
assertEquals(rootDirectory.getRelative("a/b/c").asFragment(), TestingRepositoryFunction.getTargetPath(rule, rootDirectory));
}
use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.
the class CompileOneDependencyTransformer method transformCompileOneDependency.
private Target transformCompileOneDependency(ExtendedEventHandler eventHandler, Target target) throws TargetParsingException, InterruptedException {
if (!(target instanceof FileTarget)) {
throw new TargetParsingException("--compile_one_dependency target '" + target.getLabel() + "' must be a file");
}
Rule result = null;
Iterable<Rule> orderedRuleList = getOrderedRuleList(target.getPackage());
for (Rule rule : orderedRuleList) {
Set<Label> labels = getInputLabels(rule);
if (listContainsFile(eventHandler, labels, target.getLabel(), Sets.<Label>newHashSet())) {
if (rule.getRuleClassObject().isPreferredDependency(target.getName())) {
result = rule;
break;
}
if (result == null) {
result = rule;
}
}
}
if (result == null) {
throw new TargetParsingException("Couldn't find dependency on target '" + target.getLabel() + "'");
}
// If the rule has source targets, return it.
if (!RawAttributeMapper.of(result).getMergedValues("srcs", BuildType.LABEL_LIST).isEmpty()) {
return result;
}
// Try to find a rule in the same package that has 'result' as a dependency.
for (Rule rule : orderedRuleList) {
RawAttributeMapper attributes = RawAttributeMapper.of(rule);
// We don't know which path to follow for configurable attributes, so skip them.
if (attributes.isConfigurable("deps") || attributes.isConfigurable("srcs")) {
continue;
}
RuleClass ruleClass = rule.getRuleClassObject();
if (ruleClass.hasAttr("deps", BuildType.LABEL_LIST) && ruleClass.hasAttr("srcs", BuildType.LABEL_LIST)) {
for (Label dep : attributes.get("deps", BuildType.LABEL_LIST)) {
if (dep.equals(result.getLabel())) {
if (!attributes.get("srcs", BuildType.LABEL_LIST).isEmpty()) {
return rule;
}
}
}
}
}
return result;
}
use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.
the class PreciseAspectResolver method computeAspectDependencies.
@Override
public ImmutableMultimap<Attribute, Label> computeAspectDependencies(Target target, DependencyFilter dependencyFilter) throws InterruptedException {
Multimap<Attribute, Label> result = LinkedListMultimap.create();
if (target instanceof Rule) {
Multimap<Attribute, Label> transitions = ((Rule) target).getTransitions(DependencyFilter.NO_NODEP_ATTRIBUTES);
for (Entry<Attribute, Label> entry : transitions.entries()) {
Target toTarget;
try {
toTarget = packageProvider.getTarget(eventHandler, entry.getValue());
result.putAll(AspectDefinition.visitAspectsIfRequired(target, entry.getKey(), toTarget, dependencyFilter));
} catch (NoSuchThingException e) {
// Do nothing. One of target direct deps has an error. The dependency on the BUILD file
// (or one of the files included in it) will be reported in the query result of :BUILD.
}
}
}
return ImmutableMultimap.copyOf(result);
}
use of com.google.devtools.build.lib.packages.Rule 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