aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/pkgcache/SrcTargetUtil.java
blob: 4fd5969a19022060215a1dace5bceb93f99c81eb (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2014 Google Inc. 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.pkgcache;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.packages.AttributeMap;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.FileTarget;
import com.google.devtools.build.lib.packages.NoSuchPackageException;
import com.google.devtools.build.lib.packages.NoSuchTargetException;
import com.google.devtools.build.lib.packages.RawAttributeMapper;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.Target;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * A helper class for getting source and header files from a given {@link Rule}.
 */
public final class SrcTargetUtil {
  private SrcTargetUtil() {
  }

  /**
   * Given a Rule, returns an immutable list of FileTarget for its sources, in the order they appear
   * in its "srcs", "src" or "srcjar" attribute, and any filegroups or other rules it references.
   * An empty list is returned if no "srcs" or "src" attribute exists for this rule. The list may
   * contain OutputFiles if the sources were generated by another rule.
   *
   * <p>This method should be considered only a heuristic, and should not be used during the
   * analysis phase.
   *
   * <p>(We could remove the throws clauses if we restrict the results to srcs within the same
   * package.)
   *
   * @throws NoSuchTargetException or NoSuchPackageException when a source label cannot be resolved
   *         to a Target
   */
  @ThreadSafety.ThreadSafe
  public static List<FileTarget> getSrcTargets(EventHandler eventHandler, Rule rule,
                                               TargetProvider provider)
      throws NoSuchTargetException, NoSuchPackageException, InterruptedException  {
    return getTargets(eventHandler, rule, SOURCE_ATTRIBUTES, Sets.newHashSet(rule.getLabel()),
        provider);
  }

  // Attributes referring to "sources".
  private static final ImmutableSet<String> SOURCE_ATTRIBUTES =
      ImmutableSet.of("srcs", "src", "srcjar");

  // Attribute referring to "headers".
  private static final String HEADER_ATTRIBUTE = "hdrs";

  // Attribute referring to "textual headers".
  private static final String TEXTUAL_HEADER_ATTRIBUTE = "textual_hdrs";

  // The attribute to search in filegroups.
  private static final ImmutableSet<String> FILEGROUP_ATTRIBUTES =
      ImmutableSet.of("srcs");

  /**
   * Same as {@link #getSrcTargets}, but for both source and headers (i.e. also traversing
   * the "hdrs" attribute).
   */
  @ThreadSafety.ThreadSafe
  public static List<FileTarget> getSrcAndHdrTargets(EventHandler eventHandler, Rule rule,
                                                     TargetProvider provider)
      throws NoSuchTargetException, NoSuchPackageException, InterruptedException  {
    ImmutableSet<String> srcAndHdrAttributes = ImmutableSet.<String>builder()
        .addAll(SOURCE_ATTRIBUTES)
        .add(HEADER_ATTRIBUTE)
        .add(TEXTUAL_HEADER_ATTRIBUTE)
        .build();
    return getTargets(eventHandler, rule, srcAndHdrAttributes, Sets.newHashSet(rule.getLabel()),
        provider);
  }

  @ThreadSafety.ThreadSafe
  public static List<FileTarget> getHdrTargets(EventHandler eventHandler, Rule rule,
                                                     TargetProvider provider)
      throws NoSuchTargetException, NoSuchPackageException, InterruptedException  {
    return getTargets(
        eventHandler, rule, ImmutableSet.of(HEADER_ATTRIBUTE), Sets.newHashSet(rule.getLabel()),
        provider);
  }

  @ThreadSafety.ThreadSafe
  public static List<FileTarget> getTextualHdrTargets(
      EventHandler eventHandler, Rule rule, TargetProvider provider)
      throws NoSuchTargetException, NoSuchPackageException, InterruptedException {
    return getTargets(eventHandler, rule, ImmutableSet.of(TEXTUAL_HEADER_ATTRIBUTE),
        Sets.newHashSet(rule.getLabel()), provider);
  }

  /**
   * @see #getSrcTargets(EventHandler, Rule, TargetProvider)
   */
  private static List<FileTarget> getTargets(EventHandler eventHandler,
      Rule rule,
      ImmutableSet<String> attributes,
      Set<Label> visitedRuleLabels,
      TargetProvider targetProvider)
      throws NoSuchTargetException, NoSuchPackageException, InterruptedException {
    List<Label> srcLabels = Lists.newArrayList();
    AttributeMap attributeMap = RawAttributeMapper.of(rule);
    for (String attrName : attributes) {
      if (rule.isConfigurableAttribute(attrName)) {
        // We don't know which path to follow for configurable attributes. So skip them.
        continue;
      } else if (rule.isAttrDefined(attrName, BuildType.LABEL_LIST)) {
        srcLabels.addAll(attributeMap.get(attrName, BuildType.LABEL_LIST));
      } else if (rule.isAttrDefined(attrName, BuildType.LABEL)) {
        Label srcLabel = attributeMap.get(attrName, BuildType.LABEL);
        if (srcLabel != null) {
          srcLabels.add(srcLabel);
        }
      }
    }
    if (srcLabels.isEmpty()) {
      return ImmutableList.of();
    }
    List<FileTarget> srcTargets = new ArrayList<>();
    for (Label label : srcLabels) {
      Target target = targetProvider.getTarget(eventHandler, label);
      if (target instanceof FileTarget) {
        srcTargets.add((FileTarget) target);
      } else {
        Rule srcRule = target.getAssociatedRule();
        if (srcRule != null && !visitedRuleLabels.contains(srcRule.getLabel())) {
          visitedRuleLabels.add(srcRule.getLabel());
          if ("filegroup".equals(srcRule.getRuleClass())) {
            srcTargets.addAll(getTargets(eventHandler, srcRule, FILEGROUP_ATTRIBUTES,
                visitedRuleLabels, targetProvider));
          } else {
            srcTargets.addAll(srcRule.getOutputFiles());
          }
        }
      }
    }
    return ImmutableList.copyOf(srcTargets);
  }
}