Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | |
| 15 | package com.google.devtools.build.lib.analysis; |
| 16 | |
tomlu | f2720dc | 2018-10-02 12:10:51 -0700 | [diff] [blame] | 17 | import static java.util.stream.Collectors.toSet; |
| 18 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.common.annotations.VisibleForTesting; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 20 | import com.google.common.base.Preconditions; |
ulfjack | 0629519 | 2019-01-16 09:13:34 -0800 | [diff] [blame] | 21 | import com.google.common.base.Supplier; |
| 22 | import com.google.common.base.Suppliers; |
Greg Estren | 7883bf1 | 2016-11-18 20:03:43 +0000 | [diff] [blame] | 23 | import com.google.common.collect.ArrayListMultimap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableList; |
| 25 | import com.google.common.collect.ImmutableMap; |
| 26 | import com.google.common.collect.ImmutableSet; |
Greg Estren | 7883bf1 | 2016-11-18 20:03:43 +0000 | [diff] [blame] | 27 | import com.google.common.collect.Multimap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import com.google.common.collect.Sets; |
| 29 | import com.google.common.eventbus.EventBus; |
janakr | dbd34bc | 2019-06-17 06:12:48 -0700 | [diff] [blame] | 30 | import com.google.common.flogger.GoogleLogger; |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.actions.ActionAnalysisMetadata; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.ActionGraph; |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.ActionLookupData; |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.ActionLookupKey; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.ActionLookupValue; |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.Actions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | import com.google.devtools.build.lib.actions.Artifact; |
| 38 | import com.google.devtools.build.lib.actions.ArtifactFactory; |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 39 | import com.google.devtools.build.lib.actions.PackageRoots; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 41 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection; |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 42 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 43 | import com.google.devtools.build.lib.analysis.config.ConfigurationResolver.TopLevelTargetsAndConfigsResult; |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 44 | import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 45 | import com.google.devtools.build.lib.analysis.constraints.TopLevelConstraintSemantics; |
ulfjack | ab21d18 | 2017-08-10 15:36:14 +0200 | [diff] [blame] | 46 | import com.google.devtools.build.lib.analysis.test.CoverageReportActionFactory; |
| 47 | import com.google.devtools.build.lib.analysis.test.CoverageReportActionFactory.CoverageReportActionsWrapper; |
cparsons | b83c5f8 | 2018-11-21 14:46:57 -0800 | [diff] [blame] | 48 | import com.google.devtools.build.lib.analysis.test.InstrumentedFilesInfo; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.cmdline.Label; |
Googler | b2e1fe3 | 2019-09-20 14:00:39 -0700 | [diff] [blame] | 50 | import com.google.devtools.build.lib.cmdline.LabelSyntaxException; |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 51 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 52 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | import com.google.devtools.build.lib.events.Event; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 55 | import com.google.devtools.build.lib.events.ExtendedEventHandler; |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.lib.packages.AspectClass; |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 57 | import com.google.devtools.build.lib.packages.AspectDescriptor; |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 58 | import com.google.devtools.build.lib.packages.AspectParameters; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | import com.google.devtools.build.lib.packages.Attribute; |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 60 | import com.google.devtools.build.lib.packages.NativeAspectClass; |
Cal Peyser | 8a638d5 | 2017-03-24 15:44:24 +0000 | [diff] [blame] | 61 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 62 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | import com.google.devtools.build.lib.packages.Rule; |
| 64 | import com.google.devtools.build.lib.packages.Target; |
| 65 | import com.google.devtools.build.lib.packages.TargetUtils; |
laszlocsomor | d237db7 | 2020-01-09 08:15:07 -0800 | [diff] [blame] | 66 | import com.google.devtools.build.lib.pkgcache.PackageManager; |
nharmata | c4335cf | 2017-07-18 21:39:13 +0200 | [diff] [blame] | 67 | import com.google.devtools.build.lib.pkgcache.PackageManager.PackageManagerStatistics; |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 68 | import com.google.devtools.build.lib.profiler.Profiler; |
| 69 | import com.google.devtools.build.lib.profiler.SilentCloseable; |
mschaller | 75216c7 | 2020-06-25 16:04:29 -0700 | [diff] [blame] | 70 | import com.google.devtools.build.lib.server.FailureDetails; |
| 71 | import com.google.devtools.build.lib.server.FailureDetails.Analysis; |
| 72 | import com.google.devtools.build.lib.server.FailureDetails.FailureDetail; |
| 73 | import com.google.devtools.build.lib.server.FailureDetails.TargetPatterns; |
| 74 | import com.google.devtools.build.lib.server.FailureDetails.TargetPatterns.Code; |
jcater | 40549e7 | 2020-04-16 10:52:40 -0700 | [diff] [blame] | 75 | import com.google.devtools.build.lib.skyframe.AspectValueKey; |
| 76 | import com.google.devtools.build.lib.skyframe.AspectValueKey.AspectKey; |
ulfjack | 0629519 | 2019-01-16 09:13:34 -0800 | [diff] [blame] | 77 | import com.google.devtools.build.lib.skyframe.BuildConfigurationValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 78 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey; |
| 79 | import com.google.devtools.build.lib.skyframe.CoverageReportValue; |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 80 | import com.google.devtools.build.lib.skyframe.PrepareAnalysisPhaseValue; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 81 | import com.google.devtools.build.lib.skyframe.SkyframeAnalysisResult; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | import com.google.devtools.build.lib.skyframe.SkyframeBuildView; |
| 83 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor; |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame] | 84 | import com.google.devtools.build.lib.skyframe.TargetPatternPhaseValue; |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 85 | import com.google.devtools.build.lib.util.Pair; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | import com.google.devtools.build.lib.util.RegexFilter; |
Laszlo Csomor | 2bef778 | 2020-01-07 01:15:16 -0800 | [diff] [blame] | 87 | import com.google.devtools.build.skyframe.SkyKey; |
Eric Fellheimer | 9c1e12f | 2015-08-06 20:23:55 +0000 | [diff] [blame] | 88 | import com.google.devtools.build.skyframe.WalkableGraph; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 89 | import java.util.ArrayList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | import java.util.Collection; |
ulfjack | 0629519 | 2019-01-16 09:13:34 -0800 | [diff] [blame] | 91 | import java.util.HashMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 92 | import java.util.HashSet; |
| 93 | import java.util.LinkedHashSet; |
| 94 | import java.util.List; |
ulfjack | 0629519 | 2019-01-16 09:13:34 -0800 | [diff] [blame] | 95 | import java.util.Map; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | import java.util.Set; |
janakr | dbd34bc | 2019-06-17 06:12:48 -0700 | [diff] [blame] | 97 | import java.util.concurrent.TimeUnit; |
janakr | 1a4066a | 2020-04-13 13:24:27 -0700 | [diff] [blame] | 98 | import java.util.function.Predicate; |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 99 | import java.util.stream.Collectors; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | import javax.annotation.Nullable; |
| 101 | |
| 102 | /** |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 103 | * The BuildView presents a semantically-consistent and transitively-closed |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 104 | * dependency graph for some set of packages. |
| 105 | * |
| 106 | * <h2>Package design</h2> |
| 107 | * |
| 108 | * <p>This package contains the Blaze dependency analysis framework (aka |
| 109 | * "analysis phase"). The goal of this code is to perform semantic analysis of |
| 110 | * all of the build targets required for a given build, to report |
| 111 | * errors/warnings for any problems in the input, and to construct an "action |
| 112 | * graph" (see {@code lib.actions} package) correctly representing the work to |
| 113 | * be done during the execution phase of the build. |
| 114 | * |
| 115 | * <p><b>Configurations</b> the inputs to a build come from two sources: the |
| 116 | * intrinsic inputs, specified in the BUILD file, are called <em>targets</em>. |
| 117 | * The environmental inputs, coming from the build tool, the command-line, or |
| 118 | * configuration files, are called the <em>configuration</em>. Only when a |
| 119 | * target and a configuration are combined is there sufficient information to |
| 120 | * perform a build. </p> |
| 121 | * |
| 122 | * <p>Targets are implemented by the {@link Target} hierarchy in the {@code |
| 123 | * lib.packages} code. Configurations are implemented by {@link |
| 124 | * BuildConfiguration}. The pair of these together is represented by an |
| 125 | * instance of class {@link ConfiguredTarget}; this is the root of a hierarchy |
| 126 | * with different implementations for each kind of target: source file, derived |
| 127 | * file, rules, etc. |
| 128 | * |
| 129 | * <p>The framework code in this package (as opposed to its subpackages) is |
| 130 | * responsible for constructing the {@code ConfiguredTarget} graph for a given |
| 131 | * target and configuration, taking care of such issues as: |
| 132 | * <ul> |
| 133 | * <li>caching common subgraphs. |
| 134 | * <li>detecting and reporting cycles. |
| 135 | * <li>correct propagation of errors through the graph. |
| 136 | * <li>reporting universal errors, such as dependencies from production code |
| 137 | * to tests, or to experimental branches. |
| 138 | * <li>capturing and replaying errors. |
| 139 | * <li>maintaining the graph from one build to the next to |
| 140 | * avoid unnecessary recomputation. |
| 141 | * <li>checking software licenses. |
| 142 | * </ul> |
| 143 | * |
| 144 | * <p>See also {@link ConfiguredTarget} which documents some important |
| 145 | * invariants. |
| 146 | */ |
| 147 | public class BuildView { |
janakr | dbd34bc | 2019-06-17 06:12:48 -0700 | [diff] [blame] | 148 | private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 149 | |
| 150 | private final BlazeDirectories directories; |
| 151 | |
| 152 | private final SkyframeExecutor skyframeExecutor; |
| 153 | private final SkyframeBuildView skyframeBuildView; |
| 154 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 155 | private final ConfiguredRuleClassProvider ruleClassProvider; |
| 156 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 157 | /** |
| 158 | * A factory class to create the coverage report action. May be null. |
| 159 | */ |
| 160 | @Nullable private final CoverageReportActionFactory coverageReportActionFactory; |
| 161 | |
Ulf Adams | 59dbf68 | 2015-09-17 11:36:43 +0000 | [diff] [blame] | 162 | public BuildView(BlazeDirectories directories, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 163 | ConfiguredRuleClassProvider ruleClassProvider, |
| 164 | SkyframeExecutor skyframeExecutor, |
Ulf Adams | 5a96d53 | 2015-09-26 09:00:57 +0000 | [diff] [blame] | 165 | CoverageReportActionFactory coverageReportActionFactory) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 166 | this.directories = directories; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 167 | this.coverageReportActionFactory = coverageReportActionFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 168 | this.ruleClassProvider = ruleClassProvider; |
| 169 | this.skyframeExecutor = Preconditions.checkNotNull(skyframeExecutor); |
Ulf Adams | 89eefd7 | 2015-09-23 08:00:43 +0000 | [diff] [blame] | 170 | this.skyframeBuildView = skyframeExecutor.getSkyframeBuildView(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Laszlo Csomor | 2bef778 | 2020-01-07 01:15:16 -0800 | [diff] [blame] | 173 | /** |
| 174 | * Returns two numbers: number of analyzed and number of loaded targets. |
| 175 | * |
| 176 | * <p>The first number: configured targets freshly evaluated in the last analysis run. |
| 177 | * |
| 178 | * <p>The second number: targets (not configured targets) loaded in the last analysis run. |
| 179 | */ |
| 180 | public Pair<Integer, Integer> getTargetsConfiguredAndLoaded() { |
| 181 | ImmutableSet<SkyKey> keys = skyframeBuildView.getEvaluatedTargetKeys(); |
| 182 | int targetsConfigured = keys.size(); |
| 183 | int targetsLoaded = |
| 184 | keys.stream().map(key -> ((ConfiguredTargetKey) key).getLabel()).collect(toSet()).size(); |
| 185 | return Pair.of(targetsConfigured, targetsLoaded); |
tomlu | f2720dc | 2018-10-02 12:10:51 -0700 | [diff] [blame] | 186 | } |
| 187 | |
tomlu | a0e8aae | 2018-06-19 15:07:26 -0700 | [diff] [blame] | 188 | public int getActionsConstructed() { |
| 189 | return skyframeBuildView.getEvaluatedActionCount(); |
| 190 | } |
| 191 | |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 192 | public PackageManagerStatistics getAndClearPkgManagerStatistics() { |
| 193 | return skyframeExecutor.getPackageManager().getAndClearStatistics(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 194 | } |
| 195 | |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 196 | private ArtifactFactory getArtifactFactory() { |
Ulf Adams | 89eefd7 | 2015-09-23 08:00:43 +0000 | [diff] [blame] | 197 | return skyframeBuildView.getArtifactFactory(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 198 | } |
| 199 | |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 200 | /** Returns the collection of configured targets corresponding to any of the provided targets. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 201 | @VisibleForTesting |
mjhalupka | e253f83 | 2018-06-13 14:19:26 -0700 | [diff] [blame] | 202 | static LinkedHashSet<ConfiguredTarget> filterTestsByTargets( |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame] | 203 | Collection<ConfiguredTarget> targets, Set<Label> allowedTargetLabels) { |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 204 | return targets |
| 205 | .stream() |
mjhalupka | e253f83 | 2018-06-13 14:19:26 -0700 | [diff] [blame] | 206 | .filter(ct -> allowedTargetLabels.contains(ct.getLabel())) |
| 207 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 210 | @ThreadCompatible |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 211 | public AnalysisResult update( |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame] | 212 | TargetPatternPhaseValue loadingResult, |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 213 | BuildOptions targetOptions, |
| 214 | Set<String> multiCpu, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 215 | List<String> aspects, |
ulfjack | b5daae3 | 2018-06-13 07:59:33 -0700 | [diff] [blame] | 216 | AnalysisOptions viewOptions, |
juliexxia | 8a2baea | 2017-12-18 14:33:45 -0800 | [diff] [blame] | 217 | boolean keepGoing, |
juliexxia | f8b57c5 | 2017-12-21 11:37:46 -0800 | [diff] [blame] | 218 | int loadingPhaseThreads, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 219 | TopLevelArtifactContext topLevelOptions, |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 220 | ExtendedEventHandler eventHandler, |
Ulf Adams | 8c4ae67 | 2016-03-30 11:20:41 +0000 | [diff] [blame] | 221 | EventBus eventBus) |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 222 | throws ViewCreationFailedException, InvalidConfigurationException, InterruptedException { |
janakr | dbd34bc | 2019-06-17 06:12:48 -0700 | [diff] [blame] | 223 | logger.atInfo().log("Starting analysis"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 224 | pollInterruptedStatus(); |
| 225 | |
Laszlo Csomor | 2bef778 | 2020-01-07 01:15:16 -0800 | [diff] [blame] | 226 | skyframeBuildView.resetProgressReceiver(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 227 | |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame] | 228 | // TODO(ulfjack): Expensive. Maybe we don't actually need the targets, only the labels? |
| 229 | Collection<Target> targets = |
| 230 | loadingResult.getTargets(eventHandler, skyframeExecutor.getPackageManager()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 231 | eventBus.post(new AnalysisPhaseStartedEvent(targets)); |
| 232 | |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 233 | // Prepare the analysis phase |
| 234 | BuildConfigurationCollection configurations; |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 235 | TopLevelTargetsAndConfigsResult topLevelTargetsWithConfigsResult; |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 236 | if (viewOptions.skyframePrepareAnalysis) { |
| 237 | PrepareAnalysisPhaseValue prepareAnalysisPhaseValue; |
| 238 | try (SilentCloseable c = Profiler.instance().profile("Prepare analysis phase")) { |
| 239 | prepareAnalysisPhaseValue = skyframeExecutor.prepareAnalysisPhase( |
| 240 | eventHandler, targetOptions, multiCpu, loadingResult.getTargetLabels()); |
ulfjack | 0629519 | 2019-01-16 09:13:34 -0800 | [diff] [blame] | 241 | |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 242 | // Determine the configurations |
| 243 | configurations = |
| 244 | prepareAnalysisPhaseValue.getConfigurations(eventHandler, skyframeExecutor); |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 245 | topLevelTargetsWithConfigsResult = |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 246 | prepareAnalysisPhaseValue.getTopLevelCts(eventHandler, skyframeExecutor); |
| 247 | } |
| 248 | } else { |
| 249 | // Configuration creation. |
| 250 | // TODO(gregce): Consider dropping this phase and passing on-the-fly target / host configs as |
| 251 | // needed. This requires cleaning up the invalidation in SkyframeBuildView.setConfigurations. |
| 252 | try (SilentCloseable c = Profiler.instance().profile("createConfigurations")) { |
| 253 | configurations = |
| 254 | skyframeExecutor |
| 255 | .createConfigurations( |
| 256 | eventHandler, |
| 257 | targetOptions, |
| 258 | multiCpu, |
| 259 | keepGoing); |
| 260 | } |
| 261 | try (SilentCloseable c = Profiler.instance().profile("AnalysisUtils.getTargetsWithConfigs")) { |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 262 | topLevelTargetsWithConfigsResult = |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 263 | AnalysisUtils.getTargetsWithConfigs( |
| 264 | configurations, targets, eventHandler, ruleClassProvider, skyframeExecutor); |
| 265 | } |
| 266 | } |
| 267 | |
mstaib | 6cf6278 | 2018-11-14 11:54:33 -0800 | [diff] [blame] | 268 | skyframeBuildView.setConfigurations( |
| 269 | eventHandler, configurations, viewOptions.maxConfigChangesToShow); |
Marian Lobur | 86bd4fd | 2015-09-16 10:01:38 +0000 | [diff] [blame] | 270 | |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 271 | if (configurations.getTargetConfigurations().size() == 1) { |
| 272 | eventBus |
| 273 | .post( |
| 274 | new MakeEnvironmentEvent( |
| 275 | configurations.getTargetConfigurations().get(0).getMakeEnvironment())); |
| 276 | } |
felly | 392e3e4 | 2019-08-05 15:16:29 -0700 | [diff] [blame] | 277 | for (BuildConfiguration targetConfig : configurations.getTargetConfigurations()) { |
| 278 | eventBus.post(targetConfig.toBuildEvent()); |
| 279 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 280 | |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 281 | Collection<TargetAndConfiguration> topLevelTargetsWithConfigs = |
| 282 | topLevelTargetsWithConfigsResult.getTargetsAndConfigs(); |
| 283 | |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 284 | // Report the generated association of targets to configurations |
| 285 | Multimap<Label, BuildConfiguration> byLabel = |
| 286 | ArrayListMultimap.<Label, BuildConfiguration>create(); |
| 287 | for (TargetAndConfiguration pair : topLevelTargetsWithConfigs) { |
| 288 | byLabel.put(pair.getLabel(), pair.getConfiguration()); |
| 289 | } |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 290 | for (Target target : targets) { |
| 291 | eventBus.post(new TargetConfiguredEvent(target, byLabel.get(target.getLabel()))); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 292 | } |
| 293 | |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 294 | List<ConfiguredTargetKey> topLevelCtKeys = |
jcater | 83221e3 | 2020-05-28 11:37:39 -0700 | [diff] [blame] | 295 | topLevelTargetsWithConfigs.stream() |
jcater | 4488f0c | 2020-05-28 12:47:40 -0700 | [diff] [blame] | 296 | .map(TargetAndConfiguration::getConfiguredTargetKey) |
ulfjack | 36fbbde | 2018-08-02 05:16:00 -0700 | [diff] [blame] | 297 | .collect(Collectors.toList()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 298 | |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 299 | Multimap<Pair<Label, String>, BuildConfiguration> aspectConfigurations = |
| 300 | ArrayListMultimap.create(); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 301 | |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 302 | List<AspectValueKey> aspectKeys = new ArrayList<>(); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 303 | for (String aspect : aspects) { |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 304 | // Syntax: label%aspect |
| 305 | int delimiterPosition = aspect.indexOf('%'); |
| 306 | if (delimiterPosition >= 0) { |
gregce | ca48e9a | 2020-04-14 08:54:38 -0700 | [diff] [blame] | 307 | // TODO(jfield): For consistency with Starlark loads, the aspect should be specified |
laurentlb | 940dbc5 | 2018-03-01 08:07:14 -0800 | [diff] [blame] | 308 | // as an absolute label. |
| 309 | // We convert it for compatibility reasons (this will be removed in the future). |
Dmitry Lomov | 51aafc1 | 2016-11-18 14:02:54 +0000 | [diff] [blame] | 310 | String bzlFileLoadLikeString = aspect.substring(0, delimiterPosition); |
| 311 | if (!bzlFileLoadLikeString.startsWith("//") && !bzlFileLoadLikeString.startsWith("@")) { |
| 312 | // "Legacy" behavior of '--aspects' parameter. |
laurentlb | 940dbc5 | 2018-03-01 08:07:14 -0800 | [diff] [blame] | 313 | if (bzlFileLoadLikeString.startsWith("/")) { |
| 314 | bzlFileLoadLikeString = bzlFileLoadLikeString.substring(1); |
| 315 | } |
| 316 | int lastSlashPosition = bzlFileLoadLikeString.lastIndexOf('/'); |
| 317 | if (lastSlashPosition >= 0) { |
| 318 | bzlFileLoadLikeString = |
| 319 | "//" |
| 320 | + bzlFileLoadLikeString.substring(0, lastSlashPosition) |
| 321 | + ":" |
| 322 | + bzlFileLoadLikeString.substring(lastSlashPosition + 1); |
| 323 | } else { |
| 324 | bzlFileLoadLikeString = "//:" + bzlFileLoadLikeString; |
| 325 | } |
| 326 | if (!bzlFileLoadLikeString.endsWith(".bzl")) { |
| 327 | bzlFileLoadLikeString = bzlFileLoadLikeString + ".bzl"; |
Dmitry Lomov | 51aafc1 | 2016-11-18 14:02:54 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
gregce | 773b95f | 2020-05-19 09:51:09 -0700 | [diff] [blame] | 330 | Label starlarkFileLabel; |
Dmitry Lomov | 51aafc1 | 2016-11-18 14:02:54 +0000 | [diff] [blame] | 331 | try { |
gregce | 773b95f | 2020-05-19 09:51:09 -0700 | [diff] [blame] | 332 | starlarkFileLabel = |
Googler | b2e1fe3 | 2019-09-20 14:00:39 -0700 | [diff] [blame] | 333 | Label.parseAbsolute( |
dannark | f4b9ff4 | 2018-06-12 09:28:45 -0700 | [diff] [blame] | 334 | bzlFileLoadLikeString, /* repositoryMapping= */ ImmutableMap.of()); |
Googler | b2e1fe3 | 2019-09-20 14:00:39 -0700 | [diff] [blame] | 335 | } catch (LabelSyntaxException e) { |
Dmitry Lomov | 51aafc1 | 2016-11-18 14:02:54 +0000 | [diff] [blame] | 336 | throw new ViewCreationFailedException( |
| 337 | String.format("Invalid aspect '%s': %s", aspect, e.getMessage()), e); |
| 338 | } |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 339 | |
gregce | 773b95f | 2020-05-19 09:51:09 -0700 | [diff] [blame] | 340 | String starlarkFunctionName = aspect.substring(delimiterPosition + 1); |
Dmitry Lomov | 01f85c0 | 2016-06-29 06:37:50 +0000 | [diff] [blame] | 341 | for (TargetAndConfiguration targetSpec : topLevelTargetsWithConfigs) { |
mstaib | 7c7a701 | 2019-04-23 16:19:09 -0700 | [diff] [blame] | 342 | if (targetSpec.getConfiguration() != null |
| 343 | && targetSpec.getConfiguration().trimConfigurationsRetroactively()) { |
| 344 | throw new ViewCreationFailedException( |
| 345 | "Aspects were requested, but are not supported in retroactive trimming mode."); |
| 346 | } |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 347 | aspectConfigurations.put( |
| 348 | Pair.of(targetSpec.getLabel(), aspect), targetSpec.getConfiguration()); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 349 | aspectKeys.add( |
gregce | 5c8a5f5 | 2020-05-13 10:35:36 -0700 | [diff] [blame] | 350 | AspectValueKey.createStarlarkAspectKey( |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 351 | targetSpec.getLabel(), |
Michael Staib | 04f6f24 | 2016-03-01 15:40:29 +0000 | [diff] [blame] | 352 | // For invoking top-level aspects, use the top-level configuration for both the |
| 353 | // aspect and the base target while the top-level configuration is untrimmed. |
| 354 | targetSpec.getConfiguration(), |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 355 | targetSpec.getConfiguration(), |
gregce | 773b95f | 2020-05-19 09:51:09 -0700 | [diff] [blame] | 356 | starlarkFileLabel, |
| 357 | starlarkFunctionName)); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 358 | } |
| 359 | } else { |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 360 | final NativeAspectClass aspectFactoryClass = |
| 361 | ruleClassProvider.getNativeAspectClassMap().get(aspect); |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 362 | |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 363 | if (aspectFactoryClass != null) { |
Dmitry Lomov | 01f85c0 | 2016-06-29 06:37:50 +0000 | [diff] [blame] | 364 | for (TargetAndConfiguration targetSpec : topLevelTargetsWithConfigs) { |
mstaib | 7c7a701 | 2019-04-23 16:19:09 -0700 | [diff] [blame] | 365 | if (targetSpec.getConfiguration() != null |
| 366 | && targetSpec.getConfiguration().trimConfigurationsRetroactively()) { |
| 367 | throw new ViewCreationFailedException( |
| 368 | "Aspects were requested, but are not supported in retroactive trimming mode."); |
| 369 | } |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 370 | // For invoking top-level aspects, use the top-level configuration for both the |
| 371 | // aspect and the base target while the top-level configuration is untrimmed. |
| 372 | BuildConfiguration configuration = targetSpec.getConfiguration(); |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 373 | aspectConfigurations.put(Pair.of(targetSpec.getLabel(), aspect), configuration); |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 374 | aspectKeys.add( |
jcater | 40549e7 | 2020-04-16 10:52:40 -0700 | [diff] [blame] | 375 | AspectValueKey.createAspectKey( |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 376 | targetSpec.getLabel(), |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 377 | configuration, |
dslomov | fa50c3d | 2017-05-08 08:47:44 -0400 | [diff] [blame] | 378 | new AspectDescriptor(aspectFactoryClass, AspectParameters.EMPTY), |
jcater | 40549e7 | 2020-04-16 10:52:40 -0700 | [diff] [blame] | 379 | configuration)); |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 380 | } |
| 381 | } else { |
| 382 | throw new ViewCreationFailedException("Aspect '" + aspect + "' is unknown"); |
| 383 | } |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 387 | for (Pair<Label, String> target : aspectConfigurations.keys()) { |
| 388 | eventBus.post( |
| 389 | new AspectConfiguredEvent( |
| 390 | target.getFirst(), target.getSecond(), aspectConfigurations.get(target))); |
| 391 | } |
| 392 | |
janakr | 1f08d4c8 | 2018-09-15 16:33:35 -0700 | [diff] [blame] | 393 | getArtifactFactory().noteAnalysisStarting(); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 394 | SkyframeAnalysisResult skyframeAnalysisResult; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 395 | try { |
ulfjack | 0629519 | 2019-01-16 09:13:34 -0800 | [diff] [blame] | 396 | Supplier<Map<BuildConfigurationValue.Key, BuildConfiguration>> configurationLookupSupplier = |
| 397 | () -> { |
| 398 | Map<BuildConfigurationValue.Key, BuildConfiguration> result = new HashMap<>(); |
| 399 | for (TargetAndConfiguration node : topLevelTargetsWithConfigs) { |
| 400 | if (node.getConfiguration() != null) { |
| 401 | result.put( |
| 402 | BuildConfigurationValue.key(node.getConfiguration()), node.getConfiguration()); |
| 403 | } |
| 404 | } |
| 405 | return result; |
| 406 | }; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 407 | skyframeAnalysisResult = |
| 408 | skyframeBuildView.configureTargets( |
ulfjack | 0629519 | 2019-01-16 09:13:34 -0800 | [diff] [blame] | 409 | eventHandler, |
| 410 | topLevelCtKeys, |
| 411 | aspectKeys, |
| 412 | Suppliers.memoize(configurationLookupSupplier), |
janakr | 6d185b8 | 2020-03-30 14:08:01 -0700 | [diff] [blame] | 413 | topLevelOptions, |
ulfjack | 0629519 | 2019-01-16 09:13:34 -0800 | [diff] [blame] | 414 | eventBus, |
| 415 | keepGoing, |
janakr | 1a4066a | 2020-04-13 13:24:27 -0700 | [diff] [blame] | 416 | loadingPhaseThreads, |
| 417 | viewOptions.strictConflictChecks); |
Kristina Chodorow | 94e2742 | 2016-08-25 14:17:30 +0000 | [diff] [blame] | 418 | setArtifactRoots(skyframeAnalysisResult.getPackageRoots()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 419 | } finally { |
| 420 | skyframeBuildView.clearInvalidatedConfiguredTargets(); |
| 421 | } |
| 422 | |
Greg Estren | 7971e67 | 2016-06-01 23:22:48 +0000 | [diff] [blame] | 423 | int numTargetsToAnalyze = topLevelTargetsWithConfigs.size(); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 424 | int numSuccessful = skyframeAnalysisResult.getConfiguredTargets().size(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 425 | if (0 < numSuccessful && numSuccessful < numTargetsToAnalyze) { |
| 426 | String msg = String.format("Analysis succeeded for only %d of %d top-level targets", |
| 427 | numSuccessful, numTargetsToAnalyze); |
| 428 | eventHandler.handle(Event.info(msg)); |
janakr | dbd34bc | 2019-06-17 06:12:48 -0700 | [diff] [blame] | 429 | logger.atInfo().log(msg); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 430 | } |
| 431 | |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 432 | Set<ConfiguredTarget> targetsToSkip = |
janakr | 171a7eb | 2018-03-26 09:26:53 -0700 | [diff] [blame] | 433 | new TopLevelConstraintSemantics( |
| 434 | skyframeExecutor.getPackageManager(), |
| 435 | input -> skyframeExecutor.getConfiguration(eventHandler, input), |
| 436 | eventHandler) |
gregce | 21fdc48 | 2017-12-05 16:10:47 -0800 | [diff] [blame] | 437 | .checkTargetEnvironmentRestrictions(skyframeAnalysisResult.getConfiguredTargets()); |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 438 | |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 439 | AnalysisResult result = |
| 440 | createResult( |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 441 | eventHandler, |
ulfjack | b2cb640 | 2019-01-24 04:04:48 -0800 | [diff] [blame] | 442 | eventBus, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 443 | loadingResult, |
ulfjack | 940fbd7 | 2018-06-15 03:23:58 -0700 | [diff] [blame] | 444 | configurations, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 445 | topLevelOptions, |
| 446 | viewOptions, |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 447 | skyframeAnalysisResult, |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 448 | targetsToSkip, |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 449 | topLevelTargetsWithConfigsResult); |
janakr | dbd34bc | 2019-06-17 06:12:48 -0700 | [diff] [blame] | 450 | logger.atInfo().log("Finished analysis"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 451 | return result; |
| 452 | } |
| 453 | |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 454 | private AnalysisResult createResult( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 455 | ExtendedEventHandler eventHandler, |
ulfjack | b2cb640 | 2019-01-24 04:04:48 -0800 | [diff] [blame] | 456 | EventBus eventBus, |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame] | 457 | TargetPatternPhaseValue loadingResult, |
ulfjack | 940fbd7 | 2018-06-15 03:23:58 -0700 | [diff] [blame] | 458 | BuildConfigurationCollection configurations, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 459 | TopLevelArtifactContext topLevelOptions, |
ulfjack | b5daae3 | 2018-06-13 07:59:33 -0700 | [diff] [blame] | 460 | AnalysisOptions viewOptions, |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 461 | SkyframeAnalysisResult skyframeAnalysisResult, |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 462 | Set<ConfiguredTarget> targetsToSkip, |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 463 | TopLevelTargetsAndConfigsResult topLevelTargetsWithConfigs) |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 464 | throws InterruptedException { |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame] | 465 | Set<Label> testsToRun = loadingResult.getTestsToRunLabels(); |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 466 | Set<ConfiguredTarget> configuredTargets = |
| 467 | Sets.newLinkedHashSet(skyframeAnalysisResult.getConfiguredTargets()); |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 468 | ImmutableMap<AspectKey, ConfiguredAspect> aspects = skyframeAnalysisResult.getAspects(); |
Dmitry Lomov | cf6021a | 2016-08-09 16:49:32 +0000 | [diff] [blame] | 469 | |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 470 | Set<ConfiguredTarget> allTargetsToTest = null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 471 | if (testsToRun != null) { |
| 472 | // Determine the subset of configured targets that are meant to be run as tests. |
mjhalupka | e253f83 | 2018-06-13 14:19:26 -0700 | [diff] [blame] | 473 | allTargetsToTest = filterTestsByTargets(configuredTargets, testsToRun); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 474 | } |
| 475 | |
janakr | ccb7456 | 2018-07-30 10:56:57 -0700 | [diff] [blame] | 476 | ArtifactsToOwnerLabels.Builder artifactsToOwnerLabelsBuilder = |
| 477 | new ArtifactsToOwnerLabels.Builder(); |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 478 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 479 | // build-info and build-changelist. |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 480 | Collection<Artifact> buildInfoArtifacts = |
| 481 | skyframeExecutor.getWorkspaceStatusArtifacts(eventHandler); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 482 | Preconditions.checkState(buildInfoArtifacts.size() == 2, buildInfoArtifacts); |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 483 | |
| 484 | // Extra actions |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 485 | addExtraActionsIfRequested( |
janakr | ccb7456 | 2018-07-30 10:56:57 -0700 | [diff] [blame] | 486 | viewOptions, configuredTargets, aspects, artifactsToOwnerLabelsBuilder, eventHandler); |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 487 | |
| 488 | // Coverage |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 489 | NestedSet<Artifact> baselineCoverageArtifacts = |
janakr | ccb7456 | 2018-07-30 10:56:57 -0700 | [diff] [blame] | 490 | getBaselineCoverageArtifacts(configuredTargets, artifactsToOwnerLabelsBuilder); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 491 | if (coverageReportActionFactory != null) { |
Googler | cdc90e9 | 2015-02-11 00:18:07 +0000 | [diff] [blame] | 492 | CoverageReportActionsWrapper actionsWrapper; |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 493 | actionsWrapper = |
| 494 | coverageReportActionFactory.createCoverageReportActionsWrapper( |
| 495 | eventHandler, |
ulfjack | b2cb640 | 2019-01-24 04:04:48 -0800 | [diff] [blame] | 496 | eventBus, |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 497 | directories, |
| 498 | allTargetsToTest, |
| 499 | baselineCoverageArtifacts, |
| 500 | getArtifactFactory(), |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 501 | skyframeExecutor.getActionKeyContext(), |
elenairina | 9438d10 | 2018-07-06 03:06:21 -0700 | [diff] [blame] | 502 | CoverageReportValue.COVERAGE_REPORT_KEY, |
| 503 | loadingResult.getWorkspaceName()); |
Googler | cdc90e9 | 2015-02-11 00:18:07 +0000 | [diff] [blame] | 504 | if (actionsWrapper != null) { |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 505 | Actions.GeneratingActions actions = actionsWrapper.getActions(); |
Googler | cdc90e9 | 2015-02-11 00:18:07 +0000 | [diff] [blame] | 506 | skyframeExecutor.injectCoverageReportData(actions); |
janakr | ccb7456 | 2018-07-30 10:56:57 -0700 | [diff] [blame] | 507 | actionsWrapper.getCoverageOutputs().forEach(artifactsToOwnerLabelsBuilder::addArtifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 508 | } |
| 509 | } |
janakr | 1a4066a | 2020-04-13 13:24:27 -0700 | [diff] [blame] | 510 | // TODO(cparsons): If extra actions are ever removed, this filtering step can probably be |
| 511 | // removed as well: the only concern would be action conflicts involving coverage artifacts, |
| 512 | // which seems far-fetched. |
| 513 | if (skyframeAnalysisResult.hasActionConflicts()) { |
| 514 | ArtifactsToOwnerLabels tempOwners = artifactsToOwnerLabelsBuilder.build(); |
| 515 | // We don't remove the (hopefully unnecessary) guard in SkyframeBuildView that enables/ |
| 516 | // disables analysis, since no new targets should actually be analyzed. |
| 517 | Set<Artifact> artifacts = tempOwners.getArtifacts(); |
| 518 | Predicate<Artifact> errorFreeArtifacts = |
| 519 | skyframeExecutor.filterActionConflictsForTopLevelArtifacts(eventHandler, artifacts); |
| 520 | artifactsToOwnerLabelsBuilder = tempOwners.toBuilder().filterArtifacts(errorFreeArtifacts); |
| 521 | } |
| 522 | // Build-info artifacts are always conflict-free, and can't be checked easily. |
| 523 | buildInfoArtifacts.forEach(artifactsToOwnerLabelsBuilder::addArtifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 524 | |
laszlocsomor | d237db7 | 2020-01-09 08:15:07 -0800 | [diff] [blame] | 525 | // Tests. |
| 526 | Pair<ImmutableSet<ConfiguredTarget>, ImmutableSet<ConfiguredTarget>> testsPair = |
| 527 | collectTests( |
| 528 | topLevelOptions, allTargetsToTest, skyframeExecutor.getPackageManager(), eventHandler); |
| 529 | ImmutableSet<ConfiguredTarget> parallelTests = testsPair.first; |
| 530 | ImmutableSet<ConfiguredTarget> exclusiveTests = testsPair.second; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 531 | |
mschaller | 75216c7 | 2020-06-25 16:04:29 -0700 | [diff] [blame] | 532 | FailureDetail failureDetail = |
| 533 | createFailureDetail(loadingResult, skyframeAnalysisResult, topLevelTargetsWithConfigs); |
Eric Fellheimer | 9c1e12f | 2015-08-06 20:23:55 +0000 | [diff] [blame] | 534 | |
Ulf Adams | 53abece | 2016-02-03 08:30:07 +0000 | [diff] [blame] | 535 | final WalkableGraph graph = skyframeAnalysisResult.getWalkableGraph(); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 536 | final ActionGraph actionGraph = |
| 537 | new ActionGraph() { |
| 538 | @Nullable |
| 539 | @Override |
| 540 | public ActionAnalysisMetadata getGeneratingAction(Artifact artifact) { |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 541 | if (artifact.isSourceArtifact()) { |
| 542 | return null; |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 543 | } |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 544 | ActionLookupData generatingActionKey = |
| 545 | ((Artifact.DerivedArtifact) artifact).getGeneratingActionKey(); |
| 546 | ActionLookupValue val; |
| 547 | try { |
| 548 | val = (ActionLookupValue) graph.getValue(generatingActionKey.getActionLookupKey()); |
| 549 | } catch (InterruptedException e) { |
| 550 | throw new IllegalStateException( |
| 551 | "Interruption not expected from this graph: " + generatingActionKey, e); |
| 552 | } |
| 553 | if (val == null) { |
janakr | dbd34bc | 2019-06-17 06:12:48 -0700 | [diff] [blame] | 554 | logger.atWarning().atMostEvery(1, TimeUnit.SECONDS).log( |
| 555 | "Missing generating action for %s (%s)", artifact, generatingActionKey); |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 556 | return null; |
| 557 | } |
Googler | 2714b88 | 2019-08-01 12:30:08 -0700 | [diff] [blame] | 558 | return val.getActions().get(generatingActionKey.getActionIndex()); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 559 | } |
| 560 | }; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 561 | return new AnalysisResult( |
ulfjack | 940fbd7 | 2018-06-15 03:23:58 -0700 | [diff] [blame] | 562 | configurations, |
laszlocsomor | 3c8883d | 2019-12-19 05:19:28 -0800 | [diff] [blame] | 563 | ImmutableSet.copyOf(configuredTargets), |
Dmitry Lomov | cf6021a | 2016-08-09 16:49:32 +0000 | [diff] [blame] | 564 | aspects, |
laszlocsomor | 3c8883d | 2019-12-19 05:19:28 -0800 | [diff] [blame] | 565 | allTargetsToTest == null ? null : ImmutableList.copyOf(allTargetsToTest), |
| 566 | ImmutableSet.copyOf(targetsToSkip), |
mschaller | 75216c7 | 2020-06-25 16:04:29 -0700 | [diff] [blame] | 567 | failureDetail, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 568 | actionGraph, |
janakr | ccb7456 | 2018-07-30 10:56:57 -0700 | [diff] [blame] | 569 | artifactsToOwnerLabelsBuilder.build(), |
laszlocsomor | d237db7 | 2020-01-09 08:15:07 -0800 | [diff] [blame] | 570 | parallelTests, |
| 571 | exclusiveTests, |
Marian Lobur | 86bd4fd | 2015-09-16 10:01:38 +0000 | [diff] [blame] | 572 | topLevelOptions, |
Kristina Chodorow | 36a6c17 | 2016-07-22 15:18:07 +0000 | [diff] [blame] | 573 | skyframeAnalysisResult.getPackageRoots(), |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 574 | loadingResult.getWorkspaceName(), |
ichern | 121933e | 2020-01-27 02:08:19 -0800 | [diff] [blame] | 575 | topLevelTargetsWithConfigs.getTargetsAndConfigs(), |
| 576 | loadingResult.getNotSymlinkedInExecrootDirectories()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 577 | } |
| 578 | |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 579 | /** |
| 580 | * Check for errors in "chronological" order (acknowledge that loading and analysis are |
| 581 | * interleaved, but sequential on the single target scale). |
| 582 | */ |
Janak Ramakrishnan | 6f9d7d1 | 2016-08-09 08:45:34 +0000 | [diff] [blame] | 583 | @Nullable |
mschaller | 75216c7 | 2020-06-25 16:04:29 -0700 | [diff] [blame] | 584 | public static FailureDetail createFailureDetail( |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame] | 585 | TargetPatternPhaseValue loadingResult, |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 586 | @Nullable SkyframeAnalysisResult skyframeAnalysisResult, |
| 587 | @Nullable TopLevelTargetsAndConfigsResult topLevelTargetsAndConfigs) { |
| 588 | if (loadingResult.hasError()) { |
mschaller | 75216c7 | 2020-06-25 16:04:29 -0700 | [diff] [blame] | 589 | return FailureDetail.newBuilder() |
| 590 | .setMessage("command succeeded, but there were errors parsing the target pattern") |
| 591 | .setTargetPatterns(TargetPatterns.newBuilder().setCode(Code.TARGET_PATTERN_PARSE_FAILURE)) |
| 592 | .build(); |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 593 | } |
| 594 | if (loadingResult.hasPostExpansionError() |
| 595 | || (skyframeAnalysisResult != null && skyframeAnalysisResult.hasLoadingError())) { |
mschaller | 75216c7 | 2020-06-25 16:04:29 -0700 | [diff] [blame] | 596 | return FailureDetail.newBuilder() |
| 597 | .setMessage("command succeeded, but there were loading phase errors") |
| 598 | .setAnalysis(Analysis.newBuilder().setCode(Analysis.Code.GENERIC_LOADING_PHASE_FAILURE)) |
| 599 | .build(); |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 600 | } |
| 601 | if (topLevelTargetsAndConfigs != null && topLevelTargetsAndConfigs.hasError()) { |
mschaller | 75216c7 | 2020-06-25 16:04:29 -0700 | [diff] [blame] | 602 | return FailureDetail.newBuilder() |
| 603 | .setMessage("command succeeded, but top level configurations could not be created") |
| 604 | .setBuildConfiguration( |
| 605 | FailureDetails.BuildConfiguration.newBuilder() |
| 606 | .setCode( |
| 607 | FailureDetails.BuildConfiguration.Code |
| 608 | .TOP_LEVEL_CONFIGURATION_CREATION_FAILURE)) |
| 609 | .build(); |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 610 | } |
| 611 | if (skyframeAnalysisResult != null && skyframeAnalysisResult.hasAnalysisError()) { |
mschaller | 75216c7 | 2020-06-25 16:04:29 -0700 | [diff] [blame] | 612 | return FailureDetail.newBuilder() |
| 613 | .setMessage("command succeeded, but not all targets were analyzed") |
| 614 | .setAnalysis(Analysis.newBuilder().setCode(Analysis.Code.NOT_ALL_TARGETS_ANALYZED)) |
| 615 | .build(); |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 616 | } |
| 617 | return null; |
Janak Ramakrishnan | 6f9d7d1 | 2016-08-09 08:45:34 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 620 | private static NestedSet<Artifact> getBaselineCoverageArtifacts( |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 621 | Collection<ConfiguredTarget> configuredTargets, |
janakr | ccb7456 | 2018-07-30 10:56:57 -0700 | [diff] [blame] | 622 | ArtifactsToOwnerLabels.Builder topLevelArtifactsToOwnerLabels) { |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 623 | NestedSetBuilder<Artifact> baselineCoverageArtifacts = NestedSetBuilder.stableOrder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 624 | for (ConfiguredTarget target : configuredTargets) { |
gregce | ff94123 | 2020-04-21 08:03:32 -0700 | [diff] [blame] | 625 | InstrumentedFilesInfo provider = target.get(InstrumentedFilesInfo.STARLARK_CONSTRUCTOR); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 626 | if (provider != null) { |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 627 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 628 | provider.getBaselineCoverageArtifacts(), |
| 629 | null, |
| 630 | target.getLabel(), |
| 631 | topLevelArtifactsToOwnerLabels); |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 632 | baselineCoverageArtifacts.addTransitive(provider.getBaselineCoverageArtifacts()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 633 | } |
| 634 | } |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 635 | return baselineCoverageArtifacts.build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 636 | } |
| 637 | |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 638 | private void addExtraActionsIfRequested( |
ulfjack | b5daae3 | 2018-06-13 07:59:33 -0700 | [diff] [blame] | 639 | AnalysisOptions viewOptions, |
Dmitry Lomov | cf6021a | 2016-08-09 16:49:32 +0000 | [diff] [blame] | 640 | Collection<ConfiguredTarget> configuredTargets, |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 641 | ImmutableMap<AspectKey, ConfiguredAspect> aspects, |
janakr | ccb7456 | 2018-07-30 10:56:57 -0700 | [diff] [blame] | 642 | ArtifactsToOwnerLabels.Builder artifactsToTopLevelLabelsMap, |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 643 | ExtendedEventHandler eventHandler) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 644 | RegexFilter filter = viewOptions.extraActionFilter; |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 645 | for (ConfiguredTarget target : configuredTargets) { |
| 646 | ExtraActionArtifactsProvider provider = |
| 647 | target.getProvider(ExtraActionArtifactsProvider.class); |
| 648 | if (provider != null) { |
| 649 | if (viewOptions.extraActionTopLevelOnly) { |
lberki | 9137a75 | 2017-06-09 12:49:10 +0200 | [diff] [blame] | 650 | // Collect all aspect-classes that topLevel might inject. |
| 651 | Set<AspectClass> aspectClasses = new HashSet<>(); |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 652 | Target actualTarget = null; |
| 653 | try { |
| 654 | actualTarget = |
| 655 | skyframeExecutor.getPackageManager().getTarget(eventHandler, target.getLabel()); |
| 656 | } catch (NoSuchPackageException | NoSuchTargetException | InterruptedException e) { |
| 657 | eventHandler.handle(Event.error("")); |
| 658 | } |
| 659 | for (Attribute attr : actualTarget.getAssociatedRule().getAttributes()) { |
lberki | 9137a75 | 2017-06-09 12:49:10 +0200 | [diff] [blame] | 660 | aspectClasses.addAll(attr.getAspectClasses()); |
| 661 | } |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 662 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 663 | provider.getExtraActionArtifacts(), |
| 664 | filter, |
| 665 | target.getLabel(), |
| 666 | artifactsToTopLevelLabelsMap); |
lberki | 9137a75 | 2017-06-09 12:49:10 +0200 | [diff] [blame] | 667 | if (!aspectClasses.isEmpty()) { |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 668 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 669 | filterTransitiveExtraActions(provider, aspectClasses), |
| 670 | filter, |
| 671 | target.getLabel(), |
| 672 | artifactsToTopLevelLabelsMap); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 673 | } |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 674 | } else { |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 675 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 676 | provider.getTransitiveExtraActionArtifacts(), |
| 677 | filter, |
| 678 | target.getLabel(), |
| 679 | artifactsToTopLevelLabelsMap); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | } |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 683 | for (Map.Entry<AspectKey, ConfiguredAspect> aspectEntry : aspects.entrySet()) { |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 684 | ExtraActionArtifactsProvider provider = |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 685 | aspectEntry.getValue().getProvider(ExtraActionArtifactsProvider.class); |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 686 | if (provider != null) { |
| 687 | if (viewOptions.extraActionTopLevelOnly) { |
| 688 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 689 | provider.getExtraActionArtifacts(), |
| 690 | filter, |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 691 | aspectEntry.getKey().getLabel(), |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 692 | artifactsToTopLevelLabelsMap); |
| 693 | } else { |
| 694 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 695 | provider.getTransitiveExtraActionArtifacts(), |
| 696 | filter, |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 697 | aspectEntry.getKey().getLabel(), |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 698 | artifactsToTopLevelLabelsMap); |
| 699 | } |
| 700 | } |
| 701 | } |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /** |
janakr | 658d47f | 2019-05-29 11:11:30 -0700 | [diff] [blame] | 705 | * Returns a list of artifacts from 'provider' that were registered by an aspect from |
| 706 | * 'aspectClasses'. All artifacts in 'provider' are considered - both direct and transitive. |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 707 | */ |
laszlocsomor | d237db7 | 2020-01-09 08:15:07 -0800 | [diff] [blame] | 708 | private static ImmutableList<Artifact> filterTransitiveExtraActions( |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 709 | ExtraActionArtifactsProvider provider, Set<AspectClass> aspectClasses) { |
| 710 | ImmutableList.Builder<Artifact> artifacts = ImmutableList.builder(); |
| 711 | // Add to 'artifacts' all extra-actions which were registered by aspects which 'topLevel' |
| 712 | // might have injected. |
ulfjack | 61216fd | 2019-12-18 12:31:03 -0800 | [diff] [blame] | 713 | for (Artifact.DerivedArtifact artifact : |
| 714 | provider.getTransitiveExtraActionArtifacts().toList()) { |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 715 | ActionLookupKey owner = artifact.getArtifactOwner(); |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 716 | if (owner instanceof AspectKey) { |
| 717 | if (aspectClasses.contains(((AspectKey) owner).getAspectClass())) { |
| 718 | artifacts.add(artifact); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | } |
| 722 | return artifacts.build(); |
| 723 | } |
| 724 | |
laszlocsomor | d237db7 | 2020-01-09 08:15:07 -0800 | [diff] [blame] | 725 | private static Pair<ImmutableSet<ConfiguredTarget>, ImmutableSet<ConfiguredTarget>> collectTests( |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 726 | TopLevelArtifactContext topLevelOptions, |
laszlocsomor | d237db7 | 2020-01-09 08:15:07 -0800 | [diff] [blame] | 727 | @Nullable Iterable<ConfiguredTarget> allTestTargets, |
| 728 | PackageManager packageManager, |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 729 | ExtendedEventHandler eventHandler) |
| 730 | throws InterruptedException { |
Lukacs Berki | 3f4d4e9 | 2015-02-24 10:28:26 +0000 | [diff] [blame] | 731 | Set<String> outputGroups = topLevelOptions.outputGroups(); |
dslomov | 69c45f8 | 2017-12-14 11:15:43 -0500 | [diff] [blame] | 732 | if (!outputGroups.contains(OutputGroupInfo.FILES_TO_COMPILE) |
| 733 | && !outputGroups.contains(OutputGroupInfo.COMPILATION_PREREQUISITES) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 734 | && allTestTargets != null) { |
laszlocsomor | d237db7 | 2020-01-09 08:15:07 -0800 | [diff] [blame] | 735 | final boolean isExclusive = topLevelOptions.runTestsExclusively(); |
| 736 | ImmutableSet.Builder<ConfiguredTarget> targetsToTest = ImmutableSet.builder(); |
| 737 | ImmutableSet.Builder<ConfiguredTarget> targetsToTestExclusive = ImmutableSet.builder(); |
| 738 | for (ConfiguredTarget configuredTarget : allTestTargets) { |
| 739 | Target target = null; |
| 740 | try { |
| 741 | target = packageManager.getTarget(eventHandler, configuredTarget.getLabel()); |
| 742 | } catch (NoSuchTargetException | NoSuchPackageException e) { |
| 743 | eventHandler.handle(Event.error("Failed to get target when scheduling tests")); |
| 744 | continue; |
| 745 | } |
| 746 | if (target instanceof Rule) { |
| 747 | if (isExclusive || TargetUtils.isExclusiveTestRule((Rule) target)) { |
| 748 | targetsToTestExclusive.add(configuredTarget); |
| 749 | } else { |
| 750 | targetsToTest.add(configuredTarget); |
| 751 | } |
| 752 | } |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 753 | } |
laszlocsomor | d237db7 | 2020-01-09 08:15:07 -0800 | [diff] [blame] | 754 | return Pair.of(targetsToTest.build(), targetsToTestExclusive.build()); |
| 755 | } else { |
| 756 | return Pair.of(ImmutableSet.of(), ImmutableSet.of()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
Greg Estren | 7971e67 | 2016-06-01 23:22:48 +0000 | [diff] [blame] | 760 | /** |
Ulf Adams | a385d3e | 2015-09-28 13:21:45 +0000 | [diff] [blame] | 761 | * Sets the possible artifact roots in the artifact factory. This allows the factory to resolve |
| 762 | * paths with unknown roots to artifacts. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 763 | */ |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 764 | private void setArtifactRoots(PackageRoots packageRoots) { |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 765 | getArtifactFactory().setPackageRoots(packageRoots.getPackageRootLookup()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /** |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 769 | * Tests and clears the current thread's pending "interrupted" status, and |
| 770 | * throws InterruptedException iff it was set. |
| 771 | */ |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 772 | private final void pollInterruptedStatus() throws InterruptedException { |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 773 | if (Thread.interrupted()) { |
| 774 | throw new InterruptedException(); |
| 775 | } |
| 776 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 777 | } |