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 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | import com.google.common.base.Function; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 17 | import com.google.common.base.Verify; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ArrayListMultimap; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Iterables; |
Greg Estren | 123de31 | 2015-11-17 19:47:58 +0000 | [diff] [blame] | 22 | import com.google.common.collect.LinkedListMultimap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import com.google.common.collect.ListMultimap; |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 24 | import com.google.common.collect.Maps; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 25 | import com.google.common.collect.Multimap; |
Janak Ramakrishnan | b3a6ca7 | 2015-03-27 20:07:28 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.Action; |
| 27 | import com.google.devtools.build.lib.actions.Actions; |
| 28 | import com.google.devtools.build.lib.actions.Artifact; |
| 29 | import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import com.google.devtools.build.lib.analysis.CachingAnalysisEnvironment; |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.analysis.ConfiguredAspect; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
| 33 | import com.google.devtools.build.lib.analysis.DependencyResolver.Dependency; |
| 34 | import com.google.devtools.build.lib.analysis.LabelAndConfiguration; |
| 35 | import com.google.devtools.build.lib.analysis.RuleConfiguredTarget; |
| 36 | import com.google.devtools.build.lib.analysis.TargetAndConfiguration; |
| 37 | import com.google.devtools.build.lib.analysis.TransitiveInfoProvider; |
| 38 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.analysis.config.HostTransition; |
| 42 | import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; |
| 43 | import com.google.devtools.build.lib.analysis.config.PatchTransition; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.cmdline.Label; |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 45 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 46 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | import com.google.devtools.build.lib.events.Event; |
| 48 | import com.google.devtools.build.lib.events.StoredEventHandler; |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.packages.Aspect; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | import com.google.devtools.build.lib.packages.AspectDefinition; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | import com.google.devtools.build.lib.packages.Attribute; |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 52 | import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 53 | import com.google.devtools.build.lib.packages.BuildType; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
| 55 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.lib.packages.Package; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 57 | import com.google.devtools.build.lib.packages.RawAttributeMapper; |
| 58 | import com.google.devtools.build.lib.packages.Rule; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 59 | import com.google.devtools.build.lib.packages.RuleClassProvider; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | import com.google.devtools.build.lib.packages.Target; |
| 61 | import com.google.devtools.build.lib.packages.TargetUtils; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | import com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException; |
| 63 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor.BuildViewProvider; |
| 64 | import com.google.devtools.build.lib.syntax.EvalException; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 65 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | import com.google.devtools.build.skyframe.SkyFunction; |
| 67 | import com.google.devtools.build.skyframe.SkyFunctionException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 68 | import com.google.devtools.build.skyframe.SkyKey; |
| 69 | import com.google.devtools.build.skyframe.SkyValue; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 70 | import com.google.devtools.build.skyframe.ValueOrException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 71 | import com.google.devtools.build.skyframe.ValueOrException3; |
| 72 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | import java.util.Collection; |
| 74 | import java.util.HashMap; |
| 75 | import java.util.HashSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | import java.util.Map; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 77 | import java.util.Objects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 78 | import java.util.Set; |
| 79 | |
| 80 | import javax.annotation.Nullable; |
| 81 | |
| 82 | /** |
| 83 | * SkyFunction for {@link ConfiguredTargetValue}s. |
| 84 | */ |
| 85 | final class ConfiguredTargetFunction implements SkyFunction { |
| 86 | |
| 87 | /** |
| 88 | * Exception class that signals an error during the evaluation of a dependency. |
| 89 | */ |
| 90 | public static class DependencyEvaluationException extends Exception { |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 91 | public DependencyEvaluationException(InvalidConfigurationException cause) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 92 | super(cause); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 95 | public DependencyEvaluationException(ConfiguredValueCreationException cause) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | super(cause); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 100 | public synchronized Exception getCause() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | return (Exception) super.getCause(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private static final Function<Dependency, SkyKey> TO_KEYS = |
| 106 | new Function<Dependency, SkyKey>() { |
| 107 | @Override |
| 108 | public SkyKey apply(Dependency input) { |
| 109 | return ConfiguredTargetValue.key(input.getLabel(), input.getConfiguration()); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | private final BuildViewProvider buildViewProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 114 | private final RuleClassProvider ruleClassProvider; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 115 | |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 116 | ConfiguredTargetFunction(BuildViewProvider buildViewProvider, |
| 117 | RuleClassProvider ruleClassProvider) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 118 | this.buildViewProvider = buildViewProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 119 | this.ruleClassProvider = ruleClassProvider; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public SkyValue compute(SkyKey key, Environment env) throws ConfiguredTargetFunctionException, |
| 124 | InterruptedException { |
| 125 | SkyframeBuildView view = buildViewProvider.getSkyframeBuildView(); |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 126 | NestedSetBuilder<Package> transitivePackages = NestedSetBuilder.stableOrder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 127 | ConfiguredTargetKey configuredTargetKey = (ConfiguredTargetKey) key.argument(); |
| 128 | LabelAndConfiguration lc = LabelAndConfiguration.of( |
| 129 | configuredTargetKey.getLabel(), configuredTargetKey.getConfiguration()); |
| 130 | |
| 131 | BuildConfiguration configuration = lc.getConfiguration(); |
| 132 | |
| 133 | PackageValue packageValue = |
| 134 | (PackageValue) env.getValue(PackageValue.key(lc.getLabel().getPackageIdentifier())); |
| 135 | if (packageValue == null) { |
| 136 | return null; |
| 137 | } |
| 138 | |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 139 | Package pkg = packageValue.getPackage(); |
| 140 | if (pkg.containsErrors()) { |
| 141 | throw new ConfiguredTargetFunctionException( |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 142 | new BuildFileContainsErrorsException(lc.getLabel().getPackageIdentifier())); |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 143 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 144 | Target target; |
| 145 | try { |
| 146 | target = packageValue.getPackage().getTarget(lc.getLabel().getName()); |
Ulf Adams | 9668361 | 2016-01-25 09:04:54 +0000 | [diff] [blame] | 147 | } catch (NoSuchTargetException e) { |
| 148 | throw new ConfiguredTargetFunctionException(e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 149 | } |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 150 | transitivePackages.add(packageValue.getPackage()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 151 | // TODO(bazel-team): This is problematic - we create the right key, but then end up with a value |
| 152 | // that doesn't match; we can even have the same value multiple times. However, I think it's |
| 153 | // only triggered in tests (i.e., in normal operation, the configuration passed in is already |
| 154 | // null). |
Greg Estren | a6c8896 | 2015-09-28 19:35:18 +0000 | [diff] [blame] | 155 | if (!target.isConfigurable()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 156 | configuration = null; |
| 157 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 158 | |
| 159 | SkyframeDependencyResolver resolver = view.createDependencyResolver(env); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 160 | |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 161 | TargetAndConfiguration ctgValue = |
| 162 | new TargetAndConfiguration(target, configuration); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 163 | try { |
| 164 | // Get the configuration targets that trigger this rule's configurable attributes. |
| 165 | Set<ConfigMatchingProvider> configConditions = |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 166 | getConfigConditions(ctgValue.getTarget(), env, resolver, ctgValue, transitivePackages); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 167 | if (env.valuesMissing()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 168 | return null; |
| 169 | } |
| 170 | |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 171 | ListMultimap<Attribute, ConfiguredTarget> depValueMap = |
| 172 | computeDependencies( |
| 173 | env, |
| 174 | resolver, |
| 175 | ctgValue, |
| 176 | null, |
| 177 | configConditions, |
| 178 | ruleClassProvider, |
| 179 | view.getHostConfiguration(configuration), |
| 180 | transitivePackages); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 181 | ConfiguredTargetValue ans = createConfiguredTarget( |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 182 | view, env, target, configuration, depValueMap, configConditions, transitivePackages); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 183 | return ans; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 184 | } catch (DependencyEvaluationException e) { |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 185 | if (e.getCause() instanceof ConfiguredValueCreationException) { |
| 186 | throw new ConfiguredTargetFunctionException( |
| 187 | (ConfiguredValueCreationException) e.getCause()); |
| 188 | } else { |
| 189 | // Cast to InvalidConfigurationException as a consistency check. If you add any |
| 190 | // DependencyEvaluationException constructors, you may need to change this code, too. |
| 191 | InvalidConfigurationException cause = (InvalidConfigurationException) e.getCause(); |
| 192 | throw new ConfiguredTargetFunctionException( |
| 193 | new ConfiguredValueCreationException(cause.getMessage(), target.getLabel())); |
| 194 | } |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 195 | } catch (AspectCreationException e) { |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 196 | // getAnalysisRootCause may be null if the analysis of the aspect itself failed. |
| 197 | Label rootCause = target.getLabel(); |
| 198 | if (e.getAnalysisRootCause() != null) { |
| 199 | rootCause = e.getAnalysisRootCause(); |
| 200 | } |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 201 | throw new ConfiguredTargetFunctionException( |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 202 | new ConfiguredValueCreationException(e.getMessage(), rootCause)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Computes the direct dependencies of a node in the configured target graph (a configured |
| 208 | * target or an aspect). |
| 209 | * |
| 210 | * <p>Returns null if Skyframe hasn't evaluated the required dependencies yet. In this case, the |
| 211 | * caller should also return null to Skyframe. |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 212 | * @param env the Skyframe environment |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 213 | * @param resolver The dependency resolver |
| 214 | * @param ctgValue The label and the configuration of the node |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 215 | * @param aspect |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 216 | * @param configConditions the configuration conditions for evaluating the attributes of the node |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 217 | * @param ruleClassProvider rule class provider for determining the right configuration fragments |
| 218 | * to apply to deps |
| 219 | * @param hostConfiguration the host configuration. There's a noticeable performance hit from |
| 220 | * instantiating this on demand for every dependency that wants it, so it's best to compute |
| 221 | * the host configuration as early as possible and pass this reference to all consumers |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 222 | * */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 223 | @Nullable |
| 224 | static ListMultimap<Attribute, ConfiguredTarget> computeDependencies( |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 225 | Environment env, |
| 226 | SkyframeDependencyResolver resolver, |
| 227 | TargetAndConfiguration ctgValue, |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 228 | Aspect aspect, |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 229 | Set<ConfigMatchingProvider> configConditions, |
| 230 | RuleClassProvider ruleClassProvider, |
| 231 | BuildConfiguration hostConfiguration, |
| 232 | NestedSetBuilder<Package> transitivePackages) |
Florian Weikert | 4b67d4f | 2015-09-14 13:35:34 +0000 | [diff] [blame] | 233 | throws DependencyEvaluationException, AspectCreationException, InterruptedException { |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 234 | // Create the map from attributes to list of (target, configuration) pairs. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 235 | ListMultimap<Attribute, Dependency> depValueNames; |
| 236 | try { |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 237 | depValueNames = |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 238 | resolver.dependentNodeMap(ctgValue, hostConfiguration, aspect, configConditions); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 239 | } catch (EvalException e) { |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 240 | // EvalException can only be thrown by computed Skylark attributes in the current rule. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 241 | env.getListener().handle(Event.error(e.getLocation(), e.getMessage())); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 242 | throw new DependencyEvaluationException( |
| 243 | new ConfiguredValueCreationException(e.print(), ctgValue.getLabel())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 246 | // Trim each dep's configuration so it only includes the fragments needed by its transitive |
| 247 | // closure (only dynamic configurations support this). |
| 248 | if (ctgValue.getConfiguration() != null |
| 249 | && ctgValue.getConfiguration().useDynamicConfigurations()) { |
| 250 | depValueNames = trimConfigurations(env, ctgValue, depValueNames, hostConfiguration, |
| 251 | ruleClassProvider); |
| 252 | if (depValueNames == null) { |
| 253 | return null; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Resolve configured target dependencies and handle errors. |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 258 | Map<SkyKey, ConfiguredTarget> depValues = resolveConfiguredTargetDependencies(env, |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 259 | depValueNames.values(), transitivePackages); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 260 | if (depValues == null) { |
| 261 | return null; |
| 262 | } |
| 263 | |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 264 | // Resolve required aspects. |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 265 | ListMultimap<SkyKey, ConfiguredAspect> depAspects = |
| 266 | resolveAspectDependencies(env, depValues, depValueNames.values(), transitivePackages); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 267 | if (depAspects == null) { |
| 268 | return null; |
| 269 | } |
| 270 | |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 271 | // Merge the dependent configured targets and aspects into a single map. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 272 | return mergeAspects(depValueNames, depValues, depAspects); |
| 273 | } |
| 274 | |
| 275 | /** |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 276 | * Helper class for {@link #trimConfigurations} - encapsulates a set of config fragments and |
| 277 | * a dynamic transition. This can be used to determine the exact build options needed to |
| 278 | * set a dynamic configuration. |
| 279 | */ |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 280 | @Immutable |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 281 | private static final class FragmentsAndTransition { |
| 282 | // Treat this as immutable. The only reason this isn't an ImmutableSet is because it |
| 283 | // gets bound to a NestedSet.toSet() reference, which returns a Set interface. |
| 284 | final Set<Class<? extends BuildConfiguration.Fragment>> fragments; |
| 285 | final Attribute.Transition transition; |
| 286 | private final int hashCode; |
| 287 | |
| 288 | FragmentsAndTransition(Set<Class<? extends BuildConfiguration.Fragment>> fragments, |
| 289 | Attribute.Transition transition) { |
| 290 | this.fragments = fragments; |
| 291 | this.transition = transition; |
| 292 | hashCode = Objects.hash(this.fragments, this.transition); |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public boolean equals(Object o) { |
| 297 | if (o == this) { |
| 298 | return true; |
| 299 | } else if (o == null) { |
| 300 | return false; |
| 301 | } else { |
| 302 | FragmentsAndTransition other = (FragmentsAndTransition) o; |
| 303 | return other.transition.equals(transition) && other.fragments.equals(fragments); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public int hashCode() { |
| 309 | return hashCode; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 314 | * Helper class for {@link #trimConfigurations} - encapsulates an <attribute, label> pair that |
| 315 | * can be used to map from an input dependency to a trimmed dependency. |
| 316 | */ |
| 317 | @Immutable |
| 318 | private static final class AttributeAndLabel { |
| 319 | final Attribute attribute; |
| 320 | final Label label; |
| 321 | final int hashCode; |
| 322 | |
| 323 | AttributeAndLabel(Attribute attribute, Label label) { |
| 324 | this.attribute = attribute; |
| 325 | this.label = label; |
| 326 | this.hashCode = Objects.hash(this.attribute, this.label); |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public boolean equals(Object o) { |
| 331 | if (!(o instanceof AttributeAndLabel)) { |
| 332 | return false; |
| 333 | } |
| 334 | AttributeAndLabel other = (AttributeAndLabel) o; |
| 335 | return Objects.equals(other.attribute, attribute) && other.label.equals(label); |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public int hashCode() { |
| 340 | return hashCode; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Variation of {@link Map#put} that triggers an exception if another value already exists. |
| 346 | */ |
| 347 | private static <K, V> void putOnlyEntry(Map<K, V> map, K key, V value) { |
| 348 | Verify.verify(map.put(key, value) == null, |
| 349 | "couldn't insert %s: map already has key %s", value.toString(), key.toString()); |
| 350 | } |
| 351 | |
| 352 | /** |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 353 | * Creates a dynamic configuration for each dep that's custom-fitted specifically for that dep. |
| 354 | * |
| 355 | * <p>More specifically: given a set of {@link Dependency} instances holding dynamic config |
| 356 | * transition requests (e.g. {@link Dependency#hasStaticConfiguration()} == false}), returns |
| 357 | * equivalent dependencies containing dynamically created configurations that a) apply those |
| 358 | * transitions and b) only contain the fragments needed by the dep and everything in its |
| 359 | * transitive closure. |
| 360 | * |
| 361 | * <p>This method is heavily performance-optimized. Because it, in aggregate, reads over every |
| 362 | * edge in the configured target graph, small inefficiencies can have observable impact on |
| 363 | * build analysis time. Keep this in mind when making modifications and performance-test any |
| 364 | * changes you make. |
| 365 | */ |
| 366 | @Nullable |
| 367 | static ListMultimap<Attribute, Dependency> trimConfigurations(Environment env, |
| 368 | TargetAndConfiguration ctgValue, ListMultimap<Attribute, Dependency> originalDeps, |
| 369 | BuildConfiguration hostConfiguration, RuleClassProvider ruleClassProvider) |
| 370 | throws DependencyEvaluationException { |
| 371 | |
| 372 | // Maps each Skyframe-evaluated BuildConfiguration to the dependencies that need that |
| 373 | // configuration. For cases where Skyframe isn't needed to get the configuration (e.g. when |
| 374 | // we just re-used the original rule's configuration), we should skip this outright. |
Greg Estren | 123de31 | 2015-11-17 19:47:58 +0000 | [diff] [blame] | 375 | Multimap<SkyKey, Map.Entry<Attribute, Dependency>> keysToEntries = LinkedListMultimap.create(); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 376 | |
| 377 | // Stores the result of applying a dynamic transition to the current configuration using a |
| 378 | // particular subset of fragments. By caching this, we save from redundantly computing the |
| 379 | // same transition for every dependency edge that requests that transition. This can have |
| 380 | // real effect on analysis time for commonly triggered transitions. |
| 381 | Map<FragmentsAndTransition, BuildOptions> transitionsMap = new HashMap<>(); |
| 382 | |
| 383 | // The fragments used by the current target's configuration. |
| 384 | Set<Class<? extends BuildConfiguration.Fragment>> ctgFragments = |
| 385 | ctgValue.getConfiguration().fragmentClasses(); |
| 386 | BuildOptions ctgOptions = ctgValue.getConfiguration().getOptions(); |
| 387 | |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 388 | // Stores the trimmed versions of each dependency. This method must preserve the original label |
| 389 | // ordering of each attribute. For example, if originalDeps.get("data") is [":a", ":b"], the |
| 390 | // trimmed variant must also be [":a", ":b"] in the same order. Because we may not actualize |
| 391 | // the results in order (some results need Skyframe-evaluated configurations while others can |
| 392 | // be computed trivially), we dump them all into this map, then as a final step iterate through |
| 393 | // the original list and pluck out values from here for the final value. |
| 394 | Map<AttributeAndLabel, Dependency> trimmedDeps = new HashMap<>(); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 395 | |
| 396 | for (Map.Entry<Attribute, Dependency> depsEntry : originalDeps.entries()) { |
| 397 | Dependency dep = depsEntry.getValue(); |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 398 | AttributeAndLabel attributeAndLabel = |
| 399 | new AttributeAndLabel(depsEntry.getKey(), dep.getLabel()); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 400 | |
| 401 | if (dep.hasStaticConfiguration()) { |
| 402 | // Certain targets (like output files) trivially pass their configurations to their deps. |
| 403 | // So no need to transform them in any way. |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 404 | putOnlyEntry(trimmedDeps, attributeAndLabel, |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 405 | new Dependency(dep.getLabel(), dep.getConfiguration(), dep.getAspects())); |
| 406 | continue; |
| 407 | } else if (dep.getTransition() == Attribute.ConfigurationTransition.NULL) { |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 408 | putOnlyEntry(trimmedDeps, attributeAndLabel, |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 409 | new Dependency(dep.getLabel(), (BuildConfiguration) null, dep.getAspects())); |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | // Figure out the required fragments for this dep and its transitive closure. |
| 414 | SkyKey fragmentsKey = TransitiveTargetValue.key(dep.getLabel()); |
| 415 | TransitiveTargetValue transitiveDepInfo = (TransitiveTargetValue) env.getValue(fragmentsKey); |
| 416 | if (transitiveDepInfo == null) { |
| 417 | // This should only be possible for tests. In actual runs, this was already called |
| 418 | // as a routine part of the loading phase. |
| 419 | // TODO(bazel-team): check this only occurs in a test context. |
| 420 | return null; |
| 421 | } |
| 422 | Set<Class<? extends BuildConfiguration.Fragment>> depFragments = |
| 423 | transitiveDepInfo.getTransitiveConfigFragments().toSet(); |
| 424 | |
| 425 | boolean sameFragments = depFragments.equals(ctgFragments); |
| 426 | Attribute.Transition transition = dep.getTransition(); |
| 427 | |
| 428 | if (sameFragments) { |
| 429 | if (transition == Attribute.ConfigurationTransition.NONE) { |
| 430 | // The dep uses the same exact configuration. |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 431 | putOnlyEntry(trimmedDeps, attributeAndLabel, |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 432 | new Dependency(dep.getLabel(), ctgValue.getConfiguration(), dep.getAspects())); |
| 433 | continue; |
| 434 | } else if (transition == HostTransition.INSTANCE) { |
| 435 | // The current rule's host configuration can also be used for the dep. We short-circuit |
| 436 | // the standard transition logic for host transitions because these transitions are |
| 437 | // uniquely frequent. It's possible, e.g., for every node in the configured target graph |
| 438 | // to incur multiple host transitions. So we aggressively optimize to avoid hurting |
| 439 | // analysis time. |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 440 | putOnlyEntry(trimmedDeps, attributeAndLabel, |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 441 | new Dependency(dep.getLabel(), hostConfiguration, dep.getAspects())); |
| 442 | continue; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // Apply the transition or use the cached result if it was already applied. |
| 447 | FragmentsAndTransition transitionKey = new FragmentsAndTransition(depFragments, transition); |
| 448 | BuildOptions toOptions = transitionsMap.get(transitionKey); |
| 449 | if (toOptions == null) { |
| 450 | Verify.verify(transition == Attribute.ConfigurationTransition.NONE |
| 451 | || transition instanceof PatchTransition); |
| 452 | BuildOptions fromOptions = ctgOptions; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 453 | // TODO(bazel-team): safety-check that the below call never mutates fromOptions. |
| 454 | toOptions = transition == Attribute.ConfigurationTransition.NONE |
| 455 | ? fromOptions |
| 456 | : ((PatchTransition) transition).apply(fromOptions); |
Greg Estren | 3f241a4 | 2015-11-11 22:34:05 +0000 | [diff] [blame] | 457 | if (!sameFragments) { |
| 458 | // TODO(bazel-team): pre-compute getOptionsClasses in the constructor. |
| 459 | toOptions = toOptions.trim(BuildConfiguration.getOptionsClasses( |
| 460 | transitiveDepInfo.getTransitiveConfigFragments(), ruleClassProvider)); |
| 461 | } |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 462 | transitionsMap.put(transitionKey, toOptions); |
| 463 | } |
| 464 | |
| 465 | // If the transition doesn't change the configuration, trivially re-use the original |
| 466 | // configuration. |
| 467 | if (sameFragments && toOptions.equals(ctgOptions)) { |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 468 | putOnlyEntry(trimmedDeps, attributeAndLabel, |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 469 | new Dependency(dep.getLabel(), ctgValue.getConfiguration(), dep.getAspects())); |
| 470 | continue; |
| 471 | } |
| 472 | |
| 473 | // If we get here, we have to get the configuration from Skyframe. |
| 474 | keysToEntries.put(BuildConfigurationValue.key(depFragments, toOptions), depsEntry); |
| 475 | } |
| 476 | |
| 477 | // Get all BuildConfigurations we need to get from Skyframe. |
| 478 | Map<SkyKey, ValueOrException<InvalidConfigurationException>> depConfigValues = |
| 479 | env.getValuesOrThrow(keysToEntries.keySet(), InvalidConfigurationException.class); |
| 480 | if (env.valuesMissing()) { |
| 481 | return null; |
| 482 | } |
| 483 | |
| 484 | // Now fill in the remaining unresolved deps with the now-resolved configurations. |
| 485 | try { |
| 486 | for (Map.Entry<SkyKey, ValueOrException<InvalidConfigurationException>> entry : |
| 487 | depConfigValues.entrySet()) { |
| 488 | SkyKey key = entry.getKey(); |
| 489 | BuildConfigurationValue trimmedConfig = (BuildConfigurationValue) entry.getValue().get(); |
| 490 | for (Map.Entry<Attribute, Dependency> info : keysToEntries.get(key)) { |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 491 | Dependency originalDep = info.getValue(); |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 492 | putOnlyEntry(trimmedDeps, new AttributeAndLabel(info.getKey(), originalDep.getLabel()), |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 493 | new Dependency(originalDep.getLabel(), trimmedConfig.getConfiguration(), |
| 494 | originalDep.getAspects())); |
| 495 | } |
| 496 | } |
| 497 | } catch (InvalidConfigurationException e) { |
| 498 | throw new DependencyEvaluationException(e); |
| 499 | } |
| 500 | |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 501 | // Re-assemble the output map with the same value ordering (e.g. each attribute's dep labels |
| 502 | // appear in the same order) as the input. |
| 503 | ListMultimap<Attribute, Dependency> result = ArrayListMultimap.create(); |
| 504 | for (Map.Entry<Attribute, Dependency> depsEntry : originalDeps.entries()) { |
| 505 | Dependency trimmedDep = Verify.verifyNotNull( |
| 506 | trimmedDeps.get( |
| 507 | new AttributeAndLabel(depsEntry.getKey(), depsEntry.getValue().getLabel()))); |
| 508 | result.put(depsEntry.getKey(), trimmedDep); |
| 509 | } |
| 510 | return result; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 514 | * Merges the each direct dependency configured target with the aspects associated with it. |
| 515 | * |
| 516 | * <p>Note that the combination of a configured target and its associated aspects are not |
| 517 | * represented by a Skyframe node. This is because there can possibly be many different |
| 518 | * combinations of aspects for a particular configured target, so it would result in a |
| 519 | * combinatiorial explosion of Skyframe nodes. |
| 520 | */ |
| 521 | private static ListMultimap<Attribute, ConfiguredTarget> mergeAspects( |
| 522 | ListMultimap<Attribute, Dependency> depValueNames, |
| 523 | Map<SkyKey, ConfiguredTarget> depConfiguredTargetMap, |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 524 | ListMultimap<SkyKey, ConfiguredAspect> depAspectMap) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 525 | ListMultimap<Attribute, ConfiguredTarget> result = ArrayListMultimap.create(); |
| 526 | |
| 527 | for (Map.Entry<Attribute, Dependency> entry : depValueNames.entries()) { |
| 528 | Dependency dep = entry.getValue(); |
| 529 | SkyKey depKey = TO_KEYS.apply(dep); |
| 530 | ConfiguredTarget depConfiguredTarget = depConfiguredTargetMap.get(depKey); |
Lukacs Berki | 047b742 | 2015-07-22 08:32:37 +0000 | [diff] [blame] | 531 | result.put(entry.getKey(), |
| 532 | RuleConfiguredTarget.mergeAspects(depConfiguredTarget, depAspectMap.get(depKey))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | return result; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Given a list of {@link Dependency} objects, returns a multimap from the {@link SkyKey} of the |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 540 | * dependency to the {@link ConfiguredAspect} instances that should be merged into it. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 541 | * |
| 542 | * <p>Returns null if the required aspects are not computed yet. |
| 543 | */ |
| 544 | @Nullable |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 545 | private static ListMultimap<SkyKey, ConfiguredAspect> resolveAspectDependencies( |
| 546 | Environment env, |
| 547 | Map<SkyKey, ConfiguredTarget> configuredTargetMap, |
| 548 | Iterable<Dependency> deps, |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 549 | NestedSetBuilder<Package> transitivePackages) |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 550 | throws AspectCreationException { |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 551 | ListMultimap<SkyKey, ConfiguredAspect> result = ArrayListMultimap.create(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 552 | Set<SkyKey> aspectKeys = new HashSet<>(); |
| 553 | for (Dependency dep : deps) { |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 554 | for (Aspect depAspect : dep.getAspects()) { |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 555 | aspectKeys.add(createAspectKey(dep.getLabel(), dep.getConfiguration(), depAspect)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | Map<SkyKey, ValueOrException3< |
| 560 | AspectCreationException, NoSuchThingException, ConfiguredValueCreationException>> |
| 561 | depAspects = env.getValuesOrThrow(aspectKeys, AspectCreationException.class, |
| 562 | NoSuchThingException.class, ConfiguredValueCreationException.class); |
| 563 | |
| 564 | for (Dependency dep : deps) { |
| 565 | SkyKey depKey = TO_KEYS.apply(dep); |
Marian Lobur | 2099da0 | 2015-05-12 14:42:01 +0000 | [diff] [blame] | 566 | // If the same target was declared in different attributes of rule, we should not process it |
| 567 | // twice. |
| 568 | if (result.containsKey(depKey)) { |
| 569 | continue; |
| 570 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 571 | ConfiguredTarget depConfiguredTarget = configuredTargetMap.get(depKey); |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 572 | for (Aspect depAspect : dep.getAspects()) { |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 573 | if (!aspectMatchesConfiguredTarget(depConfiguredTarget, depAspect)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 574 | continue; |
| 575 | } |
| 576 | |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 577 | SkyKey aspectKey = createAspectKey(dep.getLabel(), dep.getConfiguration(), depAspect); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 578 | AspectValue aspectValue = null; |
| 579 | try { |
| 580 | aspectValue = (AspectValue) depAspects.get(aspectKey).get(); |
| 581 | } catch (ConfiguredValueCreationException e) { |
| 582 | // The configured target should have been created in resolveConfiguredTargetDependencies() |
| 583 | throw new IllegalStateException(e); |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 584 | } catch (NoSuchThingException e) { |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 585 | throw new AspectCreationException( |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 586 | String.format( |
| 587 | "Evaluation of aspect %s on %s failed: %s", |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 588 | depAspect.getDefinition().getName(), |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 589 | dep.getLabel(), |
| 590 | e.toString())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | if (aspectValue == null) { |
| 594 | // Dependent aspect has either not been computed yet or is in error. |
| 595 | return null; |
| 596 | } |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 597 | result.put(depKey, aspectValue.getConfiguredAspect()); |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 598 | transitivePackages.addTransitive(aspectValue.getTransitivePackages()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 599 | } |
| 600 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 601 | return result; |
| 602 | } |
| 603 | |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 604 | public static SkyKey createAspectKey( |
| 605 | Label label, BuildConfiguration buildConfiguration, Aspect depAspect) { |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 606 | return AspectValue.key(label, |
| 607 | buildConfiguration, |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 608 | depAspect.getAspectClass(), |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 609 | depAspect.getParameters()); |
| 610 | } |
| 611 | |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 612 | private static boolean aspectMatchesConfiguredTarget(ConfiguredTarget dep, Aspect aspectClass) { |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 613 | AspectDefinition aspectDefinition = aspectClass.getDefinition(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 614 | for (Class<?> provider : aspectDefinition.getRequiredProviders()) { |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 615 | if (dep.getProvider(provider.asSubclass(TransitiveInfoProvider.class)) == null) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 616 | return false; |
| 617 | } |
| 618 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 619 | return true; |
| 620 | } |
| 621 | |
| 622 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 623 | * Returns the set of {@link ConfigMatchingProvider}s that key the configurable attributes |
| 624 | * used by this rule. |
| 625 | * |
| 626 | * <p>>If the configured targets supplying those providers aren't yet resolved by the |
| 627 | * dependency resolver, returns null. |
| 628 | */ |
| 629 | @Nullable |
| 630 | static Set<ConfigMatchingProvider> getConfigConditions(Target target, Environment env, |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 631 | SkyframeDependencyResolver resolver, TargetAndConfiguration ctgValue, |
| 632 | NestedSetBuilder<Package> transitivePackages) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 633 | throws DependencyEvaluationException { |
| 634 | if (!(target instanceof Rule)) { |
| 635 | return ImmutableSet.of(); |
| 636 | } |
| 637 | |
| 638 | ImmutableSet.Builder<ConfigMatchingProvider> configConditions = ImmutableSet.builder(); |
| 639 | |
| 640 | // Collect the labels of the configured targets we need to resolve. |
| 641 | ListMultimap<Attribute, LabelAndConfiguration> configLabelMap = ArrayListMultimap.create(); |
| 642 | RawAttributeMapper attributeMap = RawAttributeMapper.of(((Rule) target)); |
| 643 | for (Attribute a : ((Rule) target).getAttributes()) { |
| 644 | for (Label configLabel : attributeMap.getConfigurabilityKeys(a.getName(), a.getType())) { |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 645 | if (!BuildType.Selector.isReservedLabel(configLabel)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 646 | configLabelMap.put(a, LabelAndConfiguration.of( |
Lukacs Berki | fc2a01b | 2016-01-14 11:56:32 +0000 | [diff] [blame] | 647 | target.getLabel().resolveRepositoryRelative(configLabel), |
| 648 | ctgValue.getConfiguration())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | } |
| 652 | if (configLabelMap.isEmpty()) { |
| 653 | return ImmutableSet.of(); |
| 654 | } |
| 655 | |
| 656 | // Collect the corresponding Skyframe configured target values. Abort early if they haven't |
| 657 | // been computed yet. |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 658 | Collection<Dependency> configValueNames = resolver.resolveRuleLabels(ctgValue, configLabelMap); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 659 | |
| 660 | // No need to get new configs from Skyframe - config_setting rules always use the current |
| 661 | // target's config. |
| 662 | // TODO(bazel-team): remove the need for this special transformation. We can probably do this by |
| 663 | // simply passing this through trimConfigurations. |
| 664 | BuildConfiguration targetConfig = ctgValue.getConfiguration(); |
| 665 | if (targetConfig != null && targetConfig.useDynamicConfigurations()) { |
| 666 | ImmutableList.Builder<Dependency> staticConfigs = ImmutableList.builder(); |
| 667 | for (Dependency dep : configValueNames) { |
| 668 | staticConfigs.add(new Dependency(dep.getLabel(), targetConfig, dep.getAspects())); |
| 669 | } |
| 670 | configValueNames = staticConfigs.build(); |
| 671 | } |
| 672 | |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 673 | Map<SkyKey, ConfiguredTarget> configValues = resolveConfiguredTargetDependencies( |
| 674 | env, configValueNames, transitivePackages); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 675 | if (configValues == null) { |
| 676 | return null; |
| 677 | } |
| 678 | |
| 679 | // Get the configured targets as ConfigMatchingProvider interfaces. |
| 680 | for (Dependency entry : configValueNames) { |
| 681 | ConfiguredTarget value = configValues.get(TO_KEYS.apply(entry)); |
| 682 | // The code above guarantees that value is non-null here. |
| 683 | ConfigMatchingProvider provider = value.getProvider(ConfigMatchingProvider.class); |
| 684 | if (provider != null) { |
| 685 | configConditions.add(provider); |
| 686 | } else { |
| 687 | // Not a valid provider for configuration conditions. |
| 688 | String message = |
| 689 | entry.getLabel() + " is not a valid configuration key for " + target.getLabel(); |
| 690 | env.getListener().handle(Event.error(TargetUtils.getLocationMaybe(target), message)); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 691 | throw new DependencyEvaluationException(new ConfiguredValueCreationException( |
| 692 | message, target.getLabel())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | return configConditions.build(); |
| 697 | } |
| 698 | |
| 699 | /*** |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 700 | * Resolves the targets referenced in depValueNames and returns their ConfiguredTarget instances. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 701 | * |
| 702 | * <p>Returns null if not all instances are available yet. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 703 | */ |
| 704 | @Nullable |
| 705 | private static Map<SkyKey, ConfiguredTarget> resolveConfiguredTargetDependencies( |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 706 | Environment env, Collection<Dependency> deps, NestedSetBuilder<Package> transitivePackages) |
| 707 | throws DependencyEvaluationException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 708 | boolean ok = !env.valuesMissing(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 709 | Iterable<SkyKey> depKeys = Iterables.transform(deps, TO_KEYS); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 710 | Map<SkyKey, ValueOrException<ConfiguredValueCreationException>> depValues = |
| 711 | env.getValuesOrThrow(depKeys, ConfiguredValueCreationException.class); |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 712 | Map<SkyKey, ConfiguredTarget> result = Maps.newHashMapWithExpectedSize(depValues.size()); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 713 | for (Map.Entry<SkyKey, ValueOrException<ConfiguredValueCreationException>> entry : |
| 714 | depValues.entrySet()) { |
| 715 | ConfiguredTargetValue depValue; |
| 716 | try { |
| 717 | depValue = (ConfiguredTargetValue) entry.getValue().get(); |
| 718 | } catch (ConfiguredValueCreationException e) { |
| 719 | throw new DependencyEvaluationException(e); |
| 720 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 721 | if (depValue == null) { |
| 722 | ok = false; |
| 723 | } else { |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 724 | result.put(entry.getKey(), depValue.getConfiguredTarget()); |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 725 | transitivePackages.addTransitive(depValue.getTransitivePackages()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 726 | } |
| 727 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 728 | if (!ok) { |
| 729 | return null; |
| 730 | } else { |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 731 | return result; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
| 735 | |
| 736 | @Override |
| 737 | public String extractTag(SkyKey skyKey) { |
| 738 | return Label.print(((ConfiguredTargetKey) skyKey.argument()).getLabel()); |
| 739 | } |
| 740 | |
| 741 | @Nullable |
| 742 | private ConfiguredTargetValue createConfiguredTarget(SkyframeBuildView view, |
| 743 | Environment env, Target target, BuildConfiguration configuration, |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 744 | ListMultimap<Attribute, ConfiguredTarget> depValueMap, |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 745 | Set<ConfigMatchingProvider> configConditions, |
| 746 | NestedSetBuilder<Package> transitivePackages) |
Greg Estren | 9eb1cf0 | 2015-06-26 22:18:35 +0000 | [diff] [blame] | 747 | throws ConfiguredTargetFunctionException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 748 | StoredEventHandler events = new StoredEventHandler(); |
| 749 | BuildConfiguration ownerConfig = (configuration == null) |
| 750 | ? null : configuration.getArtifactOwnerConfiguration(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 751 | CachingAnalysisEnvironment analysisEnvironment = view.createAnalysisEnvironment( |
| 752 | new ConfiguredTargetKey(target.getLabel(), ownerConfig), false, |
Ulf Adams | bfb76a9 | 2015-04-22 20:44:35 +0000 | [diff] [blame] | 753 | events, env, configuration); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 754 | if (env.valuesMissing()) { |
| 755 | return null; |
| 756 | } |
| 757 | |
| 758 | ConfiguredTarget configuredTarget = view.createConfiguredTarget(target, configuration, |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 759 | analysisEnvironment, depValueMap, configConditions); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 760 | |
| 761 | events.replayOn(env.getListener()); |
| 762 | if (events.hasErrors()) { |
| 763 | analysisEnvironment.disable(target); |
| 764 | throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException( |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 765 | "Analysis of target '" + target.getLabel() + "' failed; build aborted", |
| 766 | target.getLabel())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 767 | } |
| 768 | Preconditions.checkState(!analysisEnvironment.hasErrors(), |
| 769 | "Analysis environment hasError() but no errors reported"); |
| 770 | if (env.valuesMissing()) { |
| 771 | return null; |
| 772 | } |
| 773 | |
| 774 | analysisEnvironment.disable(target); |
| 775 | Preconditions.checkNotNull(configuredTarget, target); |
| 776 | |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 777 | Map<Artifact, Action> generatingActions; |
| 778 | // Check for conflicting actions within this configured target (that indicates a bug in the |
| 779 | // rule implementation). |
Janak Ramakrishnan | b3a6ca7 | 2015-03-27 20:07:28 +0000 | [diff] [blame] | 780 | try { |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 781 | generatingActions = filterSharedActionsAndThrowIfConflict(analysisEnvironment.getRegisteredActions()); |
Janak Ramakrishnan | b3a6ca7 | 2015-03-27 20:07:28 +0000 | [diff] [blame] | 782 | } catch (ActionConflictException e) { |
| 783 | throw new ConfiguredTargetFunctionException(e); |
| 784 | } |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 785 | return new ConfiguredTargetValue( |
| 786 | configuredTarget, generatingActions, transitivePackages.build()); |
Janak Ramakrishnan | b3a6ca7 | 2015-03-27 20:07:28 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | static Map<Artifact, Action> filterSharedActionsAndThrowIfConflict(Iterable<Action> actions) |
| 790 | throws ActionConflictException { |
| 791 | Map<Artifact, Action> generatingActions = new HashMap<>(); |
| 792 | for (Action action : actions) { |
| 793 | for (Artifact artifact : action.getOutputs()) { |
| 794 | Action previousAction = generatingActions.put(artifact, action); |
| 795 | if (previousAction != null && previousAction != action |
| 796 | && !Actions.canBeShared(previousAction, action)) { |
| 797 | throw new ActionConflictException(artifact, previousAction, action); |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | return generatingActions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /** |
| 805 | * An exception indicating that there was a problem during the construction of |
| 806 | * a ConfiguredTargetValue. |
| 807 | */ |
| 808 | public static final class ConfiguredValueCreationException extends Exception { |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 809 | // TODO(ulfjack): Collect all analysis root causes, not just the first one. |
| 810 | private final Label analysisRootCause; |
| 811 | |
| 812 | public ConfiguredValueCreationException(String message, Label currentTarget) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 813 | super(message); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 814 | this.analysisRootCause = Preconditions.checkNotNull(currentTarget); |
| 815 | } |
| 816 | |
| 817 | public Label getAnalysisRootCause() { |
| 818 | return analysisRootCause; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * Used to declare all the exception types that can be wrapped in the exception thrown by |
| 824 | * {@link ConfiguredTargetFunction#compute}. |
| 825 | */ |
| 826 | public static final class ConfiguredTargetFunctionException extends SkyFunctionException { |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 827 | public ConfiguredTargetFunctionException(NoSuchThingException e) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 828 | super(e, Transience.PERSISTENT); |
| 829 | } |
| 830 | |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame^] | 831 | private ConfiguredTargetFunctionException(ConfiguredValueCreationException e) { |
| 832 | super(e, Transience.PERSISTENT); |
Ulf Adams | 3ab82f7 | 2015-09-04 12:10:53 +0000 | [diff] [blame] | 833 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 834 | |
Janak Ramakrishnan | b3a6ca7 | 2015-03-27 20:07:28 +0000 | [diff] [blame] | 835 | private ConfiguredTargetFunctionException(ActionConflictException e) { |
| 836 | super(e, Transience.PERSISTENT); |
| 837 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 838 | } |
| 839 | } |