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 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.common.annotations.VisibleForTesting; |
| 18 | import com.google.common.base.Function; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Greg Estren | 7883bf1 | 2016-11-18 20:03:43 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ArrayListMultimap; |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 21 | import com.google.common.collect.HashMultimap; |
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; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.common.collect.Lists; |
Greg Estren | 7883bf1 | 2016-11-18 20:03:43 +0000 | [diff] [blame] | 26 | import com.google.common.collect.Multimap; |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 27 | import com.google.common.collect.SetMultimap; |
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; |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.actions.ActionAnalysisMetadata; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import com.google.devtools.build.lib.actions.ActionGraph; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.ActionLookupValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.Artifact; |
| 34 | import com.google.devtools.build.lib.actions.ArtifactFactory; |
Eric Fellheimer | 9c1e12f | 2015-08-06 20:23:55 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.ArtifactOwner; |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.PackageRoots; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 38 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection; |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 39 | import com.google.devtools.build.lib.analysis.constraints.TopLevelConstraintSemantics; |
ulfjack | ab21d18 | 2017-08-10 15:36:14 +0200 | [diff] [blame] | 40 | import com.google.devtools.build.lib.analysis.test.CoverageReportActionFactory; |
| 41 | import com.google.devtools.build.lib.analysis.test.CoverageReportActionFactory.CoverageReportActionsWrapper; |
| 42 | import com.google.devtools.build.lib.analysis.test.InstrumentedFilesProvider; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.cmdline.Label; |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | import com.google.devtools.build.lib.events.Event; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 48 | import com.google.devtools.build.lib.events.ExtendedEventHandler; |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.packages.AspectClass; |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.packages.AspectDescriptor; |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 51 | import com.google.devtools.build.lib.packages.AspectParameters; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 52 | import com.google.devtools.build.lib.packages.Attribute; |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 53 | import com.google.devtools.build.lib.packages.NativeAspectClass; |
Cal Peyser | 8a638d5 | 2017-03-24 15:44:24 +0000 | [diff] [blame] | 54 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 55 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | import com.google.devtools.build.lib.packages.Rule; |
| 57 | import com.google.devtools.build.lib.packages.Target; |
| 58 | import com.google.devtools.build.lib.packages.TargetUtils; |
nharmata | c4335cf | 2017-07-18 21:39:13 +0200 | [diff] [blame] | 59 | import com.google.devtools.build.lib.pkgcache.PackageManager.PackageManagerStatistics; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 60 | import com.google.devtools.build.lib.skyframe.AspectValue; |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 61 | import com.google.devtools.build.lib.skyframe.AspectValue.AspectKey; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 62 | import com.google.devtools.build.lib.skyframe.AspectValue.AspectValueKey; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey; |
| 64 | import com.google.devtools.build.lib.skyframe.CoverageReportValue; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 65 | import com.google.devtools.build.lib.skyframe.SkyframeAnalysisResult; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | import com.google.devtools.build.lib.skyframe.SkyframeBuildView; |
| 67 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor; |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame^] | 68 | import com.google.devtools.build.lib.skyframe.TargetPatternPhaseValue; |
Dmitry Lomov | 51aafc1 | 2016-11-18 14:02:54 +0000 | [diff] [blame] | 69 | import com.google.devtools.build.lib.syntax.SkylarkImport; |
| 70 | import com.google.devtools.build.lib.syntax.SkylarkImports; |
| 71 | import com.google.devtools.build.lib.syntax.SkylarkImports.SkylarkImportSyntaxException; |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 72 | import com.google.devtools.build.lib.util.Pair; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | import com.google.devtools.build.lib.util.RegexFilter; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | import com.google.devtools.build.skyframe.SkyKey; |
Eric Fellheimer | 9c1e12f | 2015-08-06 20:23:55 +0000 | [diff] [blame] | 75 | import com.google.devtools.build.skyframe.WalkableGraph; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 76 | import java.util.ArrayList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 77 | import java.util.Collection; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 78 | import java.util.HashSet; |
| 79 | import java.util.LinkedHashSet; |
| 80 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 81 | import java.util.Set; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | import java.util.logging.Logger; |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 83 | import java.util.stream.Collectors; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 84 | import javax.annotation.Nullable; |
| 85 | |
| 86 | /** |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 87 | * The BuildView presents a semantically-consistent and transitively-closed |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 88 | * dependency graph for some set of packages. |
| 89 | * |
| 90 | * <h2>Package design</h2> |
| 91 | * |
| 92 | * <p>This package contains the Blaze dependency analysis framework (aka |
| 93 | * "analysis phase"). The goal of this code is to perform semantic analysis of |
| 94 | * all of the build targets required for a given build, to report |
| 95 | * errors/warnings for any problems in the input, and to construct an "action |
| 96 | * graph" (see {@code lib.actions} package) correctly representing the work to |
| 97 | * be done during the execution phase of the build. |
| 98 | * |
| 99 | * <p><b>Configurations</b> the inputs to a build come from two sources: the |
| 100 | * intrinsic inputs, specified in the BUILD file, are called <em>targets</em>. |
| 101 | * The environmental inputs, coming from the build tool, the command-line, or |
| 102 | * configuration files, are called the <em>configuration</em>. Only when a |
| 103 | * target and a configuration are combined is there sufficient information to |
| 104 | * perform a build. </p> |
| 105 | * |
| 106 | * <p>Targets are implemented by the {@link Target} hierarchy in the {@code |
| 107 | * lib.packages} code. Configurations are implemented by {@link |
| 108 | * BuildConfiguration}. The pair of these together is represented by an |
| 109 | * instance of class {@link ConfiguredTarget}; this is the root of a hierarchy |
| 110 | * with different implementations for each kind of target: source file, derived |
| 111 | * file, rules, etc. |
| 112 | * |
| 113 | * <p>The framework code in this package (as opposed to its subpackages) is |
| 114 | * responsible for constructing the {@code ConfiguredTarget} graph for a given |
| 115 | * target and configuration, taking care of such issues as: |
| 116 | * <ul> |
| 117 | * <li>caching common subgraphs. |
| 118 | * <li>detecting and reporting cycles. |
| 119 | * <li>correct propagation of errors through the graph. |
| 120 | * <li>reporting universal errors, such as dependencies from production code |
| 121 | * to tests, or to experimental branches. |
| 122 | * <li>capturing and replaying errors. |
| 123 | * <li>maintaining the graph from one build to the next to |
| 124 | * avoid unnecessary recomputation. |
| 125 | * <li>checking software licenses. |
| 126 | * </ul> |
| 127 | * |
| 128 | * <p>See also {@link ConfiguredTarget} which documents some important |
| 129 | * invariants. |
| 130 | */ |
| 131 | public class BuildView { |
lberki | 998ec92 | 2017-09-05 10:08:25 +0200 | [diff] [blame] | 132 | private static final Logger logger = Logger.getLogger(BuildView.class.getName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 133 | |
| 134 | private final BlazeDirectories directories; |
| 135 | |
| 136 | private final SkyframeExecutor skyframeExecutor; |
| 137 | private final SkyframeBuildView skyframeBuildView; |
| 138 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 139 | private final ConfiguredRuleClassProvider ruleClassProvider; |
| 140 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 141 | /** |
| 142 | * A factory class to create the coverage report action. May be null. |
| 143 | */ |
| 144 | @Nullable private final CoverageReportActionFactory coverageReportActionFactory; |
| 145 | |
Ulf Adams | 59dbf68 | 2015-09-17 11:36:43 +0000 | [diff] [blame] | 146 | public BuildView(BlazeDirectories directories, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 147 | ConfiguredRuleClassProvider ruleClassProvider, |
| 148 | SkyframeExecutor skyframeExecutor, |
Ulf Adams | 5a96d53 | 2015-09-26 09:00:57 +0000 | [diff] [blame] | 149 | CoverageReportActionFactory coverageReportActionFactory) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 150 | this.directories = directories; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 151 | this.coverageReportActionFactory = coverageReportActionFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 152 | this.ruleClassProvider = ruleClassProvider; |
| 153 | this.skyframeExecutor = Preconditions.checkNotNull(skyframeExecutor); |
Ulf Adams | 89eefd7 | 2015-09-23 08:00:43 +0000 | [diff] [blame] | 154 | this.skyframeBuildView = skyframeExecutor.getSkyframeBuildView(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 155 | } |
| 156 | |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 157 | /** The number of targets freshly evaluated in the last analysis run. */ |
| 158 | public int getTargetsVisited() { |
| 159 | return skyframeBuildView.getEvaluatedTargetKeys().size(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 160 | } |
| 161 | |
tomlu | a0e8aae | 2018-06-19 15:07:26 -0700 | [diff] [blame] | 162 | public int getActionsConstructed() { |
| 163 | return skyframeBuildView.getEvaluatedActionCount(); |
| 164 | } |
| 165 | |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 166 | public PackageManagerStatistics getAndClearPkgManagerStatistics() { |
| 167 | return skyframeExecutor.getPackageManager().getAndClearStatistics(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 168 | } |
| 169 | |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 170 | private ArtifactFactory getArtifactFactory() { |
Ulf Adams | 89eefd7 | 2015-09-23 08:00:43 +0000 | [diff] [blame] | 171 | return skyframeBuildView.getArtifactFactory(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 172 | } |
| 173 | |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 174 | /** 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] | 175 | @VisibleForTesting |
mjhalupka | e253f83 | 2018-06-13 14:19:26 -0700 | [diff] [blame] | 176 | static LinkedHashSet<ConfiguredTarget> filterTestsByTargets( |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame^] | 177 | Collection<ConfiguredTarget> targets, Set<Label> allowedTargetLabels) { |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 178 | return targets |
| 179 | .stream() |
mjhalupka | e253f83 | 2018-06-13 14:19:26 -0700 | [diff] [blame] | 180 | .filter(ct -> allowedTargetLabels.contains(ct.getLabel())) |
| 181 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 184 | @ThreadCompatible |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 185 | public AnalysisResult update( |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame^] | 186 | TargetPatternPhaseValue loadingResult, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 187 | BuildConfigurationCollection configurations, |
| 188 | List<String> aspects, |
ulfjack | b5daae3 | 2018-06-13 07:59:33 -0700 | [diff] [blame] | 189 | AnalysisOptions viewOptions, |
juliexxia | 8a2baea | 2017-12-18 14:33:45 -0800 | [diff] [blame] | 190 | boolean keepGoing, |
juliexxia | f8b57c5 | 2017-12-21 11:37:46 -0800 | [diff] [blame] | 191 | int loadingPhaseThreads, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 192 | TopLevelArtifactContext topLevelOptions, |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 193 | ExtendedEventHandler eventHandler, |
Ulf Adams | 8c4ae67 | 2016-03-30 11:20:41 +0000 | [diff] [blame] | 194 | EventBus eventBus) |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 195 | throws ViewCreationFailedException, InterruptedException { |
lberki | 998ec92 | 2017-09-05 10:08:25 +0200 | [diff] [blame] | 196 | logger.info("Starting analysis"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 197 | pollInterruptedStatus(); |
| 198 | |
| 199 | skyframeBuildView.resetEvaluatedConfiguredTargetKeysSet(); |
tomlu | a0e8aae | 2018-06-19 15:07:26 -0700 | [diff] [blame] | 200 | skyframeBuildView.resetEvaluationActionCount(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 201 | |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame^] | 202 | // TODO(ulfjack): Expensive. Maybe we don't actually need the targets, only the labels? |
| 203 | Collection<Target> targets = |
| 204 | loadingResult.getTargets(eventHandler, skyframeExecutor.getPackageManager()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 205 | eventBus.post(new AnalysisPhaseStartedEvent(targets)); |
| 206 | |
tomlu | 7f22066 | 2018-04-27 16:07:24 -0700 | [diff] [blame] | 207 | skyframeBuildView.setConfigurations(eventHandler, configurations); |
Marian Lobur | 86bd4fd | 2015-09-16 10:01:38 +0000 | [diff] [blame] | 208 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 209 | // Determine the configurations. |
Greg Estren | 7971e67 | 2016-06-01 23:22:48 +0000 | [diff] [blame] | 210 | List<TargetAndConfiguration> topLevelTargetsWithConfigs = |
juliexxia | 8ee423d | 2017-10-18 16:36:57 -0400 | [diff] [blame] | 211 | AnalysisUtils.getTargetsWithConfigs( |
| 212 | configurations, targets, eventHandler, ruleClassProvider, skyframeExecutor); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 213 | |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 214 | // Report the generated association of targets to configurations |
| 215 | Multimap<Label, BuildConfiguration> byLabel = |
| 216 | ArrayListMultimap.<Label, BuildConfiguration>create(); |
| 217 | for (TargetAndConfiguration pair : topLevelTargetsWithConfigs) { |
| 218 | byLabel.put(pair.getLabel(), pair.getConfiguration()); |
| 219 | } |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 220 | for (Target target : targets) { |
| 221 | eventBus.post(new TargetConfiguredEvent(target, byLabel.get(target.getLabel()))); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 222 | } |
| 223 | |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 224 | List<ConfiguredTargetKey> topLevelCtKeys = |
| 225 | Lists.transform( |
| 226 | topLevelTargetsWithConfigs, |
| 227 | new Function<TargetAndConfiguration, ConfiguredTargetKey>() { |
| 228 | @Override |
| 229 | public ConfiguredTargetKey apply(TargetAndConfiguration node) { |
| 230 | return ConfiguredTargetKey.of(node.getLabel(), node.getConfiguration()); |
| 231 | } |
| 232 | }); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 233 | |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 234 | Multimap<Pair<Label, String>, BuildConfiguration> aspectConfigurations = |
| 235 | ArrayListMultimap.create(); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 236 | |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 237 | List<AspectValueKey> aspectKeys = new ArrayList<>(); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 238 | for (String aspect : aspects) { |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 239 | // Syntax: label%aspect |
| 240 | int delimiterPosition = aspect.indexOf('%'); |
| 241 | if (delimiterPosition >= 0) { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 242 | // TODO(jfield): For consistency with Skylark loads, the aspect should be specified |
laurentlb | 940dbc5 | 2018-03-01 08:07:14 -0800 | [diff] [blame] | 243 | // as an absolute label. |
| 244 | // 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] | 245 | String bzlFileLoadLikeString = aspect.substring(0, delimiterPosition); |
| 246 | if (!bzlFileLoadLikeString.startsWith("//") && !bzlFileLoadLikeString.startsWith("@")) { |
| 247 | // "Legacy" behavior of '--aspects' parameter. |
laurentlb | 940dbc5 | 2018-03-01 08:07:14 -0800 | [diff] [blame] | 248 | if (bzlFileLoadLikeString.startsWith("/")) { |
| 249 | bzlFileLoadLikeString = bzlFileLoadLikeString.substring(1); |
| 250 | } |
| 251 | int lastSlashPosition = bzlFileLoadLikeString.lastIndexOf('/'); |
| 252 | if (lastSlashPosition >= 0) { |
| 253 | bzlFileLoadLikeString = |
| 254 | "//" |
| 255 | + bzlFileLoadLikeString.substring(0, lastSlashPosition) |
| 256 | + ":" |
| 257 | + bzlFileLoadLikeString.substring(lastSlashPosition + 1); |
| 258 | } else { |
| 259 | bzlFileLoadLikeString = "//:" + bzlFileLoadLikeString; |
| 260 | } |
| 261 | if (!bzlFileLoadLikeString.endsWith(".bzl")) { |
| 262 | bzlFileLoadLikeString = bzlFileLoadLikeString + ".bzl"; |
Dmitry Lomov | 51aafc1 | 2016-11-18 14:02:54 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | SkylarkImport skylarkImport; |
| 266 | try { |
dannark | f4b9ff4 | 2018-06-12 09:28:45 -0700 | [diff] [blame] | 267 | skylarkImport = |
| 268 | SkylarkImports.create( |
| 269 | bzlFileLoadLikeString, /* repositoryMapping= */ ImmutableMap.of()); |
Dmitry Lomov | 51aafc1 | 2016-11-18 14:02:54 +0000 | [diff] [blame] | 270 | } catch (SkylarkImportSyntaxException e) { |
| 271 | throw new ViewCreationFailedException( |
| 272 | String.format("Invalid aspect '%s': %s", aspect, e.getMessage()), e); |
| 273 | } |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 274 | |
| 275 | String skylarkFunctionName = aspect.substring(delimiterPosition + 1); |
Dmitry Lomov | 01f85c0 | 2016-06-29 06:37:50 +0000 | [diff] [blame] | 276 | for (TargetAndConfiguration targetSpec : topLevelTargetsWithConfigs) { |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 277 | aspectConfigurations.put( |
| 278 | Pair.of(targetSpec.getLabel(), aspect), targetSpec.getConfiguration()); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 279 | aspectKeys.add( |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 280 | AspectValue.createSkylarkAspectKey( |
| 281 | targetSpec.getLabel(), |
Michael Staib | 04f6f24 | 2016-03-01 15:40:29 +0000 | [diff] [blame] | 282 | // For invoking top-level aspects, use the top-level configuration for both the |
| 283 | // aspect and the base target while the top-level configuration is untrimmed. |
| 284 | targetSpec.getConfiguration(), |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 285 | targetSpec.getConfiguration(), |
Dmitry Lomov | 51aafc1 | 2016-11-18 14:02:54 +0000 | [diff] [blame] | 286 | skylarkImport, |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 287 | skylarkFunctionName)); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 288 | } |
| 289 | } else { |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 290 | final NativeAspectClass aspectFactoryClass = |
| 291 | ruleClassProvider.getNativeAspectClassMap().get(aspect); |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 292 | |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 293 | if (aspectFactoryClass != null) { |
Dmitry Lomov | 01f85c0 | 2016-06-29 06:37:50 +0000 | [diff] [blame] | 294 | for (TargetAndConfiguration targetSpec : topLevelTargetsWithConfigs) { |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 295 | // For invoking top-level aspects, use the top-level configuration for both the |
| 296 | // aspect and the base target while the top-level configuration is untrimmed. |
| 297 | BuildConfiguration configuration = targetSpec.getConfiguration(); |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 298 | aspectConfigurations.put(Pair.of(targetSpec.getLabel(), aspect), configuration); |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 299 | aspectKeys.add( |
| 300 | AspectValue.createAspectKey( |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 301 | targetSpec.getLabel(), |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 302 | configuration, |
dslomov | fa50c3d | 2017-05-08 08:47:44 -0400 | [diff] [blame] | 303 | new AspectDescriptor(aspectFactoryClass, AspectParameters.EMPTY), |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 304 | configuration |
| 305 | )); |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 306 | } |
| 307 | } else { |
| 308 | throw new ViewCreationFailedException("Aspect '" + aspect + "' is unknown"); |
| 309 | } |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 313 | for (Pair<Label, String> target : aspectConfigurations.keys()) { |
| 314 | eventBus.post( |
| 315 | new AspectConfiguredEvent( |
| 316 | target.getFirst(), target.getSecond(), aspectConfigurations.get(target))); |
| 317 | } |
| 318 | |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 319 | SkyframeAnalysisResult skyframeAnalysisResult; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 320 | try { |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 321 | skyframeAnalysisResult = |
| 322 | skyframeBuildView.configureTargets( |
juliexxia | f8b57c5 | 2017-12-21 11:37:46 -0800 | [diff] [blame] | 323 | eventHandler, topLevelCtKeys, aspectKeys, eventBus, keepGoing, loadingPhaseThreads); |
Kristina Chodorow | 94e2742 | 2016-08-25 14:17:30 +0000 | [diff] [blame] | 324 | setArtifactRoots(skyframeAnalysisResult.getPackageRoots()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 325 | } finally { |
| 326 | skyframeBuildView.clearInvalidatedConfiguredTargets(); |
| 327 | } |
| 328 | |
Greg Estren | 7971e67 | 2016-06-01 23:22:48 +0000 | [diff] [blame] | 329 | int numTargetsToAnalyze = topLevelTargetsWithConfigs.size(); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 330 | int numSuccessful = skyframeAnalysisResult.getConfiguredTargets().size(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 331 | if (0 < numSuccessful && numSuccessful < numTargetsToAnalyze) { |
| 332 | String msg = String.format("Analysis succeeded for only %d of %d top-level targets", |
| 333 | numSuccessful, numTargetsToAnalyze); |
| 334 | eventHandler.handle(Event.info(msg)); |
lberki | 998ec92 | 2017-09-05 10:08:25 +0200 | [diff] [blame] | 335 | logger.info(msg); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 336 | } |
| 337 | |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 338 | Set<ConfiguredTarget> targetsToSkip = |
janakr | 171a7eb | 2018-03-26 09:26:53 -0700 | [diff] [blame] | 339 | new TopLevelConstraintSemantics( |
| 340 | skyframeExecutor.getPackageManager(), |
| 341 | input -> skyframeExecutor.getConfiguration(eventHandler, input), |
| 342 | eventHandler) |
gregce | 21fdc48 | 2017-12-05 16:10:47 -0800 | [diff] [blame] | 343 | .checkTargetEnvironmentRestrictions(skyframeAnalysisResult.getConfiguredTargets()); |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 344 | |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 345 | AnalysisResult result = |
| 346 | createResult( |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 347 | eventHandler, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 348 | loadingResult, |
ulfjack | 940fbd7 | 2018-06-15 03:23:58 -0700 | [diff] [blame] | 349 | configurations, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 350 | topLevelOptions, |
| 351 | viewOptions, |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 352 | skyframeAnalysisResult, |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 353 | targetsToSkip, |
| 354 | topLevelTargetsWithConfigs); |
lberki | 998ec92 | 2017-09-05 10:08:25 +0200 | [diff] [blame] | 355 | logger.info("Finished analysis"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 356 | return result; |
| 357 | } |
| 358 | |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 359 | private AnalysisResult createResult( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 360 | ExtendedEventHandler eventHandler, |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame^] | 361 | TargetPatternPhaseValue loadingResult, |
ulfjack | 940fbd7 | 2018-06-15 03:23:58 -0700 | [diff] [blame] | 362 | BuildConfigurationCollection configurations, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 363 | TopLevelArtifactContext topLevelOptions, |
ulfjack | b5daae3 | 2018-06-13 07:59:33 -0700 | [diff] [blame] | 364 | AnalysisOptions viewOptions, |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 365 | SkyframeAnalysisResult skyframeAnalysisResult, |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 366 | Set<ConfiguredTarget> targetsToSkip, |
| 367 | List<TargetAndConfiguration> topLevelTargetsWithConfigs) |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 368 | throws InterruptedException { |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame^] | 369 | Set<Label> testsToRun = loadingResult.getTestsToRunLabels(); |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 370 | Set<ConfiguredTarget> configuredTargets = |
| 371 | Sets.newLinkedHashSet(skyframeAnalysisResult.getConfiguredTargets()); |
Googler | 9204499 | 2018-03-14 14:26:30 -0700 | [diff] [blame] | 372 | ImmutableSet<AspectValue> aspects = ImmutableSet.copyOf(skyframeAnalysisResult.getAspects()); |
Dmitry Lomov | cf6021a | 2016-08-09 16:49:32 +0000 | [diff] [blame] | 373 | |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 374 | Set<ConfiguredTarget> allTargetsToTest = null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 375 | if (testsToRun != null) { |
| 376 | // 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] | 377 | allTargetsToTest = filterTestsByTargets(configuredTargets, testsToRun); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 378 | } |
| 379 | |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 380 | SetMultimap<Artifact, Label> topLevelArtifactsToOwnerLabels = HashMultimap.create(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 381 | Set<ConfiguredTarget> parallelTests = new HashSet<>(); |
| 382 | Set<ConfiguredTarget> exclusiveTests = new HashSet<>(); |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 383 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 384 | // build-info and build-changelist. |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 385 | Collection<Artifact> buildInfoArtifacts = |
| 386 | skyframeExecutor.getWorkspaceStatusArtifacts(eventHandler); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 387 | Preconditions.checkState(buildInfoArtifacts.size() == 2, buildInfoArtifacts); |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 388 | addArtifactsWithNoOwner(buildInfoArtifacts, topLevelArtifactsToOwnerLabels); |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 389 | |
| 390 | // Extra actions |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 391 | addExtraActionsIfRequested( |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 392 | viewOptions, configuredTargets, aspects, topLevelArtifactsToOwnerLabels, eventHandler); |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 393 | |
| 394 | // Coverage |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 395 | NestedSet<Artifact> baselineCoverageArtifacts = |
| 396 | getBaselineCoverageArtifacts(configuredTargets, topLevelArtifactsToOwnerLabels); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 397 | if (coverageReportActionFactory != null) { |
Googler | cdc90e9 | 2015-02-11 00:18:07 +0000 | [diff] [blame] | 398 | CoverageReportActionsWrapper actionsWrapper; |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 399 | actionsWrapper = |
| 400 | coverageReportActionFactory.createCoverageReportActionsWrapper( |
| 401 | eventHandler, |
| 402 | directories, |
| 403 | allTargetsToTest, |
| 404 | baselineCoverageArtifacts, |
| 405 | getArtifactFactory(), |
elenairina | 9438d10 | 2018-07-06 03:06:21 -0700 | [diff] [blame] | 406 | CoverageReportValue.COVERAGE_REPORT_KEY, |
| 407 | loadingResult.getWorkspaceName()); |
Googler | cdc90e9 | 2015-02-11 00:18:07 +0000 | [diff] [blame] | 408 | if (actionsWrapper != null) { |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 409 | ImmutableList<ActionAnalysisMetadata> actions = actionsWrapper.getActions(); |
Googler | cdc90e9 | 2015-02-11 00:18:07 +0000 | [diff] [blame] | 410 | skyframeExecutor.injectCoverageReportData(actions); |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 411 | addArtifactsWithNoOwner( |
| 412 | actionsWrapper.getCoverageOutputs(), topLevelArtifactsToOwnerLabels); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 416 | // Tests. This must come last, so that the exclusive tests are scheduled after everything else. |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 417 | scheduleTestsIfRequested( |
| 418 | parallelTests, |
| 419 | exclusiveTests, |
| 420 | topLevelOptions, |
| 421 | allTargetsToTest, |
| 422 | skyframeExecutor, |
| 423 | eventHandler); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 424 | |
Janak Ramakrishnan | 6f9d7d1 | 2016-08-09 08:45:34 +0000 | [diff] [blame] | 425 | String error = createErrorMessage(loadingResult, skyframeAnalysisResult); |
Eric Fellheimer | 9c1e12f | 2015-08-06 20:23:55 +0000 | [diff] [blame] | 426 | |
Ulf Adams | 53abece | 2016-02-03 08:30:07 +0000 | [diff] [blame] | 427 | final WalkableGraph graph = skyframeAnalysisResult.getWalkableGraph(); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 428 | final ActionGraph actionGraph = |
| 429 | new ActionGraph() { |
| 430 | @Nullable |
| 431 | @Override |
| 432 | public ActionAnalysisMetadata getGeneratingAction(Artifact artifact) { |
| 433 | ArtifactOwner artifactOwner = artifact.getArtifactOwner(); |
| 434 | if (artifactOwner instanceof ActionLookupValue.ActionLookupKey) { |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 435 | SkyKey key = (ActionLookupValue.ActionLookupKey) artifactOwner; |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 436 | ActionLookupValue val; |
| 437 | try { |
| 438 | val = (ActionLookupValue) graph.getValue(key); |
| 439 | } catch (InterruptedException e) { |
| 440 | throw new IllegalStateException( |
| 441 | "Interruption not expected from this graph: " + key, e); |
| 442 | } |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 443 | return val == null ? null : val.getGeneratingActionDangerousReadJavadoc(artifact); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 444 | } |
| 445 | return null; |
| 446 | } |
| 447 | }; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 448 | return new AnalysisResult( |
ulfjack | 940fbd7 | 2018-06-15 03:23:58 -0700 | [diff] [blame] | 449 | configurations, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 450 | configuredTargets, |
Dmitry Lomov | cf6021a | 2016-08-09 16:49:32 +0000 | [diff] [blame] | 451 | aspects, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 452 | allTargetsToTest, |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 453 | targetsToSkip, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 454 | error, |
| 455 | actionGraph, |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 456 | topLevelArtifactsToOwnerLabels, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 457 | parallelTests, |
| 458 | exclusiveTests, |
Marian Lobur | 86bd4fd | 2015-09-16 10:01:38 +0000 | [diff] [blame] | 459 | topLevelOptions, |
Kristina Chodorow | 36a6c17 | 2016-07-22 15:18:07 +0000 | [diff] [blame] | 460 | skyframeAnalysisResult.getPackageRoots(), |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 461 | loadingResult.getWorkspaceName(), |
| 462 | topLevelTargetsWithConfigs); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 463 | } |
| 464 | |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 465 | private static void addArtifactsWithNoOwner( |
| 466 | Collection<Artifact> artifacts, SetMultimap<Artifact, Label> topLevelArtifactsToOwnerLabels) { |
| 467 | artifacts.forEach((a) -> topLevelArtifactsToOwnerLabels.put(a, a.getOwnerLabel())); |
| 468 | } |
| 469 | |
Janak Ramakrishnan | 6f9d7d1 | 2016-08-09 08:45:34 +0000 | [diff] [blame] | 470 | @Nullable |
| 471 | public static String createErrorMessage( |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame^] | 472 | TargetPatternPhaseValue loadingResult, |
| 473 | @Nullable SkyframeAnalysisResult skyframeAnalysisResult) { |
| 474 | return loadingResult.hasError() |
Janak Ramakrishnan | 6f9d7d1 | 2016-08-09 08:45:34 +0000 | [diff] [blame] | 475 | ? "command succeeded, but there were errors parsing the target pattern" |
ulfjack | 12bb59f5 | 2018-07-25 07:19:24 -0700 | [diff] [blame^] | 476 | : loadingResult.hasPostExpansionError() |
Janak Ramakrishnan | 6f9d7d1 | 2016-08-09 08:45:34 +0000 | [diff] [blame] | 477 | || (skyframeAnalysisResult != null && skyframeAnalysisResult.hasLoadingError()) |
| 478 | ? "command succeeded, but there were loading phase errors" |
| 479 | : (skyframeAnalysisResult != null && skyframeAnalysisResult.hasAnalysisError()) |
| 480 | ? "command succeeded, but not all targets were analyzed" |
| 481 | : null; |
| 482 | } |
| 483 | |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 484 | private static NestedSet<Artifact> getBaselineCoverageArtifacts( |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 485 | Collection<ConfiguredTarget> configuredTargets, |
| 486 | SetMultimap<Artifact, Label> topLevelArtifactsToOwnerLabels) { |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 487 | NestedSetBuilder<Artifact> baselineCoverageArtifacts = NestedSetBuilder.stableOrder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 488 | for (ConfiguredTarget target : configuredTargets) { |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 489 | InstrumentedFilesProvider provider = target.getProvider(InstrumentedFilesProvider.class); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 490 | if (provider != null) { |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 491 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 492 | provider.getBaselineCoverageArtifacts(), |
| 493 | null, |
| 494 | target.getLabel(), |
| 495 | topLevelArtifactsToOwnerLabels); |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 496 | baselineCoverageArtifacts.addTransitive(provider.getBaselineCoverageArtifacts()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 497 | } |
| 498 | } |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 499 | return baselineCoverageArtifacts.build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 500 | } |
| 501 | |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 502 | private void addExtraActionsIfRequested( |
ulfjack | b5daae3 | 2018-06-13 07:59:33 -0700 | [diff] [blame] | 503 | AnalysisOptions viewOptions, |
Dmitry Lomov | cf6021a | 2016-08-09 16:49:32 +0000 | [diff] [blame] | 504 | Collection<ConfiguredTarget> configuredTargets, |
| 505 | Collection<AspectValue> aspects, |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 506 | SetMultimap<Artifact, Label> artifactsToTopLevelLabelsMap, |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 507 | ExtendedEventHandler eventHandler) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 508 | RegexFilter filter = viewOptions.extraActionFilter; |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 509 | for (ConfiguredTarget target : configuredTargets) { |
| 510 | ExtraActionArtifactsProvider provider = |
| 511 | target.getProvider(ExtraActionArtifactsProvider.class); |
| 512 | if (provider != null) { |
| 513 | if (viewOptions.extraActionTopLevelOnly) { |
lberki | 9137a75 | 2017-06-09 12:49:10 +0200 | [diff] [blame] | 514 | // Collect all aspect-classes that topLevel might inject. |
| 515 | Set<AspectClass> aspectClasses = new HashSet<>(); |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 516 | Target actualTarget = null; |
| 517 | try { |
| 518 | actualTarget = |
| 519 | skyframeExecutor.getPackageManager().getTarget(eventHandler, target.getLabel()); |
| 520 | } catch (NoSuchPackageException | NoSuchTargetException | InterruptedException e) { |
| 521 | eventHandler.handle(Event.error("")); |
| 522 | } |
| 523 | for (Attribute attr : actualTarget.getAssociatedRule().getAttributes()) { |
lberki | 9137a75 | 2017-06-09 12:49:10 +0200 | [diff] [blame] | 524 | aspectClasses.addAll(attr.getAspectClasses()); |
| 525 | } |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 526 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 527 | provider.getExtraActionArtifacts(), |
| 528 | filter, |
| 529 | target.getLabel(), |
| 530 | artifactsToTopLevelLabelsMap); |
lberki | 9137a75 | 2017-06-09 12:49:10 +0200 | [diff] [blame] | 531 | if (!aspectClasses.isEmpty()) { |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 532 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 533 | filterTransitiveExtraActions(provider, aspectClasses), |
| 534 | filter, |
| 535 | target.getLabel(), |
| 536 | artifactsToTopLevelLabelsMap); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 537 | } |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 538 | } else { |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 539 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 540 | provider.getTransitiveExtraActionArtifacts(), |
| 541 | filter, |
| 542 | target.getLabel(), |
| 543 | artifactsToTopLevelLabelsMap); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | } |
janakr | 75bc18a | 2018-07-13 00:52:17 -0700 | [diff] [blame] | 547 | for (AspectValue aspect : aspects) { |
| 548 | ExtraActionArtifactsProvider provider = |
| 549 | aspect.getConfiguredAspect().getProvider(ExtraActionArtifactsProvider.class); |
| 550 | if (provider != null) { |
| 551 | if (viewOptions.extraActionTopLevelOnly) { |
| 552 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 553 | provider.getExtraActionArtifacts(), |
| 554 | filter, |
| 555 | aspect.getLabel(), |
| 556 | artifactsToTopLevelLabelsMap); |
| 557 | } else { |
| 558 | TopLevelArtifactHelper.addArtifactsWithOwnerLabel( |
| 559 | provider.getTransitiveExtraActionArtifacts(), |
| 560 | filter, |
| 561 | aspect.getLabel(), |
| 562 | artifactsToTopLevelLabelsMap); |
| 563 | } |
| 564 | } |
| 565 | } |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Returns a list of actions from 'provider' that were registered by an aspect from |
| 570 | * 'aspectClasses'. All actions in 'provider' are considered - both direct and transitive. |
| 571 | */ |
| 572 | private ImmutableList<Artifact> filterTransitiveExtraActions( |
| 573 | ExtraActionArtifactsProvider provider, Set<AspectClass> aspectClasses) { |
| 574 | ImmutableList.Builder<Artifact> artifacts = ImmutableList.builder(); |
| 575 | // Add to 'artifacts' all extra-actions which were registered by aspects which 'topLevel' |
| 576 | // might have injected. |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 577 | for (Artifact artifact : provider.getTransitiveExtraActionArtifacts()) { |
| 578 | ArtifactOwner owner = artifact.getArtifactOwner(); |
| 579 | if (owner instanceof AspectKey) { |
| 580 | if (aspectClasses.contains(((AspectKey) owner).getAspectClass())) { |
| 581 | artifacts.add(artifact); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | } |
| 585 | return artifacts.build(); |
| 586 | } |
| 587 | |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 588 | private static void scheduleTestsIfRequested( |
| 589 | Collection<ConfiguredTarget> targetsToTest, |
| 590 | Collection<ConfiguredTarget> targetsToTestExclusive, |
| 591 | TopLevelArtifactContext topLevelOptions, |
| 592 | Collection<ConfiguredTarget> allTestTargets, |
| 593 | SkyframeExecutor skyframeExecutor, |
| 594 | ExtendedEventHandler eventHandler) |
| 595 | throws InterruptedException { |
Lukacs Berki | 3f4d4e9 | 2015-02-24 10:28:26 +0000 | [diff] [blame] | 596 | Set<String> outputGroups = topLevelOptions.outputGroups(); |
dslomov | 69c45f8 | 2017-12-14 11:15:43 -0500 | [diff] [blame] | 597 | if (!outputGroups.contains(OutputGroupInfo.FILES_TO_COMPILE) |
| 598 | && !outputGroups.contains(OutputGroupInfo.COMPILATION_PREREQUISITES) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 599 | && allTestTargets != null) { |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 600 | scheduleTests( |
| 601 | targetsToTest, |
| 602 | targetsToTestExclusive, |
| 603 | allTestTargets, |
| 604 | topLevelOptions.runTestsExclusively(), |
| 605 | skyframeExecutor, |
| 606 | eventHandler); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 610 | /** |
| 611 | * Returns set of artifacts representing test results, writing into targetsToTest and |
| 612 | * targetsToTestExclusive. |
| 613 | */ |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 614 | private static void scheduleTests( |
| 615 | Collection<ConfiguredTarget> targetsToTest, |
| 616 | Collection<ConfiguredTarget> targetsToTestExclusive, |
| 617 | Collection<ConfiguredTarget> allTestTargets, |
| 618 | boolean isExclusive, |
| 619 | SkyframeExecutor skyframeExecutor, |
| 620 | ExtendedEventHandler eventHandler) |
| 621 | throws InterruptedException { |
| 622 | for (ConfiguredTarget configuredTarget : allTestTargets) { |
| 623 | Target target = null; |
| 624 | try { |
| 625 | target = |
| 626 | skyframeExecutor |
| 627 | .getPackageManager() |
| 628 | .getTarget(eventHandler, configuredTarget.getLabel()); |
| 629 | } catch (NoSuchTargetException | NoSuchPackageException e) { |
| 630 | eventHandler.handle(Event.error("Failed to get target when scheduling tests")); |
| 631 | continue; |
| 632 | } |
| 633 | if (target instanceof Rule) { |
| 634 | boolean exclusive = isExclusive || TargetUtils.isExclusiveTestRule((Rule) target); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 635 | Collection<ConfiguredTarget> testCollection = exclusive |
| 636 | ? targetsToTestExclusive |
| 637 | : targetsToTest; |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 638 | testCollection.add(configuredTarget); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
Greg Estren | 7971e67 | 2016-06-01 23:22:48 +0000 | [diff] [blame] | 643 | /** |
Ulf Adams | a385d3e | 2015-09-28 13:21:45 +0000 | [diff] [blame] | 644 | * Sets the possible artifact roots in the artifact factory. This allows the factory to resolve |
| 645 | * paths with unknown roots to artifacts. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 646 | */ |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 647 | private void setArtifactRoots(PackageRoots packageRoots) { |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 648 | getArtifactFactory().setPackageRoots(packageRoots.getPackageRootLookup()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /** |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 652 | * Tests and clears the current thread's pending "interrupted" status, and |
| 653 | * throws InterruptedException iff it was set. |
| 654 | */ |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 655 | private final void pollInterruptedStatus() throws InterruptedException { |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 656 | if (Thread.interrupted()) { |
| 657 | throw new InterruptedException(); |
| 658 | } |
| 659 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 660 | } |