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