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