Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 17 | import com.google.common.base.Supplier; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
cpeyser | fb82999 | 2017-09-07 17:17:03 +0200 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.ActionAnalysisMetadata; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.ActionLookupValue; |
ulfjack | 90e2e98 | 2017-08-07 11:27:32 +0200 | [diff] [blame] | 23 | import com.google.devtools.build.lib.analysis.AliasProvider; |
gregce | 4c3ef11 | 2017-09-20 23:43:14 +0200 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.AspectResolver; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.CachingAnalysisEnvironment; |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.ConfiguredAspect; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.ConfiguredAspectFactory; |
| 28 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import com.google.devtools.build.lib.analysis.TargetAndConfiguration; |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 31 | import com.google.devtools.build.lib.analysis.ToolchainContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 33 | import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider; |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; |
gregce | 2aee44b | 2017-09-16 07:16:44 +0200 | [diff] [blame] | 35 | import com.google.devtools.build.lib.analysis.configuredtargets.MergedConfiguredTarget; |
| 36 | import com.google.devtools.build.lib.analysis.configuredtargets.MergedConfiguredTarget.DuplicateException; |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.cmdline.Label; |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.collect.nestedset.Order; |
Ulf Adams | 9e16f0a | 2016-01-25 12:43:32 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.events.Event; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | import com.google.devtools.build.lib.events.StoredEventHandler; |
Dmitry Lomov | 2eb8bdd | 2016-04-06 08:47:30 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.packages.Aspect; |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.packages.AspectDescriptor; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | import com.google.devtools.build.lib.packages.Attribute; |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 46 | import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 47 | import com.google.devtools.build.lib.packages.NativeAspectClass; |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 48 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.packages.Package; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.packages.RuleClassProvider; |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 51 | import com.google.devtools.build.lib.packages.SkylarkAspect; |
Dmitry Lomov | 82e0377 | 2015-11-30 12:13:22 +0000 | [diff] [blame] | 52 | import com.google.devtools.build.lib.packages.SkylarkAspectClass; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | import com.google.devtools.build.lib.packages.Target; |
| 54 | import com.google.devtools.build.lib.skyframe.AspectValue.AspectKey; |
Dmitry Lomov | 9b2fc5c | 2016-11-11 11:18:48 +0000 | [diff] [blame] | 55 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.ConfiguredTargetFunctionException; |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.ConfiguredValueCreationException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 57 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.DependencyEvaluationException; |
| 58 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor.BuildViewProvider; |
Ulf Adams | a47ca01 | 2016-01-20 10:35:19 +0000 | [diff] [blame] | 59 | import com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException; |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 60 | import com.google.devtools.build.lib.skyframe.ToolchainUtil.ToolchainContextException; |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 61 | import com.google.devtools.build.lib.syntax.Type.ConversionException; |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 62 | import com.google.devtools.build.lib.util.OrderedSetMultimap; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 63 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | import com.google.devtools.build.skyframe.SkyFunction; |
| 65 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 66 | import com.google.devtools.build.skyframe.SkyKey; |
| 67 | import com.google.devtools.build.skyframe.SkyValue; |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 68 | import java.util.ArrayList; |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 69 | import java.util.HashMap; |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 70 | import java.util.Map; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 71 | import javax.annotation.Nullable; |
| 72 | |
| 73 | /** |
| 74 | * The Skyframe function that generates aspects. |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 75 | * |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 76 | * This class, together with {@link ConfiguredTargetFunction} drives the analysis phase. For more |
ulfjack | 26d0e49 | 2017-08-07 13:42:33 +0200 | [diff] [blame] | 77 | * information, see {@link com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory}. |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 78 | * |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 79 | * {@link AspectFunction} takes a SkyKey containing an {@link AspectKey} [a tuple of |
| 80 | * (target label, configurations, aspect class and aspect parameters)], |
| 81 | * loads an {@link Aspect} from aspect class and aspect parameters, |
| 82 | * gets a {@link ConfiguredTarget} for label and configurations, and then creates |
| 83 | * a {@link ConfiguredAspect} for a given {@link AspectKey}. |
| 84 | * |
| 85 | * See {@link com.google.devtools.build.lib.packages.AspectClass} documentation |
| 86 | * for an overview of aspect-related classes |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 87 | * |
ulfjack | 26d0e49 | 2017-08-07 13:42:33 +0200 | [diff] [blame] | 88 | * @see com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 89 | * @see com.google.devtools.build.lib.packages.AspectClass |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | */ |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 91 | public final class AspectFunction implements SkyFunction { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 92 | private final BuildViewProvider buildViewProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 93 | private final RuleClassProvider ruleClassProvider; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 94 | private final Supplier<Boolean> removeActionsAfterEvaluation; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 95 | |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 96 | AspectFunction( |
| 97 | BuildViewProvider buildViewProvider, |
| 98 | RuleClassProvider ruleClassProvider, |
| 99 | Supplier<Boolean> removeActionsAfterEvaluation) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | this.buildViewProvider = buildViewProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 101 | this.ruleClassProvider = ruleClassProvider; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 102 | this.removeActionsAfterEvaluation = Preconditions.checkNotNull(removeActionsAfterEvaluation); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Load Skylark aspect from an extension file. Is to be called from a SkyFunction. |
| 107 | * |
| 108 | * @return {@code null} if dependencies cannot be satisfied. |
| 109 | */ |
| 110 | @Nullable |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 111 | static SkylarkAspect loadSkylarkAspect( |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 112 | Environment env, Label extensionLabel, String skylarkValueName) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 113 | throws AspectCreationException, InterruptedException { |
Damien Martin-Guillerez | 8ca065c2 | 2015-11-27 16:22:59 +0000 | [diff] [blame] | 114 | SkyKey importFileKey = SkylarkImportLookupValue.key(extensionLabel, false); |
Ulf Adams | 0678e79 | 2016-01-25 15:30:45 +0000 | [diff] [blame] | 115 | try { |
| 116 | SkylarkImportLookupValue skylarkImportLookupValue = |
| 117 | (SkylarkImportLookupValue) env.getValueOrThrow( |
| 118 | importFileKey, SkylarkImportFailedException.class); |
| 119 | if (skylarkImportLookupValue == null) { |
| 120 | return null; |
| 121 | } |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 122 | |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 123 | Object skylarkValue = skylarkImportLookupValue.getEnvironmentExtension().getBindings() |
Ulf Adams | 0678e79 | 2016-01-25 15:30:45 +0000 | [diff] [blame] | 124 | .get(skylarkValueName); |
Dmitry Lomov | 1920dd9 | 2017-07-21 12:57:18 +0200 | [diff] [blame] | 125 | if (skylarkValue == null) { |
| 126 | throw new ConversionException( |
| 127 | String.format( |
| 128 | "%s is not exported from %s", skylarkValueName, extensionLabel.toString())); |
| 129 | } |
Ulf Adams | 0678e79 | 2016-01-25 15:30:45 +0000 | [diff] [blame] | 130 | if (!(skylarkValue instanceof SkylarkAspect)) { |
| 131 | throw new ConversionException( |
Dmitry Lomov | 1920dd9 | 2017-07-21 12:57:18 +0200 | [diff] [blame] | 132 | String.format( |
| 133 | "%s from %s is not an aspect", skylarkValueName, extensionLabel.toString())); |
Ulf Adams | 0678e79 | 2016-01-25 15:30:45 +0000 | [diff] [blame] | 134 | } |
| 135 | return (SkylarkAspect) skylarkValue; |
| 136 | } catch (SkylarkImportFailedException | ConversionException e) { |
| 137 | env.getListener().handle(Event.error(e.getMessage())); |
| 138 | throw new AspectCreationException(e.getMessage()); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 139 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Nullable |
| 143 | @Override |
| 144 | public SkyValue compute(SkyKey skyKey, Environment env) |
Florian Weikert | 4b67d4f | 2015-09-14 13:35:34 +0000 | [diff] [blame] | 145 | throws AspectFunctionException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 146 | SkyframeBuildView view = buildViewProvider.getSkyframeBuildView(); |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 147 | NestedSetBuilder<Package> transitivePackages = NestedSetBuilder.stableOrder(); |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 148 | NestedSetBuilder<Label> transitiveRootCauses = NestedSetBuilder.stableOrder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 149 | AspectKey key = (AspectKey) skyKey.argument(); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 150 | ConfiguredAspectFactory aspectFactory; |
Dmitry Lomov | 2eb8bdd | 2016-04-06 08:47:30 +0000 | [diff] [blame] | 151 | Aspect aspect; |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 152 | if (key.getAspectClass() instanceof NativeAspectClass) { |
| 153 | NativeAspectClass nativeAspectClass = (NativeAspectClass) key.getAspectClass(); |
| 154 | aspectFactory = (ConfiguredAspectFactory) nativeAspectClass; |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 155 | aspect = Aspect.forNative(nativeAspectClass, key.getParameters()); |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 156 | } else if (key.getAspectClass() instanceof SkylarkAspectClass) { |
| 157 | SkylarkAspectClass skylarkAspectClass = (SkylarkAspectClass) key.getAspectClass(); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 158 | SkylarkAspect skylarkAspect; |
| 159 | try { |
| 160 | skylarkAspect = |
| 161 | loadSkylarkAspect( |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 162 | env, skylarkAspectClass.getExtensionLabel(), skylarkAspectClass.getExportedName()); |
Ulf Adams | 0678e79 | 2016-01-25 15:30:45 +0000 | [diff] [blame] | 163 | } catch (AspectCreationException e) { |
| 164 | throw new AspectFunctionException(e); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 165 | } |
| 166 | if (skylarkAspect == null) { |
| 167 | return null; |
| 168 | } |
| 169 | |
Dmitry Lomov | 777845c | 2016-04-06 15:24:36 +0000 | [diff] [blame] | 170 | aspectFactory = new SkylarkAspectFactory(skylarkAspect); |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 171 | aspect = Aspect.forSkylark( |
| 172 | skylarkAspect.getAspectClass(), |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 173 | skylarkAspect.getDefinition(key.getParameters()), |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 174 | key.getParameters()); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 175 | } else { |
| 176 | throw new IllegalStateException(); |
| 177 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 178 | |
Ulf Adams | 9e16f0a | 2016-01-25 12:43:32 +0000 | [diff] [blame] | 179 | // Keep this in sync with the same code in ConfiguredTargetFunction. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 180 | PackageValue packageValue = |
| 181 | (PackageValue) env.getValue(PackageValue.key(key.getLabel().getPackageIdentifier())); |
| 182 | if (packageValue == null) { |
| 183 | return null; |
| 184 | } |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 185 | Package pkg = packageValue.getPackage(); |
| 186 | if (pkg.containsErrors()) { |
| 187 | throw new AspectFunctionException( |
Ulf Adams | 9e16f0a | 2016-01-25 12:43:32 +0000 | [diff] [blame] | 188 | new BuildFileContainsErrorsException(key.getLabel().getPackageIdentifier())); |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 189 | } |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 190 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 191 | |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 192 | ConfiguredTargetValue configuredTargetValue; |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 193 | try { |
| 194 | configuredTargetValue = |
| 195 | (ConfiguredTargetValue) env.getValueOrThrow( |
Michael Staib | 04f6f24 | 2016-03-01 15:40:29 +0000 | [diff] [blame] | 196 | ConfiguredTargetValue.key(key.getLabel(), key.getBaseConfiguration()), |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 197 | ConfiguredValueCreationException.class); |
| 198 | } catch (ConfiguredValueCreationException e) { |
| 199 | throw new AspectFunctionException(new AspectCreationException(e.getRootCauses())); |
| 200 | } |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 201 | if (configuredTargetValue == null) { |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 202 | // TODO(bazel-team): remove this check when top-level targets also use dynamic configurations. |
| 203 | // Right now the key configuration may be dynamic while the original target's configuration |
| 204 | // is static, resulting in a Skyframe cache miss even though the original target is, in fact, |
| 205 | // precomputed. |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 206 | return null; |
| 207 | } |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 208 | |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 209 | |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 210 | if (configuredTargetValue.getConfiguredTarget() == null) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 211 | return null; |
| 212 | } |
| 213 | |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 214 | ConfiguredTarget associatedTarget = configuredTargetValue.getConfiguredTarget(); |
| 215 | |
| 216 | Target target = associatedTarget.getTarget(); |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 217 | |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 218 | if (configuredTargetValue.getConfiguredTarget().getProvider(AliasProvider.class) != null) { |
| 219 | return createAliasAspect(env, target, aspect, key, |
| 220 | configuredTargetValue.getConfiguredTarget()); |
| 221 | } |
| 222 | |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 223 | |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 224 | ImmutableList.Builder<Aspect> aspectPathBuilder = ImmutableList.builder(); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 225 | |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 226 | if (!key.getBaseKeys().isEmpty()) { |
| 227 | // We transitively collect all required aspects to reduce the number of restarts. |
| 228 | // Semantically it is enough to just request key.getBaseKeys(). |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 229 | ImmutableList.Builder<SkyKey> aspectPathSkyKeysBuilder = ImmutableList.builder(); |
| 230 | ImmutableMap<AspectDescriptor, SkyKey> aspectKeys = |
| 231 | getSkyKeysForAspectsAndCollectAspectPath(key.getBaseKeys(), aspectPathSkyKeysBuilder); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 232 | |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 233 | Map<SkyKey, SkyValue> values = env.getValues(aspectKeys.values()); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 234 | if (env.valuesMissing()) { |
| 235 | return null; |
| 236 | } |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 237 | ImmutableList<SkyKey> aspectPathSkyKeys = aspectPathSkyKeysBuilder.build(); |
| 238 | for (SkyKey aspectPathSkyKey : aspectPathSkyKeys) { |
| 239 | aspectPathBuilder.add(((AspectValue) values.get(aspectPathSkyKey)).getAspect()); |
| 240 | } |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 241 | try { |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 242 | associatedTarget = getBaseTarget( |
| 243 | associatedTarget, key.getBaseKeys(), values); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 244 | } catch (DuplicateException e) { |
| 245 | env.getListener().handle( |
| 246 | Event.error(associatedTarget.getTarget().getLocation(), e.getMessage())); |
| 247 | |
| 248 | throw new AspectFunctionException( |
| 249 | new AspectCreationException(e.getMessage(), associatedTarget.getLabel())); |
| 250 | |
| 251 | } |
| 252 | } |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 253 | aspectPathBuilder.add(aspect); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 254 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 255 | SkyframeDependencyResolver resolver = view.createDependencyResolver(env); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 256 | |
Michael Staib | 04f6f24 | 2016-03-01 15:40:29 +0000 | [diff] [blame] | 257 | // When getting the dependencies of this hybrid aspect+base target, use the aspect's |
| 258 | // configuration. The configuration of the aspect will always be a superset of the target's |
gregce | e0bbe75 | 2017-09-12 23:58:34 +0200 | [diff] [blame] | 259 | // (trimmed configuration mode: target is part of the aspect's config fragment requirements; |
| 260 | // untrimmed mode: target is the same configuration as the aspect), so the fragments |
Michael Staib | 04f6f24 | 2016-03-01 15:40:29 +0000 | [diff] [blame] | 261 | // required by all dependencies (both those of the aspect and those of the base target) |
| 262 | // will be present this way. |
| 263 | TargetAndConfiguration originalTargetAndAspectConfiguration = |
| 264 | new TargetAndConfiguration(target, key.getAspectConfiguration()); |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 265 | ImmutableList<Aspect> aspectPath = aspectPathBuilder.build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 266 | try { |
| 267 | // Get the configuration targets that trigger this rule's configurable attributes. |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 268 | ImmutableMap<Label, ConfigMatchingProvider> configConditions = |
| 269 | ConfiguredTargetFunction.getConfigConditions( |
| 270 | target, env, resolver, originalTargetAndAspectConfiguration, |
| 271 | transitivePackages, transitiveRootCauses); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 272 | if (configConditions == null) { |
| 273 | // Those targets haven't yet been resolved. |
| 274 | return null; |
| 275 | } |
| 276 | |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 277 | // Determine what toolchains are needed by this target. |
| 278 | ToolchainContext toolchainContext; |
| 279 | try { |
cpeyser | fb82999 | 2017-09-07 17:17:03 +0200 | [diff] [blame] | 280 | ImmutableSet<Label> requiredToolchains = aspect.getDefinition().getRequiredToolchains(); |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 281 | toolchainContext = |
| 282 | ToolchainUtil.createToolchainContext( |
John Cater | 53a87b9 | 2017-08-01 00:53:39 +0200 | [diff] [blame] | 283 | env, |
| 284 | String.format( |
| 285 | "aspect %s applied to %s", |
| 286 | aspect.getDescriptor().getDescription(), target.toString()), |
| 287 | requiredToolchains, |
| 288 | key.getAspectConfiguration()); |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 289 | } catch (ToolchainContextException e) { |
| 290 | // TODO(katre): better error handling |
| 291 | throw new AspectCreationException(e.getMessage()); |
| 292 | } |
| 293 | if (env.valuesMissing()) { |
| 294 | return null; |
| 295 | } |
| 296 | |
Dmitry Lomov | 9b2fc5c | 2016-11-11 11:18:48 +0000 | [diff] [blame] | 297 | OrderedSetMultimap<Attribute, ConfiguredTarget> depValueMap; |
| 298 | try { |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 299 | depValueMap = |
| 300 | ConfiguredTargetFunction.computeDependencies( |
| 301 | env, |
| 302 | resolver, |
| 303 | originalTargetAndAspectConfiguration, |
| 304 | aspectPath, |
| 305 | configConditions, |
| 306 | toolchainContext, |
| 307 | ruleClassProvider, |
| 308 | view.getHostConfiguration(originalTargetAndAspectConfiguration.getConfiguration()), |
| 309 | transitivePackages, |
| 310 | transitiveRootCauses); |
Dmitry Lomov | 9b2fc5c | 2016-11-11 11:18:48 +0000 | [diff] [blame] | 311 | } catch (ConfiguredTargetFunctionException e) { |
| 312 | throw new AspectCreationException(e.getMessage()); |
| 313 | } |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 314 | if (depValueMap == null) { |
| 315 | return null; |
| 316 | } |
| 317 | if (!transitiveRootCauses.isEmpty()) { |
| 318 | throw new AspectFunctionException( |
| 319 | new AspectCreationException("Loading failed", transitiveRootCauses.build())); |
| 320 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 321 | |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 322 | return createAspect( |
| 323 | env, |
| 324 | key, |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 325 | aspectPath, |
Dmitry Lomov | 2eb8bdd | 2016-04-06 08:47:30 +0000 | [diff] [blame] | 326 | aspect, |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 327 | aspectFactory, |
| 328 | associatedTarget, |
Michael Staib | 04f6f24 | 2016-03-01 15:40:29 +0000 | [diff] [blame] | 329 | key.getAspectConfiguration(), |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 330 | configConditions, |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 331 | toolchainContext, |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 332 | depValueMap, |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 333 | transitivePackages); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 334 | } catch (DependencyEvaluationException e) { |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 335 | if (e.getCause() instanceof ConfiguredValueCreationException) { |
| 336 | ConfiguredValueCreationException cause = (ConfiguredValueCreationException) e.getCause(); |
| 337 | throw new AspectFunctionException(new AspectCreationException( |
| 338 | cause.getMessage(), cause.getAnalysisRootCause())); |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 339 | } else if (e.getCause() instanceof InconsistentAspectOrderException) { |
| 340 | InconsistentAspectOrderException cause = (InconsistentAspectOrderException) e.getCause(); |
| 341 | throw new AspectFunctionException(new AspectCreationException( |
| 342 | cause.getMessage())); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 343 | } else { |
| 344 | // Cast to InvalidConfigurationException as a consistency check. If you add any |
| 345 | // DependencyEvaluationException constructors, you may need to change this code, too. |
| 346 | InvalidConfigurationException cause = (InvalidConfigurationException) e.getCause(); |
| 347 | throw new AspectFunctionException(new AspectCreationException(cause.getMessage())); |
| 348 | } |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 349 | } catch (AspectCreationException e) { |
| 350 | throw new AspectFunctionException(e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 354 | /** |
| 355 | * Merges aspects defined by {@code aspectKeys} into the {@code target} using |
| 356 | * previously computed {@code values}. |
| 357 | * |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 358 | * @return A {@link ConfiguredTarget} that is a result of a merge. |
| 359 | * @throws DuplicateException if there is a duplicate provider provided by aspects. |
| 360 | */ |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 361 | private ConfiguredTarget getBaseTarget(ConfiguredTarget target, |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 362 | ImmutableList<AspectKey> aspectKeys, |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 363 | Map<SkyKey, SkyValue> values) |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 364 | throws DuplicateException { |
| 365 | ArrayList<ConfiguredAspect> aspectValues = new ArrayList<>(); |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 366 | for (AspectKey aspectKey : aspectKeys) { |
| 367 | SkyKey skyAspectKey = aspectKey.getSkyKey(); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 368 | AspectValue aspectValue = (AspectValue) values.get(skyAspectKey); |
| 369 | ConfiguredAspect configuredAspect = aspectValue.getConfiguredAspect(); |
| 370 | aspectValues.add(configuredAspect); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 371 | } |
| 372 | return MergedConfiguredTarget.of(target, aspectValues); |
| 373 | } |
| 374 | |
| 375 | /** |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 376 | * Collect all SkyKeys that are needed for a given list of AspectKeys, |
| 377 | * including transitive dependencies. |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 378 | * |
| 379 | * Also collects all propagating aspects in correct order. |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 380 | */ |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 381 | private ImmutableMap<AspectDescriptor, SkyKey> getSkyKeysForAspectsAndCollectAspectPath( |
| 382 | ImmutableList<AspectKey> keys, |
| 383 | ImmutableList.Builder<SkyKey> aspectPathBuilder) { |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 384 | HashMap<AspectDescriptor, SkyKey> result = new HashMap<>(); |
| 385 | for (AspectKey key : keys) { |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 386 | buildSkyKeys(key, result, aspectPathBuilder); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 387 | } |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 388 | return ImmutableMap.copyOf(result); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 389 | } |
| 390 | |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 391 | private void buildSkyKeys(AspectKey key, HashMap<AspectDescriptor, SkyKey> result, |
| 392 | ImmutableList.Builder<SkyKey> aspectPathBuilder) { |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 393 | if (result.containsKey(key.getAspectDescriptor())) { |
| 394 | return; |
| 395 | } |
| 396 | ImmutableList<AspectKey> baseKeys = key.getBaseKeys(); |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 397 | SkyKey skyKey = key.getSkyKey(); |
| 398 | result.put(key.getAspectDescriptor(), skyKey); |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 399 | for (AspectKey baseKey : baseKeys) { |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 400 | buildSkyKeys(baseKey, result, aspectPathBuilder); |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 401 | } |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 402 | // Post-order list of aspect SkyKeys gives the order of propagating aspects: |
| 403 | // the aspect comes after all aspects it transitively sees. |
| 404 | aspectPathBuilder.add(skyKey); |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 405 | } |
dslomov | fa50c3d | 2017-05-08 08:47:44 -0400 | [diff] [blame] | 406 | |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 407 | private SkyValue createAliasAspect( |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 408 | Environment env, |
| 409 | Target originalTarget, |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 410 | Aspect aspect, |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 411 | AspectKey originalKey, |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 412 | ConfiguredTarget configuredTarget) |
| 413 | throws InterruptedException { |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 414 | ImmutableList<Label> aliasChain = configuredTarget.getProvider(AliasProvider.class) |
| 415 | .getAliasChain(); |
| 416 | // Find the next alias in the chain: either the next alias (if there are two) or the name of |
| 417 | // the real configured target. |
| 418 | Label aliasLabel = aliasChain.size() > 1 ? aliasChain.get(1) : configuredTarget.getLabel(); |
| 419 | |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 420 | SkyKey depKey = ActionLookupValue.key(originalKey.withLabel(aliasLabel)); |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 421 | |
| 422 | // Compute the AspectValue of the target the alias refers to (which can itself be either an |
| 423 | // alias or a real target) |
Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 424 | AspectValue real = (AspectValue) env.getValue(depKey); |
| 425 | if (env.valuesMissing()) { |
| 426 | return null; |
| 427 | } |
| 428 | |
| 429 | NestedSet<Package> transitivePackages = NestedSetBuilder.<Package>stableOrder() |
| 430 | .addTransitive(real.getTransitivePackages()) |
| 431 | .add(originalTarget.getPackage()) |
| 432 | .build(); |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 433 | |
Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 434 | return new AspectValue( |
| 435 | originalKey, |
| 436 | aspect, |
| 437 | originalTarget.getLabel(), |
| 438 | originalTarget.getLocation(), |
| 439 | ConfiguredAspect.forAlias(real.getConfiguredAspect()), |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 440 | ImmutableList.<ActionAnalysisMetadata>of(), |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 441 | transitivePackages, |
| 442 | removeActionsAfterEvaluation.get()); |
Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 445 | @Nullable |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 446 | private AspectValue createAspect( |
| 447 | Environment env, |
| 448 | AspectKey key, |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 449 | ImmutableList<Aspect> aspectPath, |
Dmitry Lomov | 2eb8bdd | 2016-04-06 08:47:30 +0000 | [diff] [blame] | 450 | Aspect aspect, |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 451 | ConfiguredAspectFactory aspectFactory, |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 452 | ConfiguredTarget associatedTarget, |
Michael Staib | 04f6f24 | 2016-03-01 15:40:29 +0000 | [diff] [blame] | 453 | BuildConfiguration aspectConfiguration, |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 454 | ImmutableMap<Label, ConfigMatchingProvider> configConditions, |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 455 | ToolchainContext toolchainContext, |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 456 | OrderedSetMultimap<Attribute, ConfiguredTarget> directDeps, |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 457 | NestedSetBuilder<Package> transitivePackages) |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 458 | throws AspectFunctionException, InterruptedException { |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 459 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 460 | SkyframeBuildView view = buildViewProvider.getSkyframeBuildView(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 461 | |
| 462 | StoredEventHandler events = new StoredEventHandler(); |
| 463 | CachingAnalysisEnvironment analysisEnvironment = view.createAnalysisEnvironment( |
Michael Staib | 04f6f24 | 2016-03-01 15:40:29 +0000 | [diff] [blame] | 464 | key, false, events, env, aspectConfiguration); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 465 | if (env.valuesMissing()) { |
| 466 | return null; |
| 467 | } |
| 468 | |
Dmitry Lomov | 6cd9897 | 2017-03-01 15:44:00 +0000 | [diff] [blame] | 469 | ConfiguredAspect configuredAspect; |
gregce | 4c3ef11 | 2017-09-20 23:43:14 +0200 | [diff] [blame] | 470 | if (AspectResolver.aspectMatchesConfiguredTarget(associatedTarget, aspect)) { |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 471 | configuredAspect = |
| 472 | view.getConfiguredTargetFactory() |
| 473 | .createAspect( |
| 474 | analysisEnvironment, |
| 475 | associatedTarget, |
| 476 | aspectPath, |
| 477 | aspectFactory, |
| 478 | aspect, |
| 479 | directDeps, |
| 480 | configConditions, |
| 481 | toolchainContext, |
| 482 | aspectConfiguration, |
| 483 | view.getHostConfiguration(aspectConfiguration)); |
Dmitry Lomov | 6cd9897 | 2017-03-01 15:44:00 +0000 | [diff] [blame] | 484 | } else { |
| 485 | configuredAspect = ConfiguredAspect.forNonapplicableTarget(aspect.getDescriptor()); |
| 486 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 487 | |
| 488 | events.replayOn(env.getListener()); |
| 489 | if (events.hasErrors()) { |
| 490 | analysisEnvironment.disable(associatedTarget.getTarget()); |
| 491 | throw new AspectFunctionException(new AspectCreationException( |
| 492 | "Analysis of target '" + associatedTarget.getLabel() + "' failed; build aborted")); |
| 493 | } |
| 494 | Preconditions.checkState(!analysisEnvironment.hasErrors(), |
| 495 | "Analysis environment hasError() but no errors reported"); |
| 496 | |
| 497 | if (env.valuesMissing()) { |
| 498 | return null; |
| 499 | } |
| 500 | |
| 501 | analysisEnvironment.disable(associatedTarget.getTarget()); |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 502 | Preconditions.checkNotNull(configuredAspect); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 503 | |
| 504 | return new AspectValue( |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 505 | key, |
Dmitry Lomov | 2eb8bdd | 2016-04-06 08:47:30 +0000 | [diff] [blame] | 506 | aspect, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 507 | associatedTarget.getLabel(), |
| 508 | associatedTarget.getTarget().getLocation(), |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 509 | configuredAspect, |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 510 | ImmutableList.copyOf(analysisEnvironment.getRegisteredActions()), |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 511 | transitivePackages.build(), |
| 512 | removeActionsAfterEvaluation.get()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 515 | @Override |
| 516 | public String extractTag(SkyKey skyKey) { |
Michael Staib | 2707a88 | 2016-09-16 21:06:40 +0000 | [diff] [blame] | 517 | AspectKey aspectKey = (AspectKey) skyKey.argument(); |
| 518 | return Label.print(aspectKey.getLabel()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 519 | } |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 520 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 521 | /** |
| 522 | * An exception indicating that there was a problem creating an aspect. |
| 523 | */ |
| 524 | public static final class AspectCreationException extends Exception { |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 525 | /** Targets in the transitive closure that failed to load. May be empty. */ |
| 526 | private final NestedSet<Label> loadingRootCauses; |
| 527 | |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 528 | /** |
| 529 | * The target for which analysis failed, if any. We can't represent aspects with labels, so if |
| 530 | * the aspect analysis fails, this will be {@code null}. |
| 531 | */ |
| 532 | @Nullable private final Label analysisRootCause; |
| 533 | |
| 534 | public AspectCreationException(String message, Label analysisRootCause) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 535 | super(message); |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 536 | this.loadingRootCauses = NestedSetBuilder.<Label>emptySet(Order.STABLE_ORDER); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 537 | this.analysisRootCause = analysisRootCause; |
| 538 | } |
| 539 | |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 540 | public AspectCreationException(String message, NestedSet<Label> loadingRootCauses) { |
| 541 | super(message); |
| 542 | this.loadingRootCauses = loadingRootCauses; |
| 543 | this.analysisRootCause = null; |
| 544 | } |
| 545 | |
| 546 | public AspectCreationException(NestedSet<Label> loadingRootCauses) { |
| 547 | this("Loading failed", loadingRootCauses); |
| 548 | } |
| 549 | |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 550 | public AspectCreationException(String message) { |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 551 | this(message, (Label) null); |
| 552 | } |
| 553 | |
| 554 | public NestedSet<Label> getRootCauses() { |
| 555 | return loadingRootCauses; |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | @Nullable public Label getAnalysisRootCause() { |
| 559 | return analysisRootCause; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Used to indicate errors during the computation of an {@link AspectValue}. |
| 565 | */ |
| 566 | private static final class AspectFunctionException extends SkyFunctionException { |
Ulf Adams | 9e16f0a | 2016-01-25 12:43:32 +0000 | [diff] [blame] | 567 | public AspectFunctionException(NoSuchThingException e) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 568 | super(e, Transience.PERSISTENT); |
| 569 | } |
| 570 | |
Ulf Adams | 9e16f0a | 2016-01-25 12:43:32 +0000 | [diff] [blame] | 571 | public AspectFunctionException(AspectCreationException e) { |
| 572 | super(e, Transience.PERSISTENT); |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 573 | } |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 574 | |
| 575 | public AspectFunctionException(InconsistentAspectOrderException cause) { |
| 576 | super(cause, Transience.PERSISTENT); |
| 577 | } |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 578 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 579 | } |