Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.pkgcache; |
| 15 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | import com.google.common.collect.ImmutableList; |
| 17 | import com.google.common.collect.ImmutableSet; |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Sets; |
| 20 | import com.google.devtools.build.lib.concurrent.ThreadSafety; |
| 21 | import com.google.devtools.build.lib.events.EventHandler; |
| 22 | import com.google.devtools.build.lib.packages.AttributeMap; |
| 23 | import com.google.devtools.build.lib.packages.FileTarget; |
| 24 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 25 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
| 26 | import com.google.devtools.build.lib.packages.RawAttributeMapper; |
| 27 | import com.google.devtools.build.lib.packages.Rule; |
| 28 | import com.google.devtools.build.lib.packages.Target; |
| 29 | import com.google.devtools.build.lib.packages.Type; |
| 30 | import com.google.devtools.build.lib.syntax.Label; |
| 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | import java.util.Set; |
| 35 | |
| 36 | /** |
| 37 | * A helper class for getting source and header files from a given {@link Rule}. |
| 38 | */ |
| 39 | public final class SrcTargetUtil { |
| 40 | private SrcTargetUtil() { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Given a Rule, returns an immutable list of FileTarget for its sources, in the order they appear |
| 45 | * in its "srcs", "src" or "srcjar" attribute, and any filegroups or other rules it references. |
| 46 | * An empty list is returned if no "srcs" or "src" attribute exists for this rule. The list may |
| 47 | * contain OutputFiles if the sources were generated by another rule. |
| 48 | * |
| 49 | * <p>This method should be considered only a heuristic, and should not be used during the |
| 50 | * analysis phase. |
| 51 | * |
| 52 | * <p>(We could remove the throws clauses if we restrict the results to srcs within the same |
| 53 | * package.) |
| 54 | * |
| 55 | * @throws NoSuchTargetException or NoSuchPackageException when a source label cannot be resolved |
| 56 | * to a Target |
| 57 | */ |
| 58 | @ThreadSafety.ThreadSafe |
| 59 | public static List<FileTarget> getSrcTargets(EventHandler eventHandler, Rule rule, |
| 60 | TargetProvider provider) |
| 61 | throws NoSuchTargetException, NoSuchPackageException, InterruptedException { |
| 62 | return getTargets(eventHandler, rule, SOURCE_ATTRIBUTES, Sets.newHashSet(rule), provider); |
| 63 | } |
| 64 | |
| 65 | // Attributes referring to "sources". |
| 66 | private static final ImmutableSet<String> SOURCE_ATTRIBUTES = |
| 67 | ImmutableSet.of("srcs", "src", "srcjar"); |
| 68 | |
Googler | 1c87a36 | 2015-06-19 13:59:49 +0000 | [diff] [blame] | 69 | // Attribute referring to "headers". |
| 70 | private static final String HEADER_ATTRIBUTE = "hdrs"; |
| 71 | |
| 72 | // Attribute referring to "textual headers". |
| 73 | private static final String TEXTUAL_HEADER_ATTRIBUTE = "textual_hdrs"; |
| 74 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | // The attribute to search in filegroups. |
| 76 | private static final ImmutableSet<String> FILEGROUP_ATTRIBUTES = |
| 77 | ImmutableSet.of("srcs"); |
| 78 | |
| 79 | /** |
| 80 | * Same as {@link #getSrcTargets}, but for both source and headers (i.e. also traversing |
| 81 | * the "hdrs" attribute). |
| 82 | */ |
| 83 | @ThreadSafety.ThreadSafe |
| 84 | public static List<FileTarget> getSrcAndHdrTargets(EventHandler eventHandler, Rule rule, |
| 85 | TargetProvider provider) |
| 86 | throws NoSuchTargetException, NoSuchPackageException, InterruptedException { |
| 87 | ImmutableSet<String> srcAndHdrAttributes = ImmutableSet.<String>builder() |
| 88 | .addAll(SOURCE_ATTRIBUTES) |
Googler | 1c87a36 | 2015-06-19 13:59:49 +0000 | [diff] [blame] | 89 | .add(HEADER_ATTRIBUTE) |
| 90 | .add(TEXTUAL_HEADER_ATTRIBUTE) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 91 | .build(); |
| 92 | return getTargets(eventHandler, rule, srcAndHdrAttributes, Sets.newHashSet(rule), provider); |
| 93 | } |
| 94 | |
| 95 | @ThreadSafety.ThreadSafe |
| 96 | public static List<FileTarget> getHdrTargets(EventHandler eventHandler, Rule rule, |
| 97 | TargetProvider provider) |
| 98 | throws NoSuchTargetException, NoSuchPackageException, InterruptedException { |
Googler | 1c87a36 | 2015-06-19 13:59:49 +0000 | [diff] [blame] | 99 | return getTargets( |
| 100 | eventHandler, rule, ImmutableSet.of(HEADER_ATTRIBUTE), Sets.newHashSet(rule), provider); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | } |
Googler | 1c87a36 | 2015-06-19 13:59:49 +0000 | [diff] [blame] | 102 | |
| 103 | @ThreadSafety.ThreadSafe |
| 104 | public static List<FileTarget> getTextualHdrTargets( |
| 105 | EventHandler eventHandler, Rule rule, TargetProvider provider) |
| 106 | throws NoSuchTargetException, NoSuchPackageException, InterruptedException { |
| 107 | return getTargets(eventHandler, rule, ImmutableSet.of(TEXTUAL_HEADER_ATTRIBUTE), |
| 108 | Sets.newHashSet(rule), provider); |
| 109 | } |
| 110 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 111 | /** |
| 112 | * @see #getSrcTargets(EventHandler, Rule, TargetProvider) |
| 113 | */ |
| 114 | private static List<FileTarget> getTargets(EventHandler eventHandler, |
| 115 | Rule rule, |
| 116 | ImmutableSet<String> attributes, |
| 117 | Set<Rule> visitedRules, |
| 118 | TargetProvider targetProvider) |
| 119 | throws NoSuchTargetException, NoSuchPackageException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 120 | List<Label> srcLabels = Lists.newArrayList(); |
| 121 | AttributeMap attributeMap = RawAttributeMapper.of(rule); |
| 122 | for (String attrName : attributes) { |
Greg Estren | 0ef0634 | 2015-04-22 18:39:33 +0000 | [diff] [blame] | 123 | if (rule.isConfigurableAttribute(attrName)) { |
| 124 | // We don't know which path to follow for configurable attributes. So skip them. |
| 125 | continue; |
| 126 | } else if (rule.isAttrDefined(attrName, Type.LABEL_LIST)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 127 | srcLabels.addAll(attributeMap.get(attrName, Type.LABEL_LIST)); |
| 128 | } else if (rule.isAttrDefined(attrName, Type.LABEL)) { |
| 129 | Label srcLabel = attributeMap.get(attrName, Type.LABEL); |
| 130 | if (srcLabel != null) { |
| 131 | srcLabels.add(srcLabel); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | if (srcLabels.isEmpty()) { |
| 136 | return ImmutableList.of(); |
| 137 | } |
| 138 | List<FileTarget> srcTargets = new ArrayList<>(); |
| 139 | for (Label label : srcLabels) { |
| 140 | Target target = targetProvider.getTarget(eventHandler, label); |
| 141 | if (target instanceof FileTarget) { |
| 142 | srcTargets.add((FileTarget) target); |
| 143 | } else { |
| 144 | Rule srcRule = target.getAssociatedRule(); |
| 145 | if (srcRule != null && !visitedRules.contains(srcRule)) { |
| 146 | visitedRules.add(srcRule); |
| 147 | if ("filegroup".equals(srcRule.getRuleClass())) { |
| 148 | srcTargets.addAll(getTargets(eventHandler, srcRule, FILEGROUP_ATTRIBUTES, visitedRules, |
| 149 | targetProvider)); |
| 150 | } else { |
| 151 | srcTargets.addAll(srcRule.getOutputFiles()); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | return ImmutableList.copyOf(srcTargets); |
| 157 | } |
| 158 | } |