Refactor dependency filtering out of Rule class.
--
MOS_MIGRATED_REVID=112717648
diff --git a/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java b/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java
index 3e7bf76..43704f2 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java
@@ -24,7 +24,6 @@
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy.MissingFragmentPolicy;
import com.google.devtools.build.lib.packages.NativeAspectClass.NativeAspectFactory;
-import com.google.devtools.build.lib.util.BinaryPredicate;
import com.google.devtools.build.lib.util.Preconditions;
import java.util.Collection;
@@ -168,7 +167,8 @@
candidateClass.getDefinition().getRequiredProviderNames())) {
continue;
}
- addAllAttributesOfAspect(from, result, candidateClass.getDefinition(), Rule.ALL_DEPS);
+ addAllAttributesOfAspect(
+ from, result, candidateClass.getDefinition(), DependencyFilter.ALL_DEPS);
}
return ImmutableMultimap.copyOf(result);
}
@@ -184,9 +184,11 @@
/**
* Collects all attribute labels from the specified aspectDefinition.
*/
- public static void addAllAttributesOfAspect(Rule from,
- Multimap<Attribute, Label> labelBuilder, AspectDefinition aspectDefinition,
- BinaryPredicate<Rule, Attribute> predicate) {
+ public static void addAllAttributesOfAspect(
+ Rule from,
+ Multimap<Attribute, Label> labelBuilder,
+ AspectDefinition aspectDefinition,
+ DependencyFilter predicate) {
ImmutableMap<String, Attribute> attributes = aspectDefinition.getAttributes();
for (Attribute aspectAttribute : attributes.values()) {
if (!predicate.apply(from, aspectAttribute)) {
diff --git a/src/main/java/com/google/devtools/build/lib/packages/DependencyFilter.java b/src/main/java/com/google/devtools/build/lib/packages/DependencyFilter.java
new file mode 100644
index 0000000..6d1b943d
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/packages/DependencyFilter.java
@@ -0,0 +1,96 @@
+// Copyright 2016 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.google.devtools.build.lib.packages;
+
+import com.google.devtools.build.lib.packages.Attribute.ConfigurationTransition;
+import com.google.devtools.build.lib.util.BinaryPredicate;
+
+/**
+ * A predicate that returns true if an dependency attribute should be included in the result of
+ * <code>blaze query</code>.
+ * Used to implement <code>--[no]implicit_deps</code>, <code>--[no]host_deps</code> etc.
+ */
+public abstract class DependencyFilter implements BinaryPredicate<Rule, Attribute> {
+
+ /** Dependency predicate that includes all dependencies */
+ public static final DependencyFilter ALL_DEPS =
+ new DependencyFilter() {
+ @Override
+ public boolean apply(Rule x, Attribute y) {
+ return true;
+ }
+ };
+ /** Dependency predicate that excludes host dependencies */
+ public static final DependencyFilter NO_HOST_DEPS =
+ new DependencyFilter() {
+ @Override
+ public boolean apply(Rule rule, Attribute attribute) {
+ // isHostConfiguration() is only defined for labels and label lists.
+ if (attribute.getType() != BuildType.LABEL && attribute.getType() != BuildType.LABEL_LIST) {
+ return true;
+ }
+
+ return attribute.getConfigurationTransition() != ConfigurationTransition.HOST;
+ }
+ };
+ /** Dependency predicate that excludes implicit dependencies */
+ public static final DependencyFilter NO_IMPLICIT_DEPS =
+ new DependencyFilter() {
+ @Override
+ public boolean apply(Rule rule, Attribute attribute) {
+ return rule.isAttributeValueExplicitlySpecified(attribute);
+ }
+ };
+ /**
+ * Dependency predicate that excludes those edges that are not present in
+ * the loading phase target dependency graph.
+ */
+ public static final DependencyFilter NO_NODEP_ATTRIBUTES =
+ new DependencyFilter() {
+ @Override
+ public boolean apply(Rule rule, Attribute attribute) {
+ return attribute.getType() != BuildType.NODEP_LABEL
+ && attribute.getType() != BuildType.NODEP_LABEL_LIST;
+ }
+ };
+ /**
+ * Checks to see if the attribute has the isDirectCompileTimeInput property.
+ */
+ public static final DependencyFilter DIRECT_COMPILE_TIME_INPUT =
+ new DependencyFilter() {
+ @Override
+ public boolean apply(Rule rule, Attribute attribute) {
+ return attribute.isDirectCompileTimeInput();
+ }
+ };
+
+ /**
+ * Returns true if a given attribute should be processed.
+ */
+ @Override
+ public abstract boolean apply(Rule rule, Attribute attribute);
+
+ /**
+ * Returns a predicate that computes the logical and of the two given predicates.
+ */
+ public static DependencyFilter and(
+ final DependencyFilter a, final DependencyFilter b) {
+ return new DependencyFilter() {
+ @Override
+ public boolean apply(Rule rule, Attribute attribute) {
+ return a.apply(rule, attribute) && b.apply(rule, attribute);
+ }
+ };
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/packages/Rule.java b/src/main/java/com/google/devtools/build/lib/packages/Rule.java
index ff16662..c18d763 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/Rule.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/Rule.java
@@ -31,12 +31,10 @@
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.events.Location;
-import com.google.devtools.build.lib.packages.Attribute.ConfigurationTransition;
import com.google.devtools.build.lib.packages.License.DistributionType;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.GlobList;
import com.google.devtools.build.lib.syntax.Type;
-import com.google.devtools.build.lib.util.BinaryPredicate;
import com.google.devtools.build.lib.util.Preconditions;
import java.util.Collection;
@@ -61,78 +59,10 @@
* </pre>
*/
public final class Rule implements Target {
- /** Dependency predicate that includes all dependencies */
- public static final BinaryPredicate<Rule, Attribute> ALL_DEPS =
- new BinaryPredicate<Rule, Attribute>() {
- @Override
- public boolean apply(Rule x, Attribute y) {
- return true;
- }
- };
-
- /** Dependency predicate that excludes host dependencies */
- public static final BinaryPredicate<Rule, Attribute> NO_HOST_DEPS =
- new BinaryPredicate<Rule, Attribute>() {
- @Override
- public boolean apply(Rule rule, Attribute attribute) {
- // isHostConfiguration() is only defined for labels and label lists.
- if (attribute.getType() != BuildType.LABEL && attribute.getType() != BuildType.LABEL_LIST) {
- return true;
- }
-
- return attribute.getConfigurationTransition() != ConfigurationTransition.HOST;
- }
- };
-
- /** Dependency predicate that excludes implicit dependencies */
- public static final BinaryPredicate<Rule, Attribute> NO_IMPLICIT_DEPS =
- new BinaryPredicate<Rule, Attribute>() {
- @Override
- public boolean apply(Rule rule, Attribute attribute) {
- return rule.isAttributeValueExplicitlySpecified(attribute);
- }
- };
-
- /**
- * Dependency predicate that excludes those edges that are not present in the
- * configured target graph.
- */
- public static final BinaryPredicate<Rule, Attribute> NO_NODEP_ATTRIBUTES =
- new BinaryPredicate<Rule, Attribute>() {
- @Override
- public boolean apply(Rule rule, Attribute attribute) {
- return attribute.getType() != BuildType.NODEP_LABEL
- && attribute.getType() != BuildType.NODEP_LABEL_LIST;
- }
- };
/** Label predicate that allows every label. */
public static final Predicate<Label> ALL_LABELS = Predicates.alwaysTrue();
- /**
- * Checks to see if the attribute has the isDirectCompileTimeInput property.
- */
- public static final BinaryPredicate<Rule, Attribute> DIRECT_COMPILE_TIME_INPUT =
- new BinaryPredicate<Rule, Attribute>() {
- @Override
- public boolean apply(Rule rule, Attribute attribute) {
- return attribute.isDirectCompileTimeInput();
- }
- };
-
- /**
- * Returns a predicate that computes the logical and of the two given predicates.
- */
- public static <X, Y> BinaryPredicate<X, Y> and(
- final BinaryPredicate<X, Y> a, final BinaryPredicate<X, Y> b) {
- return new BinaryPredicate<X, Y>() {
- @Override
- public boolean apply(X x, Y y) {
- return a.apply(x, y) && b.apply(x, y);
- }
- };
- }
-
private final Label label;
private final Package pkg;
@@ -457,7 +387,7 @@
* Returns a new List instance containing all direct dependencies (all types).
*/
public Collection<Label> getLabels() {
- return getLabels(Rule.ALL_DEPS);
+ return getLabels(DependencyFilter.ALL_DEPS);
}
/**
@@ -469,7 +399,7 @@
* the attribute that contains the label. The label will be contained in the
* result iff (the predicate returned {@code true} and the labels are not outputs)
*/
- public Collection<Label> getLabels(final BinaryPredicate<Rule, Attribute> predicate) {
+ public Collection<Label> getLabels(DependencyFilter predicate) {
return ImmutableSortedSet.copyOf(getTransitions(predicate).values());
}
@@ -482,8 +412,7 @@
* the attribute that contains the label. The label will be contained in the
* result iff (the predicate returned {@code true} and the labels are not outputs)
*/
- public Multimap<Attribute, Label> getTransitions(
- final BinaryPredicate<Rule, Attribute> predicate) {
+ public Multimap<Attribute, Label> getTransitions(final DependencyFilter predicate) {
final Multimap<Attribute, Label> transitions = HashMultimap.create();
// TODO(bazel-team): move this to AttributeMap, too. Just like visitLabels, which labels should
// be visited may depend on the calling context. We shouldn't implicitly decide this for
@@ -733,8 +662,7 @@
* Computes labels of additional dependencies that can be provided by aspects that this rule
* can require from its direct dependencies.
*/
- public Collection<? extends Label> getAspectLabelsSuperset(
- BinaryPredicate<Rule, Attribute> predicate) {
+ public Collection<? extends Label> getAspectLabelsSuperset(DependencyFilter predicate) {
LinkedHashMultimap<Attribute, Label> labels = LinkedHashMultimap.create();
for (Attribute attribute : this.getAttributes()) {
for (Aspect candidateClass : attribute.getAspects(this)) {