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 | |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 17 | import static com.google.common.collect.Iterables.concat; |
| 18 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.common.annotations.VisibleForTesting; |
| 20 | import com.google.common.base.Function; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 21 | import com.google.common.base.Preconditions; |
Greg Estren | 7883bf1 | 2016-11-18 20:03:43 +0000 | [diff] [blame] | 22 | import com.google.common.collect.ArrayListMultimap; |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Collections2; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableList; |
| 25 | import com.google.common.collect.ImmutableMap; |
Greg Estren | 0db9b11 | 2016-12-05 21:55:54 +0000 | [diff] [blame] | 26 | import com.google.common.collect.ImmutableMultimap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import com.google.common.collect.ImmutableSet; |
| 28 | import com.google.common.collect.Iterables; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | import com.google.common.collect.Lists; |
Greg Estren | 7883bf1 | 2016-11-18 20:03:43 +0000 | [diff] [blame] | 30 | import com.google.common.collect.Multimap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import com.google.common.collect.Sets; |
| 32 | import com.google.common.eventbus.EventBus; |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.ActionAnalysisMetadata; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.ActionGraph; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.ActionLookupValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.Artifact; |
| 37 | import com.google.devtools.build.lib.actions.ArtifactFactory; |
Eric Fellheimer | 9c1e12f | 2015-08-06 20:23:55 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.actions.ArtifactOwner; |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 39 | import com.google.devtools.build.lib.actions.PackageRoots; |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 42 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection; |
Greg Estren | 373e3e2 | 2016-08-09 22:36:51 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider; |
juliexxia | 8ee423d | 2017-10-18 16:36:57 -0400 | [diff] [blame] | 45 | import com.google.devtools.build.lib.analysis.config.ConfigurationResolver; |
janakr | a39a562 | 2018-01-24 08:43:35 -0800 | [diff] [blame] | 46 | import com.google.devtools.build.lib.analysis.config.FragmentClassSet; |
Greg Estren | 373e3e2 | 2016-08-09 22:36:51 +0000 | [diff] [blame] | 47 | import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; |
gregce | be55e11 | 2018-01-30 11:04:53 -0800 | [diff] [blame] | 48 | import com.google.devtools.build.lib.analysis.config.transitions.ConfigurationTransition; |
gregce | 7fa23ea | 2018-01-18 12:46:04 -0800 | [diff] [blame] | 49 | import com.google.devtools.build.lib.analysis.config.transitions.NoTransition; |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 50 | import com.google.devtools.build.lib.analysis.constraints.TopLevelConstraintSemantics; |
ulfjack | ab21d18 | 2017-08-10 15:36:14 +0200 | [diff] [blame] | 51 | import com.google.devtools.build.lib.analysis.test.CoverageReportActionFactory; |
| 52 | import com.google.devtools.build.lib.analysis.test.CoverageReportActionFactory.CoverageReportActionsWrapper; |
| 53 | import com.google.devtools.build.lib.analysis.test.InstrumentedFilesProvider; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 54 | import com.google.devtools.build.lib.cmdline.Label; |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 55 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 57 | import com.google.devtools.build.lib.collect.nestedset.Order; |
| 58 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | import com.google.devtools.build.lib.events.Event; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 60 | import com.google.devtools.build.lib.events.ExtendedEventHandler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 61 | import com.google.devtools.build.lib.events.StoredEventHandler; |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 62 | import com.google.devtools.build.lib.packages.AspectClass; |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 63 | import com.google.devtools.build.lib.packages.AspectDescriptor; |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 64 | import com.google.devtools.build.lib.packages.AspectParameters; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | import com.google.devtools.build.lib.packages.Attribute; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 66 | import com.google.devtools.build.lib.packages.BuildType; |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 67 | import com.google.devtools.build.lib.packages.NativeAspectClass; |
Cal Peyser | 8a638d5 | 2017-03-24 15:44:24 +0000 | [diff] [blame] | 68 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 69 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 70 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 71 | import com.google.devtools.build.lib.packages.PackageSpecification; |
cushon | 34ff85e | 2017-11-15 08:59:27 -0800 | [diff] [blame] | 72 | import com.google.devtools.build.lib.packages.PackageSpecification.PackageGroupContents; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | import com.google.devtools.build.lib.packages.RawAttributeMapper; |
| 74 | import com.google.devtools.build.lib.packages.Rule; |
Michael Staib | 2a67520 | 2017-03-20 18:06:48 +0000 | [diff] [blame] | 75 | import com.google.devtools.build.lib.packages.RuleTransitionFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | import com.google.devtools.build.lib.packages.Target; |
| 77 | import com.google.devtools.build.lib.packages.TargetUtils; |
Ulf Adams | d809936 | 2015-11-02 14:56:46 +0000 | [diff] [blame] | 78 | import com.google.devtools.build.lib.pkgcache.LoadingResult; |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 79 | import com.google.devtools.build.lib.pkgcache.PackageManager; |
nharmata | c4335cf | 2017-07-18 21:39:13 +0200 | [diff] [blame] | 80 | import com.google.devtools.build.lib.pkgcache.PackageManager.PackageManagerStatistics; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 81 | import com.google.devtools.build.lib.skyframe.AspectValue; |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 82 | import com.google.devtools.build.lib.skyframe.AspectValue.AspectKey; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 83 | import com.google.devtools.build.lib.skyframe.AspectValue.AspectValueKey; |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 84 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey; |
| 86 | import com.google.devtools.build.lib.skyframe.CoverageReportValue; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 87 | import com.google.devtools.build.lib.skyframe.SkyframeAnalysisResult; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 88 | import com.google.devtools.build.lib.skyframe.SkyframeBuildView; |
| 89 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor; |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 90 | import com.google.devtools.build.lib.skyframe.ToolchainUtil.ToolchainContextException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 91 | import com.google.devtools.build.lib.syntax.EvalException; |
Dmitry Lomov | 51aafc1 | 2016-11-18 14:02:54 +0000 | [diff] [blame] | 92 | import com.google.devtools.build.lib.syntax.SkylarkImport; |
| 93 | import com.google.devtools.build.lib.syntax.SkylarkImports; |
| 94 | import com.google.devtools.build.lib.syntax.SkylarkImports.SkylarkImportSyntaxException; |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 95 | import com.google.devtools.build.lib.util.OrderedSetMultimap; |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 96 | import com.google.devtools.build.lib.util.Pair; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | import com.google.devtools.build.lib.util.RegexFilter; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 98 | import com.google.devtools.build.skyframe.SkyKey; |
Eric Fellheimer | 9c1e12f | 2015-08-06 20:23:55 +0000 | [diff] [blame] | 99 | import com.google.devtools.build.skyframe.WalkableGraph; |
Janak Ramakrishnan | bede7b4 | 2016-11-17 18:34:08 +0000 | [diff] [blame] | 100 | import com.google.devtools.common.options.Converter; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | import com.google.devtools.common.options.Option; |
ccalvarin | 3bc1547 | 2017-06-27 17:58:35 +0200 | [diff] [blame] | 102 | import com.google.devtools.common.options.OptionDocumentationCategory; |
ccalvarin | c82a197 | 2017-07-17 21:13:39 +0200 | [diff] [blame] | 103 | import com.google.devtools.common.options.OptionEffectTag; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 104 | import com.google.devtools.common.options.OptionsBase; |
Janak Ramakrishnan | bede7b4 | 2016-11-17 18:34:08 +0000 | [diff] [blame] | 105 | import com.google.devtools.common.options.OptionsParsingException; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 106 | import java.util.ArrayList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 107 | import java.util.Collection; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 108 | import java.util.HashSet; |
Lukacs Berki | 53e0391 | 2016-05-19 16:26:09 +0000 | [diff] [blame] | 109 | import java.util.LinkedHashMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 110 | import java.util.LinkedHashSet; |
| 111 | import java.util.List; |
| 112 | import java.util.Map; |
| 113 | import java.util.Set; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 114 | import java.util.logging.Logger; |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 115 | import java.util.stream.Collectors; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 116 | import javax.annotation.Nullable; |
| 117 | |
| 118 | /** |
| 119 | * <p>The BuildView presents a semantically-consistent and transitively-closed |
| 120 | * dependency graph for some set of packages. |
| 121 | * |
| 122 | * <h2>Package design</h2> |
| 123 | * |
| 124 | * <p>This package contains the Blaze dependency analysis framework (aka |
| 125 | * "analysis phase"). The goal of this code is to perform semantic analysis of |
| 126 | * all of the build targets required for a given build, to report |
| 127 | * errors/warnings for any problems in the input, and to construct an "action |
| 128 | * graph" (see {@code lib.actions} package) correctly representing the work to |
| 129 | * be done during the execution phase of the build. |
| 130 | * |
| 131 | * <p><b>Configurations</b> the inputs to a build come from two sources: the |
| 132 | * intrinsic inputs, specified in the BUILD file, are called <em>targets</em>. |
| 133 | * The environmental inputs, coming from the build tool, the command-line, or |
| 134 | * configuration files, are called the <em>configuration</em>. Only when a |
| 135 | * target and a configuration are combined is there sufficient information to |
| 136 | * perform a build. </p> |
| 137 | * |
| 138 | * <p>Targets are implemented by the {@link Target} hierarchy in the {@code |
| 139 | * lib.packages} code. Configurations are implemented by {@link |
| 140 | * BuildConfiguration}. The pair of these together is represented by an |
| 141 | * instance of class {@link ConfiguredTarget}; this is the root of a hierarchy |
| 142 | * with different implementations for each kind of target: source file, derived |
| 143 | * file, rules, etc. |
| 144 | * |
| 145 | * <p>The framework code in this package (as opposed to its subpackages) is |
| 146 | * responsible for constructing the {@code ConfiguredTarget} graph for a given |
| 147 | * target and configuration, taking care of such issues as: |
| 148 | * <ul> |
| 149 | * <li>caching common subgraphs. |
| 150 | * <li>detecting and reporting cycles. |
| 151 | * <li>correct propagation of errors through the graph. |
| 152 | * <li>reporting universal errors, such as dependencies from production code |
| 153 | * to tests, or to experimental branches. |
| 154 | * <li>capturing and replaying errors. |
| 155 | * <li>maintaining the graph from one build to the next to |
| 156 | * avoid unnecessary recomputation. |
| 157 | * <li>checking software licenses. |
| 158 | * </ul> |
| 159 | * |
| 160 | * <p>See also {@link ConfiguredTarget} which documents some important |
| 161 | * invariants. |
| 162 | */ |
| 163 | public class BuildView { |
| 164 | |
| 165 | /** |
ccalvarin | 2eaa02e | 2017-04-17 23:37:46 +0200 | [diff] [blame] | 166 | * Options that affect the <i>mechanism</i> of analysis. These are distinct from {@link |
| 167 | * com.google.devtools.build.lib.analysis.config.BuildOptions}, which affect the <i>value</i> of a |
| 168 | * BuildConfiguration. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 169 | */ |
| 170 | public static class Options extends OptionsBase { |
Janak Ramakrishnan | bede7b4 | 2016-11-17 18:34:08 +0000 | [diff] [blame] | 171 | @Option( |
ccalvarin | 2eaa02e | 2017-04-17 23:37:46 +0200 | [diff] [blame] | 172 | name = "analysis_warnings_as_errors", |
| 173 | deprecationWarning = |
| 174 | "analysis_warnings_as_errors is now a no-op and will be removed in" |
| 175 | + " an upcoming Blaze release", |
| 176 | defaultValue = "false", |
| 177 | category = "strategy", |
Benjamin Peterson | 152e464 | 2018-01-25 08:20:20 -0800 | [diff] [blame] | 178 | documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, |
| 179 | effectTags = {OptionEffectTag.NO_OP}, |
ccalvarin | 2eaa02e | 2017-04-17 23:37:46 +0200 | [diff] [blame] | 180 | help = "Treat visible analysis warnings as errors." |
| 181 | ) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 182 | public boolean analysisWarningsAsErrors; |
| 183 | |
ccalvarin | 2eaa02e | 2017-04-17 23:37:46 +0200 | [diff] [blame] | 184 | @Option( |
| 185 | name = "discard_analysis_cache", |
| 186 | defaultValue = "false", |
| 187 | category = "strategy", |
ccalvarin | 3bc1547 | 2017-06-27 17:58:35 +0200 | [diff] [blame] | 188 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 189 | effectTags = {OptionEffectTag.UNKNOWN}, |
ccalvarin | 2eaa02e | 2017-04-17 23:37:46 +0200 | [diff] [blame] | 190 | help = |
| 191 | "Discard the analysis cache immediately after the analysis phase completes." |
| 192 | + " Reduces memory usage by ~10%, but makes further incremental builds slower." |
| 193 | ) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 194 | public boolean discardAnalysisCache; |
| 195 | |
ccalvarin | 2eaa02e | 2017-04-17 23:37:46 +0200 | [diff] [blame] | 196 | @Option( |
| 197 | name = "experimental_extra_action_filter", |
| 198 | defaultValue = "", |
| 199 | category = "experimental", |
| 200 | converter = RegexFilter.RegexFilterConverter.class, |
ccalvarin | 3bc1547 | 2017-06-27 17:58:35 +0200 | [diff] [blame] | 201 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 202 | effectTags = {OptionEffectTag.UNKNOWN}, |
ccalvarin | 2eaa02e | 2017-04-17 23:37:46 +0200 | [diff] [blame] | 203 | help = "Filters set of targets to schedule extra_actions for." |
| 204 | ) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 205 | public RegexFilter extraActionFilter; |
| 206 | |
ccalvarin | 2eaa02e | 2017-04-17 23:37:46 +0200 | [diff] [blame] | 207 | @Option( |
| 208 | name = "experimental_extra_action_top_level_only", |
| 209 | defaultValue = "false", |
| 210 | category = "experimental", |
ccalvarin | 3bc1547 | 2017-06-27 17:58:35 +0200 | [diff] [blame] | 211 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 212 | effectTags = {OptionEffectTag.UNKNOWN}, |
ccalvarin | 2eaa02e | 2017-04-17 23:37:46 +0200 | [diff] [blame] | 213 | help = "Only schedules extra_actions for top level targets." |
| 214 | ) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 215 | public boolean extraActionTopLevelOnly; |
| 216 | |
Carmi Grushko | 9a796de | 2016-11-13 12:22:30 +0000 | [diff] [blame] | 217 | @Option( |
ccalvarin | 2eaa02e | 2017-04-17 23:37:46 +0200 | [diff] [blame] | 218 | name = "version_window_for_dirty_node_gc", |
| 219 | defaultValue = "0", |
ccalvarin | 456adb2 | 2017-07-11 14:23:46 +0200 | [diff] [blame] | 220 | documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, |
ccalvarin | 3bc1547 | 2017-06-27 17:58:35 +0200 | [diff] [blame] | 221 | effectTags = {OptionEffectTag.UNKNOWN}, |
ccalvarin | 2eaa02e | 2017-04-17 23:37:46 +0200 | [diff] [blame] | 222 | help = |
| 223 | "Nodes that have been dirty for more than this many versions will be deleted" |
| 224 | + " from the graph upon the next update. Values must be non-negative long integers," |
| 225 | + " or -1 indicating the maximum possible window." |
| 226 | ) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 227 | public long versionWindowForDirtyNodeGc; |
Ulf Adams | ab64e59 | 2016-09-05 09:40:13 +0000 | [diff] [blame] | 228 | |
| 229 | @Deprecated |
| 230 | @Option( |
| 231 | name = "experimental_interleave_loading_and_analysis", |
| 232 | defaultValue = "true", |
| 233 | category = "experimental", |
ccalvarin | 3bc1547 | 2017-06-27 17:58:35 +0200 | [diff] [blame] | 234 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 235 | effectTags = {OptionEffectTag.UNKNOWN}, |
Ulf Adams | ab64e59 | 2016-09-05 09:40:13 +0000 | [diff] [blame] | 236 | help = "No-op." |
| 237 | ) |
| 238 | public boolean interleaveLoadingAndAnalysis; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 239 | } |
| 240 | |
lberki | 998ec92 | 2017-09-05 10:08:25 +0200 | [diff] [blame] | 241 | private static final Logger logger = Logger.getLogger(BuildView.class.getName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 242 | |
| 243 | private final BlazeDirectories directories; |
| 244 | |
| 245 | private final SkyframeExecutor skyframeExecutor; |
| 246 | private final SkyframeBuildView skyframeBuildView; |
| 247 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 248 | private final ConfiguredRuleClassProvider ruleClassProvider; |
| 249 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 250 | /** |
| 251 | * A factory class to create the coverage report action. May be null. |
| 252 | */ |
| 253 | @Nullable private final CoverageReportActionFactory coverageReportActionFactory; |
| 254 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 255 | @VisibleForTesting |
| 256 | public Set<SkyKey> getSkyframeEvaluatedTargetKeysForTesting() { |
| 257 | return skyframeBuildView.getEvaluatedTargetKeys(); |
| 258 | } |
| 259 | |
| 260 | /** The number of targets freshly evaluated in the last analysis run. */ |
| 261 | public int getTargetsVisited() { |
| 262 | return skyframeBuildView.getEvaluatedTargetKeys().size(); |
| 263 | } |
| 264 | |
nharmata | c4335cf | 2017-07-18 21:39:13 +0200 | [diff] [blame] | 265 | public PackageManagerStatistics getAndClearPkgManagerStatistics() { |
| 266 | return skyframeExecutor.getPackageManager().getAndClearStatistics(); |
| 267 | } |
| 268 | |
Ulf Adams | 59dbf68 | 2015-09-17 11:36:43 +0000 | [diff] [blame] | 269 | public BuildView(BlazeDirectories directories, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 270 | ConfiguredRuleClassProvider ruleClassProvider, |
| 271 | SkyframeExecutor skyframeExecutor, |
Ulf Adams | 5a96d53 | 2015-09-26 09:00:57 +0000 | [diff] [blame] | 272 | CoverageReportActionFactory coverageReportActionFactory) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 273 | this.directories = directories; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 274 | this.coverageReportActionFactory = coverageReportActionFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 275 | this.ruleClassProvider = ruleClassProvider; |
| 276 | this.skyframeExecutor = Preconditions.checkNotNull(skyframeExecutor); |
Ulf Adams | 89eefd7 | 2015-09-23 08:00:43 +0000 | [diff] [blame] | 277 | this.skyframeBuildView = skyframeExecutor.getSkyframeBuildView(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 280 | /** |
| 281 | * Returns whether the given configured target has errors. |
| 282 | */ |
| 283 | @VisibleForTesting |
| 284 | public boolean hasErrors(ConfiguredTarget configuredTarget) { |
| 285 | return configuredTarget == null; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Sets the configurations. Not thread-safe. DO NOT CALL except from tests! |
| 290 | */ |
| 291 | @VisibleForTesting |
Ulf Adams | 79f0521 | 2015-02-27 16:40:00 +0000 | [diff] [blame] | 292 | public void setConfigurationsForTesting(BuildConfigurationCollection configurations) { |
Ulf Adams | 003fe96 | 2015-09-26 09:51:39 +0000 | [diff] [blame] | 293 | skyframeBuildView.setConfigurations(configurations); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 296 | public ArtifactFactory getArtifactFactory() { |
Ulf Adams | 89eefd7 | 2015-09-23 08:00:43 +0000 | [diff] [blame] | 297 | return skyframeBuildView.getArtifactFactory(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | @VisibleForTesting |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 301 | WorkspaceStatusAction getLastWorkspaceBuildInfoActionForTesting() throws InterruptedException { |
janakr | c8dce99 | 2017-09-25 21:48:27 +0200 | [diff] [blame] | 302 | return skyframeExecutor.getLastWorkspaceStatusAction(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 303 | } |
| 304 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 305 | @Override |
| 306 | public int hashCode() { |
| 307 | throw new UnsupportedOperationException(); // avoid nondeterminism |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Return value for {@link BuildView#update} and {@code BuildTool.prepareToBuild}. |
| 312 | */ |
| 313 | public static final class AnalysisResult { |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 314 | private final ImmutableSet<ConfiguredTarget> targetsToBuild; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 315 | @Nullable private final ImmutableList<ConfiguredTarget> targetsToTest; |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 316 | private final ImmutableSet<ConfiguredTarget> targetsToSkip; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 317 | @Nullable private final String error; |
| 318 | private final ActionGraph actionGraph; |
| 319 | private final ImmutableSet<Artifact> artifactsToBuild; |
| 320 | private final ImmutableSet<ConfiguredTarget> parallelTests; |
| 321 | private final ImmutableSet<ConfiguredTarget> exclusiveTests; |
| 322 | @Nullable private final TopLevelArtifactContext topLevelContext; |
Googler | 9204499 | 2018-03-14 14:26:30 -0700 | [diff] [blame] | 323 | private final ImmutableSet<AspectValue> aspects; |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 324 | private final PackageRoots packageRoots; |
Kristina Chodorow | 36a6c17 | 2016-07-22 15:18:07 +0000 | [diff] [blame] | 325 | private final String workspaceName; |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 326 | List<TargetAndConfiguration> topLevelTargetsWithConfigs; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 327 | |
| 328 | private AnalysisResult( |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 329 | Collection<ConfiguredTarget> targetsToBuild, |
Googler | 9204499 | 2018-03-14 14:26:30 -0700 | [diff] [blame] | 330 | ImmutableSet<AspectValue> aspects, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 331 | Collection<ConfiguredTarget> targetsToTest, |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 332 | Collection<ConfiguredTarget> targetsToSkip, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 333 | @Nullable String error, |
| 334 | ActionGraph actionGraph, |
| 335 | Collection<Artifact> artifactsToBuild, |
| 336 | Collection<ConfiguredTarget> parallelTests, |
| 337 | Collection<ConfiguredTarget> exclusiveTests, |
Marian Lobur | 86bd4fd | 2015-09-16 10:01:38 +0000 | [diff] [blame] | 338 | TopLevelArtifactContext topLevelContext, |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 339 | PackageRoots packageRoots, |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 340 | String workspaceName, |
| 341 | List<TargetAndConfiguration> topLevelTargetsWithConfigs) { |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 342 | this.targetsToBuild = ImmutableSet.copyOf(targetsToBuild); |
Googler | 9204499 | 2018-03-14 14:26:30 -0700 | [diff] [blame] | 343 | this.aspects = aspects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 344 | this.targetsToTest = targetsToTest == null ? null : ImmutableList.copyOf(targetsToTest); |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 345 | this.targetsToSkip = ImmutableSet.copyOf(targetsToSkip); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 346 | this.error = error; |
| 347 | this.actionGraph = actionGraph; |
| 348 | this.artifactsToBuild = ImmutableSet.copyOf(artifactsToBuild); |
| 349 | this.parallelTests = ImmutableSet.copyOf(parallelTests); |
| 350 | this.exclusiveTests = ImmutableSet.copyOf(exclusiveTests); |
| 351 | this.topLevelContext = topLevelContext; |
Marian Lobur | 86bd4fd | 2015-09-16 10:01:38 +0000 | [diff] [blame] | 352 | this.packageRoots = packageRoots; |
Kristina Chodorow | 36a6c17 | 2016-07-22 15:18:07 +0000 | [diff] [blame] | 353 | this.workspaceName = workspaceName; |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 354 | this.topLevelTargetsWithConfigs = topLevelTargetsWithConfigs; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Returns configured targets to build. |
| 359 | */ |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 360 | public ImmutableSet<ConfiguredTarget> getTargetsToBuild() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 361 | return targetsToBuild; |
| 362 | } |
| 363 | |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 364 | /** @see PackageRoots */ |
| 365 | public PackageRoots getPackageRoots() { |
Marian Lobur | 86bd4fd | 2015-09-16 10:01:38 +0000 | [diff] [blame] | 366 | return packageRoots; |
| 367 | } |
| 368 | |
| 369 | /** |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 370 | * Returns aspects of configured targets to build. |
| 371 | * |
| 372 | * <p>If this list is empty, build the targets returned by {@code getTargetsToBuild()}. |
| 373 | * Otherwise, only build these aspects of the targets returned by {@code getTargetsToBuild()}. |
| 374 | */ |
Googler | 9204499 | 2018-03-14 14:26:30 -0700 | [diff] [blame] | 375 | public ImmutableSet<AspectValue> getAspects() { |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 376 | return aspects; |
| 377 | } |
| 378 | |
| 379 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 380 | * Returns the configured targets to run as tests, or {@code null} if testing was not |
| 381 | * requested (e.g. "build" command rather than "test" command). |
| 382 | */ |
| 383 | @Nullable |
| 384 | public Collection<ConfiguredTarget> getTargetsToTest() { |
| 385 | return targetsToTest; |
| 386 | } |
| 387 | |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 388 | /** |
| 389 | * Returns the configured targets that should not be executed because they're not |
| 390 | * platform-compatible with the current build. |
| 391 | * |
| 392 | * <p>For example: tests that aren't intended for the designated CPU. |
| 393 | */ |
| 394 | public ImmutableSet<ConfiguredTarget> getTargetsToSkip() { |
| 395 | return targetsToSkip; |
| 396 | } |
| 397 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 398 | public ImmutableSet<Artifact> getAdditionalArtifactsToBuild() { |
| 399 | return artifactsToBuild; |
| 400 | } |
| 401 | |
| 402 | public ImmutableSet<ConfiguredTarget> getExclusiveTests() { |
| 403 | return exclusiveTests; |
| 404 | } |
| 405 | |
| 406 | public ImmutableSet<ConfiguredTarget> getParallelTests() { |
| 407 | return parallelTests; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Returns an error description (if any). |
| 412 | */ |
| 413 | @Nullable public String getError() { |
| 414 | return error; |
| 415 | } |
| 416 | |
Ulf Adams | 988bb21 | 2015-08-25 12:51:48 +0000 | [diff] [blame] | 417 | public boolean hasError() { |
| 418 | return error != null; |
| 419 | } |
| 420 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 421 | /** |
| 422 | * Returns the action graph. |
| 423 | */ |
| 424 | public ActionGraph getActionGraph() { |
| 425 | return actionGraph; |
| 426 | } |
| 427 | |
| 428 | public TopLevelArtifactContext getTopLevelContext() { |
| 429 | return topLevelContext; |
| 430 | } |
Kristina Chodorow | 36a6c17 | 2016-07-22 15:18:07 +0000 | [diff] [blame] | 431 | |
| 432 | public String getWorkspaceName() { |
| 433 | return workspaceName; |
| 434 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 435 | |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 436 | public List<TargetAndConfiguration> getTopLevelTargetsWithConfigs() { |
| 437 | return topLevelTargetsWithConfigs; |
| 438 | } |
| 439 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 440 | |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 441 | /** 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] | 442 | @VisibleForTesting |
| 443 | static Iterable<? extends ConfiguredTarget> filterTestsByTargets( |
| 444 | Collection<? extends ConfiguredTarget> targets, |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 445 | final Set<? extends Target> allowedTargets, |
| 446 | ExtendedEventHandler eventHandler, |
| 447 | PackageManager packageManager) { |
| 448 | return targets |
| 449 | .stream() |
| 450 | .filter( |
| 451 | ct -> { |
| 452 | Target target = null; |
| 453 | try { |
| 454 | target = packageManager.getTarget(eventHandler, ct.getLabel()); |
| 455 | } catch (NoSuchTargetException | NoSuchPackageException | InterruptedException e) { |
| 456 | eventHandler.handle( |
| 457 | Event.error("Failed to get target from package when filtering.")); |
| 458 | return false; |
| 459 | } |
| 460 | return allowedTargets.contains(target); |
| 461 | }) |
| 462 | .collect(Collectors.toSet()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 465 | @ThreadCompatible |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 466 | public AnalysisResult update( |
| 467 | LoadingResult loadingResult, |
| 468 | BuildConfigurationCollection configurations, |
| 469 | List<String> aspects, |
| 470 | Options viewOptions, |
juliexxia | 8a2baea | 2017-12-18 14:33:45 -0800 | [diff] [blame] | 471 | boolean keepGoing, |
juliexxia | f8b57c5 | 2017-12-21 11:37:46 -0800 | [diff] [blame] | 472 | int loadingPhaseThreads, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 473 | TopLevelArtifactContext topLevelOptions, |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 474 | ExtendedEventHandler eventHandler, |
Ulf Adams | 8c4ae67 | 2016-03-30 11:20:41 +0000 | [diff] [blame] | 475 | EventBus eventBus) |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 476 | throws ViewCreationFailedException, InterruptedException { |
lberki | 998ec92 | 2017-09-05 10:08:25 +0200 | [diff] [blame] | 477 | logger.info("Starting analysis"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 478 | pollInterruptedStatus(); |
| 479 | |
| 480 | skyframeBuildView.resetEvaluatedConfiguredTargetKeysSet(); |
| 481 | |
| 482 | Collection<Target> targets = loadingResult.getTargets(); |
| 483 | eventBus.post(new AnalysisPhaseStartedEvent(targets)); |
| 484 | |
Ulf Adams | 003fe96 | 2015-09-26 09:51:39 +0000 | [diff] [blame] | 485 | skyframeBuildView.setConfigurations(configurations); |
Marian Lobur | 86bd4fd | 2015-09-16 10:01:38 +0000 | [diff] [blame] | 486 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 487 | // Determine the configurations. |
Greg Estren | 7971e67 | 2016-06-01 23:22:48 +0000 | [diff] [blame] | 488 | List<TargetAndConfiguration> topLevelTargetsWithConfigs = |
juliexxia | 8ee423d | 2017-10-18 16:36:57 -0400 | [diff] [blame] | 489 | AnalysisUtils.getTargetsWithConfigs( |
| 490 | configurations, targets, eventHandler, ruleClassProvider, skyframeExecutor); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 491 | |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 492 | // Report the generated association of targets to configurations |
| 493 | Multimap<Label, BuildConfiguration> byLabel = |
| 494 | ArrayListMultimap.<Label, BuildConfiguration>create(); |
| 495 | for (TargetAndConfiguration pair : topLevelTargetsWithConfigs) { |
| 496 | byLabel.put(pair.getLabel(), pair.getConfiguration()); |
| 497 | } |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 498 | for (Target target : targets) { |
| 499 | eventBus.post(new TargetConfiguredEvent(target, byLabel.get(target.getLabel()))); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 500 | } |
| 501 | |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 502 | List<ConfiguredTargetKey> topLevelCtKeys = |
| 503 | Lists.transform( |
| 504 | topLevelTargetsWithConfigs, |
| 505 | new Function<TargetAndConfiguration, ConfiguredTargetKey>() { |
| 506 | @Override |
| 507 | public ConfiguredTargetKey apply(TargetAndConfiguration node) { |
| 508 | return ConfiguredTargetKey.of(node.getLabel(), node.getConfiguration()); |
| 509 | } |
| 510 | }); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 511 | |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 512 | Multimap<Pair<Label, String>, BuildConfiguration> aspectConfigurations = |
| 513 | ArrayListMultimap.create(); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 514 | |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 515 | List<AspectValueKey> aspectKeys = new ArrayList<>(); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 516 | for (String aspect : aspects) { |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 517 | // Syntax: label%aspect |
| 518 | int delimiterPosition = aspect.indexOf('%'); |
| 519 | if (delimiterPosition >= 0) { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 520 | // TODO(jfield): For consistency with Skylark loads, the aspect should be specified |
laurentlb | 940dbc5 | 2018-03-01 08:07:14 -0800 | [diff] [blame] | 521 | // as an absolute label. |
| 522 | // 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] | 523 | String bzlFileLoadLikeString = aspect.substring(0, delimiterPosition); |
| 524 | if (!bzlFileLoadLikeString.startsWith("//") && !bzlFileLoadLikeString.startsWith("@")) { |
| 525 | // "Legacy" behavior of '--aspects' parameter. |
laurentlb | 940dbc5 | 2018-03-01 08:07:14 -0800 | [diff] [blame] | 526 | if (bzlFileLoadLikeString.startsWith("/")) { |
| 527 | bzlFileLoadLikeString = bzlFileLoadLikeString.substring(1); |
| 528 | } |
| 529 | int lastSlashPosition = bzlFileLoadLikeString.lastIndexOf('/'); |
| 530 | if (lastSlashPosition >= 0) { |
| 531 | bzlFileLoadLikeString = |
| 532 | "//" |
| 533 | + bzlFileLoadLikeString.substring(0, lastSlashPosition) |
| 534 | + ":" |
| 535 | + bzlFileLoadLikeString.substring(lastSlashPosition + 1); |
| 536 | } else { |
| 537 | bzlFileLoadLikeString = "//:" + bzlFileLoadLikeString; |
| 538 | } |
| 539 | if (!bzlFileLoadLikeString.endsWith(".bzl")) { |
| 540 | bzlFileLoadLikeString = bzlFileLoadLikeString + ".bzl"; |
Dmitry Lomov | 51aafc1 | 2016-11-18 14:02:54 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | SkylarkImport skylarkImport; |
| 544 | try { |
| 545 | skylarkImport = SkylarkImports.create(bzlFileLoadLikeString); |
| 546 | } catch (SkylarkImportSyntaxException e) { |
| 547 | throw new ViewCreationFailedException( |
| 548 | String.format("Invalid aspect '%s': %s", aspect, e.getMessage()), e); |
| 549 | } |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 550 | |
| 551 | String skylarkFunctionName = aspect.substring(delimiterPosition + 1); |
Dmitry Lomov | 01f85c0 | 2016-06-29 06:37:50 +0000 | [diff] [blame] | 552 | for (TargetAndConfiguration targetSpec : topLevelTargetsWithConfigs) { |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 553 | aspectConfigurations.put( |
| 554 | Pair.of(targetSpec.getLabel(), aspect), targetSpec.getConfiguration()); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 555 | aspectKeys.add( |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 556 | AspectValue.createSkylarkAspectKey( |
| 557 | targetSpec.getLabel(), |
Michael Staib | 04f6f24 | 2016-03-01 15:40:29 +0000 | [diff] [blame] | 558 | // For invoking top-level aspects, use the top-level configuration for both the |
| 559 | // aspect and the base target while the top-level configuration is untrimmed. |
| 560 | targetSpec.getConfiguration(), |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 561 | targetSpec.getConfiguration(), |
Dmitry Lomov | 51aafc1 | 2016-11-18 14:02:54 +0000 | [diff] [blame] | 562 | skylarkImport, |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 563 | skylarkFunctionName)); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 564 | } |
| 565 | } else { |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 566 | final NativeAspectClass aspectFactoryClass = |
| 567 | ruleClassProvider.getNativeAspectClassMap().get(aspect); |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 568 | |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 569 | if (aspectFactoryClass != null) { |
Dmitry Lomov | 01f85c0 | 2016-06-29 06:37:50 +0000 | [diff] [blame] | 570 | for (TargetAndConfiguration targetSpec : topLevelTargetsWithConfigs) { |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 571 | // For invoking top-level aspects, use the top-level configuration for both the |
| 572 | // aspect and the base target while the top-level configuration is untrimmed. |
| 573 | BuildConfiguration configuration = targetSpec.getConfiguration(); |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 574 | aspectConfigurations.put(Pair.of(targetSpec.getLabel(), aspect), configuration); |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 575 | aspectKeys.add( |
| 576 | AspectValue.createAspectKey( |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 577 | targetSpec.getLabel(), |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 578 | configuration, |
dslomov | fa50c3d | 2017-05-08 08:47:44 -0400 | [diff] [blame] | 579 | new AspectDescriptor(aspectFactoryClass, AspectParameters.EMPTY), |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 580 | configuration |
| 581 | )); |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 582 | } |
| 583 | } else { |
| 584 | throw new ViewCreationFailedException("Aspect '" + aspect + "' is unknown"); |
| 585 | } |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
Klaus Aehlig | c27b4da | 2017-09-01 10:33:22 +0200 | [diff] [blame] | 589 | for (Pair<Label, String> target : aspectConfigurations.keys()) { |
| 590 | eventBus.post( |
| 591 | new AspectConfiguredEvent( |
| 592 | target.getFirst(), target.getSecond(), aspectConfigurations.get(target))); |
| 593 | } |
| 594 | |
janakr | c8dce99 | 2017-09-25 21:48:27 +0200 | [diff] [blame] | 595 | skyframeExecutor.maybeInvalidateWorkspaceStatusValue(loadingResult.getWorkspaceName()); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 596 | SkyframeAnalysisResult skyframeAnalysisResult; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 597 | try { |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 598 | skyframeAnalysisResult = |
| 599 | skyframeBuildView.configureTargets( |
juliexxia | f8b57c5 | 2017-12-21 11:37:46 -0800 | [diff] [blame] | 600 | eventHandler, topLevelCtKeys, aspectKeys, eventBus, keepGoing, loadingPhaseThreads); |
Kristina Chodorow | 94e2742 | 2016-08-25 14:17:30 +0000 | [diff] [blame] | 601 | setArtifactRoots(skyframeAnalysisResult.getPackageRoots()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 602 | } finally { |
| 603 | skyframeBuildView.clearInvalidatedConfiguredTargets(); |
| 604 | } |
| 605 | |
Greg Estren | 7971e67 | 2016-06-01 23:22:48 +0000 | [diff] [blame] | 606 | int numTargetsToAnalyze = topLevelTargetsWithConfigs.size(); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 607 | int numSuccessful = skyframeAnalysisResult.getConfiguredTargets().size(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 608 | if (0 < numSuccessful && numSuccessful < numTargetsToAnalyze) { |
| 609 | String msg = String.format("Analysis succeeded for only %d of %d top-level targets", |
| 610 | numSuccessful, numTargetsToAnalyze); |
| 611 | eventHandler.handle(Event.info(msg)); |
lberki | 998ec92 | 2017-09-05 10:08:25 +0200 | [diff] [blame] | 612 | logger.info(msg); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 613 | } |
| 614 | |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 615 | Set<ConfiguredTarget> targetsToSkip = |
gregce | 21fdc48 | 2017-12-05 16:10:47 -0800 | [diff] [blame] | 616 | new TopLevelConstraintSemantics(skyframeExecutor.getPackageManager(), eventHandler) |
| 617 | .checkTargetEnvironmentRestrictions(skyframeAnalysisResult.getConfiguredTargets()); |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 618 | |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 619 | AnalysisResult result = |
| 620 | createResult( |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 621 | eventHandler, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 622 | loadingResult, |
| 623 | topLevelOptions, |
| 624 | viewOptions, |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 625 | skyframeAnalysisResult, |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 626 | targetsToSkip, |
| 627 | topLevelTargetsWithConfigs); |
lberki | 998ec92 | 2017-09-05 10:08:25 +0200 | [diff] [blame] | 628 | logger.info("Finished analysis"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 629 | return result; |
| 630 | } |
| 631 | |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 632 | private AnalysisResult createResult( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 633 | ExtendedEventHandler eventHandler, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 634 | LoadingResult loadingResult, |
| 635 | TopLevelArtifactContext topLevelOptions, |
| 636 | BuildView.Options viewOptions, |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 637 | SkyframeAnalysisResult skyframeAnalysisResult, |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 638 | Set<ConfiguredTarget> targetsToSkip, |
| 639 | List<TargetAndConfiguration> topLevelTargetsWithConfigs) |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 640 | throws InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 641 | Collection<Target> testsToRun = loadingResult.getTestsToRun(); |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 642 | Set<ConfiguredTarget> configuredTargets = |
| 643 | Sets.newLinkedHashSet(skyframeAnalysisResult.getConfiguredTargets()); |
Googler | 9204499 | 2018-03-14 14:26:30 -0700 | [diff] [blame] | 644 | ImmutableSet<AspectValue> aspects = ImmutableSet.copyOf(skyframeAnalysisResult.getAspects()); |
Dmitry Lomov | cf6021a | 2016-08-09 16:49:32 +0000 | [diff] [blame] | 645 | |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 646 | Set<ConfiguredTarget> allTargetsToTest = null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 647 | if (testsToRun != null) { |
| 648 | // Determine the subset of configured targets that are meant to be run as tests. |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 649 | allTargetsToTest = |
| 650 | Sets.newLinkedHashSet( |
| 651 | filterTestsByTargets( |
| 652 | configuredTargets, |
| 653 | Sets.newHashSet(testsToRun), |
| 654 | eventHandler, |
| 655 | skyframeExecutor.getPackageManager())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 656 | } |
| 657 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 658 | Set<Artifact> artifactsToBuild = new HashSet<>(); |
| 659 | Set<ConfiguredTarget> parallelTests = new HashSet<>(); |
| 660 | Set<ConfiguredTarget> exclusiveTests = new HashSet<>(); |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 661 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 662 | // build-info and build-changelist. |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 663 | Collection<Artifact> buildInfoArtifacts = |
| 664 | skyframeExecutor.getWorkspaceStatusArtifacts(eventHandler); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 665 | Preconditions.checkState(buildInfoArtifacts.size() == 2, buildInfoArtifacts); |
| 666 | artifactsToBuild.addAll(buildInfoArtifacts); |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 667 | |
| 668 | // Extra actions |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 669 | addExtraActionsIfRequested( |
| 670 | viewOptions, configuredTargets, aspects, artifactsToBuild, eventHandler); |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 671 | |
| 672 | // Coverage |
| 673 | NestedSet<Artifact> baselineCoverageArtifacts = getBaselineCoverageArtifacts(configuredTargets); |
| 674 | Iterables.addAll(artifactsToBuild, baselineCoverageArtifacts); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 675 | if (coverageReportActionFactory != null) { |
Googler | cdc90e9 | 2015-02-11 00:18:07 +0000 | [diff] [blame] | 676 | CoverageReportActionsWrapper actionsWrapper; |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 677 | actionsWrapper = |
| 678 | coverageReportActionFactory.createCoverageReportActionsWrapper( |
| 679 | eventHandler, |
| 680 | directories, |
| 681 | allTargetsToTest, |
| 682 | baselineCoverageArtifacts, |
| 683 | getArtifactFactory(), |
| 684 | CoverageReportValue.COVERAGE_REPORT_KEY); |
Googler | cdc90e9 | 2015-02-11 00:18:07 +0000 | [diff] [blame] | 685 | if (actionsWrapper != null) { |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 686 | ImmutableList<ActionAnalysisMetadata> actions = actionsWrapper.getActions(); |
Googler | cdc90e9 | 2015-02-11 00:18:07 +0000 | [diff] [blame] | 687 | skyframeExecutor.injectCoverageReportData(actions); |
| 688 | artifactsToBuild.addAll(actionsWrapper.getCoverageOutputs()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 692 | // 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] | 693 | scheduleTestsIfRequested( |
| 694 | parallelTests, |
| 695 | exclusiveTests, |
| 696 | topLevelOptions, |
| 697 | allTargetsToTest, |
| 698 | skyframeExecutor, |
| 699 | eventHandler); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 700 | |
Janak Ramakrishnan | 6f9d7d1 | 2016-08-09 08:45:34 +0000 | [diff] [blame] | 701 | String error = createErrorMessage(loadingResult, skyframeAnalysisResult); |
Eric Fellheimer | 9c1e12f | 2015-08-06 20:23:55 +0000 | [diff] [blame] | 702 | |
Ulf Adams | 53abece | 2016-02-03 08:30:07 +0000 | [diff] [blame] | 703 | final WalkableGraph graph = skyframeAnalysisResult.getWalkableGraph(); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 704 | final ActionGraph actionGraph = |
| 705 | new ActionGraph() { |
| 706 | @Nullable |
| 707 | @Override |
| 708 | public ActionAnalysisMetadata getGeneratingAction(Artifact artifact) { |
| 709 | ArtifactOwner artifactOwner = artifact.getArtifactOwner(); |
| 710 | if (artifactOwner instanceof ActionLookupValue.ActionLookupKey) { |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 711 | SkyKey key = (ActionLookupValue.ActionLookupKey) artifactOwner; |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 712 | ActionLookupValue val; |
| 713 | try { |
| 714 | val = (ActionLookupValue) graph.getValue(key); |
| 715 | } catch (InterruptedException e) { |
| 716 | throw new IllegalStateException( |
| 717 | "Interruption not expected from this graph: " + key, e); |
| 718 | } |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 719 | return val == null ? null : val.getGeneratingActionDangerousReadJavadoc(artifact); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 720 | } |
| 721 | return null; |
| 722 | } |
| 723 | }; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 724 | return new AnalysisResult( |
| 725 | configuredTargets, |
Dmitry Lomov | cf6021a | 2016-08-09 16:49:32 +0000 | [diff] [blame] | 726 | aspects, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 727 | allTargetsToTest, |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 728 | targetsToSkip, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 729 | error, |
| 730 | actionGraph, |
| 731 | artifactsToBuild, |
| 732 | parallelTests, |
| 733 | exclusiveTests, |
Marian Lobur | 86bd4fd | 2015-09-16 10:01:38 +0000 | [diff] [blame] | 734 | topLevelOptions, |
Kristina Chodorow | 36a6c17 | 2016-07-22 15:18:07 +0000 | [diff] [blame] | 735 | skyframeAnalysisResult.getPackageRoots(), |
juliexxia | 693abdc | 2018-01-03 08:59:18 -0800 | [diff] [blame] | 736 | loadingResult.getWorkspaceName(), |
| 737 | topLevelTargetsWithConfigs); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 738 | } |
| 739 | |
Janak Ramakrishnan | 6f9d7d1 | 2016-08-09 08:45:34 +0000 | [diff] [blame] | 740 | @Nullable |
| 741 | public static String createErrorMessage( |
| 742 | LoadingResult loadingResult, @Nullable SkyframeAnalysisResult skyframeAnalysisResult) { |
| 743 | return loadingResult.hasTargetPatternError() |
| 744 | ? "command succeeded, but there were errors parsing the target pattern" |
| 745 | : loadingResult.hasLoadingError() |
| 746 | || (skyframeAnalysisResult != null && skyframeAnalysisResult.hasLoadingError()) |
| 747 | ? "command succeeded, but there were loading phase errors" |
| 748 | : (skyframeAnalysisResult != null && skyframeAnalysisResult.hasAnalysisError()) |
| 749 | ? "command succeeded, but not all targets were analyzed" |
| 750 | : null; |
| 751 | } |
| 752 | |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 753 | private static NestedSet<Artifact> getBaselineCoverageArtifacts( |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 754 | Collection<ConfiguredTarget> configuredTargets) { |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 755 | NestedSetBuilder<Artifact> baselineCoverageArtifacts = NestedSetBuilder.stableOrder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 756 | for (ConfiguredTarget target : configuredTargets) { |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 757 | InstrumentedFilesProvider provider = target.getProvider(InstrumentedFilesProvider.class); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 758 | if (provider != null) { |
Ulf Adams | fc154ae | 2015-08-31 12:32:14 +0000 | [diff] [blame] | 759 | baselineCoverageArtifacts.addTransitive(provider.getBaselineCoverageArtifacts()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 760 | } |
| 761 | } |
Lukacs Berki | 6916be2 | 2015-02-19 13:36:06 +0000 | [diff] [blame] | 762 | return baselineCoverageArtifacts.build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 763 | } |
| 764 | |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 765 | private void addExtraActionsIfRequested( |
| 766 | Options viewOptions, |
Dmitry Lomov | cf6021a | 2016-08-09 16:49:32 +0000 | [diff] [blame] | 767 | Collection<ConfiguredTarget> configuredTargets, |
| 768 | Collection<AspectValue> aspects, |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 769 | Set<Artifact> artifactsToBuild, |
| 770 | ExtendedEventHandler eventHandler) { |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 771 | Iterable<Artifact> extraActionArtifacts = |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 772 | concat( |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 773 | addExtraActionsFromTargets(viewOptions, configuredTargets, eventHandler), |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 774 | addExtraActionsFromAspects(viewOptions, aspects)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 775 | |
| 776 | RegexFilter filter = viewOptions.extraActionFilter; |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 777 | for (Artifact artifact : extraActionArtifacts) { |
| 778 | boolean filterMatches = |
| 779 | filter == null || filter.isIncluded(artifact.getOwnerLabel().toString()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 780 | if (filterMatches) { |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 781 | artifactsToBuild.add(artifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 786 | private NestedSet<Artifact> addExtraActionsFromTargets( |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 787 | BuildView.Options viewOptions, |
| 788 | Collection<ConfiguredTarget> configuredTargets, |
| 789 | ExtendedEventHandler eventHandler) { |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 790 | NestedSetBuilder<Artifact> builder = NestedSetBuilder.stableOrder(); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 791 | for (ConfiguredTarget target : configuredTargets) { |
| 792 | ExtraActionArtifactsProvider provider = |
| 793 | target.getProvider(ExtraActionArtifactsProvider.class); |
| 794 | if (provider != null) { |
| 795 | if (viewOptions.extraActionTopLevelOnly) { |
lberki | 9137a75 | 2017-06-09 12:49:10 +0200 | [diff] [blame] | 796 | // Collect all aspect-classes that topLevel might inject. |
| 797 | Set<AspectClass> aspectClasses = new HashSet<>(); |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 798 | Target actualTarget = null; |
| 799 | try { |
| 800 | actualTarget = |
| 801 | skyframeExecutor.getPackageManager().getTarget(eventHandler, target.getLabel()); |
| 802 | } catch (NoSuchPackageException | NoSuchTargetException | InterruptedException e) { |
| 803 | eventHandler.handle(Event.error("")); |
| 804 | } |
| 805 | for (Attribute attr : actualTarget.getAssociatedRule().getAttributes()) { |
lberki | 9137a75 | 2017-06-09 12:49:10 +0200 | [diff] [blame] | 806 | aspectClasses.addAll(attr.getAspectClasses()); |
| 807 | } |
Carmi Grushko | 9a796de | 2016-11-13 12:22:30 +0000 | [diff] [blame] | 808 | |
lberki | 9137a75 | 2017-06-09 12:49:10 +0200 | [diff] [blame] | 809 | builder.addTransitive(provider.getExtraActionArtifacts()); |
| 810 | if (!aspectClasses.isEmpty()) { |
| 811 | builder.addAll(filterTransitiveExtraActions(provider, aspectClasses)); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 812 | } |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 813 | } else { |
| 814 | builder.addTransitive(provider.getTransitiveExtraActionArtifacts()); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | return builder.build(); |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * Returns a list of actions from 'provider' that were registered by an aspect from |
| 823 | * 'aspectClasses'. All actions in 'provider' are considered - both direct and transitive. |
| 824 | */ |
| 825 | private ImmutableList<Artifact> filterTransitiveExtraActions( |
| 826 | ExtraActionArtifactsProvider provider, Set<AspectClass> aspectClasses) { |
| 827 | ImmutableList.Builder<Artifact> artifacts = ImmutableList.builder(); |
| 828 | // Add to 'artifacts' all extra-actions which were registered by aspects which 'topLevel' |
| 829 | // might have injected. |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 830 | for (Artifact artifact : provider.getTransitiveExtraActionArtifacts()) { |
| 831 | ArtifactOwner owner = artifact.getArtifactOwner(); |
| 832 | if (owner instanceof AspectKey) { |
| 833 | if (aspectClasses.contains(((AspectKey) owner).getAspectClass())) { |
| 834 | artifacts.add(artifact); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | } |
| 838 | return artifacts.build(); |
| 839 | } |
| 840 | |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 841 | private NestedSet<Artifact> addExtraActionsFromAspects( |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 842 | BuildView.Options viewOptions, Collection<AspectValue> aspects) { |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 843 | NestedSetBuilder<Artifact> builder = NestedSetBuilder.stableOrder(); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 844 | for (AspectValue aspect : aspects) { |
| 845 | ExtraActionArtifactsProvider provider = |
| 846 | aspect.getConfiguredAspect().getProvider(ExtraActionArtifactsProvider.class); |
| 847 | if (provider != null) { |
| 848 | if (viewOptions.extraActionTopLevelOnly) { |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 849 | builder.addTransitive(provider.getExtraActionArtifacts()); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 850 | } else { |
| 851 | builder.addTransitive(provider.getTransitiveExtraActionArtifacts()); |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | return builder.build(); |
| 856 | } |
| 857 | |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 858 | private static void scheduleTestsIfRequested( |
| 859 | Collection<ConfiguredTarget> targetsToTest, |
| 860 | Collection<ConfiguredTarget> targetsToTestExclusive, |
| 861 | TopLevelArtifactContext topLevelOptions, |
| 862 | Collection<ConfiguredTarget> allTestTargets, |
| 863 | SkyframeExecutor skyframeExecutor, |
| 864 | ExtendedEventHandler eventHandler) |
| 865 | throws InterruptedException { |
Lukacs Berki | 3f4d4e9 | 2015-02-24 10:28:26 +0000 | [diff] [blame] | 866 | Set<String> outputGroups = topLevelOptions.outputGroups(); |
dslomov | 69c45f8 | 2017-12-14 11:15:43 -0500 | [diff] [blame] | 867 | if (!outputGroups.contains(OutputGroupInfo.FILES_TO_COMPILE) |
| 868 | && !outputGroups.contains(OutputGroupInfo.COMPILATION_PREREQUISITES) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 869 | && allTestTargets != null) { |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 870 | scheduleTests( |
| 871 | targetsToTest, |
| 872 | targetsToTestExclusive, |
| 873 | allTestTargets, |
| 874 | topLevelOptions.runTestsExclusively(), |
| 875 | skyframeExecutor, |
| 876 | eventHandler); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 880 | /** |
| 881 | * Returns set of artifacts representing test results, writing into targetsToTest and |
| 882 | * targetsToTestExclusive. |
| 883 | */ |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 884 | private static void scheduleTests( |
| 885 | Collection<ConfiguredTarget> targetsToTest, |
| 886 | Collection<ConfiguredTarget> targetsToTestExclusive, |
| 887 | Collection<ConfiguredTarget> allTestTargets, |
| 888 | boolean isExclusive, |
| 889 | SkyframeExecutor skyframeExecutor, |
| 890 | ExtendedEventHandler eventHandler) |
| 891 | throws InterruptedException { |
| 892 | for (ConfiguredTarget configuredTarget : allTestTargets) { |
| 893 | Target target = null; |
| 894 | try { |
| 895 | target = |
| 896 | skyframeExecutor |
| 897 | .getPackageManager() |
| 898 | .getTarget(eventHandler, configuredTarget.getLabel()); |
| 899 | } catch (NoSuchTargetException | NoSuchPackageException e) { |
| 900 | eventHandler.handle(Event.error("Failed to get target when scheduling tests")); |
| 901 | continue; |
| 902 | } |
| 903 | if (target instanceof Rule) { |
| 904 | boolean exclusive = isExclusive || TargetUtils.isExclusiveTestRule((Rule) target); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 905 | Collection<ConfiguredTarget> testCollection = exclusive |
| 906 | ? targetsToTestExclusive |
| 907 | : targetsToTest; |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 908 | testCollection.add(configuredTarget); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
Greg Estren | 7971e67 | 2016-06-01 23:22:48 +0000 | [diff] [blame] | 913 | /** |
gregce | 408bfe47 | 2017-09-14 00:47:27 +0200 | [diff] [blame] | 914 | * Gets a configuration for the given target. |
Greg Estren | 1d8ba90 | 2016-09-21 21:18:19 +0000 | [diff] [blame] | 915 | * |
| 916 | * <p>If {@link BuildConfiguration.Options#trimConfigurations()} is true, the configuration only |
| 917 | * includes the fragments needed by the fragment and its transitive closure. Else unconditionally |
| 918 | * includes all fragments. |
Greg Estren | 01a78e7 | 2016-06-17 16:16:16 +0000 | [diff] [blame] | 919 | */ |
| 920 | @VisibleForTesting |
gregce | 408bfe47 | 2017-09-14 00:47:27 +0200 | [diff] [blame] | 921 | public BuildConfiguration getConfigurationForTesting( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 922 | Target target, BuildConfiguration config, ExtendedEventHandler eventHandler) |
| 923 | throws InterruptedException { |
juliexxia | 8ee423d | 2017-10-18 16:36:57 -0400 | [diff] [blame] | 924 | List<TargetAndConfiguration> node = |
| 925 | ImmutableList.<TargetAndConfiguration>of(new TargetAndConfiguration(target, config)); |
| 926 | LinkedHashSet<TargetAndConfiguration> configs = |
| 927 | ConfigurationResolver.getConfigurationsFromExecutor( |
| 928 | node, |
| 929 | AnalysisUtils.targetsToDeps( |
| 930 | new LinkedHashSet<TargetAndConfiguration>(node), ruleClassProvider), |
| 931 | eventHandler, |
| 932 | skyframeExecutor); |
| 933 | return configs.iterator().next().getConfiguration(); |
Greg Estren | 01a78e7 | 2016-06-17 16:16:16 +0000 | [diff] [blame] | 934 | } |
Greg Estren | 7971e67 | 2016-06-01 23:22:48 +0000 | [diff] [blame] | 935 | |
| 936 | /** |
Ulf Adams | a385d3e | 2015-09-28 13:21:45 +0000 | [diff] [blame] | 937 | * Sets the possible artifact roots in the artifact factory. This allows the factory to resolve |
| 938 | * paths with unknown roots to artifacts. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 939 | */ |
| 940 | @VisibleForTesting // for BuildViewTestCase |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 941 | public void setArtifactRoots(PackageRoots packageRoots) { |
| 942 | getArtifactFactory().setPackageRoots(packageRoots.getPackageRootLookup()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /** |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 946 | * Tests and clears the current thread's pending "interrupted" status, and |
| 947 | * throws InterruptedException iff it was set. |
| 948 | */ |
| 949 | protected final void pollInterruptedStatus() throws InterruptedException { |
| 950 | if (Thread.interrupted()) { |
| 951 | throw new InterruptedException(); |
| 952 | } |
| 953 | } |
| 954 | |
Ulf Adams | a4ca637 | 2016-01-11 14:20:28 +0000 | [diff] [blame] | 955 | // For testing |
| 956 | @VisibleForTesting |
| 957 | public Iterable<ConfiguredTarget> getDirectPrerequisitesForTesting( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 958 | ExtendedEventHandler eventHandler, ConfiguredTarget ct, |
| 959 | BuildConfigurationCollection configurations) |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 960 | throws EvalException, InvalidConfigurationException, |
| 961 | InterruptedException, InconsistentAspectOrderException { |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 962 | return Collections2.transform( |
janakr | 9fcef03 | 2018-01-23 13:38:24 -0800 | [diff] [blame] | 963 | skyframeExecutor.getConfiguredTargetsForTesting( |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 964 | eventHandler, |
| 965 | ct.getConfiguration(), |
| 966 | ImmutableSet.copyOf( |
| 967 | getDirectPrerequisiteDependenciesForTesting( |
| 968 | eventHandler, ct, configurations, /*toolchainContext=*/ null) |
| 969 | .values())), |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 970 | ConfiguredTargetAndData::getConfiguredTarget); |
Ulf Adams | a4ca637 | 2016-01-11 14:20:28 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | @VisibleForTesting |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 974 | public OrderedSetMultimap<Attribute, Dependency> getDirectPrerequisiteDependenciesForTesting( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 975 | final ExtendedEventHandler eventHandler, |
| 976 | final ConfiguredTarget ct, |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 977 | BuildConfigurationCollection configurations, |
| 978 | ToolchainContext toolchainContext) |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 979 | throws EvalException, InvalidConfigurationException, InterruptedException, |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 980 | InconsistentAspectOrderException { |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 981 | |
| 982 | Target target = null; |
| 983 | try { |
| 984 | target = skyframeExecutor.getPackageManager().getTarget(eventHandler, ct.getLabel()); |
| 985 | } catch (NoSuchPackageException | NoSuchTargetException | InterruptedException e) { |
| 986 | eventHandler.handle( |
| 987 | Event.error("Failed to get target from package during prerequisite analysis." + e)); |
| 988 | return OrderedSetMultimap.create(); |
| 989 | } |
| 990 | |
| 991 | if (!(target instanceof Rule)) { |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 992 | return OrderedSetMultimap.create(); |
Ulf Adams | a4ca637 | 2016-01-11 14:20:28 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | class SilentDependencyResolver extends DependencyResolver { |
gregce | 8529746 | 2017-08-18 21:20:29 +0200 | [diff] [blame] | 996 | private SilentDependencyResolver() { |
gregce | 8529746 | 2017-08-18 21:20:29 +0200 | [diff] [blame] | 997 | } |
| 998 | |
Ulf Adams | a4ca637 | 2016-01-11 14:20:28 +0000 | [diff] [blame] | 999 | @Override |
| 1000 | protected void invalidVisibilityReferenceHook(TargetAndConfiguration node, Label label) { |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1001 | throw new RuntimeException("bad visibility on " + label + " during testing unexpected"); |
Ulf Adams | a4ca637 | 2016-01-11 14:20:28 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | @Override |
| 1005 | protected void invalidPackageGroupReferenceHook(TargetAndConfiguration node, Label label) { |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1006 | throw new RuntimeException("bad package group on " + label + " during testing unexpected"); |
| 1007 | } |
| 1008 | |
| 1009 | @Override |
| 1010 | protected void missingEdgeHook(Target from, Label to, NoSuchThingException e) { |
| 1011 | throw new RuntimeException( |
| 1012 | "missing dependency from " + from.getLabel() + " to " + to + ": " + e.getMessage(), |
| 1013 | e); |
Ulf Adams | a4ca637 | 2016-01-11 14:20:28 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | @Override |
Janak Ramakrishnan | 03e4f9c | 2016-08-16 18:59:56 +0000 | [diff] [blame] | 1017 | protected Target getTarget(Target from, Label label, NestedSetBuilder<Label> rootCauses) |
| 1018 | throws InterruptedException { |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 1019 | try { |
Janak Ramakrishnan | 03e4f9c | 2016-08-16 18:59:56 +0000 | [diff] [blame] | 1020 | return skyframeExecutor.getPackageManager().getTarget(eventHandler, label); |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 1021 | } catch (NoSuchThingException e) { |
| 1022 | throw new IllegalStateException(e); |
| 1023 | } |
Ulf Adams | a4ca637 | 2016-01-11 14:20:28 +0000 | [diff] [blame] | 1024 | } |
Greg Estren | 373e3e2 | 2016-08-09 22:36:51 +0000 | [diff] [blame] | 1025 | |
| 1026 | @Override |
| 1027 | protected List<BuildConfiguration> getConfigurations( |
janakr | a39a562 | 2018-01-24 08:43:35 -0800 | [diff] [blame] | 1028 | FragmentClassSet fragments, Iterable<BuildOptions> buildOptions) { |
| 1029 | Preconditions.checkArgument( |
| 1030 | ct.getConfiguration().fragmentClasses().equals(fragments), |
| 1031 | "Mismatch: %s %s", |
| 1032 | ct, |
| 1033 | fragments); |
Greg Estren | 373e3e2 | 2016-08-09 22:36:51 +0000 | [diff] [blame] | 1034 | Dependency asDep = Dependency.withTransitionAndAspects(ct.getLabel(), |
gregce | 7fa23ea | 2018-01-18 12:46:04 -0800 | [diff] [blame] | 1035 | NoTransition.INSTANCE, AspectCollection.EMPTY); |
Greg Estren | 373e3e2 | 2016-08-09 22:36:51 +0000 | [diff] [blame] | 1036 | ImmutableList.Builder<BuildConfiguration> builder = ImmutableList.builder(); |
| 1037 | for (BuildOptions options : buildOptions) { |
| 1038 | builder.add(Iterables.getOnlyElement( |
| 1039 | skyframeExecutor |
| 1040 | .getConfigurations(eventHandler, options, ImmutableList.<Dependency>of(asDep)) |
| 1041 | .values() |
| 1042 | )); |
| 1043 | } |
| 1044 | return builder.build(); |
| 1045 | } |
Ulf Adams | a4ca637 | 2016-01-11 14:20:28 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | DependencyResolver dependencyResolver = new SilentDependencyResolver(); |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 1049 | TargetAndConfiguration ctgNode = new TargetAndConfiguration(target, ct.getConfiguration()); |
Ulf Adams | a4ca637 | 2016-01-11 14:20:28 +0000 | [diff] [blame] | 1050 | return dependencyResolver.dependentNodeMap( |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 1051 | ctgNode, |
| 1052 | configurations.getHostConfiguration(), |
| 1053 | /*aspect=*/ null, |
| 1054 | getConfigurableAttributeKeysForTesting(eventHandler, ctgNode), |
juliexxia | b892a9a | 2018-03-16 08:32:31 -0700 | [diff] [blame^] | 1055 | toolchainContext == null |
| 1056 | ? ImmutableSet.of() |
| 1057 | : toolchainContext.getResolvedToolchainLabels()); |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | /** |
| 1061 | * Returns ConfigMatchingProvider instances corresponding to the configurable attribute keys |
| 1062 | * present in this rule's attributes. |
| 1063 | */ |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 1064 | private ImmutableMap<Label, ConfigMatchingProvider> getConfigurableAttributeKeysForTesting( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 1065 | ExtendedEventHandler eventHandler, TargetAndConfiguration ctg) { |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1066 | if (!(ctg.getTarget() instanceof Rule)) { |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 1067 | return ImmutableMap.of(); |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1068 | } |
| 1069 | Rule rule = (Rule) ctg.getTarget(); |
Lukacs Berki | 53e0391 | 2016-05-19 16:26:09 +0000 | [diff] [blame] | 1070 | Map<Label, ConfigMatchingProvider> keys = new LinkedHashMap<>(); |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1071 | RawAttributeMapper mapper = RawAttributeMapper.of(rule); |
| 1072 | for (Attribute attribute : rule.getAttributes()) { |
| 1073 | for (Label label : mapper.getConfigurabilityKeys(attribute.getName(), attribute.getType())) { |
| 1074 | if (BuildType.Selector.isReservedLabel(label)) { |
| 1075 | continue; |
| 1076 | } |
| 1077 | ConfiguredTarget ct = getConfiguredTargetForTesting( |
| 1078 | eventHandler, label, ctg.getConfiguration()); |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 1079 | keys.put(label, Preconditions.checkNotNull(ct.getProvider(ConfigMatchingProvider.class))); |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
Lukacs Berki | 53e0391 | 2016-05-19 16:26:09 +0000 | [diff] [blame] | 1082 | return ImmutableMap.copyOf(keys); |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1085 | private OrderedSetMultimap<Attribute, ConfiguredTargetAndData> getPrerequisiteMapForTesting( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 1086 | final ExtendedEventHandler eventHandler, |
| 1087 | ConfiguredTarget target, |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 1088 | BuildConfigurationCollection configurations, |
| 1089 | ToolchainContext toolchainContext) |
| 1090 | throws EvalException, InvalidConfigurationException, InterruptedException, |
| 1091 | InconsistentAspectOrderException { |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 1092 | OrderedSetMultimap<Attribute, Dependency> depNodeNames = |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 1093 | getDirectPrerequisiteDependenciesForTesting( |
| 1094 | eventHandler, target, configurations, toolchainContext); |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1095 | |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1096 | ImmutableMultimap<Dependency, ConfiguredTargetAndData> cts = |
janakr | 9fcef03 | 2018-01-23 13:38:24 -0800 | [diff] [blame] | 1097 | skyframeExecutor.getConfiguredTargetMapForTesting( |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 1098 | eventHandler, target.getConfiguration(), ImmutableSet.copyOf(depNodeNames.values())); |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1099 | |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1100 | OrderedSetMultimap<Attribute, ConfiguredTargetAndData> result = OrderedSetMultimap.create(); |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1101 | for (Map.Entry<Attribute, Dependency> entry : depNodeNames.entries()) { |
Greg Estren | 0db9b11 | 2016-12-05 21:55:54 +0000 | [diff] [blame] | 1102 | result.putAll(entry.getKey(), cts.get(entry.getValue())); |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1103 | } |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 1104 | return result; |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
gregce | be55e11 | 2018-01-30 11:04:53 -0800 | [diff] [blame] | 1107 | private ConfigurationTransition getTopLevelTransitionForTarget(Label label, |
| 1108 | ExtendedEventHandler handler) { |
Cal Peyser | 8a638d5 | 2017-03-24 15:44:24 +0000 | [diff] [blame] | 1109 | Rule rule; |
| 1110 | try { |
| 1111 | rule = skyframeExecutor |
| 1112 | .getPackageManager() |
| 1113 | .getTarget(handler, label) |
| 1114 | .getAssociatedRule(); |
| 1115 | } catch (NoSuchPackageException | NoSuchTargetException e) { |
gregce | 7fa23ea | 2018-01-18 12:46:04 -0800 | [diff] [blame] | 1116 | return NoTransition.INSTANCE; |
Cal Peyser | 8a638d5 | 2017-03-24 15:44:24 +0000 | [diff] [blame] | 1117 | } catch (InterruptedException e) { |
| 1118 | Thread.currentThread().interrupt(); |
| 1119 | throw new AssertionError("Configuration of " + label + " interrupted"); |
| 1120 | } |
| 1121 | if (rule == null) { |
gregce | 7fa23ea | 2018-01-18 12:46:04 -0800 | [diff] [blame] | 1122 | return NoTransition.INSTANCE; |
Cal Peyser | 8a638d5 | 2017-03-24 15:44:24 +0000 | [diff] [blame] | 1123 | } |
| 1124 | RuleTransitionFactory factory = rule |
| 1125 | .getRuleClassObject() |
| 1126 | .getTransitionFactory(); |
mstaib | 274cb26 | 2017-03-29 20:12:19 +0000 | [diff] [blame] | 1127 | if (factory == null) { |
gregce | 7fa23ea | 2018-01-18 12:46:04 -0800 | [diff] [blame] | 1128 | return NoTransition.INSTANCE; |
mstaib | 274cb26 | 2017-03-29 20:12:19 +0000 | [diff] [blame] | 1129 | } |
gregce | be55e11 | 2018-01-30 11:04:53 -0800 | [diff] [blame] | 1130 | ConfigurationTransition transition = factory.buildTransitionFor(rule); |
gregce | 7fa23ea | 2018-01-18 12:46:04 -0800 | [diff] [blame] | 1131 | return (transition == null) ? NoTransition.INSTANCE : transition; |
Cal Peyser | 8a638d5 | 2017-03-24 15:44:24 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Ulf Adams | 9c505cb | 2015-09-28 13:38:14 +0000 | [diff] [blame] | 1134 | /** |
gregce | 408bfe47 | 2017-09-14 00:47:27 +0200 | [diff] [blame] | 1135 | * Returns a configured target for the specified target and configuration. If the target in |
| 1136 | * question has a top-level rule class transition, that transition is applied in the returned |
| 1137 | * ConfiguredTarget. |
| 1138 | * |
| 1139 | * <p>Returns {@code null} if something goes wrong. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1140 | */ |
| 1141 | @VisibleForTesting |
Ulf Adams | a385d3e | 2015-09-28 13:21:45 +0000 | [diff] [blame] | 1142 | public ConfiguredTarget getConfiguredTargetForTesting( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 1143 | ExtendedEventHandler eventHandler, Label label, BuildConfiguration config) { |
Cal Peyser | 8a638d5 | 2017-03-24 15:44:24 +0000 | [diff] [blame] | 1144 | return skyframeExecutor.getConfiguredTargetForTesting(eventHandler, label, config, |
| 1145 | getTopLevelTransitionForTarget(label, eventHandler)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
mjhalupka | 2fff952 | 2018-02-07 12:22:17 -0800 | [diff] [blame] | 1148 | @VisibleForTesting |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1149 | public ConfiguredTargetAndData getConfiguredTargetAndTargetForTesting( |
mjhalupka | 2fff952 | 2018-02-07 12:22:17 -0800 | [diff] [blame] | 1150 | ExtendedEventHandler eventHandler, Label label, BuildConfiguration config) { |
| 1151 | return skyframeExecutor.getConfiguredTargetAndTargetForTesting(eventHandler, label, config); |
| 1152 | } |
| 1153 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1154 | /** |
| 1155 | * Returns a RuleContext which is the same as the original RuleContext of the target parameter. |
| 1156 | */ |
| 1157 | @VisibleForTesting |
Florian Weikert | 4b67d4f | 2015-09-14 13:35:34 +0000 | [diff] [blame] | 1158 | public RuleContext getRuleContextForTesting( |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 1159 | ConfiguredTarget target, |
| 1160 | StoredEventHandler eventHandler, |
Lukacs Berki | 76cb02e | 2017-02-17 14:06:11 +0000 | [diff] [blame] | 1161 | BuildConfigurationCollection configurations) |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 1162 | throws EvalException, InvalidConfigurationException, InterruptedException, |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 1163 | InconsistentAspectOrderException, ToolchainContextException { |
Ulf Adams | a385d3e | 2015-09-28 13:21:45 +0000 | [diff] [blame] | 1164 | BuildConfiguration targetConfig = target.getConfiguration(); |
| 1165 | CachingAnalysisEnvironment env = |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 1166 | new CachingAnalysisEnvironment( |
| 1167 | getArtifactFactory(), |
| 1168 | skyframeExecutor.getActionKeyContext(), |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 1169 | ConfiguredTargetKey.of(target.getLabel(), targetConfig), |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 1170 | /*isSystemEnv=*/ false, |
| 1171 | targetConfig.extendedSanityChecks(), |
| 1172 | eventHandler, |
| 1173 | /*env=*/ null, |
| 1174 | targetConfig.isActionsEnabled()); |
Ulf Adams | a385d3e | 2015-09-28 13:21:45 +0000 | [diff] [blame] | 1175 | return getRuleContextForTesting(eventHandler, target, env, configurations); |
Ulf Adams | 79f0521 | 2015-02-27 16:40:00 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | /** |
| 1179 | * Creates and returns a rule context that is equivalent to the one that was used to create the |
| 1180 | * given configured target. |
| 1181 | */ |
| 1182 | @VisibleForTesting |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 1183 | public RuleContext getRuleContextForTesting( |
| 1184 | ExtendedEventHandler eventHandler, |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 1185 | ConfiguredTarget configuredTarget, |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 1186 | AnalysisEnvironment env, |
| 1187 | BuildConfigurationCollection configurations) |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 1188 | throws EvalException, InvalidConfigurationException, InterruptedException, |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 1189 | InconsistentAspectOrderException, ToolchainContextException { |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 1190 | BuildConfiguration targetConfig = configuredTarget.getConfiguration(); |
| 1191 | Target target = null; |
| 1192 | try { |
| 1193 | target = |
| 1194 | skyframeExecutor.getPackageManager().getTarget(eventHandler, configuredTarget.getLabel()); |
| 1195 | } catch (NoSuchPackageException | NoSuchTargetException e) { |
| 1196 | eventHandler.handle( |
| 1197 | Event.error("Failed to get target when trying to get rule context for testing")); |
| 1198 | throw new IllegalStateException(e); |
| 1199 | } |
cpeyser | fb82999 | 2017-09-07 17:17:03 +0200 | [diff] [blame] | 1200 | Set<Label> requiredToolchains = |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 1201 | target.getAssociatedRule().getRuleClassObject().getRequiredToolchains(); |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 1202 | ToolchainContext toolchainContext = |
| 1203 | skyframeExecutor.getToolchainContextForTesting( |
| 1204 | requiredToolchains, targetConfig, eventHandler); |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1205 | OrderedSetMultimap<Attribute, ConfiguredTargetAndData> prerequisiteMap = |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 1206 | getPrerequisiteMapForTesting( |
| 1207 | eventHandler, configuredTarget, configurations, toolchainContext); |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 1208 | toolchainContext.resolveToolchains(prerequisiteMap); |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 1209 | |
Ulf Adams | 79f0521 | 2015-02-27 16:40:00 +0000 | [diff] [blame] | 1210 | return new RuleContext.Builder( |
Mark Schaller | 0312f91 | 2016-07-22 18:45:02 +0000 | [diff] [blame] | 1211 | env, |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 1212 | (Rule) target, |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 1213 | ImmutableList.of(), |
Mark Schaller | 0312f91 | 2016-07-22 18:45:02 +0000 | [diff] [blame] | 1214 | targetConfig, |
| 1215 | configurations.getHostConfiguration(), |
gregce | e046d3a | 2018-01-29 13:52:40 -0800 | [diff] [blame] | 1216 | ruleClassProvider.getLipoDataTransition(), |
Mark Schaller | 0312f91 | 2016-07-22 18:45:02 +0000 | [diff] [blame] | 1217 | ruleClassProvider.getPrerequisiteValidator(), |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 1218 | ((Rule) target).getRuleClassObject().getConfigurationFragmentPolicy()) |
Mark Schaller | 0312f91 | 2016-07-22 18:45:02 +0000 | [diff] [blame] | 1219 | .setVisibility( |
cushon | 34ff85e | 2017-11-15 08:59:27 -0800 | [diff] [blame] | 1220 | NestedSetBuilder.create( |
| 1221 | Order.STABLE_ORDER, |
| 1222 | PackageGroupContents.create(ImmutableList.of(PackageSpecification.everything())))) |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 1223 | .setPrerequisites( |
mjhalupka | 7675dbd | 2018-01-29 12:46:58 -0800 | [diff] [blame] | 1224 | getPrerequisiteMapForTesting( |
| 1225 | eventHandler, configuredTarget, configurations, toolchainContext)) |
Mark Schaller | 0312f91 | 2016-07-22 18:45:02 +0000 | [diff] [blame] | 1226 | .setConfigConditions(ImmutableMap.<Label, ConfigMatchingProvider>of()) |
| 1227 | .setUniversalFragment(ruleClassProvider.getUniversalFragment()) |
cpeyser | 1099b54 | 2017-09-06 18:19:20 +0200 | [diff] [blame] | 1228 | .setToolchainContext(toolchainContext) |
Mark Schaller | 0312f91 | 2016-07-22 18:45:02 +0000 | [diff] [blame] | 1229 | .build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /** |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 1233 | * For a configured target dependentTarget, returns the desired configured target that is depended |
| 1234 | * upon. Useful for obtaining the a target with aspects required by the dependent. |
Googler | 9263b46 | 2015-05-13 19:22:24 +0000 | [diff] [blame] | 1235 | */ |
| 1236 | @VisibleForTesting |
| 1237 | public ConfiguredTarget getPrerequisiteConfiguredTargetForTesting( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 1238 | ExtendedEventHandler eventHandler, |
| 1239 | ConfiguredTarget dependentTarget, |
| 1240 | Label desiredTarget, |
Ulf Adams | e770467 | 2015-09-21 14:37:41 +0000 | [diff] [blame] | 1241 | BuildConfigurationCollection configurations) |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 1242 | throws EvalException, InvalidConfigurationException, InterruptedException, |
| 1243 | InconsistentAspectOrderException { |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1244 | Collection<ConfiguredTargetAndData> configuredTargets = |
cpeyser | b38e3af | 2017-09-11 16:58:58 +0200 | [diff] [blame] | 1245 | getPrerequisiteMapForTesting( |
| 1246 | eventHandler, dependentTarget, configurations, /*toolchainContext=*/ null) |
| 1247 | .values(); |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1248 | for (ConfiguredTargetAndData ct : configuredTargets) { |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 1249 | if (ct.getTarget().getLabel().equals(desiredTarget)) { |
| 1250 | return ct.getConfiguredTarget(); |
Googler | 9263b46 | 2015-05-13 19:22:24 +0000 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | return null; |
| 1254 | } |
Janak Ramakrishnan | bede7b4 | 2016-11-17 18:34:08 +0000 | [diff] [blame] | 1255 | |
| 1256 | /** |
| 1257 | * A converter for loading phase thread count. Since the default is not a true constant, we create |
| 1258 | * a converter here to implement the default logic. |
| 1259 | */ |
| 1260 | public static final class LoadingPhaseThreadCountConverter implements Converter<Integer> { |
| 1261 | @Override |
| 1262 | public Integer convert(String input) throws OptionsParsingException { |
| 1263 | if ("-1".equals(input)) { |
| 1264 | // Reduce thread count while running tests. Test cases are typically small, and large thread |
| 1265 | // pools vying for a relatively small number of CPU cores may induce non-optimal |
| 1266 | // performance. |
| 1267 | return System.getenv("TEST_TMPDIR") == null ? 200 : 5; |
| 1268 | } |
| 1269 | |
| 1270 | try { |
| 1271 | int result = Integer.decode(input); |
| 1272 | if (result < 0) { |
| 1273 | throw new OptionsParsingException("'" + input + "' must be at least -1"); |
| 1274 | } |
| 1275 | return result; |
| 1276 | } catch (NumberFormatException e) { |
| 1277 | throw new OptionsParsingException("'" + input + "' is not an int"); |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | @Override |
| 1282 | public String getTypeDescription() { |
| 1283 | return "an integer"; |
| 1284 | } |
| 1285 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1286 | } |