blob: 6f867520a578e54180e6e69ffd5c235a713ee95a [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
15package com.google.devtools.build.lib.analysis;
16
gregce731900f2018-08-01 13:12:39 -070017import com.google.common.annotations.VisibleForTesting;
juliexxia8ee423d2017-10-18 16:36:57 -040018import com.google.common.collect.ArrayListMultimap;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Iterables;
juliexxia8ee423d2017-10-18 16:36:57 -040021import com.google.common.collect.Multimap;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
juliexxia8ee423d2017-10-18 16:36:57 -040023import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection;
24import com.google.devtools.build.lib.analysis.config.ConfigurationResolver;
juliexxiab75e45302019-05-24 07:48:39 -070025import com.google.devtools.build.lib.analysis.config.ConfigurationResolver.TopLevelTargetsAndConfigsResult;
schmitt3e770242019-04-22 14:12:24 -070026import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
juliexxia8ee423d2017-10-18 16:36:57 -040027import com.google.devtools.build.lib.analysis.config.TransitionResolver;
lberkia1f2ddb2019-03-04 23:43:25 -080028import com.google.devtools.build.lib.analysis.config.transitions.ConfigurationTransition;
29import com.google.devtools.build.lib.analysis.config.transitions.NoTransition;
Lukacs Berki6e91eb92015-09-21 09:12:37 +000030import com.google.devtools.build.lib.cmdline.Label;
juliexxia8ee423d2017-10-18 16:36:57 -040031import com.google.devtools.build.lib.events.ExtendedEventHandler;
Lukacs Berkiffa73ad2015-09-18 11:40:12 +000032import com.google.devtools.build.lib.packages.BuildType;
cparsons28cc8332018-05-22 14:00:22 -070033import com.google.devtools.build.lib.packages.BuiltinProvider;
cparsons4ebf6c02018-08-17 14:49:36 -070034import com.google.devtools.build.lib.packages.InfoInterface;
dslomovde965ac2017-07-31 21:07:51 +020035import com.google.devtools.build.lib.packages.NativeProvider;
juliexxia8ee423d2017-10-18 16:36:57 -040036import com.google.devtools.build.lib.packages.Target;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010037import com.google.devtools.build.lib.packages.TriState;
juliexxia8ee423d2017-10-18 16:36:57 -040038import com.google.devtools.build.lib.skyframe.SkyframeExecutor;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039import com.google.devtools.build.lib.vfs.PathFragment;
juliexxia8ee423d2017-10-18 16:36:57 -040040import java.util.Collection;
41import java.util.LinkedHashSet;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010042
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043/**
44 * Utility functions for use during analysis.
45 */
46public final class AnalysisUtils {
47
48 private AnalysisUtils() {
49 throw new IllegalStateException(); // utility class
50 }
51
52 /**
53 * Returns whether link stamping is enabled for a rule.
54 *
jcater7f4f1202019-12-02 13:18:15 -080055 * <p>This returns false for unstampable rule classes and for rules used to build tools. Otherwise
56 * it returns the value of the stamp attribute, or of the stamp option if the attribute value is
57 * -1.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010058 */
Googler38ad0bf2016-07-01 05:00:14 +000059 public static boolean isStampingEnabled(RuleContext ruleContext, BuildConfiguration config) {
jcater7f4f1202019-12-02 13:18:15 -080060 if (config.isToolConfiguration()
Lukacs Berkiffa73ad2015-09-18 11:40:12 +000061 || !ruleContext.attributes().has("stamp", BuildType.TRISTATE)) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010062 return false;
63 }
Lukacs Berkiffa73ad2015-09-18 11:40:12 +000064 TriState stamp = ruleContext.attributes().get("stamp", BuildType.TRISTATE);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010065 return stamp == TriState.YES || (stamp == TriState.AUTO && config.stampBinaries());
66 }
67
Googler38ad0bf2016-07-01 05:00:14 +000068 public static boolean isStampingEnabled(RuleContext ruleContext) {
69 return isStampingEnabled(ruleContext, ruleContext.getConfiguration());
70 }
71
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010072 // TODO(bazel-team): These need Iterable<? extends TransitiveInfoCollection> because they need to
73 // be called with Iterable<ConfiguredTarget>. Once the configured target lockdown is complete, we
74 // can eliminate the "extends" clauses.
75 /**
76 * Returns the list of providers of the specified type from a set of transitive info
77 * collections.
78 */
79 public static <C extends TransitiveInfoProvider> Iterable<C> getProviders(
80 Iterable<? extends TransitiveInfoCollection> prerequisites, Class<C> provider) {
Carmi Grushko175f9112016-05-30 22:21:39 +000081 ImmutableList.Builder<C> result = ImmutableList.builder();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010082 for (TransitiveInfoCollection prerequisite : prerequisites) {
83 C prerequisiteProvider = prerequisite.getProvider(provider);
84 if (prerequisiteProvider != null) {
85 result.add(prerequisiteProvider);
86 }
87 }
Carmi Grushko175f9112016-05-30 22:21:39 +000088 return result.build();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010089 }
90
91 /**
Sergio Campamafd931432016-12-09 21:47:35 +000092 * Returns the list of declared providers (native and Skylark) of the specified Skylark key from a
93 * set of transitive info collections.
94 */
cparsons4ebf6c02018-08-17 14:49:36 -070095 public static <T extends InfoInterface> Iterable<T> getProviders(
Sergio Campamafd931432016-12-09 21:47:35 +000096 Iterable<? extends TransitiveInfoCollection> prerequisites,
dslomovde965ac2017-07-31 21:07:51 +020097 final NativeProvider<T> skylarkKey) {
dslomovf9697342017-05-02 16:26:39 +020098 ImmutableList.Builder<T> result = ImmutableList.builder();
Sergio Campamafd931432016-12-09 21:47:35 +000099 for (TransitiveInfoCollection prerequisite : prerequisites) {
dslomov77baa4c2017-07-10 17:15:27 +0200100 T prerequisiteProvider = prerequisite.get(skylarkKey);
Sergio Campamafd931432016-12-09 21:47:35 +0000101 if (prerequisiteProvider != null) {
dslomov77baa4c2017-07-10 17:15:27 +0200102 result.add(prerequisiteProvider);
Sergio Campamafd931432016-12-09 21:47:35 +0000103 }
104 }
105 return result.build();
106 }
107
108 /**
cparsons28cc8332018-05-22 14:00:22 -0700109 * Returns the list of declared providers (native and Skylark) of the specified Skylark key from a
110 * set of transitive info collections.
111 */
cparsons4ebf6c02018-08-17 14:49:36 -0700112 public static <T extends InfoInterface> Iterable<T> getProviders(
cparsons28cc8332018-05-22 14:00:22 -0700113 Iterable<? extends TransitiveInfoCollection> prerequisites,
114 final BuiltinProvider<T> skylarkKey) {
115 ImmutableList.Builder<T> result = ImmutableList.builder();
116 for (TransitiveInfoCollection prerequisite : prerequisites) {
117 T prerequisiteProvider = prerequisite.get(skylarkKey);
118 if (prerequisiteProvider != null) {
119 result.add(prerequisiteProvider);
120 }
121 }
122 return result.build();
123 }
124
125 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100126 * Returns the iterable of collections that have the specified provider.
127 */
128 public static <S extends TransitiveInfoCollection, C extends TransitiveInfoProvider> Iterable<S>
129 filterByProvider(Iterable<S> prerequisites, final Class<C> provider) {
dslomov73527c32017-07-27 17:35:46 +0200130 return Iterables.filter(prerequisites, target -> target.getProvider(provider) != null);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100131 }
132
dslomovde965ac2017-07-31 21:07:51 +0200133 /** Returns the iterable of collections that have the specified provider. */
cparsons4ebf6c02018-08-17 14:49:36 -0700134 public static <S extends TransitiveInfoCollection, C extends InfoInterface> Iterable<S>
135 filterByProvider(Iterable<S> prerequisites, final NativeProvider<C> provider) {
dslomov73527c32017-07-27 17:35:46 +0200136 return Iterables.filter(prerequisites, target -> target.get(provider) != null);
137 }
138
Googler7ac77232019-06-04 14:26:47 -0700139 /** Returns the iterable of collections that have the specified provider. */
140 public static <S extends TransitiveInfoCollection, C extends InfoInterface>
141 Iterable<S> filterByProvider(Iterable<S> prerequisites, final BuiltinProvider<C> provider) {
142 return Iterables.filter(prerequisites, target -> target.get(provider) != null);
143 }
dslomov73527c32017-07-27 17:35:46 +0200144
145 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100146 * Returns the path of the associated manifest file for the path of a Fileset. Works for both
147 * exec paths and root relative paths.
148 */
149 public static PathFragment getManifestPathFromFilesetPath(PathFragment filesetDir) {
150 PathFragment manifestDir = filesetDir.replaceName("_" + filesetDir.getBaseName());
151 PathFragment outputManifestFrag = manifestDir.getRelative("MANIFEST");
152 return outputManifestFrag;
153 }
154
155 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100156 * Returns a path fragment qualified by the rule name and unique fragment to
157 * disambiguate artifacts produced from the source file appearing in
158 * multiple rules.
159 *
160 * <p>For example "//pkg:target" -> "pkg/&lt;fragment&gt;/target.
161 */
162 public static PathFragment getUniqueDirectory(Label label, PathFragment fragment) {
Dmitry Lomove36a66c2017-02-17 14:48:48 +0000163 return label.getPackageIdentifier().getSourceRoot().getRelative(fragment)
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100164 .getRelative(label.getName());
165 }
166
167 /**
168 * Checks that the given provider class either refers to an interface or to a value class.
169 */
170 public static <T extends TransitiveInfoProvider> void checkProvider(Class<T> clazz) {
Googlercecca152016-06-20 22:51:10 +0000171 // Write this check in terms of getName() rather than getSimpleName(); the latter is expensive.
172 if (!clazz.isInterface() && clazz.getName().contains(".AutoValue_")) {
173 // We must have a superclass due to the generic bound above.
174 throw new IllegalArgumentException(
175 clazz + " is generated by @AutoValue; use " + clazz.getSuperclass() + " instead");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100176 }
177 }
juliexxia8ee423d2017-10-18 16:36:57 -0400178
179 /**
180 * Given a set of *top-level* targets and a configuration collection, evaluate top level
181 * transitions, resolve configurations and return the appropriate <Target, Configuration> pair for
182 * each target.
183 *
184 * <p>Preserves the original input ordering.
185 */
ulfjack36fbbde2018-08-02 05:16:00 -0700186 // Keep this in sync with PrepareAnalysisPhaseFunction.
juliexxiab75e45302019-05-24 07:48:39 -0700187 public static TopLevelTargetsAndConfigsResult getTargetsWithConfigs(
juliexxia8ee423d2017-10-18 16:36:57 -0400188 BuildConfigurationCollection configurations,
189 Collection<Target> targets,
190 ExtendedEventHandler eventHandler,
191 ConfiguredRuleClassProvider ruleClassProvider,
schmitt3e770242019-04-22 14:12:24 -0700192 SkyframeExecutor skyframeExecutor)
193 throws InvalidConfigurationException {
juliexxia8ee423d2017-10-18 16:36:57 -0400194 // We use a hash set here to remove duplicate nodes; this can happen for input files and package
195 // groups.
196 LinkedHashSet<TargetAndConfiguration> nodes = new LinkedHashSet<>(targets.size());
197 for (BuildConfiguration config : configurations.getTargetConfigurations()) {
198 for (Target target : targets) {
lberki9525a602019-03-06 09:33:01 -0800199 nodes.add(new TargetAndConfiguration(target, config));
juliexxia8ee423d2017-10-18 16:36:57 -0400200 }
201 }
202
203 // We'll get the configs from SkyframeExecutor#getConfigurations, which gets configurations
204 // for deps including transitions. So to satisfy its API we resolve transitions and repackage
205 // each target as a Dependency (with a NONE transition if necessary).
206 Multimap<BuildConfiguration, Dependency> asDeps =
207 AnalysisUtils.targetsToDeps(nodes, ruleClassProvider);
208
juliexxiab75e45302019-05-24 07:48:39 -0700209 return ConfigurationResolver.getConfigurationsFromExecutor(
210 nodes, asDeps, eventHandler, skyframeExecutor);
juliexxia8ee423d2017-10-18 16:36:57 -0400211 }
212
gregce731900f2018-08-01 13:12:39 -0700213 @VisibleForTesting
juliexxia8ee423d2017-10-18 16:36:57 -0400214 public static Multimap<BuildConfiguration, Dependency> targetsToDeps(
ulfjack36fbbde2018-08-02 05:16:00 -0700215 Collection<TargetAndConfiguration> nodes, ConfiguredRuleClassProvider ruleClassProvider) {
juliexxia8ee423d2017-10-18 16:36:57 -0400216 Multimap<BuildConfiguration, Dependency> asDeps =
217 ArrayListMultimap.<BuildConfiguration, Dependency>create();
218 for (TargetAndConfiguration targetAndConfig : nodes) {
lberkia1f2ddb2019-03-04 23:43:25 -0800219 ConfigurationTransition transition =
220 TransitionResolver.evaluateTransition(
221 targetAndConfig.getConfiguration(),
222 NoTransition.INSTANCE,
223 targetAndConfig.getTarget(),
224 ruleClassProvider.getTrimmingTransitionFactory());
juliexxia8ee423d2017-10-18 16:36:57 -0400225 if (targetAndConfig.getConfiguration() != null) {
226 asDeps.put(
227 targetAndConfig.getConfiguration(),
228 Dependency.withTransitionAndAspects(
229 targetAndConfig.getLabel(),
lberkia1f2ddb2019-03-04 23:43:25 -0800230 transition,
juliexxia8ee423d2017-10-18 16:36:57 -0400231 // TODO(bazel-team): support top-level aspects
232 AspectCollection.EMPTY));
233 }
234 }
235 return asDeps;
236 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100237}