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 | |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 17 | import static com.google.common.collect.ImmutableList.toImmutableList; |
| 18 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 20 | import com.google.common.base.Predicates; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
cpeyser | fb82999 | 2017-09-07 17:17:03 +0200 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Lists; |
janakr | 0175ce3 | 2018-02-26 15:54:57 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException; |
ulfjack | 90e2e98 | 2017-08-07 11:27:32 +0200 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.AliasProvider; |
gregce | 4c3ef11 | 2017-09-20 23:43:14 +0200 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.AspectResolver; |
jcater | 44c9942 | 2020-04-20 15:52:18 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.AspectValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import com.google.devtools.build.lib.analysis.CachingAnalysisEnvironment; |
ulfjack | 3740768 | 2019-09-17 06:30:49 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.analysis.CachingAnalysisEnvironment.MissingDepException; |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.analysis.ConfiguredAspect; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import com.google.devtools.build.lib.analysis.ConfiguredAspectFactory; |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
janakr | 876deaa | 2021-02-17 07:49:48 -0800 | [diff] [blame] | 34 | import com.google.devtools.build.lib.analysis.ConfiguredTargetValue; |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.lib.analysis.Dependency; |
| 36 | import com.google.devtools.build.lib.analysis.DependencyKey; |
jcater | dad2dd3 | 2020-04-13 08:31:58 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.analysis.DependencyKind; |
jcater | 84ca556 | 2020-04-06 14:35:46 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.analysis.DuplicateException; |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 39 | import com.google.devtools.build.lib.analysis.ExecGroupCollection; |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 40 | import com.google.devtools.build.lib.analysis.ExecGroupCollection.InvalidExecGroupException; |
jcater | dad2dd3 | 2020-04-13 08:31:58 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.lib.analysis.InconsistentAspectOrderException; |
John Cater | cdfa9ca | 2019-04-05 12:32:09 -0700 | [diff] [blame] | 42 | import com.google.devtools.build.lib.analysis.ResolvedToolchainContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | import com.google.devtools.build.lib.analysis.TargetAndConfiguration; |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 44 | import com.google.devtools.build.lib.analysis.ToolchainCollection; |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 45 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue; |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 46 | import com.google.devtools.build.lib.analysis.config.ConfigConditions; |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 47 | import com.google.devtools.build.lib.analysis.config.ConfigurationResolver; |
jcater | 6718191 | 2020-04-08 10:46:29 -0700 | [diff] [blame] | 48 | import com.google.devtools.build.lib.analysis.config.DependencyEvaluationException; |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 50 | import com.google.devtools.build.lib.analysis.config.TransitionResolver; |
| 51 | import com.google.devtools.build.lib.analysis.config.transitions.ConfigurationTransition; |
| 52 | import com.google.devtools.build.lib.analysis.config.transitions.NoTransition; |
gregce | 2aee44b | 2017-09-16 07:16:44 +0200 | [diff] [blame] | 53 | import com.google.devtools.build.lib.analysis.configuredtargets.MergedConfiguredTarget; |
Samuel Giddins | eadadbd | 2020-08-17 08:00:05 -0700 | [diff] [blame] | 54 | import com.google.devtools.build.lib.analysis.starlark.StarlarkTransition.TransitionException; |
janakr | d4ec9e2 | 2022-01-26 07:52:03 -0800 | [diff] [blame] | 55 | import com.google.devtools.build.lib.bugreport.BugReport; |
ulfjack | 904a8d6 | 2018-05-29 05:17:35 -0700 | [diff] [blame] | 56 | import com.google.devtools.build.lib.causes.Cause; |
| 57 | import com.google.devtools.build.lib.causes.LabelCause; |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 58 | import com.google.devtools.build.lib.cmdline.Label; |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 59 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 60 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 61 | import com.google.devtools.build.lib.collect.nestedset.Order; |
Ulf Adams | 9e16f0a | 2016-01-25 12:43:32 +0000 | [diff] [blame] | 62 | import com.google.devtools.build.lib.events.Event; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | import com.google.devtools.build.lib.events.StoredEventHandler; |
Dmitry Lomov | 2eb8bdd | 2016-04-06 08:47:30 +0000 | [diff] [blame] | 64 | import com.google.devtools.build.lib.packages.Aspect; |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 65 | import com.google.devtools.build.lib.packages.Attribute; |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 66 | import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 67 | import com.google.devtools.build.lib.packages.NativeAspectClass; |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 68 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 69 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 70 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 71 | import com.google.devtools.build.lib.packages.OutputFile; |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 72 | import com.google.devtools.build.lib.packages.Package; |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 73 | import com.google.devtools.build.lib.packages.Rule; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 74 | import com.google.devtools.build.lib.packages.RuleClassProvider; |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 75 | import com.google.devtools.build.lib.packages.StarlarkAspectClass; |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 76 | import com.google.devtools.build.lib.packages.StarlarkDefinedAspect; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 77 | import com.google.devtools.build.lib.packages.Target; |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 78 | import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; |
tomlu | 72642a2 | 2017-10-18 06:23:14 +0200 | [diff] [blame] | 79 | import com.google.devtools.build.lib.profiler.memory.CurrentRuleTracker; |
leba | b52a190 | 2021-09-23 01:35:13 -0700 | [diff] [blame] | 80 | import com.google.devtools.build.lib.skyframe.AspectKeyCreator.AspectKey; |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 81 | import com.google.devtools.build.lib.skyframe.BzlLoadFunction.BzlLoadFailedException; |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 82 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.ComputeDependenciesState; |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 83 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.ComputedToolchainContexts; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 84 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor.BuildViewProvider; |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 85 | import com.google.devtools.build.lib.util.OrderedSetMultimap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | import com.google.devtools.build.skyframe.SkyFunction; |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 87 | import com.google.devtools.build.skyframe.SkyFunction.Environment.SkyKeyComputeState; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 88 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 89 | import com.google.devtools.build.skyframe.SkyKey; |
| 90 | import com.google.devtools.build.skyframe.SkyValue; |
emilyguo | 8ca657c | 2022-02-25 10:36:22 -0800 | [diff] [blame] | 91 | import com.google.devtools.build.skyframe.SkyframeLookupResult; |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 92 | import java.util.LinkedHashSet; |
| 93 | import java.util.List; |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 94 | import java.util.Map; |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 95 | import java.util.Objects; |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 96 | import java.util.Set; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | import javax.annotation.Nullable; |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 98 | import net.starlark.java.eval.StarlarkSemantics; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * The Skyframe function that generates aspects. |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 102 | * |
jhorvitz | 8139503 | 2021-05-05 08:48:08 -0700 | [diff] [blame] | 103 | * <p>This class, together with {@link ConfiguredTargetFunction} drives the analysis phase. For more |
ulfjack | 26d0e49 | 2017-08-07 13:42:33 +0200 | [diff] [blame] | 104 | * information, see {@link com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory}. |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 105 | * |
jhorvitz | 8139503 | 2021-05-05 08:48:08 -0700 | [diff] [blame] | 106 | * <p>{@link AspectFunction} takes a SkyKey containing an {@link AspectKey} [a tuple of (target |
| 107 | * label, configurations, aspect class and aspect parameters)], loads an {@link Aspect} from aspect |
| 108 | * class and aspect parameters, gets a {@link ConfiguredTarget} for label and configurations, and |
| 109 | * then creates a {@link ConfiguredAspect} for a given {@link AspectKey}. |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 110 | * |
jhorvitz | 8139503 | 2021-05-05 08:48:08 -0700 | [diff] [blame] | 111 | * <p>See {@link com.google.devtools.build.lib.packages.AspectClass} documentation for an overview |
| 112 | * of aspect-related classes |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 113 | * |
ulfjack | 26d0e49 | 2017-08-07 13:42:33 +0200 | [diff] [blame] | 114 | * @see com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 115 | * @see com.google.devtools.build.lib.packages.AspectClass |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 116 | */ |
jhorvitz | 8139503 | 2021-05-05 08:48:08 -0700 | [diff] [blame] | 117 | final class AspectFunction implements SkyFunction { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 118 | private final BuildViewProvider buildViewProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 119 | private final RuleClassProvider ruleClassProvider; |
janakr | 931d285 | 2017-12-15 13:48:29 -0800 | [diff] [blame] | 120 | /** |
| 121 | * Indicates whether the set of packages transitively loaded for a given {@link AspectValue} will |
| 122 | * be needed for package root resolution later in the build. If not, they are not collected and |
| 123 | * stored. |
| 124 | */ |
| 125 | private final boolean storeTransitivePackagesForPackageRootResolution; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 126 | |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 127 | AspectFunction( |
| 128 | BuildViewProvider buildViewProvider, |
| 129 | RuleClassProvider ruleClassProvider, |
jhorvitz | 8139503 | 2021-05-05 08:48:08 -0700 | [diff] [blame] | 130 | boolean storeTransitivePackagesForPackageRootResolution) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 131 | this.buildViewProvider = buildViewProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 132 | this.ruleClassProvider = ruleClassProvider; |
janakr | 931d285 | 2017-12-15 13:48:29 -0800 | [diff] [blame] | 133 | this.storeTransitivePackagesForPackageRootResolution = |
| 134 | storeTransitivePackagesForPackageRootResolution; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 135 | } |
| 136 | |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 137 | static class State implements SkyKeyComputeState { |
| 138 | /** Null if AspectFuncton is not storing this information. */ |
| 139 | @Nullable NestedSetBuilder<Package> transitivePackagesForPackageRootResolution; |
| 140 | |
| 141 | NestedSetBuilder<Cause> transitiveRootCauses = NestedSetBuilder.stableOrder(); |
| 142 | |
| 143 | @Nullable InitialValues initialValues; |
| 144 | |
| 145 | ComputeDependenciesState computeDependenciesState = new ComputeDependenciesState(); |
| 146 | |
| 147 | State(boolean storeTransitivePackagesForPackageRootResolution) { |
| 148 | this.transitivePackagesForPackageRootResolution = |
| 149 | storeTransitivePackagesForPackageRootResolution ? NestedSetBuilder.stableOrder() : null; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private static class InitialValues { |
| 154 | @Nullable private final Aspect aspect; |
| 155 | @Nullable private final ConfiguredAspectFactory aspectFactory; |
| 156 | @Nullable private final BuildConfigurationValue configuration; |
| 157 | private final ConfiguredTarget associatedTarget; |
| 158 | private final Target target; |
| 159 | |
| 160 | private InitialValues( |
| 161 | @Nullable Aspect aspect, |
| 162 | @Nullable ConfiguredAspectFactory aspectFactory, |
| 163 | @Nullable BuildConfigurationValue configuration, |
| 164 | ConfiguredTarget associatedTarget, |
| 165 | Target target) { |
| 166 | this.aspect = aspect; |
| 167 | this.aspectFactory = aspectFactory; |
| 168 | this.configuration = configuration; |
| 169 | this.associatedTarget = associatedTarget; |
| 170 | this.target = target; |
| 171 | } |
| 172 | } |
| 173 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 174 | @Nullable |
| 175 | @Override |
| 176 | public SkyValue compute(SkyKey skyKey, Environment env) |
Florian Weikert | 4b67d4f | 2015-09-14 13:35:34 +0000 | [diff] [blame] | 177 | throws AspectFunctionException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 178 | AspectKey key = (AspectKey) skyKey.argument(); |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 179 | State state = env.getState(() -> new State(storeTransitivePackagesForPackageRootResolution)); |
jhorvitz | db95cc4 | 2021-11-19 08:51:18 -0800 | [diff] [blame] | 180 | |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 181 | if (state.initialValues == null) { |
| 182 | InitialValues initialValues = getInitialValues(key, env); |
| 183 | if (initialValues == null) { |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 184 | return null; |
| 185 | } |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 186 | state.initialValues = initialValues; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 187 | } |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 188 | Aspect aspect = state.initialValues.aspect; |
| 189 | ConfiguredAspectFactory aspectFactory = state.initialValues.aspectFactory; |
| 190 | BuildConfigurationValue configuration = state.initialValues.configuration; |
| 191 | ConfiguredTarget associatedTarget = state.initialValues.associatedTarget; |
| 192 | Target target = state.initialValues.target; |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 193 | |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 194 | if (AliasProvider.isAlias(associatedTarget)) { |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 195 | return createAliasAspect( |
| 196 | env, |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 197 | buildViewProvider.getSkyframeBuildView().getHostConfiguration(), |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 198 | new TargetAndConfiguration(target, configuration), |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 199 | aspect, |
| 200 | key, |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 201 | configuration, |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 202 | associatedTarget); |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 203 | } |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 204 | // If we get here, label should match original label, and therefore the target we looked up |
| 205 | // above indeed corresponds to associatedTarget.getLabel(). |
| 206 | Preconditions.checkState( |
| 207 | associatedTarget.getOriginalLabel().equals(associatedTarget.getLabel()), |
| 208 | "Non-alias %s should have matching label but found %s", |
| 209 | associatedTarget.getOriginalLabel(), |
| 210 | associatedTarget.getLabel()); |
| 211 | |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 212 | // If the incompatible flag is set, the top-level aspect should not be applied on top-level |
| 213 | // targets whose rules do not advertise the aspect's required providers. The aspect should not |
| 214 | // also propagate to these targets dependencies. |
| 215 | StarlarkSemantics starlarkSemantics = PrecomputedValue.STARLARK_SEMANTICS.get(env); |
| 216 | if (starlarkSemantics == null) { |
| 217 | return null; |
| 218 | } |
| 219 | boolean checkRuleAdvertisedProviders = |
| 220 | starlarkSemantics.getBool( |
| 221 | BuildLanguageOptions.INCOMPATIBLE_TOP_LEVEL_ASPECTS_REQUIRE_PROVIDERS); |
| 222 | if (checkRuleAdvertisedProviders) { |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 223 | if (target instanceof Rule) { |
| 224 | if (!aspect |
| 225 | .getDefinition() |
| 226 | .getRequiredProviders() |
| 227 | .isSatisfiedBy(((Rule) target).getRuleClassObject().getAdvertisedProviders())) { |
| 228 | return new AspectValue( |
| 229 | key, |
| 230 | aspect, |
| 231 | target.getLocation(), |
| 232 | ConfiguredAspect.forNonapplicableTarget(), |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 233 | /*transitivePackagesForPackageRootResolution=*/ NestedSetBuilder.emptySet( |
| 234 | Order.STABLE_ORDER)); |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 238 | |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 239 | ImmutableList<Aspect> topologicalAspectPath; |
| 240 | if (key.getBaseKeys().isEmpty()) { |
| 241 | topologicalAspectPath = ImmutableList.of(aspect); |
| 242 | } else { |
| 243 | LinkedHashSet<AspectKey> orderedKeys = new LinkedHashSet<>(); |
| 244 | collectAspectKeysInTopologicalOrder(key.getBaseKeys(), orderedKeys); |
emilyguo | 21b0ba0 | 2022-03-16 17:07:40 -0700 | [diff] [blame] | 245 | SkyframeLookupResult aspectValues = env.getValuesAndExceptions(orderedKeys); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 246 | if (env.valuesMissing()) { |
| 247 | return null; |
| 248 | } |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 249 | ImmutableList.Builder<Aspect> topologicalAspectPathBuilder = |
| 250 | ImmutableList.builderWithExpectedSize(orderedKeys.size() + 1); |
| 251 | for (AspectKey aspectKey : orderedKeys) { |
| 252 | AspectValue aspectValue = (AspectValue) aspectValues.get(aspectKey); |
emilyguo | 21b0ba0 | 2022-03-16 17:07:40 -0700 | [diff] [blame] | 253 | if (aspectValue == null) { |
emilyguo | 7222cab | 2022-04-19 13:32:09 -0700 | [diff] [blame] | 254 | BugReport.logUnexpected( |
| 255 | "aspectValue for: '%s' was missing, this should never happen", aspectKey); |
emilyguo | 21b0ba0 | 2022-03-16 17:07:40 -0700 | [diff] [blame] | 256 | return null; |
| 257 | } |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 258 | topologicalAspectPathBuilder.add(aspectValue.getAspect()); |
dslomov | 039b9ee | 2017-04-11 08:51:30 +0000 | [diff] [blame] | 259 | } |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 260 | topologicalAspectPath = topologicalAspectPathBuilder.add(aspect).build(); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 261 | |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 262 | List<ConfiguredAspect> directlyRequiredAspects = |
| 263 | Lists.transform( |
| 264 | key.getBaseKeys(), k -> ((AspectValue) aspectValues.get(k)).getConfiguredAspect()); |
| 265 | try { |
| 266 | associatedTarget = MergedConfiguredTarget.of(associatedTarget, directlyRequiredAspects); |
| 267 | } catch (DuplicateException e) { |
| 268 | env.getListener().handle(Event.error(target.getLocation(), e.getMessage())); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 269 | throw new AspectFunctionException( |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 270 | new AspectCreationException(e.getMessage(), target.getLabel(), configuration)); |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 273 | |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 274 | SkyframeDependencyResolver resolver = new SkyframeDependencyResolver(env); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 275 | |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 276 | TargetAndConfiguration originalTargetAndConfiguration = |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 277 | new TargetAndConfiguration(target, configuration); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 278 | try { |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 279 | ConfiguredTargetFunction.ComputedToolchainContexts computedToolchainContexts = |
| 280 | getUnloadedToolchainContexts(env, key, aspect, configuration); |
Greg Estren | 12b06b2 | 2020-09-11 10:58:43 -0700 | [diff] [blame] | 281 | if (env.valuesMissing()) { |
| 282 | return null; |
| 283 | } |
| 284 | |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 285 | ToolchainCollection<UnloadedToolchainContext> unloadedToolchainContexts = null; |
| 286 | ExecGroupCollection.Builder execGroupCollectionBuilder = null; |
| 287 | if (computedToolchainContexts != null) { |
| 288 | unloadedToolchainContexts = computedToolchainContexts.toolchainCollection; |
| 289 | execGroupCollectionBuilder = computedToolchainContexts.execGroupCollectionBuilder; |
| 290 | } |
| 291 | |
Greg Estren | 12b06b2 | 2020-09-11 10:58:43 -0700 | [diff] [blame] | 292 | // Get the configuration targets that trigger this rule's configurable attributes. |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 293 | ConfigConditions configConditions = |
Greg Estren | 12b06b2 | 2020-09-11 10:58:43 -0700 | [diff] [blame] | 294 | ConfiguredTargetFunction.getConfigConditions( |
| 295 | env, |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 296 | originalTargetAndConfiguration, |
nharmata | 85e4380 | 2022-01-05 14:55:26 -0800 | [diff] [blame] | 297 | state.transitivePackagesForPackageRootResolution, |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 298 | unloadedToolchainContexts == null |
| 299 | ? null |
| 300 | : unloadedToolchainContexts.getTargetPlatform(), |
nharmata | 85e4380 | 2022-01-05 14:55:26 -0800 | [diff] [blame] | 301 | state.transitiveRootCauses); |
Greg Estren | 12b06b2 | 2020-09-11 10:58:43 -0700 | [diff] [blame] | 302 | if (configConditions == null) { |
| 303 | // Those targets haven't yet been resolved. |
| 304 | return null; |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 305 | } |
| 306 | |
lberki | 102256f | 2019-02-08 01:34:23 -0800 | [diff] [blame] | 307 | OrderedSetMultimap<DependencyKind, ConfiguredTargetAndData> depValueMap; |
Dmitry Lomov | 9b2fc5c | 2016-11-11 11:18:48 +0000 | [diff] [blame] | 308 | try { |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 309 | depValueMap = |
| 310 | ConfiguredTargetFunction.computeDependencies( |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 311 | state.computeDependenciesState, |
| 312 | state.transitivePackagesForPackageRootResolution, |
| 313 | state.transitiveRootCauses, |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 314 | env, |
| 315 | resolver, |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 316 | originalTargetAndConfiguration, |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 317 | topologicalAspectPath, |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 318 | configConditions.asProviders(), |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 319 | unloadedToolchainContexts == null |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 320 | ? null |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 321 | : unloadedToolchainContexts.asToolchainContexts(), |
John Cater | 2e56f06 | 2017-07-20 19:43:20 +0200 | [diff] [blame] | 322 | ruleClassProvider, |
nharmata | 85e4380 | 2022-01-05 14:55:26 -0800 | [diff] [blame] | 323 | buildViewProvider.getSkyframeBuildView().getHostConfiguration()); |
mschaller | 859c9ac | 2020-09-25 16:09:19 -0700 | [diff] [blame] | 324 | } catch (ConfiguredValueCreationException e) { |
| 325 | throw new AspectCreationException( |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 326 | e.getMessage(), key.getLabel(), configuration, e.getDetailedExitCode()); |
Dmitry Lomov | 9b2fc5c | 2016-11-11 11:18:48 +0000 | [diff] [blame] | 327 | } |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 328 | if (depValueMap == null) { |
| 329 | return null; |
| 330 | } |
nharmata | 85e4380 | 2022-01-05 14:55:26 -0800 | [diff] [blame] | 331 | if (!state.transitiveRootCauses.isEmpty()) { |
| 332 | NestedSet<Cause> causes = state.transitiveRootCauses.build(); |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 333 | throw new AspectFunctionException( |
mschaller | 859c9ac | 2020-09-25 16:09:19 -0700 | [diff] [blame] | 334 | new AspectCreationException( |
| 335 | "Loading failed", |
| 336 | causes, |
| 337 | ConfiguredTargetFunction.getPrioritizedDetailedExitCode(causes))); |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 338 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 339 | |
John Cater | ae4e785 | 2018-07-09 15:02:42 -0700 | [diff] [blame] | 340 | // Load the requested toolchains into the ToolchainContext, now that we have dependencies. |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 341 | ToolchainCollection<ResolvedToolchainContext> toolchainContexts = null; |
| 342 | if (unloadedToolchainContexts != null) { |
John Cater | 5074514 | 2019-04-16 07:41:24 -0700 | [diff] [blame] | 343 | String targetDescription = |
| 344 | String.format( |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 345 | "aspect %s applied to %s", aspect.getDescriptor().getDescription(), target); |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 346 | ToolchainCollection.Builder<ResolvedToolchainContext> contextsBuilder = |
| 347 | ToolchainCollection.builder(); |
| 348 | for (Map.Entry<String, UnloadedToolchainContext> unloadedContext : |
| 349 | unloadedToolchainContexts.getContextMap().entrySet()) { |
| 350 | Set<ConfiguredTargetAndData> toolchainDependencies = |
| 351 | depValueMap.get(DependencyKind.forExecGroup(unloadedContext.getKey())); |
| 352 | contextsBuilder.addContext( |
| 353 | unloadedContext.getKey(), |
| 354 | ResolvedToolchainContext.load( |
| 355 | unloadedContext.getValue(), targetDescription, toolchainDependencies)); |
| 356 | } |
| 357 | toolchainContexts = contextsBuilder.build(); |
John Cater | ae4e785 | 2018-07-09 15:02:42 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 360 | return createAspect( |
| 361 | env, |
| 362 | key, |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 363 | topologicalAspectPath, |
Dmitry Lomov | 2eb8bdd | 2016-04-06 08:47:30 +0000 | [diff] [blame] | 364 | aspect, |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 365 | aspectFactory, |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 366 | new ConfiguredTargetAndData( |
| 367 | associatedTarget, target, configuration, /*transitionKeys=*/ null), |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 368 | configuration, |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 369 | configConditions, |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 370 | toolchainContexts, |
| 371 | execGroupCollectionBuilder, |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 372 | depValueMap, |
nharmata | 85e4380 | 2022-01-05 14:55:26 -0800 | [diff] [blame] | 373 | state.transitivePackagesForPackageRootResolution); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 374 | } catch (DependencyEvaluationException e) { |
gregce | c564977 | 2021-07-08 13:10:19 -0700 | [diff] [blame] | 375 | // TODO(bazel-team): consolidate all env.getListener().handle() calls in this method, like in |
| 376 | // ConfiguredTargetFunction. This encourages clear, consistent user messages (ideally without |
| 377 | // the programmer having to think about it). |
| 378 | if (!e.depReportedOwnError()) { |
| 379 | env.getListener().handle(Event.error(e.getLocation(), e.getMessage())); |
| 380 | } |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 381 | if (e.getCause() instanceof ConfiguredValueCreationException) { |
| 382 | ConfiguredValueCreationException cause = (ConfiguredValueCreationException) e.getCause(); |
ulfjack | 904a8d6 | 2018-05-29 05:17:35 -0700 | [diff] [blame] | 383 | throw new AspectFunctionException( |
mschaller | 859c9ac | 2020-09-25 16:09:19 -0700 | [diff] [blame] | 384 | new AspectCreationException( |
| 385 | cause.getMessage(), cause.getRootCauses(), cause.getDetailedExitCode())); |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 386 | } else if (e.getCause() instanceof InconsistentAspectOrderException) { |
| 387 | InconsistentAspectOrderException cause = (InconsistentAspectOrderException) e.getCause(); |
gregce | c564977 | 2021-07-08 13:10:19 -0700 | [diff] [blame] | 388 | env.getListener().handle(Event.error(cause.getLocation(), cause.getMessage())); |
ulfjack | 904a8d6 | 2018-05-29 05:17:35 -0700 | [diff] [blame] | 389 | throw new AspectFunctionException( |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 390 | new AspectCreationException(cause.getMessage(), key.getLabel(), configuration)); |
Samuel Giddins | eadadbd | 2020-08-17 08:00:05 -0700 | [diff] [blame] | 391 | } else if (e.getCause() instanceof TransitionException) { |
| 392 | TransitionException cause = (TransitionException) e.getCause(); |
| 393 | throw new AspectFunctionException( |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 394 | new AspectCreationException(cause.getMessage(), key.getLabel(), configuration)); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 395 | } else { |
| 396 | // Cast to InvalidConfigurationException as a consistency check. If you add any |
| 397 | // DependencyEvaluationException constructors, you may need to change this code, too. |
| 398 | InvalidConfigurationException cause = (InvalidConfigurationException) e.getCause(); |
ulfjack | 904a8d6 | 2018-05-29 05:17:35 -0700 | [diff] [blame] | 399 | throw new AspectFunctionException( |
mschaller | 859c9ac | 2020-09-25 16:09:19 -0700 | [diff] [blame] | 400 | new AspectCreationException( |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 401 | cause.getMessage(), key.getLabel(), configuration, cause.getDetailedExitCode())); |
Ulf Adams | 25f03d8 | 2016-01-25 10:31:46 +0000 | [diff] [blame] | 402 | } |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 403 | } catch (AspectCreationException e) { |
| 404 | throw new AspectFunctionException(e); |
gregce | c564977 | 2021-07-08 13:10:19 -0700 | [diff] [blame] | 405 | } catch (ConfiguredValueCreationException e) { |
| 406 | throw new AspectFunctionException(e); |
jcater | 3fed869 | 2019-04-01 13:36:36 -0700 | [diff] [blame] | 407 | } catch (ToolchainException e) { |
| 408 | throw new AspectFunctionException( |
| 409 | new AspectCreationException( |
mschaller | 1511dd2 | 2020-09-21 14:19:33 -0700 | [diff] [blame] | 410 | e.getMessage(), new LabelCause(key.getLabel(), e.getDetailedExitCode()))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
janakr | 9bb80c2 | 2022-03-30 10:18:33 -0700 | [diff] [blame] | 414 | static BzlLoadValue.Key bzlLoadKeyForStarlarkAspect(StarlarkAspectClass starlarkAspectClass) { |
jhorvitz | db95cc4 | 2021-11-19 08:51:18 -0800 | [diff] [blame] | 415 | Label extensionLabel = starlarkAspectClass.getExtensionLabel(); |
| 416 | return StarlarkBuiltinsValue.isBuiltinsRepo(extensionLabel.getRepository()) |
| 417 | ? BzlLoadValue.keyForBuiltins(extensionLabel) |
| 418 | : BzlLoadValue.keyForBuild(extensionLabel); |
| 419 | } |
| 420 | |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 421 | @Nullable |
| 422 | private InitialValues getInitialValues(AspectKey key, Environment env) |
| 423 | throws AspectFunctionException, InterruptedException { |
| 424 | StarlarkAspectClass starlarkAspectClass; |
| 425 | ConfiguredAspectFactory aspectFactory = null; |
| 426 | Aspect aspect = null; |
| 427 | |
| 428 | SkyKey aspectPackageKey = PackageValue.key(key.getLabel().getPackageIdentifier()); |
| 429 | SkyKey baseConfiguredTargetKey = key.getBaseConfiguredTargetKey(); |
| 430 | SkyKey basePackageKey = |
| 431 | PackageValue.key(key.getBaseConfiguredTargetKey().getLabel().getPackageIdentifier()); |
| 432 | SkyKey configurationKey = key.getConfigurationKey(); |
janakr | 9bb80c2 | 2022-03-30 10:18:33 -0700 | [diff] [blame] | 433 | BzlLoadValue.Key bzlLoadKey; |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 434 | |
| 435 | if (key.getAspectClass() instanceof NativeAspectClass) { |
| 436 | NativeAspectClass nativeAspectClass = (NativeAspectClass) key.getAspectClass(); |
| 437 | starlarkAspectClass = null; |
| 438 | aspectFactory = (ConfiguredAspectFactory) nativeAspectClass; |
| 439 | aspect = Aspect.forNative(nativeAspectClass, key.getParameters()); |
| 440 | bzlLoadKey = null; |
| 441 | } else { |
| 442 | Preconditions.checkState( |
| 443 | key.getAspectClass() instanceof StarlarkAspectClass, "Unknown aspect class: %s", key); |
| 444 | starlarkAspectClass = (StarlarkAspectClass) key.getAspectClass(); |
| 445 | bzlLoadKey = bzlLoadKeyForStarlarkAspect(starlarkAspectClass); |
| 446 | } |
| 447 | |
| 448 | ImmutableSet.Builder<SkyKey> initialKeys = ImmutableSet.builder(); |
| 449 | initialKeys.add(aspectPackageKey).add(baseConfiguredTargetKey).add(basePackageKey); |
| 450 | if (configurationKey != null) { |
| 451 | initialKeys.add(configurationKey); |
| 452 | } |
| 453 | if (bzlLoadKey != null) { |
| 454 | initialKeys.add(bzlLoadKey); |
| 455 | } |
emilyguo | 8ca657c | 2022-02-25 10:36:22 -0800 | [diff] [blame] | 456 | SkyframeLookupResult initialValues = env.getValuesAndExceptions(initialKeys.build()); |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 457 | if (env.valuesMissing()) { |
| 458 | return null; |
| 459 | } |
| 460 | |
| 461 | if (starlarkAspectClass != null) { |
| 462 | StarlarkDefinedAspect starlarkAspect; |
| 463 | try { |
emilyguo | 8ca657c | 2022-02-25 10:36:22 -0800 | [diff] [blame] | 464 | BzlLoadValue bzlLoadvalue; |
| 465 | try { |
| 466 | bzlLoadvalue = |
| 467 | (BzlLoadValue) initialValues.getOrThrow(bzlLoadKey, BzlLoadFailedException.class); |
| 468 | if (bzlLoadvalue == null) { |
janakr | 9bb80c2 | 2022-03-30 10:18:33 -0700 | [diff] [blame] | 469 | BugReport.logUnexpected( |
| 470 | "Unexpected exception with %s and AspectKey %s", bzlLoadKey, key); |
emilyguo | 8ca657c | 2022-02-25 10:36:22 -0800 | [diff] [blame] | 471 | return null; |
| 472 | } |
| 473 | } catch (BzlLoadFailedException e) { |
| 474 | throw new AspectCreationException( |
| 475 | e.getMessage(), starlarkAspectClass.getExtensionLabel(), e.getDetailedExitCode()); |
| 476 | } |
| 477 | starlarkAspect = loadAspectFromBzl(starlarkAspectClass, bzlLoadvalue); |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 478 | } catch (AspectCreationException e) { |
| 479 | env.getListener().handle(Event.error(e.getMessage())); |
| 480 | throw new AspectFunctionException(e); |
| 481 | } |
| 482 | aspectFactory = new StarlarkAspectFactory(starlarkAspect); |
| 483 | aspect = |
| 484 | Aspect.forStarlark( |
| 485 | starlarkAspect.getAspectClass(), |
| 486 | starlarkAspect.getDefinition(key.getParameters()), |
| 487 | key.getParameters()); |
| 488 | } |
| 489 | |
| 490 | // Keep this in sync with the same code in ConfiguredTargetFunction. |
emilyguo | 8ca657c | 2022-02-25 10:36:22 -0800 | [diff] [blame] | 491 | PackageValue aspectPackage = (PackageValue) initialValues.get(aspectPackageKey); |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 492 | if (aspectPackage.getPackage().containsErrors()) { |
| 493 | throw new AspectFunctionException( |
| 494 | new BuildFileContainsErrorsException(key.getLabel().getPackageIdentifier())); |
| 495 | } |
| 496 | |
| 497 | ConfiguredTargetValue baseConfiguredTargetValue; |
| 498 | try { |
| 499 | baseConfiguredTargetValue = |
emilyguo | 8ca657c | 2022-02-25 10:36:22 -0800 | [diff] [blame] | 500 | (ConfiguredTargetValue) |
| 501 | initialValues.getOrThrow( |
| 502 | baseConfiguredTargetKey, ConfiguredValueCreationException.class); |
| 503 | if (baseConfiguredTargetValue == null) { |
janakr | 9bb80c2 | 2022-03-30 10:18:33 -0700 | [diff] [blame] | 504 | BugReport.logUnexpected( |
| 505 | "Unexpected exception with %s and AspectKey %s", baseConfiguredTargetKey, key); |
emilyguo | 8ca657c | 2022-02-25 10:36:22 -0800 | [diff] [blame] | 506 | return null; |
| 507 | } |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 508 | } catch (ConfiguredValueCreationException e) { |
| 509 | throw new AspectFunctionException( |
| 510 | new AspectCreationException(e.getMessage(), e.getRootCauses(), e.getDetailedExitCode())); |
| 511 | } |
| 512 | |
| 513 | ConfiguredTarget associatedTarget = baseConfiguredTargetValue.getConfiguredTarget(); |
| 514 | Preconditions.checkState( |
| 515 | Objects.equals(key.getConfigurationKey(), associatedTarget.getConfigurationKey()), |
| 516 | "Aspect not in same configuration as associated target: %s, %s", |
| 517 | key, |
| 518 | associatedTarget); |
| 519 | |
| 520 | BuildConfigurationValue configuration = |
| 521 | configurationKey == null |
| 522 | ? null |
emilyguo | 8ca657c | 2022-02-25 10:36:22 -0800 | [diff] [blame] | 523 | : (BuildConfigurationValue) initialValues.get(configurationKey); |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 524 | |
emilyguo | 8ca657c | 2022-02-25 10:36:22 -0800 | [diff] [blame] | 525 | PackageValue basePackage = (PackageValue) initialValues.get(basePackageKey); |
nharmata | bcd11ae | 2022-01-10 20:11:22 -0800 | [diff] [blame] | 526 | Target target; |
| 527 | try { |
| 528 | target = basePackage.getPackage().getTarget(associatedTarget.getOriginalLabel().getName()); |
| 529 | } catch (NoSuchTargetException e) { |
| 530 | throw new IllegalStateException("Name already verified", e); |
| 531 | } |
| 532 | |
| 533 | return new InitialValues(aspect, aspectFactory, configuration, associatedTarget, target); |
| 534 | } |
| 535 | |
jhorvitz | db95cc4 | 2021-11-19 08:51:18 -0800 | [diff] [blame] | 536 | /** |
| 537 | * Loads a Starlark-defined aspect from an extension file. |
| 538 | * |
| 539 | * @throws AspectCreationException if the value loaded is not a {@link StarlarkDefinedAspect} |
| 540 | */ |
| 541 | static StarlarkDefinedAspect loadAspectFromBzl( |
| 542 | StarlarkAspectClass starlarkAspectClass, BzlLoadValue bzlLoadValue) |
| 543 | throws AspectCreationException { |
| 544 | Label extensionLabel = starlarkAspectClass.getExtensionLabel(); |
| 545 | String starlarkValueName = starlarkAspectClass.getExportedName(); |
| 546 | Object starlarkValue = bzlLoadValue.getModule().getGlobal(starlarkValueName); |
| 547 | if (!(starlarkValue instanceof StarlarkDefinedAspect)) { |
| 548 | throw new AspectCreationException( |
| 549 | String.format( |
| 550 | starlarkValue == null ? "%s is not exported from %s" : "%s from %s is not an aspect", |
| 551 | starlarkValueName, |
| 552 | extensionLabel), |
| 553 | extensionLabel); |
| 554 | } |
| 555 | return (StarlarkDefinedAspect) starlarkValue; |
| 556 | } |
| 557 | |
| 558 | @Nullable |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 559 | private static ComputedToolchainContexts getUnloadedToolchainContexts( |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 560 | Environment env, |
| 561 | AspectKey key, |
| 562 | Aspect aspect, |
| 563 | @Nullable BuildConfigurationValue configuration) |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 564 | throws InterruptedException, AspectCreationException { |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 565 | if (configuration == null) { |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 566 | // Configuration can be null in the case of aspects applied to input files. In this case, |
| 567 | // there are no chances of toolchains being used, so skip it. |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 568 | return null; |
| 569 | } |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 570 | // Determine what toolchains are needed by this target. |
| 571 | try { |
| 572 | return ConfiguredTargetFunction.computeUnloadedToolchainContexts( |
| 573 | env, |
| 574 | key.getLabel(), |
| 575 | true, |
| 576 | Predicates.alwaysFalse(), |
| 577 | configuration.getKey(), |
| 578 | aspect.getDefinition().getToolchainTypes(), |
| 579 | aspect.getDefinition().execCompatibleWith(), |
| 580 | aspect.getDefinition().execGroups(), |
| 581 | null); |
| 582 | } catch (ToolchainException e) { |
| 583 | // TODO(katre): better error handling |
| 584 | throw new AspectCreationException( |
| 585 | e.getMessage(), new LabelCause(key.getLabel(), e.getDetailedExitCode())); |
| 586 | } |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 589 | /** |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 590 | * Collects {@link AspectKey} dependencies by performing a postorder traversal over {@link |
| 591 | * AspectKey#getBaseKeys}. |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 592 | * |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 593 | * <p>The resulting set of {@code orderedKeys} is topologically ordered: each aspect key appears |
| 594 | * after all of its dependencies. |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 595 | */ |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 596 | private static void collectAspectKeysInTopologicalOrder( |
| 597 | List<AspectKey> baseKeys, LinkedHashSet<AspectKey> orderedKeys) { |
| 598 | for (AspectKey key : baseKeys) { |
| 599 | if (!orderedKeys.contains(key)) { |
| 600 | collectAspectKeysInTopologicalOrder(key.getBaseKeys(), orderedKeys); |
| 601 | orderedKeys.add(key); |
| 602 | } |
Dmitry Lomov | ca9bfa4 | 2016-11-15 13:22:36 +0000 | [diff] [blame] | 603 | } |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 604 | } |
dslomov | fa50c3d | 2017-05-08 08:47:44 -0400 | [diff] [blame] | 605 | |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 606 | /** |
| 607 | * Computes the given aspectKey of an alias-like target, by depending on the corresponding key of |
| 608 | * the next target in the alias chain (if there are more), or the "real" configured target. |
| 609 | */ |
| 610 | @Nullable |
| 611 | private AspectValue createAliasAspect( |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 612 | Environment env, |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 613 | BuildConfigurationValue hostConfiguration, |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 614 | TargetAndConfiguration originalTarget, |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 615 | Aspect aspect, |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 616 | AspectKey originalKey, |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 617 | BuildConfigurationValue aspectConfiguration, |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 618 | ConfiguredTarget configuredTarget) |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 619 | throws AspectFunctionException, InterruptedException { |
| 620 | ImmutableList<Label> aliasChain = |
| 621 | configuredTarget.getProvider(AliasProvider.class).getAliasChain(); |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 622 | // Find the next alias in the chain: either the next alias (if there are two) or the name of |
| 623 | // the real configured target. |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 624 | Label aliasedLabel = aliasChain.size() > 1 ? aliasChain.get(1) : configuredTarget.getLabel(); |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 625 | |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 626 | NestedSetBuilder<Package> transitivePackagesForPackageRootResolution = |
| 627 | storeTransitivePackagesForPackageRootResolution ? NestedSetBuilder.stableOrder() : null; |
| 628 | NestedSetBuilder<Cause> transitiveRootCauses = NestedSetBuilder.stableOrder(); |
| 629 | |
| 630 | // Compute the Dependency from originalTarget to aliasedLabel |
| 631 | Dependency dep; |
| 632 | try { |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 633 | ConfiguredTargetFunction.ComputedToolchainContexts computedToolchainContexts = |
| 634 | getUnloadedToolchainContexts(env, originalKey, aspect, originalTarget.getConfiguration()); |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 635 | if (env.valuesMissing()) { |
| 636 | return null; |
| 637 | } |
| 638 | |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 639 | ToolchainCollection<UnloadedToolchainContext> unloadedToolchainContexts = |
| 640 | computedToolchainContexts.toolchainCollection; |
| 641 | |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 642 | // See comment in compute() above for why we pair target with aspectConfiguration here |
| 643 | TargetAndConfiguration originalTargetAndAspectConfiguration = |
| 644 | new TargetAndConfiguration(originalTarget.getTarget(), aspectConfiguration); |
| 645 | |
| 646 | // Get the configuration targets that trigger this rule's configurable attributes. |
| 647 | ConfigConditions configConditions = |
| 648 | ConfiguredTargetFunction.getConfigConditions( |
| 649 | env, |
| 650 | originalTargetAndAspectConfiguration, |
| 651 | transitivePackagesForPackageRootResolution, |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 652 | unloadedToolchainContexts == null |
| 653 | ? null |
| 654 | : unloadedToolchainContexts.getTargetPlatform(), |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 655 | transitiveRootCauses); |
| 656 | if (configConditions == null) { |
| 657 | // Those targets haven't yet been resolved. |
| 658 | return null; |
| 659 | } |
| 660 | |
| 661 | Target aliasedTarget = getTargetFromLabel(env, aliasedLabel); |
| 662 | if (aliasedTarget == null) { |
| 663 | return null; |
| 664 | } |
| 665 | ConfigurationTransition transition = |
| 666 | TransitionResolver.evaluateTransition( |
| 667 | aspectConfiguration, |
| 668 | NoTransition.INSTANCE, |
| 669 | aliasedTarget, |
| 670 | ((ConfiguredRuleClassProvider) ruleClassProvider).getTrimmingTransitionFactory()); |
| 671 | |
| 672 | // Use ConfigurationResolver to apply any configuration transitions on the alias edge. |
| 673 | // This is a shortened/simplified variant of ConfiguredTargetFunction.computeDependencies |
| 674 | // for just the one special attribute we care about here. |
| 675 | DependencyKey depKey = |
| 676 | DependencyKey.builder().setLabel(aliasedLabel).setTransition(transition).build(); |
| 677 | DependencyKind depKind = |
| 678 | DependencyKind.AttributeDependencyKind.forRule( |
| 679 | getAttributeContainingAlias(originalTarget.getTarget())); |
| 680 | ConfigurationResolver resolver = |
| 681 | new ConfigurationResolver( |
| 682 | env, |
| 683 | originalTargetAndAspectConfiguration, |
| 684 | hostConfiguration, |
| 685 | configConditions.asProviders()); |
nharmata | 85e4380 | 2022-01-05 14:55:26 -0800 | [diff] [blame] | 686 | ImmutableList<Dependency> deps = |
| 687 | resolver.resolveConfiguration(depKind, depKey, env.getListener()); |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 688 | if (deps == null) { |
| 689 | return null; |
| 690 | } |
| 691 | // Actual should resolve to exactly one dependency |
| 692 | Preconditions.checkState( |
| 693 | deps.size() == 1, "Unexpected split in alias %s: %s", originalTarget.getLabel(), deps); |
| 694 | dep = deps.get(0); |
| 695 | } catch (NoSuchPackageException | NoSuchTargetException e) { |
| 696 | throw new AspectFunctionException(e); |
| 697 | } catch (ConfiguredValueCreationException e) { |
| 698 | throw new AspectFunctionException(e); |
| 699 | } catch (AspectCreationException e) { |
| 700 | throw new AspectFunctionException(e); |
| 701 | } |
| 702 | |
| 703 | if (!transitiveRootCauses.isEmpty()) { |
| 704 | NestedSet<Cause> causes = transitiveRootCauses.build(); |
| 705 | throw new AspectFunctionException( |
| 706 | new AspectCreationException( |
| 707 | "Loading failed", |
| 708 | causes, |
| 709 | ConfiguredTargetFunction.getPrioritizedDetailedExitCode(causes))); |
| 710 | } |
| 711 | |
| 712 | // Now that we have a Dependency, we can compute the aliased key and depend on it |
| 713 | AspectKey actualKey = buildAliasAspectKey(originalKey, aliasedLabel, dep); |
| 714 | return createAliasAspect( |
| 715 | env, |
| 716 | originalTarget.getTarget(), |
| 717 | originalKey, |
| 718 | aspect, |
| 719 | actualKey, |
| 720 | transitivePackagesForPackageRootResolution); |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Googler | 5cc598c | 2022-07-06 03:29:45 -0700 | [diff] [blame^] | 723 | @Nullable |
jhorvitz | 86409b7 | 2021-10-04 16:55:20 -0700 | [diff] [blame] | 724 | private static AspectValue createAliasAspect( |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 725 | Environment env, |
| 726 | Target originalTarget, |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 727 | AspectKey originalKey, |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 728 | Aspect aspect, |
| 729 | AspectKey depKey, |
| 730 | @Nullable NestedSetBuilder<Package> transitivePackagesForPackageRootResolution) |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 731 | throws InterruptedException { |
Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 732 | // Compute the AspectValue of the target the alias refers to (which can itself be either an |
| 733 | // alias or a real target) |
Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 734 | AspectValue real = (AspectValue) env.getValue(depKey); |
| 735 | if (env.valuesMissing()) { |
| 736 | return null; |
| 737 | } |
| 738 | |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 739 | NestedSet<Package> finalTransitivePackagesForPackageRootResolution = null; |
| 740 | if (transitivePackagesForPackageRootResolution != null) { |
| 741 | finalTransitivePackagesForPackageRootResolution = |
| 742 | transitivePackagesForPackageRootResolution |
| 743 | .addTransitive(real.getTransitivePackagesForPackageRootResolution()) |
| 744 | .add(originalTarget.getPackage()) |
| 745 | .build(); |
| 746 | } |
Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 747 | return new AspectValue( |
| 748 | originalKey, |
| 749 | aspect, |
Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 750 | originalTarget.getLocation(), |
| 751 | ConfiguredAspect.forAlias(real.getConfiguredAspect()), |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 752 | finalTransitivePackagesForPackageRootResolution); |
| 753 | } |
| 754 | |
| 755 | @Nullable |
| 756 | private static Target getTargetFromLabel(Environment env, Label aliasLabel) |
| 757 | throws InterruptedException, NoSuchPackageException, NoSuchTargetException { |
| 758 | SkyValue val = |
| 759 | env.getValueOrThrow( |
| 760 | PackageValue.key(aliasLabel.getPackageIdentifier()), NoSuchPackageException.class); |
| 761 | if (val == null) { |
| 762 | return null; |
| 763 | } |
| 764 | |
| 765 | Package pkg = ((PackageValue) val).getPackage(); |
| 766 | return pkg.getTarget(aliasLabel.getName()); |
| 767 | } |
| 768 | |
| 769 | private static AspectKey buildAliasAspectKey( |
| 770 | AspectKey originalKey, Label aliasLabel, Dependency dep) { |
| 771 | ImmutableList<AspectKey> aliasedBaseKeys = |
| 772 | originalKey.getBaseKeys().stream() |
| 773 | .map(baseKey -> buildAliasAspectKey(baseKey, aliasLabel, dep)) |
| 774 | .collect(toImmutableList()); |
leba | b52a190 | 2021-09-23 01:35:13 -0700 | [diff] [blame] | 775 | return AspectKeyCreator.createAspectKey( |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 776 | originalKey.getAspectDescriptor(), |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 777 | aliasedBaseKeys, |
| 778 | ConfiguredTargetKey.builder() |
| 779 | .setLabel(aliasLabel) |
| 780 | .setConfiguration(dep.getConfiguration()) |
| 781 | .build()); |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Given an alias-like target, returns the attribute containing the "actual", by looking for |
| 786 | * attribute names used in known alias rules (Alias, Bind, LateBoundAlias, XcodeConfigAlias). |
| 787 | * |
| 788 | * <p>Alias and Bind rules use "actual", which will be by far the most common match here. It'll |
| 789 | * likely be rare that aspects need to traverse across other alias-like rules. |
| 790 | */ |
| 791 | // TODO(lberki,kmb): try to avoid this, maybe by recording the attribute name in AliasProvider |
| 792 | private static Attribute getAttributeContainingAlias(Target originalTarget) { |
| 793 | Attribute aliasAttr = null; |
| 794 | for (Attribute attr : originalTarget.getAssociatedRule().getAttributes()) { |
| 795 | switch (attr.getName()) { |
| 796 | case "actual": // alias and bind rules |
| 797 | case ":alias": // LateBoundAlias-derived rules |
| 798 | case ":xcode_config": // xcode_config_alias rule |
| 799 | Preconditions.checkState( |
| 800 | aliasAttr == null, |
| 801 | "Found multiple candidate attributes %s and %s in %s", |
| 802 | aliasAttr, |
| 803 | attr, |
| 804 | originalTarget); |
| 805 | aliasAttr = attr; |
| 806 | break; |
| 807 | default: |
| 808 | break; |
| 809 | } |
| 810 | } |
| 811 | Preconditions.checkState( |
| 812 | aliasAttr != null, "Attribute containing alias not found in %s", originalTarget); |
| 813 | return aliasAttr; |
Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 816 | @Nullable |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 817 | private AspectValue createAspect( |
| 818 | Environment env, |
| 819 | AspectKey key, |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 820 | ImmutableList<Aspect> topologicalAspectPath, |
Dmitry Lomov | 2eb8bdd | 2016-04-06 08:47:30 +0000 | [diff] [blame] | 821 | Aspect aspect, |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 822 | ConfiguredAspectFactory aspectFactory, |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 823 | ConfiguredTargetAndData associatedTarget, |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 824 | BuildConfigurationValue configuration, |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 825 | ConfigConditions configConditions, |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 826 | @Nullable ToolchainCollection<ResolvedToolchainContext> toolchainContexts, |
| 827 | @Nullable ExecGroupCollection.Builder execGroupCollectionBuilder, |
lberki | 102256f | 2019-02-08 01:34:23 -0800 | [diff] [blame] | 828 | OrderedSetMultimap<DependencyKind, ConfiguredTargetAndData> directDeps, |
janakr | 931d285 | 2017-12-15 13:48:29 -0800 | [diff] [blame] | 829 | @Nullable NestedSetBuilder<Package> transitivePackagesForPackageRootResolution) |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 830 | throws AspectFunctionException, InterruptedException { |
brandjon | 66b6463 | 2020-12-17 14:11:32 -0800 | [diff] [blame] | 831 | // Should be successfully evaluated and cached from the loading phase. |
| 832 | StarlarkBuiltinsValue starlarkBuiltinsValue = |
| 833 | (StarlarkBuiltinsValue) env.getValue(StarlarkBuiltinsValue.key()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 834 | if (env.valuesMissing()) { |
| 835 | return null; |
| 836 | } |
| 837 | |
brandjon | 66b6463 | 2020-12-17 14:11:32 -0800 | [diff] [blame] | 838 | SkyframeBuildView view = buildViewProvider.getSkyframeBuildView(); |
| 839 | |
| 840 | StoredEventHandler events = new StoredEventHandler(); |
| 841 | CachingAnalysisEnvironment analysisEnvironment = |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 842 | view.createAnalysisEnvironment(key, events, env, configuration, starlarkBuiltinsValue); |
brandjon | 66b6463 | 2020-12-17 14:11:32 -0800 | [diff] [blame] | 843 | |
Dmitry Lomov | 6cd9897 | 2017-03-01 15:44:00 +0000 | [diff] [blame] | 844 | ConfiguredAspect configuredAspect; |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 845 | if (aspect.getDefinition().applyToGeneratingRules() |
| 846 | && associatedTarget.getTarget() instanceof OutputFile) { |
| 847 | OutputFile outputFile = (OutputFile) associatedTarget.getTarget(); |
| 848 | Label label = outputFile.getGeneratingRule().getLabel(); |
kmb | b495a44 | 2021-08-25 19:45:28 -0700 | [diff] [blame] | 849 | return createAliasAspect( |
| 850 | env, |
| 851 | associatedTarget.getTarget(), |
| 852 | key, |
| 853 | aspect, |
| 854 | key.withLabel(label), |
| 855 | transitivePackagesForPackageRootResolution); |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 856 | } else if (AspectResolver.aspectMatchesConfiguredTarget(associatedTarget, aspect)) { |
tomlu | 72642a2 | 2017-10-18 06:23:14 +0200 | [diff] [blame] | 857 | try { |
| 858 | CurrentRuleTracker.beginConfiguredAspect(aspect.getAspectClass()); |
| 859 | configuredAspect = |
| 860 | view.getConfiguredTargetFactory() |
| 861 | .createAspect( |
| 862 | analysisEnvironment, |
| 863 | associatedTarget, |
jhorvitz | 3cf2bca | 2021-11-23 14:29:10 -0800 | [diff] [blame] | 864 | topologicalAspectPath, |
tomlu | 72642a2 | 2017-10-18 06:23:14 +0200 | [diff] [blame] | 865 | aspectFactory, |
| 866 | aspect, |
| 867 | directDeps, |
| 868 | configConditions, |
Googler | 602b7cd | 2022-06-06 05:13:12 -0700 | [diff] [blame] | 869 | toolchainContexts, |
| 870 | execGroupCollectionBuilder, |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 871 | configuration, |
jhorvitz | 86409b7 | 2021-10-04 16:55:20 -0700 | [diff] [blame] | 872 | view.getHostConfiguration(), |
janakr | eaff19c | 2019-01-31 13:59:40 -0800 | [diff] [blame] | 873 | key); |
ulfjack | 3740768 | 2019-09-17 06:30:49 -0700 | [diff] [blame] | 874 | } catch (MissingDepException e) { |
| 875 | Preconditions.checkState(env.valuesMissing()); |
| 876 | return null; |
jcater | 0b385bd | 2020-04-08 15:27:07 -0700 | [diff] [blame] | 877 | } catch (ActionConflictException e) { |
| 878 | throw new AspectFunctionException(e); |
juliexxia | 6fe70c2 | 2020-05-18 14:38:42 -0700 | [diff] [blame] | 879 | } catch (InvalidExecGroupException e) { |
| 880 | throw new AspectFunctionException(e); |
tomlu | 72642a2 | 2017-10-18 06:23:14 +0200 | [diff] [blame] | 881 | } finally { |
| 882 | CurrentRuleTracker.endConfiguredAspect(); |
| 883 | } |
Dmitry Lomov | 6cd9897 | 2017-03-01 15:44:00 +0000 | [diff] [blame] | 884 | } else { |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 885 | configuredAspect = ConfiguredAspect.forNonapplicableTarget(); |
Dmitry Lomov | 6cd9897 | 2017-03-01 15:44:00 +0000 | [diff] [blame] | 886 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 887 | |
| 888 | events.replayOn(env.getListener()); |
| 889 | if (events.hasErrors()) { |
| 890 | analysisEnvironment.disable(associatedTarget.getTarget()); |
janakr | 043acd9 | 2020-02-28 18:59:06 -0800 | [diff] [blame] | 891 | String msg = "Analysis of target '" + associatedTarget.getTarget().getLabel() + "' failed"; |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 892 | throw new AspectFunctionException( |
jhorvitz | 564a689 | 2021-11-18 20:29:24 -0800 | [diff] [blame] | 893 | new AspectCreationException(msg, key.getLabel(), configuration)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 894 | } |
| 895 | Preconditions.checkState(!analysisEnvironment.hasErrors(), |
| 896 | "Analysis environment hasError() but no errors reported"); |
| 897 | |
| 898 | if (env.valuesMissing()) { |
| 899 | return null; |
| 900 | } |
| 901 | |
| 902 | analysisEnvironment.disable(associatedTarget.getTarget()); |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 903 | Preconditions.checkNotNull(configuredAspect); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 904 | |
| 905 | return new AspectValue( |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 906 | key, |
Dmitry Lomov | 2eb8bdd | 2016-04-06 08:47:30 +0000 | [diff] [blame] | 907 | aspect, |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 908 | associatedTarget.getTarget().getLocation(), |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 909 | configuredAspect, |
janakr | 931d285 | 2017-12-15 13:48:29 -0800 | [diff] [blame] | 910 | transitivePackagesForPackageRootResolution == null |
| 911 | ? null |
Googler | ce6ad29 | 2019-12-20 10:29:21 -0800 | [diff] [blame] | 912 | : transitivePackagesForPackageRootResolution.build()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 913 | } |
| 914 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 915 | @Override |
| 916 | public String extractTag(SkyKey skyKey) { |
Michael Staib | 2707a88 | 2016-09-16 21:06:40 +0000 | [diff] [blame] | 917 | AspectKey aspectKey = (AspectKey) skyKey.argument(); |
| 918 | return Label.print(aspectKey.getLabel()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 919 | } |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 920 | |
tomlu | 39a0a38 | 2018-06-22 09:43:23 -0700 | [diff] [blame] | 921 | /** Used to indicate errors during the computation of an {@link AspectValue}. */ |
| 922 | public static final class AspectFunctionException extends SkyFunctionException { |
Ulf Adams | 9e16f0a | 2016-01-25 12:43:32 +0000 | [diff] [blame] | 923 | public AspectFunctionException(NoSuchThingException e) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 924 | super(e, Transience.PERSISTENT); |
| 925 | } |
| 926 | |
Ulf Adams | 9e16f0a | 2016-01-25 12:43:32 +0000 | [diff] [blame] | 927 | public AspectFunctionException(AspectCreationException e) { |
| 928 | super(e, Transience.PERSISTENT); |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 929 | } |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 930 | |
gregce | c564977 | 2021-07-08 13:10:19 -0700 | [diff] [blame] | 931 | public AspectFunctionException(ConfiguredValueCreationException e) { |
| 932 | super(e, Transience.PERSISTENT); |
| 933 | } |
| 934 | |
juliexxia | 6fe70c2 | 2020-05-18 14:38:42 -0700 | [diff] [blame] | 935 | public AspectFunctionException(InvalidExecGroupException e) { |
| 936 | super(e, Transience.PERSISTENT); |
| 937 | } |
| 938 | |
janakr | 0175ce3 | 2018-02-26 15:54:57 -0800 | [diff] [blame] | 939 | public AspectFunctionException(ActionConflictException cause) { |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 940 | super(cause, Transience.PERSISTENT); |
| 941 | } |
Ulf Adams | d55d7af | 2016-01-19 11:03:22 +0000 | [diff] [blame] | 942 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 943 | } |