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