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 | |
Greg Estren | ad1a77e | 2016-09-28 14:49:12 +0000 | [diff] [blame] | 16 | import com.google.common.annotations.VisibleForTesting; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.common.base.Function; |
Greg Estren | 2bc8838 | 2016-08-02 21:45:35 +0000 | [diff] [blame] | 18 | import com.google.common.base.Joiner; |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 19 | import com.google.common.base.Predicate; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 20 | import com.google.common.base.Supplier; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 21 | import com.google.common.base.Verify; |
Greg Estren | ad1a77e | 2016-09-28 14:49:12 +0000 | [diff] [blame] | 22 | import com.google.common.base.VerifyException; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableList; |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.common.collect.Iterables; |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 26 | import com.google.common.collect.LinkedHashMultimap; |
Greg Estren | 123de31 | 2015-11-17 19:47:58 +0000 | [diff] [blame] | 27 | import com.google.common.collect.LinkedListMultimap; |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 28 | import com.google.common.collect.Maps; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 29 | import com.google.common.collect.Multimap; |
Greg Estren | 2bc8838 | 2016-08-02 21:45:35 +0000 | [diff] [blame] | 30 | import com.google.common.collect.Sets; |
Janak Ramakrishnan | b3a6ca7 | 2015-03-27 20:07:28 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.actions.Actions; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.Actions.GeneratingActions; |
Janak Ramakrishnan | b3a6ca7 | 2015-03-27 20:07:28 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException; |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.analysis.AspectCollection; |
| 35 | import com.google.devtools.build.lib.analysis.AspectCollection.AspectDeps; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | import com.google.devtools.build.lib.analysis.CachingAnalysisEnvironment; |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.analysis.ConfiguredAspect; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
gregce | f19fcfe | 2017-06-02 16:04:07 -0400 | [diff] [blame] | 39 | import com.google.devtools.build.lib.analysis.ConfiguredTargetFactory; |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.analysis.Dependency; |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | import com.google.devtools.build.lib.analysis.LabelAndConfiguration; |
Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.analysis.MergedConfiguredTarget; |
Dmitry Lomov | 9b2fc5c | 2016-11-11 11:18:48 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.analysis.MergedConfiguredTarget.DuplicateException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | import com.google.devtools.build.lib.analysis.TargetAndConfiguration; |
| 46 | import com.google.devtools.build.lib.analysis.TransitiveInfoProvider; |
| 47 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 48 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.analysis.config.HostTransition; |
| 51 | import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; |
| 52 | import com.google.devtools.build.lib.analysis.config.PatchTransition; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 53 | import com.google.devtools.build.lib.cmdline.Label; |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 54 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 55 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.lib.collect.nestedset.Order; |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 57 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | import com.google.devtools.build.lib.events.Event; |
| 59 | import com.google.devtools.build.lib.events.StoredEventHandler; |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 60 | import com.google.devtools.build.lib.packages.Aspect; |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 61 | import com.google.devtools.build.lib.packages.AspectDescriptor; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | import com.google.devtools.build.lib.packages.Attribute; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 63 | import com.google.devtools.build.lib.packages.BuildType; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
| 65 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 66 | import com.google.devtools.build.lib.packages.Package; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | import com.google.devtools.build.lib.packages.RawAttributeMapper; |
| 68 | import com.google.devtools.build.lib.packages.Rule; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 69 | import com.google.devtools.build.lib.packages.RuleClassProvider; |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 70 | import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 71 | import com.google.devtools.build.lib.packages.Target; |
| 72 | import com.google.devtools.build.lib.packages.TargetUtils; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | import com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException; |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 74 | import com.google.devtools.build.lib.skyframe.AspectValue.AspectKey; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor.BuildViewProvider; |
| 76 | import com.google.devtools.build.lib.syntax.EvalException; |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 77 | import com.google.devtools.build.lib.util.OrderedSetMultimap; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 78 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | import com.google.devtools.build.skyframe.SkyFunction; |
| 80 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 81 | import com.google.devtools.build.skyframe.SkyKey; |
| 82 | import com.google.devtools.build.skyframe.SkyValue; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 83 | import com.google.devtools.build.skyframe.ValueOrException; |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 84 | import com.google.devtools.build.skyframe.ValueOrException2; |
Greg Estren | f461230 | 2016-09-29 17:29:02 +0000 | [diff] [blame] | 85 | import java.util.ArrayList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | import java.util.Collection; |
Greg Estren | dc38824 | 2016-12-07 16:47:02 +0000 | [diff] [blame] | 87 | import java.util.Collections; |
| 88 | import java.util.Comparator; |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 89 | import java.util.HashMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | import java.util.HashSet; |
Greg Estren | f461230 | 2016-09-29 17:29:02 +0000 | [diff] [blame] | 91 | import java.util.Iterator; |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 92 | import java.util.LinkedHashMap; |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 93 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 94 | import java.util.Map; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 95 | import java.util.Objects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | import java.util.Set; |
Janak Ramakrishnan | bede7b4 | 2016-11-17 18:34:08 +0000 | [diff] [blame] | 97 | import java.util.concurrent.Semaphore; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 98 | import javax.annotation.Nullable; |
| 99 | |
| 100 | /** |
| 101 | * SkyFunction for {@link ConfiguredTargetValue}s. |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 102 | * |
| 103 | * This class, together with {@link AspectFunction} drives the analysis phase. For more information, |
| 104 | * see {@link com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory}. |
| 105 | * |
| 106 | * @see com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 107 | */ |
| 108 | final class ConfiguredTargetFunction implements SkyFunction { |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 109 | // This construction is a bit funky, but guarantees that the Object reference here is globally |
| 110 | // unique. |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 111 | static final ImmutableMap<Label, ConfigMatchingProvider> NO_CONFIG_CONDITIONS = |
| 112 | ImmutableMap.<Label, ConfigMatchingProvider>of(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Exception class that signals an error during the evaluation of a dependency. |
| 116 | */ |
| 117 | public static class DependencyEvaluationException extends Exception { |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 118 | public DependencyEvaluationException(InvalidConfigurationException cause) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 119 | super(cause); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 122 | public DependencyEvaluationException(ConfiguredValueCreationException cause) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 123 | super(cause); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 126 | public DependencyEvaluationException(InconsistentAspectOrderException cause) { |
| 127 | super(cause); |
| 128 | } |
| 129 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 130 | @Override |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 131 | public synchronized Exception getCause() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 132 | return (Exception) super.getCause(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private static final Function<Dependency, SkyKey> TO_KEYS = |
| 137 | new Function<Dependency, SkyKey>() { |
| 138 | @Override |
| 139 | public SkyKey apply(Dependency input) { |
| 140 | return ConfiguredTargetValue.key(input.getLabel(), input.getConfiguration()); |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | private final BuildViewProvider buildViewProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 145 | private final RuleClassProvider ruleClassProvider; |
Janak Ramakrishnan | bede7b4 | 2016-11-17 18:34:08 +0000 | [diff] [blame] | 146 | private final Semaphore cpuBoundSemaphore; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 147 | private final Supplier<Boolean> removeActionsAfterEvaluation; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 148 | |
Janak Ramakrishnan | bede7b4 | 2016-11-17 18:34:08 +0000 | [diff] [blame] | 149 | ConfiguredTargetFunction( |
| 150 | BuildViewProvider buildViewProvider, |
| 151 | RuleClassProvider ruleClassProvider, |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 152 | Semaphore cpuBoundSemaphore, |
| 153 | Supplier<Boolean> removeActionsAfterEvaluation) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 154 | this.buildViewProvider = buildViewProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 155 | this.ruleClassProvider = ruleClassProvider; |
Janak Ramakrishnan | bede7b4 | 2016-11-17 18:34:08 +0000 | [diff] [blame] | 156 | this.cpuBoundSemaphore = cpuBoundSemaphore; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 157 | this.removeActionsAfterEvaluation = Preconditions.checkNotNull(removeActionsAfterEvaluation); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Greg Estren | b5692bd | 2016-06-08 21:09:11 +0000 | [diff] [blame] | 160 | private static boolean useDynamicConfigurations(BuildConfiguration config) { |
| 161 | return config != null && config.useDynamicConfigurations(); |
| 162 | } |
| 163 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 164 | @Override |
| 165 | public SkyValue compute(SkyKey key, Environment env) throws ConfiguredTargetFunctionException, |
| 166 | InterruptedException { |
| 167 | SkyframeBuildView view = buildViewProvider.getSkyframeBuildView(); |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 168 | NestedSetBuilder<Package> transitivePackages = NestedSetBuilder.stableOrder(); |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 169 | NestedSetBuilder<Label> transitiveLoadingRootCauses = NestedSetBuilder.stableOrder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 170 | ConfiguredTargetKey configuredTargetKey = (ConfiguredTargetKey) key.argument(); |
| 171 | LabelAndConfiguration lc = LabelAndConfiguration.of( |
| 172 | configuredTargetKey.getLabel(), configuredTargetKey.getConfiguration()); |
| 173 | |
| 174 | BuildConfiguration configuration = lc.getConfiguration(); |
| 175 | |
| 176 | PackageValue packageValue = |
| 177 | (PackageValue) env.getValue(PackageValue.key(lc.getLabel().getPackageIdentifier())); |
| 178 | if (packageValue == null) { |
| 179 | return null; |
| 180 | } |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 181 | |
| 182 | // TODO(ulfjack): This tries to match the logic in TransitiveTargetFunction / |
| 183 | // TargetMarkerFunction. Maybe we can merge the two? |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 184 | Package pkg = packageValue.getPackage(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 185 | Target target; |
| 186 | try { |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 187 | target = pkg.getTarget(lc.getLabel().getName()); |
Ulf Adams | 9668361 | 2016-01-25 09:04:54 +0000 | [diff] [blame] | 188 | } catch (NoSuchTargetException e) { |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 189 | throw new ConfiguredTargetFunctionException( |
| 190 | new ConfiguredValueCreationException(e.getMessage())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 191 | } |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 192 | if (pkg.containsErrors()) { |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 193 | transitiveLoadingRootCauses.add(lc.getLabel()); |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 194 | } |
| 195 | transitivePackages.add(pkg); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 196 | // TODO(bazel-team): This is problematic - we create the right key, but then end up with a value |
| 197 | // that doesn't match; we can even have the same value multiple times. However, I think it's |
| 198 | // only triggered in tests (i.e., in normal operation, the configuration passed in is already |
| 199 | // null). |
Greg Estren | a6c8896 | 2015-09-28 19:35:18 +0000 | [diff] [blame] | 200 | if (!target.isConfigurable()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 201 | configuration = null; |
| 202 | } |
Greg Estren | b5692bd | 2016-06-08 21:09:11 +0000 | [diff] [blame] | 203 | |
| 204 | // This line is only needed for accurate error messaging. Say this target has a circular |
| 205 | // dependency with one of its deps. With this line, loading this target fails so Bazel |
| 206 | // associates the corresponding error with this target, as expected. Without this line, |
| 207 | // the first TransitiveTargetValue call happens on its dep (in trimConfigurations), so Bazel |
| 208 | // associates the error with the dep, which is misleading. |
Greg Estren | 8d76f07 | 2016-09-23 23:24:59 +0000 | [diff] [blame] | 209 | if (useDynamicConfigurations(configuration) && configuration.trimConfigurations() |
Greg Estren | b5692bd | 2016-06-08 21:09:11 +0000 | [diff] [blame] | 210 | && env.getValue(TransitiveTargetValue.key(lc.getLabel())) == null) { |
| 211 | return null; |
| 212 | } |
| 213 | |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 214 | TargetAndConfiguration ctgValue = new TargetAndConfiguration(target, configuration); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 215 | |
| 216 | SkyframeDependencyResolver resolver = view.createDependencyResolver(env); |
Janak Ramakrishnan | bede7b4 | 2016-11-17 18:34:08 +0000 | [diff] [blame] | 217 | |
| 218 | // TODO(janakr): this acquire() call may tie up this thread indefinitely, reducing the |
| 219 | // parallelism of Skyframe. This is a strict improvement over the prior state of the code, in |
| 220 | // which we ran with #processors threads, but ideally we would call #tryAcquire here, and if we |
| 221 | // failed, would exit this SkyFunction and restart it when permits were available. |
| 222 | cpuBoundSemaphore.acquire(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 223 | try { |
| 224 | // Get the configuration targets that trigger this rule's configurable attributes. |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 225 | ImmutableMap<Label, ConfigMatchingProvider> configConditions = getConfigConditions( |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 226 | ctgValue.getTarget(), env, resolver, ctgValue, transitivePackages, |
| 227 | transitiveLoadingRootCauses); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 228 | if (env.valuesMissing()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 229 | return null; |
| 230 | } |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 231 | // TODO(ulfjack): ConfiguredAttributeMapper (indirectly used from computeDependencies) isn't |
| 232 | // safe to use if there are missing config conditions, so we stop here, but only if there are |
| 233 | // config conditions - though note that we can't check if configConditions is non-empty - it |
| 234 | // may be empty for other reasons. It would be better to continue here so that we can collect |
| 235 | // more root causes during computeDependencies. |
| 236 | // Note that this doesn't apply to AspectFunction, because aspects can't have configurable |
| 237 | // attributes. |
| 238 | if (!transitiveLoadingRootCauses.isEmpty() && configConditions != NO_CONFIG_CONDITIONS) { |
| 239 | throw new ConfiguredTargetFunctionException( |
| 240 | new ConfiguredValueCreationException(transitiveLoadingRootCauses.build())); |
| 241 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 242 | |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 243 | OrderedSetMultimap<Attribute, ConfiguredTarget> depValueMap = |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 244 | computeDependencies( |
| 245 | env, |
| 246 | resolver, |
| 247 | ctgValue, |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 248 | ImmutableList.<Aspect>of(), |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 249 | configConditions, |
| 250 | ruleClassProvider, |
| 251 | view.getHostConfiguration(configuration), |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 252 | transitivePackages, |
| 253 | transitiveLoadingRootCauses); |
| 254 | if (env.valuesMissing()) { |
| 255 | return null; |
| 256 | } |
| 257 | if (!transitiveLoadingRootCauses.isEmpty()) { |
| 258 | throw new ConfiguredTargetFunctionException( |
| 259 | new ConfiguredValueCreationException(transitiveLoadingRootCauses.build())); |
| 260 | } |
| 261 | Preconditions.checkNotNull(depValueMap); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 262 | ConfiguredTargetValue ans = createConfiguredTarget( |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 263 | view, env, target, configuration, depValueMap, configConditions, transitivePackages); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 264 | return ans; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 265 | } catch (DependencyEvaluationException e) { |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 266 | if (e.getCause() instanceof ConfiguredValueCreationException) { |
| 267 | throw new ConfiguredTargetFunctionException( |
| 268 | (ConfiguredValueCreationException) e.getCause()); |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 269 | } else if (e.getCause() instanceof InconsistentAspectOrderException) { |
| 270 | InconsistentAspectOrderException cause = (InconsistentAspectOrderException) e.getCause(); |
| 271 | throw new ConfiguredTargetFunctionException( |
| 272 | new ConfiguredValueCreationException(cause.getMessage(), target.getLabel())); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 273 | } else { |
| 274 | // Cast to InvalidConfigurationException as a consistency check. If you add any |
| 275 | // DependencyEvaluationException constructors, you may need to change this code, too. |
| 276 | InvalidConfigurationException cause = (InvalidConfigurationException) e.getCause(); |
| 277 | throw new ConfiguredTargetFunctionException( |
| 278 | new ConfiguredValueCreationException(cause.getMessage(), target.getLabel())); |
| 279 | } |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 280 | } catch (AspectCreationException e) { |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 281 | // getAnalysisRootCause may be null if the analysis of the aspect itself failed. |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 282 | Label analysisRootCause = target.getLabel(); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 283 | if (e.getAnalysisRootCause() != null) { |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 284 | analysisRootCause = e.getAnalysisRootCause(); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 285 | } |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 286 | throw new ConfiguredTargetFunctionException( |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 287 | new ConfiguredValueCreationException(e.getMessage(), analysisRootCause)); |
Janak Ramakrishnan | bede7b4 | 2016-11-17 18:34:08 +0000 | [diff] [blame] | 288 | } finally { |
| 289 | cpuBoundSemaphore.release(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Computes the direct dependencies of a node in the configured target graph (a configured |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 295 | * target or an aspects). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 296 | * |
| 297 | * <p>Returns null if Skyframe hasn't evaluated the required dependencies yet. In this case, the |
| 298 | * caller should also return null to Skyframe. |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 299 | * @param env the Skyframe environment |
Greg Estren | 86045da | 2016-12-14 19:46:48 +0000 | [diff] [blame] | 300 | * @param resolver the dependency resolver |
| 301 | * @param ctgValue the label and the configuration of the node |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 302 | * @param aspects |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 303 | * @param configConditions the configuration conditions for evaluating the attributes of the node |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 304 | * @param ruleClassProvider rule class provider for determining the right configuration fragments |
| 305 | * to apply to deps |
| 306 | * @param hostConfiguration the host configuration. There's a noticeable performance hit from |
| 307 | * instantiating this on demand for every dependency that wants it, so it's best to compute |
| 308 | * 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] | 309 | * */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 310 | @Nullable |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 311 | static OrderedSetMultimap<Attribute, ConfiguredTarget> computeDependencies( |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 312 | Environment env, |
| 313 | SkyframeDependencyResolver resolver, |
| 314 | TargetAndConfiguration ctgValue, |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 315 | Iterable<Aspect> aspects, |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 316 | ImmutableMap<Label, ConfigMatchingProvider> configConditions, |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 317 | RuleClassProvider ruleClassProvider, |
| 318 | BuildConfiguration hostConfiguration, |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 319 | NestedSetBuilder<Package> transitivePackages, |
| 320 | NestedSetBuilder<Label> transitiveLoadingRootCauses) |
Dmitry Lomov | 9b2fc5c | 2016-11-11 11:18:48 +0000 | [diff] [blame] | 321 | throws DependencyEvaluationException, ConfiguredTargetFunctionException, |
| 322 | AspectCreationException, InterruptedException { |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 323 | // Create the map from attributes to set of (target, configuration) pairs. |
| 324 | OrderedSetMultimap<Attribute, Dependency> depValueNames; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 325 | try { |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 326 | depValueNames = resolver.dependentNodeMap( |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 327 | ctgValue, hostConfiguration, aspects, configConditions, transitiveLoadingRootCauses); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 328 | } catch (EvalException e) { |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 329 | // 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] | 330 | env.getListener().handle(Event.error(e.getLocation(), e.getMessage())); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 331 | throw new DependencyEvaluationException( |
| 332 | new ConfiguredValueCreationException(e.print(), ctgValue.getLabel())); |
Greg Estren | 373e3e2 | 2016-08-09 22:36:51 +0000 | [diff] [blame] | 333 | } catch (InvalidConfigurationException e) { |
| 334 | throw new DependencyEvaluationException(e); |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 335 | } catch (InconsistentAspectOrderException e) { |
| 336 | env.getListener().handle(Event.error(e.getLocation(), e.getMessage())); |
| 337 | throw new DependencyEvaluationException(e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 340 | // Trim each dep's configuration so it only includes the fragments needed by its transitive |
| 341 | // closure (only dynamic configurations support this). |
Greg Estren | b5692bd | 2016-06-08 21:09:11 +0000 | [diff] [blame] | 342 | if (useDynamicConfigurations(ctgValue.getConfiguration())) { |
Greg Estren | 1d8ba90 | 2016-09-21 21:18:19 +0000 | [diff] [blame] | 343 | depValueNames = getDynamicConfigurations(env, ctgValue, depValueNames, hostConfiguration, |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 344 | ruleClassProvider); |
Greg Estren | 86045da | 2016-12-14 19:46:48 +0000 | [diff] [blame] | 345 | // It's important that we don't use "if (env.missingValues()) { return null }" here (or |
| 346 | // in the following lines). See the comments in getDynamicConfigurations' Skyframe call |
| 347 | // for explanation. |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 348 | if (depValueNames == null) { |
| 349 | return null; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // Resolve configured target dependencies and handle errors. |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 354 | Map<SkyKey, ConfiguredTarget> depValues = resolveConfiguredTargetDependencies(env, |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 355 | depValueNames.values(), transitivePackages, transitiveLoadingRootCauses); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 356 | if (depValues == null) { |
| 357 | return null; |
| 358 | } |
| 359 | |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 360 | // Resolve required aspects. |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 361 | OrderedSetMultimap<SkyKey, ConfiguredAspect> depAspects = resolveAspectDependencies( |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 362 | env, depValues, depValueNames.values(), transitivePackages); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 363 | if (depAspects == null) { |
| 364 | return null; |
| 365 | } |
| 366 | |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 367 | // Merge the dependent configured targets and aspects into a single map. |
Dmitry Lomov | 9b2fc5c | 2016-11-11 11:18:48 +0000 | [diff] [blame] | 368 | try { |
| 369 | return mergeAspects(depValueNames, depValues, depAspects); |
| 370 | } catch (DuplicateException e) { |
| 371 | env.getListener().handle( |
| 372 | Event.error(ctgValue.getTarget().getLocation(), e.getMessage())); |
| 373 | |
| 374 | throw new ConfiguredTargetFunctionException( |
| 375 | new ConfiguredValueCreationException(e.getMessage(), ctgValue.getLabel())); |
| 376 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /** |
Greg Estren | 1d8ba90 | 2016-09-21 21:18:19 +0000 | [diff] [blame] | 380 | * Helper class for {@link #getDynamicConfigurations} - encapsulates a set of config fragments and |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 381 | * a dynamic transition. This can be used to determine the exact build options needed to |
| 382 | * set a dynamic configuration. |
| 383 | */ |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 384 | @Immutable |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 385 | private static final class FragmentsAndTransition { |
| 386 | // Treat this as immutable. The only reason this isn't an ImmutableSet is because it |
| 387 | // gets bound to a NestedSet.toSet() reference, which returns a Set interface. |
| 388 | final Set<Class<? extends BuildConfiguration.Fragment>> fragments; |
| 389 | final Attribute.Transition transition; |
| 390 | private final int hashCode; |
| 391 | |
| 392 | FragmentsAndTransition(Set<Class<? extends BuildConfiguration.Fragment>> fragments, |
| 393 | Attribute.Transition transition) { |
| 394 | this.fragments = fragments; |
| 395 | this.transition = transition; |
| 396 | hashCode = Objects.hash(this.fragments, this.transition); |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public boolean equals(Object o) { |
| 401 | if (o == this) { |
| 402 | return true; |
| 403 | } else if (o == null) { |
| 404 | return false; |
| 405 | } else { |
| 406 | FragmentsAndTransition other = (FragmentsAndTransition) o; |
| 407 | return other.transition.equals(transition) && other.fragments.equals(fragments); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public int hashCode() { |
| 413 | return hashCode; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /** |
Greg Estren | 1d8ba90 | 2016-09-21 21:18:19 +0000 | [diff] [blame] | 418 | * Helper class for {@link #getDynamicConfigurations} - encapsulates an <attribute, label> pair |
| 419 | * that can be used to map from an input dependency to a trimmed dependency. |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 420 | */ |
| 421 | @Immutable |
| 422 | private static final class AttributeAndLabel { |
| 423 | final Attribute attribute; |
| 424 | final Label label; |
Greg Estren | 13a73e4 | 2016-09-29 18:44:03 +0000 | [diff] [blame] | 425 | Integer hashCode; |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 426 | |
| 427 | AttributeAndLabel(Attribute attribute, Label label) { |
| 428 | this.attribute = attribute; |
| 429 | this.label = label; |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public boolean equals(Object o) { |
| 434 | if (!(o instanceof AttributeAndLabel)) { |
| 435 | return false; |
| 436 | } |
| 437 | AttributeAndLabel other = (AttributeAndLabel) o; |
| 438 | return Objects.equals(other.attribute, attribute) && other.label.equals(label); |
| 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public int hashCode() { |
Greg Estren | 13a73e4 | 2016-09-29 18:44:03 +0000 | [diff] [blame] | 443 | if (hashCode == null) { |
| 444 | // Not every <Attribute, Label> pair gets hashed. So only evaluate for the instances that |
| 445 | // need it. This can significantly reduce the number of evaluations. |
| 446 | hashCode = Objects.hash(this.attribute, this.label); |
| 447 | } |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 448 | return hashCode; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /** |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 453 | * Variation of {@link Multimap#put} that triggers an exception if a value already exists. |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 454 | */ |
Greg Estren | ad1a77e | 2016-09-28 14:49:12 +0000 | [diff] [blame] | 455 | @VisibleForTesting |
| 456 | static <K, V> void putOnlyEntry(Multimap<K, V> map, K key, V value) { |
| 457 | // Performance note: while "Verify.verify(!map.containsKey(key, value), String.format(...)))" |
| 458 | // is simpler code, profiling shows a substantial performance penalty to that approach |
| 459 | // (~10% extra analysis phase time on a simple cc_binary). Most of that is from the cost of |
| 460 | // evaluating value.toString() on every call. This approach essentially eliminates the overhead. |
| 461 | if (map.containsKey(key)) { |
| 462 | throw new VerifyException( |
| 463 | String.format("couldn't insert %s: map already has key %s", |
| 464 | value.toString(), key.toString())); |
| 465 | } |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 466 | map.put(key, value); |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /** |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 470 | * Creates a dynamic configuration for each dep that's custom-fitted specifically for that dep. |
| 471 | * |
| 472 | * <p>More specifically: given a set of {@link Dependency} instances holding dynamic config |
| 473 | * transition requests (e.g. {@link Dependency#hasStaticConfiguration()} == false}), returns |
Greg Estren | 1d8ba90 | 2016-09-21 21:18:19 +0000 | [diff] [blame] | 474 | * equivalent dependencies containing dynamically created configurations applying those |
| 475 | * transitions. If {@link BuildConfiguration.Options#trimConfigurations()} is true, these |
| 476 | * configurations only contain the fragments needed by the dep and its transitive closure. Else |
| 477 | * the configurations unconditionally include all fragments. |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 478 | * |
| 479 | * <p>This method is heavily performance-optimized. Because it, in aggregate, reads over every |
Greg Estren | 86045da | 2016-12-14 19:46:48 +0000 | [diff] [blame] | 480 | * edge in the configured target graph, small inefficiencies can have observable impact on |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 481 | * analysis time. Keep this in mind when making modifications and performance-test any changes you |
| 482 | * make. |
Greg Estren | 86045da | 2016-12-14 19:46:48 +0000 | [diff] [blame] | 483 | * |
| 484 | * @param env Skyframe evaluation environment |
| 485 | * @param ctgValue the label and the configuration of the node |
| 486 | * @param originalDeps the set of configuration transition requests for this target's attributes |
| 487 | * @param hostConfiguration the host configuration |
| 488 | * @param ruleClassProvider the rule class provider for determining the right configuration |
| 489 | * fragments to apply to deps |
| 490 | * |
| 491 | * @return a mapping from each attribute to the {@link BuildConfiguration}s and {@link Label}s |
| 492 | * to use for that attribute's deps. Returns null if not all Skyframe dependencies are |
| 493 | * available yet. |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 494 | */ |
| 495 | @Nullable |
Greg Estren | 1d8ba90 | 2016-09-21 21:18:19 +0000 | [diff] [blame] | 496 | static OrderedSetMultimap<Attribute, Dependency> getDynamicConfigurations( |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 497 | Environment env, |
| 498 | TargetAndConfiguration ctgValue, |
| 499 | OrderedSetMultimap<Attribute, Dependency> originalDeps, |
| 500 | BuildConfiguration hostConfiguration, |
| 501 | RuleClassProvider ruleClassProvider) |
| 502 | throws DependencyEvaluationException, InterruptedException { |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 503 | |
| 504 | // Maps each Skyframe-evaluated BuildConfiguration to the dependencies that need that |
| 505 | // configuration. For cases where Skyframe isn't needed to get the configuration (e.g. when |
| 506 | // 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] | 507 | Multimap<SkyKey, Map.Entry<Attribute, Dependency>> keysToEntries = LinkedListMultimap.create(); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 508 | |
| 509 | // Stores the result of applying a dynamic transition to the current configuration using a |
| 510 | // particular subset of fragments. By caching this, we save from redundantly computing the |
| 511 | // same transition for every dependency edge that requests that transition. This can have |
| 512 | // real effect on analysis time for commonly triggered transitions. |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 513 | // |
| 514 | // Split transitions may map to multiple values. All other transitions map to one. |
| 515 | Map<FragmentsAndTransition, List<BuildOptions>> transitionsMap = new LinkedHashMap<>(); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 516 | |
| 517 | // The fragments used by the current target's configuration. |
| 518 | Set<Class<? extends BuildConfiguration.Fragment>> ctgFragments = |
| 519 | ctgValue.getConfiguration().fragmentClasses(); |
| 520 | BuildOptions ctgOptions = ctgValue.getConfiguration().getOptions(); |
| 521 | |
Greg Estren | dc38824 | 2016-12-07 16:47:02 +0000 | [diff] [blame] | 522 | // Stores the dynamically configured versions of each dependency. This method must preserve the |
| 523 | // original label ordering of each attribute. For example, if originalDeps.get("data") is |
| 524 | // [":a", ":b"], the dynamic variant must also be [":a", ":b"] in the same order. Because we may |
| 525 | // not actualize the results in order (some results need Skyframe-evaluated configurations while |
| 526 | // others can be computed trivially), we dump them all into this map, then as a final step |
| 527 | // iterate through the original list and pluck out values from here for the final value. |
| 528 | // |
| 529 | // For split transitions, originaldeps.get("data") = [":a", ":b"] can produce the output |
| 530 | // [":a"<config1>, ":a"<config2>, ..., ":b"<config1>, ":b"<config2>, ...]. All instances of ":a" |
| 531 | // still appear before all instances of ":b". But the [":a"<config1>, ":a"<config2>"] subset may |
| 532 | // be in any (deterministic) order. In particular, this may not be the same order as |
| 533 | // SplitTransition.split. If needed, this code can be modified to use that order, but that |
| 534 | // involves more runtime in performance-critical code, so we won't make that change without a |
| 535 | // clear need. |
Greg Estren | ad1a77e | 2016-09-28 14:49:12 +0000 | [diff] [blame] | 536 | // |
| 537 | // This map is used heavily by all builds. Inserts and gets should be as fast as possible. |
Greg Estren | dc38824 | 2016-12-07 16:47:02 +0000 | [diff] [blame] | 538 | Multimap<AttributeAndLabel, Dependency> dynamicDeps = LinkedHashMultimap.create(); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 539 | |
Greg Estren | f461230 | 2016-09-29 17:29:02 +0000 | [diff] [blame] | 540 | // Performance optimization: This method iterates over originalDeps twice. By storing |
| 541 | // AttributeAndLabel instances in this list, we avoid having to recreate them the second time |
| 542 | // (particularly avoid recomputing their hash codes). Profiling shows this shaves 25% off this |
| 543 | // method's execution time (at the time of this comment). |
| 544 | ArrayList<AttributeAndLabel> attributesAndLabels = new ArrayList<>(originalDeps.size()); |
| 545 | |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 546 | for (Map.Entry<Attribute, Dependency> depsEntry : originalDeps.entries()) { |
| 547 | Dependency dep = depsEntry.getValue(); |
Greg Estren | e5c9dbe0 | 2015-11-18 20:47:43 +0000 | [diff] [blame] | 548 | AttributeAndLabel attributeAndLabel = |
| 549 | new AttributeAndLabel(depsEntry.getKey(), dep.getLabel()); |
Greg Estren | f461230 | 2016-09-29 17:29:02 +0000 | [diff] [blame] | 550 | attributesAndLabels.add(attributeAndLabel); |
Greg Estren | ad1a77e | 2016-09-28 14:49:12 +0000 | [diff] [blame] | 551 | // Certain targets (like output files) trivially re-use their input configuration. Likewise, |
| 552 | // deps with null configurations (e.g. source files), can be trivially computed. So we skip |
| 553 | // all logic in this method for these cases and just reinsert their original configurations |
Greg Estren | 9e26f0f | 2016-09-29 01:01:57 +0000 | [diff] [blame] | 554 | // back at the end (note that null-configured targets will have a static |
| 555 | // NullConfigurationDependency instead of dynamic |
| 556 | // Dependency(label, transition=Attribute.Configuration.Transition.NULL)). |
Greg Estren | ad1a77e | 2016-09-28 14:49:12 +0000 | [diff] [blame] | 557 | // |
| 558 | // A *lot* of targets have null deps, so this produces real savings. Profiling tests over a |
| 559 | // simple cc_binary show this saves ~1% of total analysis phase time. |
Greg Estren | 9e26f0f | 2016-09-29 01:01:57 +0000 | [diff] [blame] | 560 | if (dep.hasStaticConfiguration()) { |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 561 | continue; |
| 562 | } |
| 563 | |
| 564 | // Figure out the required fragments for this dep and its transitive closure. |
Greg Estren | 1d8ba90 | 2016-09-21 21:18:19 +0000 | [diff] [blame] | 565 | Set<Class<? extends BuildConfiguration.Fragment>> depFragments = |
| 566 | getTransitiveFragments(env, dep.getLabel(), ctgValue.getConfiguration()); |
| 567 | if (depFragments == null) { |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 568 | return null; |
| 569 | } |
Greg Estren | 2bc8838 | 2016-08-02 21:45:35 +0000 | [diff] [blame] | 570 | // TODO(gregce): remove the below call once we have confidence dynamic configurations always |
Greg Estren | ad1a77e | 2016-09-28 14:49:12 +0000 | [diff] [blame] | 571 | // provide needed fragments. This unnecessarily drags performance on the critical path (up |
| 572 | // to 0.5% of total analysis time as profiled over a simple cc_binary). |
| 573 | if (ctgValue.getConfiguration().trimConfigurations()) { |
| 574 | checkForMissingFragments(env, ctgValue, attributeAndLabel.attribute.getName(), dep, |
| 575 | depFragments); |
| 576 | } |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 577 | |
| 578 | boolean sameFragments = depFragments.equals(ctgFragments); |
| 579 | Attribute.Transition transition = dep.getTransition(); |
| 580 | |
| 581 | if (sameFragments) { |
| 582 | if (transition == Attribute.ConfigurationTransition.NONE) { |
| 583 | // The dep uses the same exact configuration. |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 584 | putOnlyEntry( |
Greg Estren | dc38824 | 2016-12-07 16:47:02 +0000 | [diff] [blame] | 585 | dynamicDeps, |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 586 | attributeAndLabel, |
| 587 | Dependency.withConfigurationAndAspects( |
| 588 | dep.getLabel(), ctgValue.getConfiguration(), dep.getAspects())); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 589 | continue; |
| 590 | } else if (transition == HostTransition.INSTANCE) { |
| 591 | // The current rule's host configuration can also be used for the dep. We short-circuit |
| 592 | // the standard transition logic for host transitions because these transitions are |
| 593 | // uniquely frequent. It's possible, e.g., for every node in the configured target graph |
| 594 | // to incur multiple host transitions. So we aggressively optimize to avoid hurting |
| 595 | // analysis time. |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 596 | putOnlyEntry( |
Greg Estren | dc38824 | 2016-12-07 16:47:02 +0000 | [diff] [blame] | 597 | dynamicDeps, |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 598 | attributeAndLabel, |
| 599 | Dependency.withConfigurationAndAspects( |
| 600 | dep.getLabel(), hostConfiguration, dep.getAspects())); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 601 | continue; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | // Apply the transition or use the cached result if it was already applied. |
| 606 | FragmentsAndTransition transitionKey = new FragmentsAndTransition(depFragments, transition); |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 607 | List<BuildOptions> toOptions = transitionsMap.get(transitionKey); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 608 | if (toOptions == null) { |
Greg Estren | b0de919 | 2016-11-16 17:44:16 +0000 | [diff] [blame] | 609 | toOptions = getDynamicTransitionOptions(ctgOptions, transition, depFragments, |
| 610 | ruleClassProvider, !sameFragments); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 611 | transitionsMap.put(transitionKey, toOptions); |
| 612 | } |
| 613 | |
| 614 | // If the transition doesn't change the configuration, trivially re-use the original |
| 615 | // configuration. |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 616 | if (sameFragments && toOptions.size() == 1 |
| 617 | && Iterables.getOnlyElement(toOptions).equals(ctgOptions)) { |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 618 | putOnlyEntry( |
Greg Estren | dc38824 | 2016-12-07 16:47:02 +0000 | [diff] [blame] | 619 | dynamicDeps, |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 620 | attributeAndLabel, |
| 621 | Dependency.withConfigurationAndAspects( |
| 622 | dep.getLabel(), ctgValue.getConfiguration(), dep.getAspects())); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 623 | continue; |
| 624 | } |
| 625 | |
| 626 | // If we get here, we have to get the configuration from Skyframe. |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 627 | for (BuildOptions options : toOptions) { |
| 628 | keysToEntries.put(BuildConfigurationValue.key(depFragments, options), depsEntry); |
| 629 | } |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Greg Estren | 86045da | 2016-12-14 19:46:48 +0000 | [diff] [blame] | 632 | // Get all BuildConfigurations we need from Skyframe. While not every value might be available, |
| 633 | // we don't call env.valuesMissing() here because that could be true from the earlier |
| 634 | // resolver.dependentNodeMap call in computeDependencies, which also calls Skyframe. This method |
| 635 | // doesn't need those missing values, but it still has to be called after |
| 636 | // resolver.dependentNodeMap because it consumes that method's output. The reason the missing |
| 637 | // values don't matter is because resolver.dependentNodeMap still returns "partial" results |
| 638 | // and this method runs over whatever's available. |
| 639 | // |
| 640 | // While there would be no *correctness* harm in nulling out early, there's significant |
| 641 | // *performance* harm. Profiling shows that putting "if (env.valuesMissing()) { return null; }" |
| 642 | // here (or even after resolver.dependentNodeMap) produces a ~30% performance hit on the |
| 643 | // analysis phase. That's because resolveConfiguredTargetDependencies and |
| 644 | // resolveAspectDependencies don't get a chance to make their own Skyframe requests before |
| 645 | // bailing out of this ConfiguredTargetFunction call. Ideally we could batch all requests |
| 646 | // from all methods into a single Skyframe call, but there are enough subtle data flow |
| 647 | // dependencies in ConfiguredTargetFucntion to make that impractical. |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 648 | Map<SkyKey, ValueOrException<InvalidConfigurationException>> depConfigValues = |
| 649 | env.getValuesOrThrow(keysToEntries.keySet(), InvalidConfigurationException.class); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 650 | |
| 651 | // Now fill in the remaining unresolved deps with the now-resolved configurations. |
| 652 | try { |
| 653 | for (Map.Entry<SkyKey, ValueOrException<InvalidConfigurationException>> entry : |
| 654 | depConfigValues.entrySet()) { |
| 655 | SkyKey key = entry.getKey(); |
Greg Estren | 86045da | 2016-12-14 19:46:48 +0000 | [diff] [blame] | 656 | ValueOrException<InvalidConfigurationException> valueOrException = entry.getValue(); |
| 657 | if (valueOrException.get() == null) { |
| 658 | // Instead of env.missingValues(), check for missing values here. This guarantees we only |
| 659 | // null out on missing values from *this specific Skyframe request*. |
| 660 | return null; |
| 661 | } |
| 662 | BuildConfigurationValue trimmedConfig = (BuildConfigurationValue) valueOrException.get(); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 663 | for (Map.Entry<Attribute, Dependency> info : keysToEntries.get(key)) { |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 664 | Dependency originalDep = info.getValue(); |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 665 | AttributeAndLabel attr = new AttributeAndLabel(info.getKey(), originalDep.getLabel()); |
| 666 | Dependency resolvedDep = Dependency.withConfigurationAndAspects(originalDep.getLabel(), |
| 667 | trimmedConfig.getConfiguration(), originalDep.getAspects()); |
Greg Estren | 373e3e2 | 2016-08-09 22:36:51 +0000 | [diff] [blame] | 668 | if (attr.attribute.hasSplitConfigurationTransition()) { |
Greg Estren | dc38824 | 2016-12-07 16:47:02 +0000 | [diff] [blame] | 669 | dynamicDeps.put(attr, resolvedDep); |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 670 | } else { |
Greg Estren | dc38824 | 2016-12-07 16:47:02 +0000 | [diff] [blame] | 671 | putOnlyEntry(dynamicDeps, attr, resolvedDep); |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 672 | } |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | } catch (InvalidConfigurationException e) { |
| 676 | throw new DependencyEvaluationException(e); |
| 677 | } |
| 678 | |
Greg Estren | dc38824 | 2016-12-07 16:47:02 +0000 | [diff] [blame] | 679 | return sortDynamicallyConfiguredDeps(originalDeps, dynamicDeps, attributesAndLabels); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /** |
Greg Estren | 1d8ba90 | 2016-09-21 21:18:19 +0000 | [diff] [blame] | 683 | * Returns the configuration fragments required by a dep and its transitive closure. |
| 684 | * Returns null if Skyframe dependencies aren't yet available. |
| 685 | * |
| 686 | * @param env Skyframe evaluation environment |
| 687 | * @param dep label of the dep to check |
| 688 | * @param parentConfig configuration of the rule depending on the dep |
| 689 | */ |
| 690 | @Nullable |
| 691 | private static Set<Class<? extends BuildConfiguration.Fragment>> getTransitiveFragments( |
| 692 | Environment env, Label dep, BuildConfiguration parentConfig) throws InterruptedException { |
| 693 | Preconditions.checkArgument(parentConfig.useDynamicConfigurations()); |
| 694 | if (!parentConfig.trimConfigurations()) { |
| 695 | return parentConfig.getAllFragments().keySet(); |
| 696 | } |
| 697 | SkyKey fragmentsKey = TransitiveTargetValue.key(dep); |
| 698 | TransitiveTargetValue transitiveDepInfo = (TransitiveTargetValue) env.getValue(fragmentsKey); |
| 699 | if (transitiveDepInfo == null) { |
| 700 | // This should only be possible for tests. In actual runs, this was already called |
| 701 | // as a routine part of the loading phase. |
| 702 | // TODO(bazel-team): check this only occurs in a test context. |
| 703 | return null; |
| 704 | } |
| 705 | return transitiveDepInfo.getTransitiveConfigFragments().toSet(); |
| 706 | } |
| 707 | |
| 708 | /** |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 709 | * Applies a dynamic configuration transition over a set of build options. |
| 710 | * |
Greg Estren | b0de919 | 2016-11-16 17:44:16 +0000 | [diff] [blame] | 711 | * @return the build options for the transitioned configuration. If trimResults is true, |
| 712 | * only options needed by the required fragments are included. Else the same options as the |
| 713 | * original input are included (with different possible values, of course). |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 714 | */ |
Greg Estren | b0de919 | 2016-11-16 17:44:16 +0000 | [diff] [blame] | 715 | static List<BuildOptions> getDynamicTransitionOptions(BuildOptions fromOptions, |
| 716 | Attribute.Transition transition, |
| 717 | Iterable<Class<? extends BuildConfiguration.Fragment>> requiredFragments, |
| 718 | RuleClassProvider ruleClassProvider, boolean trimResults) { |
| 719 | List<BuildOptions> result; |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 720 | if (transition == Attribute.ConfigurationTransition.NONE) { |
Greg Estren | b0de919 | 2016-11-16 17:44:16 +0000 | [diff] [blame] | 721 | result = ImmutableList.<BuildOptions>of(fromOptions); |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 722 | } else if (transition instanceof PatchTransition) { |
| 723 | // TODO(bazel-team): safety-check that this never mutates fromOptions. |
Greg Estren | b0de919 | 2016-11-16 17:44:16 +0000 | [diff] [blame] | 724 | result = ImmutableList.<BuildOptions>of(((PatchTransition) transition).apply(fromOptions)); |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 725 | } else if (transition instanceof Attribute.SplitTransition) { |
| 726 | @SuppressWarnings("unchecked") // Attribute.java doesn't have the BuildOptions symbol. |
| 727 | List<BuildOptions> toOptions = |
| 728 | ((Attribute.SplitTransition<BuildOptions>) transition).split(fromOptions); |
| 729 | if (toOptions.isEmpty()) { |
| 730 | // When the split returns an empty list, it's signaling it doesn't apply to this instance. |
| 731 | // Check that it's safe to skip the transition and return the original options. |
| 732 | Verify.verify(transition.defaultsToSelf()); |
Greg Estren | b0de919 | 2016-11-16 17:44:16 +0000 | [diff] [blame] | 733 | result = ImmutableList.<BuildOptions>of(fromOptions); |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 734 | } else { |
Greg Estren | b0de919 | 2016-11-16 17:44:16 +0000 | [diff] [blame] | 735 | result = toOptions; |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 736 | } |
| 737 | } else { |
| 738 | throw new IllegalStateException(String.format( |
| 739 | "unsupported dynamic transition type: %s", transition.getClass().getName())); |
| 740 | } |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 741 | |
Greg Estren | b0de919 | 2016-11-16 17:44:16 +0000 | [diff] [blame] | 742 | if (!trimResults) { |
| 743 | return result; |
| 744 | } else { |
| 745 | ImmutableList.Builder<BuildOptions> trimmedOptions = ImmutableList.builder(); |
| 746 | for (BuildOptions toOptions : result) { |
| 747 | trimmedOptions.add(toOptions.trim( |
| 748 | BuildConfiguration.getOptionsClasses(requiredFragments, ruleClassProvider))); |
| 749 | } |
| 750 | return trimmedOptions.build(); |
| 751 | } |
| 752 | } |
Greg Estren | 66cadd3 | 2016-08-05 21:07:02 +0000 | [diff] [blame] | 753 | |
| 754 | /** |
Greg Estren | 2bc8838 | 2016-08-02 21:45:35 +0000 | [diff] [blame] | 755 | * Diagnostic helper method for dynamic configurations: checks the config fragments required by |
| 756 | * a dep against the fragments in its actual configuration. If any are missing, triggers a |
| 757 | * descriptive "missing fragments" error. |
| 758 | */ |
| 759 | private static void checkForMissingFragments(Environment env, TargetAndConfiguration ctgValue, |
| 760 | String attribute, Dependency dep, |
| 761 | Set<Class<? extends BuildConfiguration.Fragment>> expectedDepFragments) |
| 762 | throws DependencyEvaluationException { |
| 763 | Set<String> ctgFragmentNames = new HashSet<>(); |
| 764 | for (BuildConfiguration.Fragment fragment : |
| 765 | ctgValue.getConfiguration().getAllFragments().values()) { |
| 766 | ctgFragmentNames.add(fragment.getClass().getSimpleName()); |
| 767 | } |
| 768 | Set<String> depFragmentNames = new HashSet<>(); |
| 769 | for (Class<? extends BuildConfiguration.Fragment> fragmentClass : expectedDepFragments) { |
| 770 | depFragmentNames.add(fragmentClass.getSimpleName()); |
| 771 | } |
| 772 | Set<String> missing = Sets.difference(depFragmentNames, ctgFragmentNames); |
| 773 | if (!missing.isEmpty()) { |
| 774 | String msg = String.format( |
| 775 | "%s: dependency %s from attribute \"%s\" is missing required config fragments: %s", |
| 776 | ctgValue.getLabel(), dep.getLabel(), attribute, Joiner.on(", ").join(missing)); |
| 777 | env.getListener().handle(Event.error(msg)); |
| 778 | throw new DependencyEvaluationException(new InvalidConfigurationException(msg)); |
| 779 | } |
| 780 | } |
| 781 | |
Greg Estren | dc38824 | 2016-12-07 16:47:02 +0000 | [diff] [blame] | 782 | /** |
| 783 | * Determines the output ordering of each <attribute, depLabel> -> |
| 784 | * [dep<config1>, dep<config2>, ...] collection produced by a split transition. |
| 785 | */ |
| 786 | @VisibleForTesting |
| 787 | static final Comparator<Dependency> DYNAMIC_SPLIT_DEP_ORDERING = |
| 788 | new Comparator<Dependency>() { |
| 789 | @Override |
| 790 | public int compare(Dependency d1, Dependency d2) { |
| 791 | return d1.getConfiguration().getMnemonic().compareTo(d2.getConfiguration().getMnemonic()); |
| 792 | } |
| 793 | }; |
| 794 | |
| 795 | /** |
| 796 | * Helper method for {@link #getDynamicConfigurations}: returns a copy of the output deps |
| 797 | * using the same key and value ordering as the input deps. |
| 798 | * |
| 799 | * @param originalDeps the input deps with the ordering to preserve |
| 800 | * @param dynamicDeps the unordered output deps |
| 801 | * @param attributesAndLabels collection of <attribute, depLabel> pairs guaranteed to match |
| 802 | * the ordering of originalDeps.entries(). This is a performance optimization: see |
| 803 | * {@link #getDynamicConfigurations#attributesAndLabels} for details. |
| 804 | */ |
| 805 | private static OrderedSetMultimap<Attribute, Dependency> sortDynamicallyConfiguredDeps( |
| 806 | OrderedSetMultimap<Attribute, Dependency> originalDeps, |
| 807 | Multimap<AttributeAndLabel, Dependency> dynamicDeps, |
| 808 | ArrayList<AttributeAndLabel> attributesAndLabels) { |
| 809 | Iterator<AttributeAndLabel> iterator = attributesAndLabels.iterator(); |
| 810 | OrderedSetMultimap<Attribute, Dependency> result = OrderedSetMultimap.create(); |
| 811 | for (Map.Entry<Attribute, Dependency> depsEntry : originalDeps.entries()) { |
| 812 | AttributeAndLabel attrAndLabel = iterator.next(); |
| 813 | if (depsEntry.getValue().hasStaticConfiguration()) { |
| 814 | result.put(attrAndLabel.attribute, depsEntry.getValue()); |
| 815 | } else { |
| 816 | Collection<Dependency> dynamicAttrDeps = dynamicDeps.get(attrAndLabel); |
| 817 | Verify.verify(!dynamicAttrDeps.isEmpty()); |
| 818 | if (dynamicAttrDeps.size() > 1) { |
| 819 | List<Dependency> sortedSplitList = new ArrayList<>(dynamicAttrDeps); |
| 820 | Collections.sort(sortedSplitList, DYNAMIC_SPLIT_DEP_ORDERING); |
| 821 | dynamicAttrDeps = sortedSplitList; |
| 822 | } |
| 823 | result.putAll(depsEntry.getKey(), dynamicAttrDeps); |
| 824 | } |
| 825 | } |
| 826 | return result; |
| 827 | } |
Greg Estren | 2bc8838 | 2016-08-02 21:45:35 +0000 | [diff] [blame] | 828 | |
| 829 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 830 | * Merges the each direct dependency configured target with the aspects associated with it. |
| 831 | * |
| 832 | * <p>Note that the combination of a configured target and its associated aspects are not |
| 833 | * represented by a Skyframe node. This is because there can possibly be many different |
| 834 | * combinations of aspects for a particular configured target, so it would result in a |
| 835 | * combinatiorial explosion of Skyframe nodes. |
| 836 | */ |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 837 | private static OrderedSetMultimap<Attribute, ConfiguredTarget> mergeAspects( |
| 838 | OrderedSetMultimap<Attribute, Dependency> depValueNames, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 839 | Map<SkyKey, ConfiguredTarget> depConfiguredTargetMap, |
Dmitry Lomov | 9b2fc5c | 2016-11-11 11:18:48 +0000 | [diff] [blame] | 840 | OrderedSetMultimap<SkyKey, ConfiguredAspect> depAspectMap) |
| 841 | throws DuplicateException { |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 842 | OrderedSetMultimap<Attribute, ConfiguredTarget> result = OrderedSetMultimap.create(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 843 | |
| 844 | for (Map.Entry<Attribute, Dependency> entry : depValueNames.entries()) { |
| 845 | Dependency dep = entry.getValue(); |
| 846 | SkyKey depKey = TO_KEYS.apply(dep); |
| 847 | ConfiguredTarget depConfiguredTarget = depConfiguredTargetMap.get(depKey); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 848 | |
Lukacs Berki | 047b742 | 2015-07-22 08:32:37 +0000 | [diff] [blame] | 849 | result.put(entry.getKey(), |
Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 850 | MergedConfiguredTarget.of(depConfiguredTarget, depAspectMap.get(depKey))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | return result; |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * 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] | 858 | * 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] | 859 | * |
| 860 | * <p>Returns null if the required aspects are not computed yet. |
| 861 | */ |
| 862 | @Nullable |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 863 | private static OrderedSetMultimap<SkyKey, ConfiguredAspect> resolveAspectDependencies( |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 864 | Environment env, |
| 865 | Map<SkyKey, ConfiguredTarget> configuredTargetMap, |
| 866 | Iterable<Dependency> deps, |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 867 | NestedSetBuilder<Package> transitivePackages) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 868 | throws AspectCreationException, InterruptedException { |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 869 | OrderedSetMultimap<SkyKey, ConfiguredAspect> result = OrderedSetMultimap.create(); |
kaipi | 64e952c | 2017-05-22 17:12:08 +0200 | [diff] [blame] | 870 | OrderedSetMultimap<SkyKey, SkyKey> processedAspects = OrderedSetMultimap.create(); |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 871 | Set<SkyKey> allAspectKeys = new HashSet<>(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 872 | for (Dependency dep : deps) { |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 873 | allAspectKeys.addAll(getAspectKeys(dep).values()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 874 | } |
| 875 | |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 876 | Map<SkyKey, ValueOrException2<AspectCreationException, NoSuchThingException>> depAspects = |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 877 | env.getValuesOrThrow(allAspectKeys, |
| 878 | AspectCreationException.class, NoSuchThingException.class); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 879 | |
| 880 | for (Dependency dep : deps) { |
| 881 | SkyKey depKey = TO_KEYS.apply(dep); |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 882 | Map<AspectDescriptor, SkyKey> aspectToKeys = getAspectKeys(dep); |
| 883 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 884 | ConfiguredTarget depConfiguredTarget = configuredTargetMap.get(depKey); |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 885 | for (AspectDeps depAspect : dep.getAspects().getVisibleAspects()) { |
| 886 | SkyKey aspectKey = aspectToKeys.get(depAspect.getAspect()); |
kaipi | 64e952c | 2017-05-22 17:12:08 +0200 | [diff] [blame] | 887 | // Skip if the aspect was already applied to the target (perhaps through different |
| 888 | // attributes). |
| 889 | if (!processedAspects.put(depKey, aspectKey)) { |
| 890 | continue; |
| 891 | } |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 892 | |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 893 | AspectValue aspectValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 894 | try { |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 895 | // TODO(ulfjack): Catch all thrown AspectCreationException and NoSuchThingException |
| 896 | // instances and merge them into a single Exception to get full root cause data. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 897 | aspectValue = (AspectValue) depAspects.get(aspectKey).get(); |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 898 | } catch (NoSuchThingException e) { |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 899 | throw new AspectCreationException( |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 900 | String.format( |
| 901 | "Evaluation of aspect %s on %s failed: %s", |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 902 | depAspect.getAspect().getAspectClass().getName(), |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 903 | dep.getLabel(), |
| 904 | e.toString())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | if (aspectValue == null) { |
| 908 | // Dependent aspect has either not been computed yet or is in error. |
| 909 | return null; |
| 910 | } |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 911 | if (!aspectMatchesConfiguredTarget(depConfiguredTarget, aspectValue.getAspect())) { |
| 912 | continue; |
| 913 | } |
| 914 | |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 915 | result.put(depKey, aspectValue.getConfiguredAspect()); |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 916 | transitivePackages.addTransitive(aspectValue.getTransitivePackages()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 917 | } |
| 918 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 919 | return result; |
| 920 | } |
| 921 | |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 922 | private static Map<AspectDescriptor, SkyKey> getAspectKeys(Dependency dep) { |
| 923 | HashMap<AspectDescriptor, SkyKey> result = new HashMap<>(); |
| 924 | AspectCollection aspects = dep.getAspects(); |
| 925 | for (AspectDeps aspectDeps : aspects.getVisibleAspects()) { |
| 926 | buildAspectKey(aspectDeps, result, dep); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 927 | } |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 928 | return result; |
| 929 | } |
| 930 | |
| 931 | private static AspectKey buildAspectKey(AspectDeps aspectDeps, |
| 932 | HashMap<AspectDescriptor, SkyKey> result, Dependency dep) { |
| 933 | if (result.containsKey(aspectDeps.getAspect())) { |
| 934 | return (AspectKey) result.get(aspectDeps.getAspect()).argument(); |
| 935 | } |
| 936 | |
| 937 | ImmutableList.Builder<AspectKey> dependentAspects = ImmutableList.builder(); |
| 938 | for (AspectDeps path : aspectDeps.getDependentAspects()) { |
| 939 | dependentAspects.add(buildAspectKey(path, result, dep)); |
| 940 | } |
| 941 | AspectKey aspectKey = AspectValue.createAspectKey( |
| 942 | dep.getLabel(), dep.getConfiguration(), |
| 943 | dependentAspects.build(), |
| 944 | aspectDeps.getAspect(), |
| 945 | dep.getAspectConfiguration(aspectDeps.getAspect())); |
| 946 | result.put(aspectKey.getAspectDescriptor(), aspectKey.getSkyKey()); |
| 947 | return aspectKey; |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Dmitry Lomov | 6cd9897 | 2017-03-01 15:44:00 +0000 | [diff] [blame] | 950 | static boolean aspectMatchesConfiguredTarget(final ConfiguredTarget dep, Aspect aspect) { |
dslomov | fa50c3d | 2017-05-08 08:47:44 -0400 | [diff] [blame] | 951 | if (!aspect.getDefinition().applyToFiles() && !(dep.getTarget() instanceof Rule)) { |
| 952 | return false; |
| 953 | } |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 954 | return aspect.getDefinition().getRequiredProviders().isSatisfiedBy( |
| 955 | new Predicate<Class<?>>() { |
| 956 | @Override |
| 957 | public boolean apply(Class<?> provider) { |
| 958 | return dep.getProvider(provider.asSubclass(TransitiveInfoProvider.class)) != null; |
| 959 | } |
| 960 | }, |
| 961 | new Predicate<SkylarkProviderIdentifier>() { |
| 962 | @Override |
| 963 | public boolean apply(SkylarkProviderIdentifier skylarkProviderIdentifier) { |
| 964 | return dep.get(skylarkProviderIdentifier) != null; |
| 965 | } |
Rumou Duan | 6f8393f | 2016-11-30 16:03:10 +0000 | [diff] [blame] | 966 | } |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 967 | ); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /** |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 971 | * Returns the set of {@link ConfigMatchingProvider}s that key the configurable attributes used by |
| 972 | * this rule. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 973 | * |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 974 | * <p>>If the configured targets supplying those providers aren't yet resolved by the dependency |
| 975 | * resolver, returns null. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 976 | */ |
| 977 | @Nullable |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 978 | static ImmutableMap<Label, ConfigMatchingProvider> getConfigConditions( |
| 979 | Target target, |
| 980 | Environment env, |
| 981 | SkyframeDependencyResolver resolver, |
| 982 | TargetAndConfiguration ctgValue, |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 983 | NestedSetBuilder<Package> transitivePackages, |
| 984 | NestedSetBuilder<Label> transitiveLoadingRootCauses) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 985 | throws DependencyEvaluationException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 986 | if (!(target instanceof Rule)) { |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 987 | return NO_CONFIG_CONDITIONS; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 988 | } |
| 989 | |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 990 | Map<Label, ConfigMatchingProvider> configConditions = new LinkedHashMap<>(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 991 | |
| 992 | // Collect the labels of the configured targets we need to resolve. |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 993 | OrderedSetMultimap<Attribute, Label> configLabelMap = OrderedSetMultimap.create(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 994 | RawAttributeMapper attributeMap = RawAttributeMapper.of(((Rule) target)); |
| 995 | for (Attribute a : ((Rule) target).getAttributes()) { |
| 996 | for (Label configLabel : attributeMap.getConfigurabilityKeys(a.getName(), a.getType())) { |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 997 | if (!BuildType.Selector.isReservedLabel(configLabel)) { |
Greg Estren | 974bbd9 | 2016-07-27 14:22:41 +0000 | [diff] [blame] | 998 | configLabelMap.put(a, target.getLabel().resolveRepositoryRelative(configLabel)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | if (configLabelMap.isEmpty()) { |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 1003 | return NO_CONFIG_CONDITIONS; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | // Collect the corresponding Skyframe configured target values. Abort early if they haven't |
| 1007 | // been computed yet. |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 1008 | Collection<Dependency> configValueNames = null; |
| 1009 | try { |
| 1010 | configValueNames = resolver.resolveRuleLabels( |
| 1011 | ctgValue, configLabelMap, transitiveLoadingRootCauses); |
| 1012 | } catch (InconsistentAspectOrderException e) { |
| 1013 | throw new DependencyEvaluationException(e); |
| 1014 | } |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 1015 | if (env.valuesMissing()) { |
| 1016 | return null; |
| 1017 | } |
| 1018 | |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 1019 | |
| 1020 | // No need to get new configs from Skyframe - config_setting rules always use the current |
| 1021 | // target's config. |
| 1022 | // TODO(bazel-team): remove the need for this special transformation. We can probably do this by |
| 1023 | // simply passing this through trimConfigurations. |
| 1024 | BuildConfiguration targetConfig = ctgValue.getConfiguration(); |
Greg Estren | b5692bd | 2016-06-08 21:09:11 +0000 | [diff] [blame] | 1025 | if (useDynamicConfigurations(targetConfig)) { |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 1026 | ImmutableList.Builder<Dependency> staticConfigs = ImmutableList.builder(); |
| 1027 | for (Dependency dep : configValueNames) { |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 1028 | staticConfigs.add( |
| 1029 | Dependency.withConfigurationAndAspects(dep.getLabel(), targetConfig, dep.getAspects())); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 1030 | } |
| 1031 | configValueNames = staticConfigs.build(); |
| 1032 | } |
| 1033 | |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 1034 | Map<SkyKey, ConfiguredTarget> configValues = resolveConfiguredTargetDependencies( |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1035 | env, configValueNames, transitivePackages, transitiveLoadingRootCauses); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1036 | if (configValues == null) { |
| 1037 | return null; |
| 1038 | } |
| 1039 | |
| 1040 | // Get the configured targets as ConfigMatchingProvider interfaces. |
| 1041 | for (Dependency entry : configValueNames) { |
| 1042 | ConfiguredTarget value = configValues.get(TO_KEYS.apply(entry)); |
| 1043 | // The code above guarantees that value is non-null here. |
| 1044 | ConfigMatchingProvider provider = value.getProvider(ConfigMatchingProvider.class); |
| 1045 | if (provider != null) { |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 1046 | configConditions.put(entry.getLabel(), provider); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1047 | } else { |
| 1048 | // Not a valid provider for configuration conditions. |
| 1049 | String message = |
| 1050 | entry.getLabel() + " is not a valid configuration key for " + target.getLabel(); |
| 1051 | env.getListener().handle(Event.error(TargetUtils.getLocationMaybe(target), message)); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 1052 | throw new DependencyEvaluationException(new ConfiguredValueCreationException( |
| 1053 | message, target.getLabel())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 1057 | return ImmutableMap.copyOf(configConditions); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1058 | } |
| 1059 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 1060 | /** |
| 1061 | * * Resolves the targets referenced in depValueNames and returns their ConfiguredTarget |
| 1062 | * instances. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1063 | * |
| 1064 | * <p>Returns null if not all instances are available yet. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1065 | */ |
| 1066 | @Nullable |
| 1067 | private static Map<SkyKey, ConfiguredTarget> resolveConfiguredTargetDependencies( |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 1068 | Environment env, |
| 1069 | Collection<Dependency> deps, |
| 1070 | NestedSetBuilder<Package> transitivePackages, |
| 1071 | NestedSetBuilder<Label> transitiveLoadingRootCauses) |
| 1072 | throws DependencyEvaluationException, InterruptedException { |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1073 | boolean missedValues = env.valuesMissing(); |
| 1074 | boolean failed = false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1075 | Iterable<SkyKey> depKeys = Iterables.transform(deps, TO_KEYS); |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1076 | Map<SkyKey, ValueOrException<ConfiguredValueCreationException>> depValuesOrExceptions = |
gregce | f19fcfe | 2017-06-02 16:04:07 -0400 | [diff] [blame] | 1077 | env.getValuesOrThrow(depKeys, ConfiguredValueCreationException.class); |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1078 | Map<SkyKey, ConfiguredTarget> result = |
| 1079 | Maps.newHashMapWithExpectedSize(depValuesOrExceptions.size()); |
| 1080 | for (Map.Entry<SkyKey, ValueOrException<ConfiguredValueCreationException>> entry |
| 1081 | : depValuesOrExceptions.entrySet()) { |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 1082 | try { |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1083 | ConfiguredTargetValue depValue = (ConfiguredTargetValue) entry.getValue().get(); |
| 1084 | if (depValue == null) { |
| 1085 | missedValues = true; |
| 1086 | } else { |
| 1087 | result.put(entry.getKey(), depValue.getConfiguredTarget()); |
| 1088 | transitivePackages.addTransitive(depValue.getTransitivePackages()); |
| 1089 | } |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 1090 | } catch (ConfiguredValueCreationException e) { |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1091 | // TODO(ulfjack): If there is an analysis root cause, we drop all loading root causes. |
| 1092 | if (e.getAnalysisRootCause() != null) { |
| 1093 | throw new DependencyEvaluationException(e); |
| 1094 | } |
| 1095 | transitiveLoadingRootCauses.addTransitive(e.loadingRootCauses); |
| 1096 | failed = true; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1097 | } |
| 1098 | } |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1099 | if (missedValues) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1100 | return null; |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1101 | } else if (failed) { |
| 1102 | throw new DependencyEvaluationException( |
| 1103 | new ConfiguredValueCreationException(transitiveLoadingRootCauses.build())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1104 | } else { |
Ulf Adams | 7cb66de | 2016-01-14 08:46:43 +0000 | [diff] [blame] | 1105 | return result; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | |
| 1110 | @Override |
| 1111 | public String extractTag(SkyKey skyKey) { |
| 1112 | return Label.print(((ConfiguredTargetKey) skyKey.argument()).getLabel()); |
| 1113 | } |
| 1114 | |
| 1115 | @Nullable |
| 1116 | private ConfiguredTargetValue createConfiguredTarget(SkyframeBuildView view, |
| 1117 | Environment env, Target target, BuildConfiguration configuration, |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 1118 | OrderedSetMultimap<Attribute, ConfiguredTarget> depValueMap, |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 1119 | ImmutableMap<Label, ConfigMatchingProvider> configConditions, |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 1120 | NestedSetBuilder<Package> transitivePackages) |
Greg Estren | 9eb1cf0 | 2015-06-26 22:18:35 +0000 | [diff] [blame] | 1121 | throws ConfiguredTargetFunctionException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1122 | StoredEventHandler events = new StoredEventHandler(); |
gregce | f19fcfe | 2017-06-02 16:04:07 -0400 | [diff] [blame] | 1123 | BuildConfiguration ownerConfig = |
| 1124 | ConfiguredTargetFactory.getArtifactOwnerConfiguration(env, configuration); |
| 1125 | if (env.valuesMissing()) { |
| 1126 | return null; |
| 1127 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1128 | CachingAnalysisEnvironment analysisEnvironment = view.createAnalysisEnvironment( |
| 1129 | new ConfiguredTargetKey(target.getLabel(), ownerConfig), false, |
Ulf Adams | bfb76a9 | 2015-04-22 20:44:35 +0000 | [diff] [blame] | 1130 | events, env, configuration); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1131 | if (env.valuesMissing()) { |
| 1132 | return null; |
| 1133 | } |
| 1134 | |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1135 | Preconditions.checkNotNull(depValueMap); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1136 | ConfiguredTarget configuredTarget = view.createConfiguredTarget(target, configuration, |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 1137 | analysisEnvironment, depValueMap, configConditions); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1138 | |
| 1139 | events.replayOn(env.getListener()); |
| 1140 | if (events.hasErrors()) { |
| 1141 | analysisEnvironment.disable(target); |
| 1142 | throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException( |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 1143 | "Analysis of target '" + target.getLabel() + "' failed; build aborted", |
| 1144 | target.getLabel())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1145 | } |
| 1146 | Preconditions.checkState(!analysisEnvironment.hasErrors(), |
| 1147 | "Analysis environment hasError() but no errors reported"); |
| 1148 | if (env.valuesMissing()) { |
| 1149 | return null; |
| 1150 | } |
| 1151 | |
| 1152 | analysisEnvironment.disable(target); |
| 1153 | Preconditions.checkNotNull(configuredTarget, target); |
| 1154 | |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 1155 | GeneratingActions generatingActions; |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 1156 | // Check for conflicting actions within this configured target (that indicates a bug in the |
| 1157 | // rule implementation). |
Janak Ramakrishnan | b3a6ca7 | 2015-03-27 20:07:28 +0000 | [diff] [blame] | 1158 | try { |
Rumou Duan | 0eb5a0c | 2016-04-18 19:13:51 +0000 | [diff] [blame] | 1159 | generatingActions = Actions.filterSharedActionsAndThrowActionConflict( |
| 1160 | analysisEnvironment.getRegisteredActions()); |
Janak Ramakrishnan | b3a6ca7 | 2015-03-27 20:07:28 +0000 | [diff] [blame] | 1161 | } catch (ActionConflictException e) { |
| 1162 | throw new ConfiguredTargetFunctionException(e); |
| 1163 | } |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 1164 | return new ConfiguredTargetValue( |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 1165 | configuredTarget, |
| 1166 | generatingActions, |
| 1167 | transitivePackages.build(), |
| 1168 | removeActionsAfterEvaluation.get()); |
Janak Ramakrishnan | b3a6ca7 | 2015-03-27 20:07:28 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1171 | /** |
| 1172 | * An exception indicating that there was a problem during the construction of |
| 1173 | * a ConfiguredTargetValue. |
| 1174 | */ |
| 1175 | public static final class ConfiguredValueCreationException extends Exception { |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1176 | private final NestedSet<Label> loadingRootCauses; |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 1177 | // TODO(ulfjack): Collect all analysis root causes, not just the first one. |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1178 | @Nullable private final Label analysisRootCause; |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 1179 | |
| 1180 | public ConfiguredValueCreationException(String message, Label currentTarget) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1181 | super(message); |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1182 | this.loadingRootCauses = NestedSetBuilder.<Label>emptySet(Order.STABLE_ORDER); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 1183 | this.analysisRootCause = Preconditions.checkNotNull(currentTarget); |
| 1184 | } |
| 1185 | |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 1186 | public ConfiguredValueCreationException(String message, NestedSet<Label> rootCauses) { |
| 1187 | super(message); |
| 1188 | this.loadingRootCauses = rootCauses; |
| 1189 | this.analysisRootCause = null; |
| 1190 | } |
| 1191 | |
| 1192 | public ConfiguredValueCreationException(NestedSet<Label> rootCauses) { |
| 1193 | this("Loading failed", rootCauses); |
| 1194 | } |
| 1195 | |
| 1196 | public ConfiguredValueCreationException(String message) { |
| 1197 | this(message, NestedSetBuilder.<Label>emptySet(Order.STABLE_ORDER)); |
| 1198 | } |
| 1199 | |
| 1200 | public NestedSet<Label> getRootCauses() { |
| 1201 | return loadingRootCauses; |
| 1202 | } |
| 1203 | |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 1204 | public Label getAnalysisRootCause() { |
| 1205 | return analysisRootCause; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | /** |
| 1210 | * Used to declare all the exception types that can be wrapped in the exception thrown by |
| 1211 | * {@link ConfiguredTargetFunction#compute}. |
| 1212 | */ |
| 1213 | public static final class ConfiguredTargetFunctionException extends SkyFunctionException { |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 1214 | public ConfiguredTargetFunctionException(NoSuchThingException e) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1215 | super(e, Transience.PERSISTENT); |
| 1216 | } |
| 1217 | |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 1218 | private ConfiguredTargetFunctionException(ConfiguredValueCreationException e) { |
| 1219 | super(e, Transience.PERSISTENT); |
Ulf Adams | 3ab82f7 | 2015-09-04 12:10:53 +0000 | [diff] [blame] | 1220 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1221 | |
Janak Ramakrishnan | b3a6ca7 | 2015-03-27 20:07:28 +0000 | [diff] [blame] | 1222 | private ConfiguredTargetFunctionException(ActionConflictException e) { |
| 1223 | super(e, Transience.PERSISTENT); |
| 1224 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1225 | } |
| 1226 | } |