aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/packages/DependencyFilter.java
blob: f3d497164746833ea81e9df8ec6cce71b6f82d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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.DependencyFilter.AttributeInfoProvider;
import com.google.devtools.build.lib.syntax.Type.LabelClass;
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<AttributeInfoProvider, Attribute> {

  /** Dependency predicate that includes all dependencies */
  public static final DependencyFilter ALL_DEPS =
      new DependencyFilter() {
        @Override
        public boolean apply(AttributeInfoProvider x, Attribute y) {
          return true;
        }
      };
  /** Dependency predicate that excludes host dependencies */
  public static final DependencyFilter NO_HOST_DEPS =
      new DependencyFilter() {
    @Override
    public boolean apply(AttributeInfoProvider infoProvider, Attribute attribute) {
      // getConfigurationTransition() is only defined for labels which introduce a dependency.
      if (attribute.getType().getLabelClass() != LabelClass.DEPENDENCY) {
        return true;
      }

      return !attribute.getConfigurationTransition().isHostTransition();
    }
  };
  /** Dependency predicate that excludes implicit dependencies */
  public static final DependencyFilter NO_IMPLICIT_DEPS =
      new DependencyFilter() {
    @Override
    public boolean apply(AttributeInfoProvider infoProvider, Attribute attribute) {
      return infoProvider.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(AttributeInfoProvider infoProvider, Attribute attribute) {
      return attribute.getType().getLabelClass() != LabelClass.NONDEP_REFERENCE;
    }
  };
  /**
   * Checks to see if the attribute has the isDirectCompileTimeInput property.
   */
  public static final DependencyFilter DIRECT_COMPILE_TIME_INPUT =
      new DependencyFilter() {
    @Override
    public boolean apply(AttributeInfoProvider infoProvider, Attribute attribute) {
      return attribute.isDirectCompileTimeInput();
    }
  };

  /**
   * Returns true if a given attribute should be processed.
   */
  @Override
  public abstract boolean apply(AttributeInfoProvider infoProvider, 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(AttributeInfoProvider infoProvider, Attribute attribute) {
        return a.apply(infoProvider, attribute) && b.apply(infoProvider, attribute);
      }
    };
  }

  /**
   * Interface to provide information about attributes to dependency filters.
   */
  public interface AttributeInfoProvider {
    /**
     * Returns true iff the value of the specified attribute is explicitly set in
     * the BUILD file (as opposed to its default value). This also returns true if
     * the value from the BUILD file is the same as the default value.
     */
    boolean isAttributeValueExplicitlySpecified(Attribute attribute);
  }
}