blob: bc575963f5715ed106758ffb9129bf62a5cff0ba [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
15package com.google.devtools.build.lib.skyframe;
16
tomlua155b532017-11-08 20:12:47 +010017import com.google.common.base.Preconditions;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.common.collect.ImmutableList;
Lukacs Berki7894c182016-05-10 12:07:01 +000019import com.google.common.collect.ImmutableMap;
cpeyserfb829992017-09-07 17:17:03 +020020import com.google.common.collect.ImmutableSet;
janakr0175ce32018-02-26 15:54:57 -080021import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
ulfjack90e2e982017-08-07 11:27:32 +020022import com.google.devtools.build.lib.analysis.AliasProvider;
gregce4c3ef112017-09-20 23:43:14 +020023import com.google.devtools.build.lib.analysis.AspectResolver;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024import com.google.devtools.build.lib.analysis.CachingAnalysisEnvironment;
ulfjack37407682019-09-17 06:30:49 -070025import com.google.devtools.build.lib.analysis.CachingAnalysisEnvironment.MissingDepException;
Dmitry Lomovb487ac62015-11-09 13:09:12 +000026import com.google.devtools.build.lib.analysis.ConfiguredAspect;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027import com.google.devtools.build.lib.analysis.ConfiguredAspectFactory;
28import com.google.devtools.build.lib.analysis.ConfiguredTarget;
lberki102256f2019-02-08 01:34:23 -080029import com.google.devtools.build.lib.analysis.DependencyResolver;
30import com.google.devtools.build.lib.analysis.DependencyResolver.DependencyKind;
Dmitry Lomovd83af9e2017-02-23 15:44:23 +000031import com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException;
jcater84ca5562020-04-06 14:35:46 -070032import com.google.devtools.build.lib.analysis.DuplicateException;
John Catercdfa9ca2019-04-05 12:32:09 -070033import com.google.devtools.build.lib.analysis.ResolvedToolchainContext;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010034import com.google.devtools.build.lib.analysis.TargetAndConfiguration;
35import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
mjhalupka5d7fa7b2018-03-22 13:37:38 -070036import com.google.devtools.build.lib.analysis.config.BuildOptions;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010037import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider;
jcater67181912020-04-08 10:46:29 -070038import com.google.devtools.build.lib.analysis.config.DependencyEvaluationException;
Ulf Adams25f03d82016-01-25 10:31:46 +000039import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
gregce2aee44b2017-09-16 07:16:44 +020040import com.google.devtools.build.lib.analysis.configuredtargets.MergedConfiguredTarget;
ulfjack904a8d62018-05-29 05:17:35 -070041import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId.ConfigurationId;
42import com.google.devtools.build.lib.causes.AnalysisFailedCause;
43import com.google.devtools.build.lib.causes.Cause;
44import com.google.devtools.build.lib.causes.LabelCause;
John Fielda97e17f2015-11-13 02:19:52 +000045import com.google.devtools.build.lib.cmdline.Label;
Ulf Adams84901732016-01-28 15:05:16 +000046import com.google.devtools.build.lib.collect.nestedset.NestedSet;
Marian Loburc62faba2015-09-09 10:08:06 +000047import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
Ulf Adams9e16f0a2016-01-25 12:43:32 +000048import com.google.devtools.build.lib.events.Event;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010049import com.google.devtools.build.lib.events.StoredEventHandler;
Dmitry Lomov2eb8bdd2016-04-06 08:47:30 +000050import com.google.devtools.build.lib.packages.Aspect;
Dmitry Lomove851fe22017-02-14 23:11:23 +000051import com.google.devtools.build.lib.packages.AspectDescriptor;
Janak Ramakrishnan0a4c6e42015-09-17 00:37:58 +000052import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException;
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +000053import com.google.devtools.build.lib.packages.NativeAspectClass;
janakrf3e6f252018-01-18 07:45:12 -080054import com.google.devtools.build.lib.packages.NoSuchTargetException;
Ulf Adamsd55d7af2016-01-19 11:03:22 +000055import com.google.devtools.build.lib.packages.NoSuchThingException;
cparsons089148b2019-09-17 08:14:41 -070056import com.google.devtools.build.lib.packages.OutputFile;
Marian Loburc62faba2015-09-09 10:08:06 +000057import com.google.devtools.build.lib.packages.Package;
Greg Estren00049432015-08-25 16:43:47 +000058import com.google.devtools.build.lib.packages.RuleClassProvider;
Googler74558fc2016-05-06 21:47:42 +000059import com.google.devtools.build.lib.packages.SkylarkAspect;
Dmitry Lomov82e03772015-11-30 12:13:22 +000060import com.google.devtools.build.lib.packages.SkylarkAspectClass;
cparsons0d55f4c2017-12-20 14:49:13 -080061import com.google.devtools.build.lib.packages.SkylarkDefinedAspect;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010062import com.google.devtools.build.lib.packages.Target;
Googlerc5fcc862019-09-06 16:17:47 -070063import com.google.devtools.build.lib.packages.Type.ConversionException;
tomlu72642a22017-10-18 06:23:14 +020064import com.google.devtools.build.lib.profiler.memory.CurrentRuleTracker;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010065import com.google.devtools.build.lib.skyframe.AspectValue.AspectKey;
Dmitry Lomov9b2fc5c2016-11-11 11:18:48 +000066import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.ConfiguredTargetFunctionException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010067import com.google.devtools.build.lib.skyframe.SkyframeExecutor.BuildViewProvider;
Ulf Adamsa47ca012016-01-20 10:35:19 +000068import com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException;
Greg Estrend5353252016-08-11 22:13:31 +000069import com.google.devtools.build.lib.util.OrderedSetMultimap;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010070import com.google.devtools.build.skyframe.SkyFunction;
71import com.google.devtools.build.skyframe.SkyFunctionException;
72import com.google.devtools.build.skyframe.SkyKey;
73import com.google.devtools.build.skyframe.SkyValue;
cpeysera8a61ee2018-01-26 09:20:37 -080074import com.google.devtools.build.skyframe.ValueOrException;
Dmitry Lomovca9bfa42016-11-15 13:22:36 +000075import java.util.ArrayList;
Dmitry Lomove851fe22017-02-14 23:11:23 +000076import java.util.HashMap;
Dmitry Lomovca9bfa42016-11-15 13:22:36 +000077import java.util.Map;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010078import javax.annotation.Nullable;
79
80/**
81 * The Skyframe function that generates aspects.
Dmitry Lomove8040172016-04-06 14:53:43 +000082 *
Lukacs Berki2300cd62016-05-19 11:06:37 +000083 * This class, together with {@link ConfiguredTargetFunction} drives the analysis phase. For more
ulfjack26d0e492017-08-07 13:42:33 +020084 * information, see {@link com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory}.
Lukacs Berki2300cd62016-05-19 11:06:37 +000085 *
Dmitry Lomove8040172016-04-06 14:53:43 +000086 * {@link AspectFunction} takes a SkyKey containing an {@link AspectKey} [a tuple of
87 * (target label, configurations, aspect class and aspect parameters)],
88 * loads an {@link Aspect} from aspect class and aspect parameters,
89 * gets a {@link ConfiguredTarget} for label and configurations, and then creates
90 * a {@link ConfiguredAspect} for a given {@link AspectKey}.
91 *
92 * See {@link com.google.devtools.build.lib.packages.AspectClass} documentation
93 * for an overview of aspect-related classes
Lukacs Berki2300cd62016-05-19 11:06:37 +000094 *
ulfjack26d0e492017-08-07 13:42:33 +020095 * @see com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory
Lukacs Berki2300cd62016-05-19 11:06:37 +000096 * @see com.google.devtools.build.lib.packages.AspectClass
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010097 */
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +000098public final class AspectFunction implements SkyFunction {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010099 private final BuildViewProvider buildViewProvider;
Greg Estren00049432015-08-25 16:43:47 +0000100 private final RuleClassProvider ruleClassProvider;
mjhalupka5d7fa7b2018-03-22 13:37:38 -0700101 private final BuildOptions defaultBuildOptions;
janakr931d2852017-12-15 13:48:29 -0800102 /**
103 * Indicates whether the set of packages transitively loaded for a given {@link AspectValue} will
104 * be needed for package root resolution later in the build. If not, they are not collected and
105 * stored.
106 */
107 private final boolean storeTransitivePackagesForPackageRootResolution;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100108
janakr93e3eea2017-03-30 22:09:37 +0000109 AspectFunction(
110 BuildViewProvider buildViewProvider,
111 RuleClassProvider ruleClassProvider,
mjhalupka5d7fa7b2018-03-22 13:37:38 -0700112 boolean storeTransitivePackagesForPackageRootResolution,
Googlerce6ad292019-12-20 10:29:21 -0800113 BuildOptions defaultBuildOptions) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100114 this.buildViewProvider = buildViewProvider;
Greg Estren00049432015-08-25 16:43:47 +0000115 this.ruleClassProvider = ruleClassProvider;
janakr931d2852017-12-15 13:48:29 -0800116 this.storeTransitivePackagesForPackageRootResolution =
117 storeTransitivePackagesForPackageRootResolution;
mjhalupka5d7fa7b2018-03-22 13:37:38 -0700118 this.defaultBuildOptions = defaultBuildOptions;
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000119 }
120
121 /**
cparsons0d55f4c2017-12-20 14:49:13 -0800122 * Load Skylark-defined aspect from an extension file. Is to be called from a SkyFunction.
123 *
124 * @return {@code null} if dependencies cannot be satisfied.
125 * @throws AspectCreationException if the value loaded is not a {@link SkylarkDefinedAspect}.
126 */
127 @Nullable
128 static SkylarkDefinedAspect loadSkylarkDefinedAspect(
nharmatadd21a932020-04-06 15:10:26 -0700129 Environment env, SkylarkAspectClass skylarkAspectClass)
cparsons0d55f4c2017-12-20 14:49:13 -0800130 throws AspectCreationException, InterruptedException {
131 Label extensionLabel = skylarkAspectClass.getExtensionLabel();
132 String skylarkValueName = skylarkAspectClass.getExportedName();
133
nharmatadd21a932020-04-06 15:10:26 -0700134 SkylarkAspect skylarkAspect = loadSkylarkAspect(env, extensionLabel, skylarkValueName);
cpeyser10798092018-03-23 12:10:25 -0700135 if (skylarkAspect == null) {
136 return null;
137 }
cparsons0d55f4c2017-12-20 14:49:13 -0800138 if (!(skylarkAspect instanceof SkylarkDefinedAspect)) {
139 throw new AspectCreationException(
140 String.format(
laurentlb7dcad732018-10-25 05:17:20 -0700141 "%s from %s is not a Starlark-defined aspect", skylarkValueName, extensionLabel),
ulfjack904a8d62018-05-29 05:17:35 -0700142 extensionLabel);
cparsons0d55f4c2017-12-20 14:49:13 -0800143 } else {
144 return (SkylarkDefinedAspect) skylarkAspect;
145 }
146 }
147
148 /**
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000149 * Load Skylark aspect from an extension file. Is to be called from a SkyFunction.
150 *
151 * @return {@code null} if dependencies cannot be satisfied.
152 */
153 @Nullable
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +0000154 static SkylarkAspect loadSkylarkAspect(
nharmatadd21a932020-04-06 15:10:26 -0700155 Environment env, Label extensionLabel, String skylarkValueName)
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +0000156 throws AspectCreationException, InterruptedException {
dannark4e42c322018-11-08 19:32:04 -0800157 SkyKey importFileKey = SkylarkImportLookupValue.key(extensionLabel);
Ulf Adams0678e792016-01-25 15:30:45 +0000158 try {
nharmatadd21a932020-04-06 15:10:26 -0700159 SkylarkImportLookupValue skylarkImportLookupValue =
160 (SkylarkImportLookupValue)
161 env.getValueOrThrow(importFileKey, SkylarkImportFailedException.class);
Ulf Adams0678e792016-01-25 15:30:45 +0000162 if (skylarkImportLookupValue == null) {
cpeyser10798092018-03-23 12:10:25 -0700163 Preconditions.checkState(
laurentlb7dcad732018-10-25 05:17:20 -0700164 env.valuesMissing(), "no Starlark import value for %s", importFileKey);
Ulf Adams0678e792016-01-25 15:30:45 +0000165 return null;
166 }
Luis Fernando Pino Duquee82713d2016-04-26 16:22:38 +0000167
brandjon357ed7a2017-05-09 11:37:22 -0400168 Object skylarkValue = skylarkImportLookupValue.getEnvironmentExtension().getBindings()
Ulf Adams0678e792016-01-25 15:30:45 +0000169 .get(skylarkValueName);
Dmitry Lomov1920dd92017-07-21 12:57:18 +0200170 if (skylarkValue == null) {
171 throw new ConversionException(
172 String.format(
173 "%s is not exported from %s", skylarkValueName, extensionLabel.toString()));
174 }
Ulf Adams0678e792016-01-25 15:30:45 +0000175 if (!(skylarkValue instanceof SkylarkAspect)) {
176 throw new ConversionException(
Dmitry Lomov1920dd92017-07-21 12:57:18 +0200177 String.format(
178 "%s from %s is not an aspect", skylarkValueName, extensionLabel.toString()));
Ulf Adams0678e792016-01-25 15:30:45 +0000179 }
180 return (SkylarkAspect) skylarkValue;
nharmatadd21a932020-04-06 15:10:26 -0700181 } catch (SkylarkImportFailedException | ConversionException e) {
Ulf Adams0678e792016-01-25 15:30:45 +0000182 env.getListener().handle(Event.error(e.getMessage()));
ulfjack904a8d62018-05-29 05:17:35 -0700183 throw new AspectCreationException(e.getMessage(), extensionLabel);
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000184 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100185 }
186
187 @Nullable
188 @Override
189 public SkyValue compute(SkyKey skyKey, Environment env)
Florian Weikert4b67d4f2015-09-14 13:35:34 +0000190 throws AspectFunctionException, InterruptedException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100191 SkyframeBuildView view = buildViewProvider.getSkyframeBuildView();
ulfjack904a8d62018-05-29 05:17:35 -0700192 NestedSetBuilder<Cause> transitiveRootCauses = NestedSetBuilder.stableOrder();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100193 AspectKey key = (AspectKey) skyKey.argument();
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000194 ConfiguredAspectFactory aspectFactory;
Dmitry Lomov2eb8bdd2016-04-06 08:47:30 +0000195 Aspect aspect;
Luis Fernando Pino Duquee82713d2016-04-26 16:22:38 +0000196 if (key.getAspectClass() instanceof NativeAspectClass) {
197 NativeAspectClass nativeAspectClass = (NativeAspectClass) key.getAspectClass();
198 aspectFactory = (ConfiguredAspectFactory) nativeAspectClass;
Dmitry Lomove8040172016-04-06 14:53:43 +0000199 aspect = Aspect.forNative(nativeAspectClass, key.getParameters());
Dmitry Lomovb487ac62015-11-09 13:09:12 +0000200 } else if (key.getAspectClass() instanceof SkylarkAspectClass) {
201 SkylarkAspectClass skylarkAspectClass = (SkylarkAspectClass) key.getAspectClass();
cparsons0d55f4c2017-12-20 14:49:13 -0800202 SkylarkDefinedAspect skylarkAspect;
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000203 try {
nharmatadd21a932020-04-06 15:10:26 -0700204 skylarkAspect = loadSkylarkDefinedAspect(env, skylarkAspectClass);
Ulf Adams0678e792016-01-25 15:30:45 +0000205 } catch (AspectCreationException e) {
206 throw new AspectFunctionException(e);
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000207 }
208 if (skylarkAspect == null) {
209 return null;
210 }
211
Dmitry Lomov777845c2016-04-06 15:24:36 +0000212 aspectFactory = new SkylarkAspectFactory(skylarkAspect);
Dmitry Lomove8040172016-04-06 14:53:43 +0000213 aspect = Aspect.forSkylark(
214 skylarkAspect.getAspectClass(),
Googler74558fc2016-05-06 21:47:42 +0000215 skylarkAspect.getDefinition(key.getParameters()),
Dmitry Lomove8040172016-04-06 14:53:43 +0000216 key.getParameters());
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000217 } else {
218 throw new IllegalStateException();
219 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100220
Ulf Adams9e16f0a2016-01-25 12:43:32 +0000221 // Keep this in sync with the same code in ConfiguredTargetFunction.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100222 PackageValue packageValue =
223 (PackageValue) env.getValue(PackageValue.key(key.getLabel().getPackageIdentifier()));
224 if (packageValue == null) {
225 return null;
226 }
Janak Ramakrishnan0a4c6e42015-09-17 00:37:58 +0000227 Package pkg = packageValue.getPackage();
228 if (pkg.containsErrors()) {
229 throw new AspectFunctionException(
Ulf Adams9e16f0a2016-01-25 12:43:32 +0000230 new BuildFileContainsErrorsException(key.getLabel().getPackageIdentifier()));
Janak Ramakrishnan0a4c6e42015-09-17 00:37:58 +0000231 }
Lukacs Berkiea988b62016-08-30 12:26:18 +0000232
cpeysera8a61ee2018-01-26 09:20:37 -0800233 boolean aspectHasConfiguration = key.getAspectConfigurationKey() != null;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100234
cpeysera8a61ee2018-01-26 09:20:37 -0800235 ImmutableSet<SkyKey> keys =
236 aspectHasConfiguration
237 ? ImmutableSet.of(key.getBaseConfiguredTargetKey(), key.getAspectConfigurationKey())
238 : ImmutableSet.of(key.getBaseConfiguredTargetKey());
239
240 Map<SkyKey, ValueOrException<ConfiguredValueCreationException>> baseAndAspectValues =
241 env.getValuesOrThrow(keys, ConfiguredValueCreationException.class);
242 if (env.valuesMissing()) {
243 return null;
244 }
245
246 ConfiguredTargetValue baseConfiguredTargetValue;
247 BuildConfiguration aspectConfiguration = null;
248
Ulf Adams84901732016-01-28 15:05:16 +0000249 try {
cpeysera8a61ee2018-01-26 09:20:37 -0800250 baseConfiguredTargetValue =
251 (ConfiguredTargetValue) baseAndAspectValues.get(key.getBaseConfiguredTargetKey()).get();
Ulf Adams84901732016-01-28 15:05:16 +0000252 } catch (ConfiguredValueCreationException e) {
ulfjack904a8d62018-05-29 05:17:35 -0700253 throw new AspectFunctionException(
254 new AspectCreationException(e.getMessage(), e.getRootCauses()));
Ulf Adams84901732016-01-28 15:05:16 +0000255 }
cpeysera8a61ee2018-01-26 09:20:37 -0800256
257 if (aspectHasConfiguration) {
258 try {
259 aspectConfiguration =
260 ((BuildConfigurationValue)
261 baseAndAspectValues.get(key.getAspectConfigurationKey()).get())
262 .getConfiguration();
263 } catch (ConfiguredValueCreationException e) {
264 throw new IllegalStateException("Unexpected exception from BuildConfigurationFunction when "
265 + "computing " + key.getAspectConfigurationKey(), e);
266 }
mstaib7c7a7012019-04-23 16:19:09 -0700267 if (aspectConfiguration.trimConfigurationsRetroactively()) {
268 throw new AssertionError("Aspects should NEVER be evaluated in retroactive trimming mode.");
269 }
Dmitry Lomove2033b12015-08-19 16:57:49 +0000270 }
Lukacs Berkiea988b62016-08-30 12:26:18 +0000271
cpeysera8a61ee2018-01-26 09:20:37 -0800272 ConfiguredTarget associatedTarget = baseConfiguredTargetValue.getConfiguredTarget();
dslomov99ea6b42017-04-25 17:46:17 +0200273
janakr9c101402018-03-10 06:48:59 -0800274 ConfiguredTargetAndData associatedConfiguredTargetAndData;
janakr171a7eb2018-03-26 09:26:53 -0700275 Package targetPkg;
276 BuildConfiguration configuration = null;
277 PackageValue.Key packageKey =
278 PackageValue.key(associatedTarget.getLabel().getPackageIdentifier());
279 if (associatedTarget.getConfigurationKey() == null) {
280 PackageValue val = ((PackageValue) env.getValue(packageKey));
281 if (val == null) {
282 // Unexpected in Bazel logic, but Skyframe makes no guarantees that this package is
283 // actually present.
284 return null;
285 }
286 targetPkg = val.getPackage();
287 } else {
288 Map<SkyKey, SkyValue> result =
289 env.getValues(ImmutableSet.of(packageKey, associatedTarget.getConfigurationKey()));
290 if (env.valuesMissing()) {
291 // Unexpected in Bazel logic, but Skyframe makes no guarantees that this package and
292 // configuration are actually present.
293 return null;
294 }
295 targetPkg = ((PackageValue) result.get(packageKey)).getPackage();
296 configuration =
297 ((BuildConfigurationValue) result.get(associatedTarget.getConfigurationKey()))
298 .getConfiguration();
mstaib7c7a7012019-04-23 16:19:09 -0700299 if (configuration.trimConfigurationsRetroactively()) {
300 throw new AssertionError("Aspects should NEVER be evaluated in retroactive trimming mode.");
301 }
janakr171a7eb2018-03-26 09:26:53 -0700302 }
janakrf3e6f252018-01-18 07:45:12 -0800303 try {
janakr9c101402018-03-10 06:48:59 -0800304 associatedConfiguredTargetAndData =
305 new ConfiguredTargetAndData(
306 associatedTarget,
307 targetPkg.getTarget(associatedTarget.getLabel().getName()),
Googler46b285a2020-03-06 13:33:24 -0800308 configuration,
309 null);
janakrf3e6f252018-01-18 07:45:12 -0800310 } catch (NoSuchTargetException e) {
311 throw new IllegalStateException("Name already verified", e);
312 }
dslomov99ea6b42017-04-25 17:46:17 +0200313
cpeysera8a61ee2018-01-26 09:20:37 -0800314 if (baseConfiguredTargetValue.getConfiguredTarget().getProvider(AliasProvider.class) != null) {
tomlu3d1a1942017-11-29 14:01:21 -0800315 return createAliasAspect(
316 env,
janakr9c101402018-03-10 06:48:59 -0800317 associatedConfiguredTargetAndData.getTarget(),
tomlu3d1a1942017-11-29 14:01:21 -0800318 aspect,
319 key,
cpeysera8a61ee2018-01-26 09:20:37 -0800320 baseConfiguredTargetValue.getConfiguredTarget());
Lukacs Berkiea988b62016-08-30 12:26:18 +0000321 }
322
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000323
Dmitry Lomov15756522016-12-16 16:52:37 +0000324 ImmutableList.Builder<Aspect> aspectPathBuilder = ImmutableList.builder();
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000325
Dmitry Lomove851fe22017-02-14 23:11:23 +0000326 if (!key.getBaseKeys().isEmpty()) {
327 // We transitively collect all required aspects to reduce the number of restarts.
328 // Semantically it is enough to just request key.getBaseKeys().
dslomov039b9ee2017-04-11 08:51:30 +0000329 ImmutableList.Builder<SkyKey> aspectPathSkyKeysBuilder = ImmutableList.builder();
330 ImmutableMap<AspectDescriptor, SkyKey> aspectKeys =
331 getSkyKeysForAspectsAndCollectAspectPath(key.getBaseKeys(), aspectPathSkyKeysBuilder);
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000332
Dmitry Lomove851fe22017-02-14 23:11:23 +0000333 Map<SkyKey, SkyValue> values = env.getValues(aspectKeys.values());
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000334 if (env.valuesMissing()) {
335 return null;
336 }
dslomov039b9ee2017-04-11 08:51:30 +0000337 ImmutableList<SkyKey> aspectPathSkyKeys = aspectPathSkyKeysBuilder.build();
338 for (SkyKey aspectPathSkyKey : aspectPathSkyKeys) {
339 aspectPathBuilder.add(((AspectValue) values.get(aspectPathSkyKey)).getAspect());
340 }
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000341 try {
dslomov039b9ee2017-04-11 08:51:30 +0000342 associatedTarget = getBaseTarget(
343 associatedTarget, key.getBaseKeys(), values);
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000344 } catch (DuplicateException e) {
mjhalupka30bf44c2018-01-25 08:22:34 -0800345 env.getListener()
346 .handle(
347 Event.error(
janakr9c101402018-03-10 06:48:59 -0800348 associatedConfiguredTargetAndData.getTarget().getLocation(), e.getMessage()));
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000349
350 throw new AspectFunctionException(
ulfjack904a8d62018-05-29 05:17:35 -0700351 new AspectCreationException(
352 e.getMessage(), associatedTarget.getLabel(), aspectConfiguration));
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000353 }
354 }
janakr9c101402018-03-10 06:48:59 -0800355 associatedConfiguredTargetAndData =
356 associatedConfiguredTargetAndData.fromConfiguredTarget(associatedTarget);
Dmitry Lomov15756522016-12-16 16:52:37 +0000357 aspectPathBuilder.add(aspect);
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000358
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100359 SkyframeDependencyResolver resolver = view.createDependencyResolver(env);
janakr931d2852017-12-15 13:48:29 -0800360 NestedSetBuilder<Package> transitivePackagesForPackageRootResolution =
361 storeTransitivePackagesForPackageRootResolution ? NestedSetBuilder.stableOrder() : null;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100362
Michael Staib04f6f242016-03-01 15:40:29 +0000363 // When getting the dependencies of this hybrid aspect+base target, use the aspect's
364 // configuration. The configuration of the aspect will always be a superset of the target's
gregcee0bbe752017-09-12 23:58:34 +0200365 // (trimmed configuration mode: target is part of the aspect's config fragment requirements;
366 // untrimmed mode: target is the same configuration as the aspect), so the fragments
Michael Staib04f6f242016-03-01 15:40:29 +0000367 // required by all dependencies (both those of the aspect and those of the base target)
368 // will be present this way.
369 TargetAndConfiguration originalTargetAndAspectConfiguration =
janakrf3e6f252018-01-18 07:45:12 -0800370 new TargetAndConfiguration(
janakr9c101402018-03-10 06:48:59 -0800371 associatedConfiguredTargetAndData.getTarget(), aspectConfiguration);
Dmitry Lomov15756522016-12-16 16:52:37 +0000372 ImmutableList<Aspect> aspectPath = aspectPathBuilder.build();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100373 try {
374 // Get the configuration targets that trigger this rule's configurable attributes.
Lukacs Berki7894c182016-05-10 12:07:01 +0000375 ImmutableMap<Label, ConfigMatchingProvider> configConditions =
376 ConfiguredTargetFunction.getConfigConditions(
janakr9c101402018-03-10 06:48:59 -0800377 associatedConfiguredTargetAndData.getTarget(),
janakr931d2852017-12-15 13:48:29 -0800378 env,
janakr931d2852017-12-15 13:48:29 -0800379 originalTargetAndAspectConfiguration,
380 transitivePackagesForPackageRootResolution,
lberkic67dcec2019-01-17 11:24:01 -0800381 transitiveRootCauses);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100382 if (configConditions == null) {
383 // Those targets haven't yet been resolved.
384 return null;
385 }
386
John Cater2e56f062017-07-20 19:43:20 +0200387 // Determine what toolchains are needed by this target.
jcater1f724162018-08-27 09:16:01 -0700388 UnloadedToolchainContext unloadedToolchainContext = null;
389 if (configuration != null) {
390 // Configuration can be null in the case of aspects applied to input files. In this case,
391 // there are no chances of toolchains being used, so skip it.
392 try {
393 ImmutableSet<Label> requiredToolchains = aspect.getDefinition().getRequiredToolchains();
394 unloadedToolchainContext =
John Catere39e5c92019-04-23 10:28:02 -0700395 (UnloadedToolchainContext)
396 env.getValueOrThrow(
397 UnloadedToolchainContext.key()
398 .configurationKey(BuildConfigurationValue.key(configuration))
399 .requiredToolchainTypeLabels(requiredToolchains)
400 .shouldSanityCheckConfiguration(
401 configuration.trimConfigurationsRetroactively())
402 .build(),
403 ToolchainException.class);
jcater1f724162018-08-27 09:16:01 -0700404 } catch (ToolchainException e) {
405 // TODO(katre): better error handling
406 throw new AspectCreationException(
407 e.getMessage(), new LabelCause(key.getLabel(), e.getMessage()));
408 }
409 if (env.valuesMissing()) {
410 return null;
411 }
John Cater2e56f062017-07-20 19:43:20 +0200412 }
413
lberki102256f2019-02-08 01:34:23 -0800414 OrderedSetMultimap<DependencyKind, ConfiguredTargetAndData> depValueMap;
Dmitry Lomov9b2fc5c2016-11-11 11:18:48 +0000415 try {
John Cater2e56f062017-07-20 19:43:20 +0200416 depValueMap =
417 ConfiguredTargetFunction.computeDependencies(
418 env,
419 resolver,
420 originalTargetAndAspectConfiguration,
421 aspectPath,
422 configConditions,
John Cater115ff572019-04-26 09:20:25 -0700423 unloadedToolchainContext,
John Cater2e56f062017-07-20 19:43:20 +0200424 ruleClassProvider,
425 view.getHostConfiguration(originalTargetAndAspectConfiguration.getConfiguration()),
janakr931d2852017-12-15 13:48:29 -0800426 transitivePackagesForPackageRootResolution,
mjhalupka5d7fa7b2018-03-22 13:37:38 -0700427 transitiveRootCauses,
428 defaultBuildOptions);
Dmitry Lomov9b2fc5c2016-11-11 11:18:48 +0000429 } catch (ConfiguredTargetFunctionException e) {
ulfjack904a8d62018-05-29 05:17:35 -0700430 throw new AspectCreationException(e.getMessage(), key.getLabel(), aspectConfiguration);
Dmitry Lomov9b2fc5c2016-11-11 11:18:48 +0000431 }
Ulf Adams84901732016-01-28 15:05:16 +0000432 if (depValueMap == null) {
433 return null;
434 }
435 if (!transitiveRootCauses.isEmpty()) {
436 throw new AspectFunctionException(
437 new AspectCreationException("Loading failed", transitiveRootCauses.build()));
438 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100439
John Caterae4e7852018-07-09 15:02:42 -0700440 // Load the requested toolchains into the ToolchainContext, now that we have dependencies.
John Catercdfa9ca2019-04-05 12:32:09 -0700441 ResolvedToolchainContext toolchainContext = null;
jcater1f724162018-08-27 09:16:01 -0700442 if (unloadedToolchainContext != null) {
John Cater50745142019-04-16 07:41:24 -0700443 String targetDescription =
444 String.format(
445 "aspect %s applied to %s",
446 aspect.getDescriptor().getDescription(),
447 associatedConfiguredTargetAndData.getTarget());
lberki102256f2019-02-08 01:34:23 -0800448 toolchainContext =
John Cater50745142019-04-16 07:41:24 -0700449 ResolvedToolchainContext.load(
Googler4ff29122019-09-04 21:14:38 -0700450 targetPkg.getRepositoryMapping(),
John Cater50745142019-04-16 07:41:24 -0700451 unloadedToolchainContext,
452 targetDescription,
453 depValueMap.get(DependencyResolver.TOOLCHAIN_DEPENDENCY));
John Caterae4e7852018-07-09 15:02:42 -0700454 }
455
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000456 return createAspect(
457 env,
458 key,
Dmitry Lomov15756522016-12-16 16:52:37 +0000459 aspectPath,
Dmitry Lomov2eb8bdd2016-04-06 08:47:30 +0000460 aspect,
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000461 aspectFactory,
janakr9c101402018-03-10 06:48:59 -0800462 associatedConfiguredTargetAndData,
cpeysera8a61ee2018-01-26 09:20:37 -0800463 aspectConfiguration,
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000464 configConditions,
John Cater2e56f062017-07-20 19:43:20 +0200465 toolchainContext,
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000466 depValueMap,
janakr931d2852017-12-15 13:48:29 -0800467 transitivePackagesForPackageRootResolution);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100468 } catch (DependencyEvaluationException e) {
Ulf Adams25f03d82016-01-25 10:31:46 +0000469 if (e.getCause() instanceof ConfiguredValueCreationException) {
470 ConfiguredValueCreationException cause = (ConfiguredValueCreationException) e.getCause();
ulfjack904a8d62018-05-29 05:17:35 -0700471 throw new AspectFunctionException(
472 new AspectCreationException(cause.getMessage(), cause.getRootCauses()));
Dmitry Lomovd83af9e2017-02-23 15:44:23 +0000473 } else if (e.getCause() instanceof InconsistentAspectOrderException) {
474 InconsistentAspectOrderException cause = (InconsistentAspectOrderException) e.getCause();
ulfjack904a8d62018-05-29 05:17:35 -0700475 throw new AspectFunctionException(
476 new AspectCreationException(cause.getMessage(), key.getLabel(), aspectConfiguration));
Ulf Adams25f03d82016-01-25 10:31:46 +0000477 } else {
478 // Cast to InvalidConfigurationException as a consistency check. If you add any
479 // DependencyEvaluationException constructors, you may need to change this code, too.
480 InvalidConfigurationException cause = (InvalidConfigurationException) e.getCause();
ulfjack904a8d62018-05-29 05:17:35 -0700481 throw new AspectFunctionException(
482 new AspectCreationException(cause.getMessage(), key.getLabel(), aspectConfiguration));
Ulf Adams25f03d82016-01-25 10:31:46 +0000483 }
Marian Loburfc567b32015-09-14 08:44:25 +0000484 } catch (AspectCreationException e) {
485 throw new AspectFunctionException(e);
jcater3fed8692019-04-01 13:36:36 -0700486 } catch (ToolchainException e) {
487 throw new AspectFunctionException(
488 new AspectCreationException(
489 e.getMessage(), new LabelCause(key.getLabel(), e.getMessage())));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100490 }
491 }
492
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000493 /**
494 * Merges aspects defined by {@code aspectKeys} into the {@code target} using
495 * previously computed {@code values}.
496 *
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000497 * @return A {@link ConfiguredTarget} that is a result of a merge.
498 * @throws DuplicateException if there is a duplicate provider provided by aspects.
499 */
dslomov039b9ee2017-04-11 08:51:30 +0000500 private ConfiguredTarget getBaseTarget(ConfiguredTarget target,
Dmitry Lomove851fe22017-02-14 23:11:23 +0000501 ImmutableList<AspectKey> aspectKeys,
dslomov039b9ee2017-04-11 08:51:30 +0000502 Map<SkyKey, SkyValue> values)
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000503 throws DuplicateException {
504 ArrayList<ConfiguredAspect> aspectValues = new ArrayList<>();
Dmitry Lomove851fe22017-02-14 23:11:23 +0000505 for (AspectKey aspectKey : aspectKeys) {
janakr573807d2018-01-11 14:02:35 -0800506 AspectValue aspectValue = (AspectValue) values.get(aspectKey);
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000507 ConfiguredAspect configuredAspect = aspectValue.getConfiguredAspect();
508 aspectValues.add(configuredAspect);
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000509 }
510 return MergedConfiguredTarget.of(target, aspectValues);
511 }
512
513 /**
Dmitry Lomove851fe22017-02-14 23:11:23 +0000514 * Collect all SkyKeys that are needed for a given list of AspectKeys,
515 * including transitive dependencies.
dslomov039b9ee2017-04-11 08:51:30 +0000516 *
517 * Also collects all propagating aspects in correct order.
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000518 */
dslomov039b9ee2017-04-11 08:51:30 +0000519 private ImmutableMap<AspectDescriptor, SkyKey> getSkyKeysForAspectsAndCollectAspectPath(
520 ImmutableList<AspectKey> keys,
521 ImmutableList.Builder<SkyKey> aspectPathBuilder) {
Dmitry Lomove851fe22017-02-14 23:11:23 +0000522 HashMap<AspectDescriptor, SkyKey> result = new HashMap<>();
523 for (AspectKey key : keys) {
dslomov039b9ee2017-04-11 08:51:30 +0000524 buildSkyKeys(key, result, aspectPathBuilder);
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000525 }
Dmitry Lomove851fe22017-02-14 23:11:23 +0000526 return ImmutableMap.copyOf(result);
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000527 }
528
dslomov039b9ee2017-04-11 08:51:30 +0000529 private void buildSkyKeys(AspectKey key, HashMap<AspectDescriptor, SkyKey> result,
530 ImmutableList.Builder<SkyKey> aspectPathBuilder) {
Dmitry Lomove851fe22017-02-14 23:11:23 +0000531 if (result.containsKey(key.getAspectDescriptor())) {
532 return;
533 }
534 ImmutableList<AspectKey> baseKeys = key.getBaseKeys();
janakr573807d2018-01-11 14:02:35 -0800535 result.put(key.getAspectDescriptor(), key);
Dmitry Lomove851fe22017-02-14 23:11:23 +0000536 for (AspectKey baseKey : baseKeys) {
dslomov039b9ee2017-04-11 08:51:30 +0000537 buildSkyKeys(baseKey, result, aspectPathBuilder);
Dmitry Lomove851fe22017-02-14 23:11:23 +0000538 }
dslomov039b9ee2017-04-11 08:51:30 +0000539 // Post-order list of aspect SkyKeys gives the order of propagating aspects:
540 // the aspect comes after all aspects it transitively sees.
janakr573807d2018-01-11 14:02:35 -0800541 aspectPathBuilder.add(key);
Dmitry Lomove851fe22017-02-14 23:11:23 +0000542 }
dslomovfa50c3d2017-05-08 08:47:44 -0400543
janakr93e3eea2017-03-30 22:09:37 +0000544 private SkyValue createAliasAspect(
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +0000545 Environment env,
546 Target originalTarget,
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +0000547 Aspect aspect,
Lukacs Berkiea988b62016-08-30 12:26:18 +0000548 AspectKey originalKey,
janakr93e3eea2017-03-30 22:09:37 +0000549 ConfiguredTarget configuredTarget)
550 throws InterruptedException {
Lukacs Berkiea988b62016-08-30 12:26:18 +0000551 ImmutableList<Label> aliasChain = configuredTarget.getProvider(AliasProvider.class)
552 .getAliasChain();
553 // Find the next alias in the chain: either the next alias (if there are two) or the name of
554 // the real configured target.
555 Label aliasLabel = aliasChain.size() > 1 ? aliasChain.get(1) : configuredTarget.getLabel();
556
cparsons089148b2019-09-17 08:14:41 -0700557 return createAliasAspect(env, originalTarget, aspect, originalKey, aliasLabel);
558 }
559
560 private AspectValue createAliasAspect(
561 Environment env,
562 Target originalTarget,
563 Aspect aspect,
564 AspectKey originalKey,
565 Label aliasLabel)
566 throws InterruptedException {
janakr573807d2018-01-11 14:02:35 -0800567 SkyKey depKey = originalKey.withLabel(aliasLabel);
Lukacs Berkiea988b62016-08-30 12:26:18 +0000568
569 // Compute the AspectValue of the target the alias refers to (which can itself be either an
570 // alias or a real target)
Lukacs Berki549bfce2016-04-22 15:29:12 +0000571 AspectValue real = (AspectValue) env.getValue(depKey);
572 if (env.valuesMissing()) {
573 return null;
574 }
575
janakr931d2852017-12-15 13:48:29 -0800576 NestedSet<Package> transitivePackagesForPackageRootResolution =
577 storeTransitivePackagesForPackageRootResolution
578 ? NestedSetBuilder.<Package>stableOrder()
579 .addTransitive(real.getTransitivePackagesForPackageRootResolution())
580 .add(originalTarget.getPackage())
581 .build()
582 : null;
Lukacs Berkiea988b62016-08-30 12:26:18 +0000583
Lukacs Berki549bfce2016-04-22 15:29:12 +0000584 return new AspectValue(
585 originalKey,
586 aspect,
587 originalTarget.getLabel(),
588 originalTarget.getLocation(),
589 ConfiguredAspect.forAlias(real.getConfiguredAspect()),
Googlerce6ad292019-12-20 10:29:21 -0800590 transitivePackagesForPackageRootResolution);
Lukacs Berki549bfce2016-04-22 15:29:12 +0000591 }
592
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100593 @Nullable
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000594 private AspectValue createAspect(
595 Environment env,
596 AspectKey key,
Dmitry Lomov15756522016-12-16 16:52:37 +0000597 ImmutableList<Aspect> aspectPath,
Dmitry Lomov2eb8bdd2016-04-06 08:47:30 +0000598 Aspect aspect,
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000599 ConfiguredAspectFactory aspectFactory,
janakr9c101402018-03-10 06:48:59 -0800600 ConfiguredTargetAndData associatedTarget,
Michael Staib04f6f242016-03-01 15:40:29 +0000601 BuildConfiguration aspectConfiguration,
Lukacs Berki7894c182016-05-10 12:07:01 +0000602 ImmutableMap<Label, ConfigMatchingProvider> configConditions,
John Catercdfa9ca2019-04-05 12:32:09 -0700603 ResolvedToolchainContext toolchainContext,
lberki102256f2019-02-08 01:34:23 -0800604 OrderedSetMultimap<DependencyKind, ConfiguredTargetAndData> directDeps,
janakr931d2852017-12-15 13:48:29 -0800605 @Nullable NestedSetBuilder<Package> transitivePackagesForPackageRootResolution)
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000606 throws AspectFunctionException, InterruptedException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100607 SkyframeBuildView view = buildViewProvider.getSkyframeBuildView();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100608
609 StoredEventHandler events = new StoredEventHandler();
610 CachingAnalysisEnvironment analysisEnvironment = view.createAnalysisEnvironment(
Michael Staib04f6f242016-03-01 15:40:29 +0000611 key, false, events, env, aspectConfiguration);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100612 if (env.valuesMissing()) {
613 return null;
614 }
615
Dmitry Lomov6cd98972017-03-01 15:44:00 +0000616 ConfiguredAspect configuredAspect;
cparsons089148b2019-09-17 08:14:41 -0700617 if (aspect.getDefinition().applyToGeneratingRules()
618 && associatedTarget.getTarget() instanceof OutputFile) {
619 OutputFile outputFile = (OutputFile) associatedTarget.getTarget();
620 Label label = outputFile.getGeneratingRule().getLabel();
621 return createAliasAspect(env, associatedTarget.getTarget(), aspect, key, label);
622 } else if (AspectResolver.aspectMatchesConfiguredTarget(associatedTarget, aspect)) {
tomlu72642a22017-10-18 06:23:14 +0200623 try {
624 CurrentRuleTracker.beginConfiguredAspect(aspect.getAspectClass());
625 configuredAspect =
626 view.getConfiguredTargetFactory()
627 .createAspect(
628 analysisEnvironment,
629 associatedTarget,
630 aspectPath,
631 aspectFactory,
632 aspect,
633 directDeps,
634 configConditions,
635 toolchainContext,
636 aspectConfiguration,
janakreaff19c2019-01-31 13:59:40 -0800637 view.getHostConfiguration(aspectConfiguration),
638 key);
ulfjack37407682019-09-17 06:30:49 -0700639 } catch (MissingDepException e) {
640 Preconditions.checkState(env.valuesMissing());
641 return null;
tomlu72642a22017-10-18 06:23:14 +0200642 } finally {
643 CurrentRuleTracker.endConfiguredAspect();
644 }
Dmitry Lomov6cd98972017-03-01 15:44:00 +0000645 } else {
646 configuredAspect = ConfiguredAspect.forNonapplicableTarget(aspect.getDescriptor());
647 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100648
649 events.replayOn(env.getListener());
650 if (events.hasErrors()) {
651 analysisEnvironment.disable(associatedTarget.getTarget());
janakr043acd92020-02-28 18:59:06 -0800652 String msg = "Analysis of target '" + associatedTarget.getTarget().getLabel() + "' failed";
janakrf3e6f252018-01-18 07:45:12 -0800653 throw new AspectFunctionException(
ulfjack904a8d62018-05-29 05:17:35 -0700654 new AspectCreationException(msg, key.getLabel(), aspectConfiguration));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100655 }
656 Preconditions.checkState(!analysisEnvironment.hasErrors(),
657 "Analysis environment hasError() but no errors reported");
658
659 if (env.valuesMissing()) {
660 return null;
661 }
662
663 analysisEnvironment.disable(associatedTarget.getTarget());
Dmitry Lomovb487ac62015-11-09 13:09:12 +0000664 Preconditions.checkNotNull(configuredAspect);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100665
666 return new AspectValue(
Dmitry Lomove2033b12015-08-19 16:57:49 +0000667 key,
Dmitry Lomov2eb8bdd2016-04-06 08:47:30 +0000668 aspect,
janakrf3e6f252018-01-18 07:45:12 -0800669 associatedTarget.getTarget().getLabel(),
Dmitry Lomove2033b12015-08-19 16:57:49 +0000670 associatedTarget.getTarget().getLocation(),
Dmitry Lomovb487ac62015-11-09 13:09:12 +0000671 configuredAspect,
janakr931d2852017-12-15 13:48:29 -0800672 transitivePackagesForPackageRootResolution == null
673 ? null
Googlerce6ad292019-12-20 10:29:21 -0800674 : transitivePackagesForPackageRootResolution.build());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100675 }
676
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100677 @Override
678 public String extractTag(SkyKey skyKey) {
Michael Staib2707a882016-09-16 21:06:40 +0000679 AspectKey aspectKey = (AspectKey) skyKey.argument();
680 return Label.print(aspectKey.getLabel());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100681 }
Marian Loburc62faba2015-09-09 10:08:06 +0000682
jcatera8ee68a2020-04-08 08:36:24 -0700683 /** An exception indicating that there was a problem creating an aspect. */
684 public static final class AspectCreationException extends Exception
685 implements SaneAnalysisException {
ulfjack904a8d62018-05-29 05:17:35 -0700686 private static ConfigurationId toId(BuildConfiguration config) {
janakr3ca24682020-04-01 09:12:03 -0700687 return config == null ? null : config.getEventId().getConfiguration();
ulfjack904a8d62018-05-29 05:17:35 -0700688 }
Ulf Adams84901732016-01-28 15:05:16 +0000689
ulfjack904a8d62018-05-29 05:17:35 -0700690 private final NestedSet<Cause> causes;
Ulf Adams25f03d82016-01-25 10:31:46 +0000691
ulfjack904a8d62018-05-29 05:17:35 -0700692 public AspectCreationException(String message, NestedSet<Cause> causes) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100693 super(message);
ulfjack904a8d62018-05-29 05:17:35 -0700694 this.causes = causes;
Ulf Adams25f03d82016-01-25 10:31:46 +0000695 }
696
ulfjack904a8d62018-05-29 05:17:35 -0700697 public AspectCreationException(
698 String message, Label currentTarget, @Nullable BuildConfiguration configuration) {
699 this(
700 message,
701 NestedSetBuilder.<Cause>stableOrder()
702 .add(new AnalysisFailedCause(currentTarget, toId(configuration), message))
703 .build());
Ulf Adams84901732016-01-28 15:05:16 +0000704 }
705
ulfjack904a8d62018-05-29 05:17:35 -0700706 public AspectCreationException(String message, Label currentTarget) {
707 this(message, currentTarget, null);
Ulf Adams84901732016-01-28 15:05:16 +0000708 }
709
ulfjack904a8d62018-05-29 05:17:35 -0700710 public AspectCreationException(String message, Cause cause) {
711 this(message, NestedSetBuilder.<Cause>stableOrder().add(cause).build());
Ulf Adams84901732016-01-28 15:05:16 +0000712 }
713
ulfjack904a8d62018-05-29 05:17:35 -0700714 public NestedSet<Cause> getCauses() {
715 return causes;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100716 }
717 }
718
tomlu39a0a382018-06-22 09:43:23 -0700719 /** Used to indicate errors during the computation of an {@link AspectValue}. */
720 public static final class AspectFunctionException extends SkyFunctionException {
Ulf Adams9e16f0a2016-01-25 12:43:32 +0000721 public AspectFunctionException(NoSuchThingException e) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100722 super(e, Transience.PERSISTENT);
723 }
724
Ulf Adams9e16f0a2016-01-25 12:43:32 +0000725 public AspectFunctionException(AspectCreationException e) {
726 super(e, Transience.PERSISTENT);
Ulf Adamsd55d7af2016-01-19 11:03:22 +0000727 }
Dmitry Lomovd83af9e2017-02-23 15:44:23 +0000728
janakr0175ce32018-02-26 15:54:57 -0800729 public AspectFunctionException(ActionConflictException cause) {
Dmitry Lomovd83af9e2017-02-23 15:44:23 +0000730 super(cause, Transience.PERSISTENT);
731 }
Ulf Adamsd55d7af2016-01-19 11:03:22 +0000732 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100733}