blob: f36d1bb6d07642ffb0fc9231c836940c961c89d8 [file] [log] [blame]
jcater40549e72020-04-16 10:52:40 -07001// Copyright 2020 The Bazel Authors. All rights reserved.
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.
14package com.google.devtools.build.lib.skyframe;
15
jcater40549e72020-04-16 10:52:40 -070016import com.google.common.base.Objects;
17import com.google.common.collect.ImmutableList;
jcater40549e72020-04-16 10:52:40 -070018import com.google.common.collect.Interner;
jhorvitz3daedc32020-07-22 18:33:55 -070019import com.google.devtools.build.lib.actions.ActionLookupKey;
jcater40549e72020-04-16 10:52:40 -070020import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
21import com.google.devtools.build.lib.cmdline.Label;
22import com.google.devtools.build.lib.concurrent.BlazeInterners;
23import com.google.devtools.build.lib.packages.AspectClass;
24import com.google.devtools.build.lib.packages.AspectDescriptor;
25import com.google.devtools.build.lib.packages.AspectParameters;
jcater40549e72020-04-16 10:52:40 -070026import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
27import com.google.devtools.build.skyframe.SkyFunctionName;
28import javax.annotation.Nullable;
29
messac4157da2021-08-06 11:17:19 -070030/** A wrapper class for sky keys needed to compute sky values for aspects. */
31public final class AspectValueKey {
32
33 private AspectValueKey() {}
jcater40549e72020-04-16 10:52:40 -070034
35 private static final Interner<AspectKey> aspectKeyInterner = BlazeInterners.newWeakInterner();
messac4157da2021-08-06 11:17:19 -070036 private static final Interner<TopLevelAspectsKey> topLevelAspectsKeyInterner =
jcater40549e72020-04-16 10:52:40 -070037 BlazeInterners.newWeakInterner();
38
jcater40549e72020-04-16 10:52:40 -070039 public static AspectKey createAspectKey(
40 Label label,
jcater02054bb2020-05-28 09:49:38 -070041 @Nullable BuildConfiguration baseConfiguration,
jcater40549e72020-04-16 10:52:40 -070042 ImmutableList<AspectKey> baseKeys,
43 AspectDescriptor aspectDescriptor,
jcater02054bb2020-05-28 09:49:38 -070044 @Nullable BuildConfiguration aspectConfiguration) {
jcater40549e72020-04-16 10:52:40 -070045 return AspectKey.createAspectKey(
jcater83221e32020-05-28 11:37:39 -070046 ConfiguredTargetKey.builder().setLabel(label).setConfiguration(baseConfiguration).build(),
jcater40549e72020-04-16 10:52:40 -070047 baseKeys,
48 aspectDescriptor,
jcater02054bb2020-05-28 09:49:38 -070049 aspectConfiguration == null ? null : BuildConfigurationValue.key(aspectConfiguration));
jcater40549e72020-04-16 10:52:40 -070050 }
51
52 public static AspectKey createAspectKey(
messa943c83a2021-06-28 07:54:28 -070053 AspectDescriptor aspectDescriptor,
54 ImmutableList<AspectKey> baseKeys,
55 BuildConfigurationValue.Key aspectConfigurationKey,
56 ConfiguredTargetKey baseConfiguredTargetKey) {
57 return AspectKey.createAspectKey(
58 baseConfiguredTargetKey, baseKeys, aspectDescriptor, aspectConfigurationKey);
59 }
60
61 public static AspectKey createAspectKey(
jcater40549e72020-04-16 10:52:40 -070062 Label label,
jcater02054bb2020-05-28 09:49:38 -070063 @Nullable BuildConfiguration baseConfiguration,
jcater40549e72020-04-16 10:52:40 -070064 AspectDescriptor aspectDescriptor,
jcater02054bb2020-05-28 09:49:38 -070065 @Nullable BuildConfiguration aspectConfiguration) {
jcater40549e72020-04-16 10:52:40 -070066 return AspectKey.createAspectKey(
jcater83221e32020-05-28 11:37:39 -070067 ConfiguredTargetKey.builder().setLabel(label).setConfiguration(baseConfiguration).build(),
jcater40549e72020-04-16 10:52:40 -070068 ImmutableList.of(),
69 aspectDescriptor,
jcater02054bb2020-05-28 09:49:38 -070070 aspectConfiguration == null ? null : BuildConfigurationValue.key(aspectConfiguration));
jcater40549e72020-04-16 10:52:40 -070071 }
72
messac4157da2021-08-06 11:17:19 -070073 public static TopLevelAspectsKey createTopLevelAspectsKey(
74 ImmutableList<AspectClass> topLevelAspectsClasses,
jcater40549e72020-04-16 10:52:40 -070075 Label targetLabel,
messac4157da2021-08-06 11:17:19 -070076 @Nullable BuildConfiguration configuration) {
77 return TopLevelAspectsKey.createInternal(
78 topLevelAspectsClasses,
jhorvitz24b73662021-04-30 15:06:19 -070079 targetLabel,
jhorvitz24b73662021-04-30 15:06:19 -070080 ConfiguredTargetKey.builder()
81 .setLabel(targetLabel)
messac4157da2021-08-06 11:17:19 -070082 .setConfiguration(configuration)
83 .build());
84 }
85
86 /** Common superclass for {@link AspectKey} and {@link TopLevelAspectsKey}. */
87 public abstract static class AspectBaseKey implements ActionLookupKey {
88 private final ConfiguredTargetKey baseConfiguredTargetKey;
89 private final int hashCode;
90
91 private AspectBaseKey(ConfiguredTargetKey baseConfiguredTargetKey, int hashCode) {
92 this.baseConfiguredTargetKey = baseConfiguredTargetKey;
93 this.hashCode = hashCode;
94 }
95
96 /** Returns the key for the base configured target for this aspect. */
97 public final ConfiguredTargetKey getBaseConfiguredTargetKey() {
98 return baseConfiguredTargetKey;
99 }
100
101 @Override
102 public final int hashCode() {
103 return hashCode;
104 }
janakr13b737a2021-07-02 14:24:25 -0700105 }
106
jcater40549e72020-04-16 10:52:40 -0700107 // Specific subtypes of aspect keys.
108
jhorvitz24b73662021-04-30 15:06:19 -0700109 /** Represents an aspect applied to a particular target. */
jcater40549e72020-04-16 10:52:40 -0700110 @AutoCodec
messac4157da2021-08-06 11:17:19 -0700111 public static final class AspectKey extends AspectBaseKey {
jcater40549e72020-04-16 10:52:40 -0700112 private final ImmutableList<AspectKey> baseKeys;
jcater02054bb2020-05-28 09:49:38 -0700113 @Nullable private final BuildConfigurationValue.Key aspectConfigurationKey;
jcater40549e72020-04-16 10:52:40 -0700114 private final AspectDescriptor aspectDescriptor;
jcater40549e72020-04-16 10:52:40 -0700115
116 private AspectKey(
jcater40549e72020-04-16 10:52:40 -0700117 ConfiguredTargetKey baseConfiguredTargetKey,
118 ImmutableList<AspectKey> baseKeys,
jhorvitz24b73662021-04-30 15:06:19 -0700119 AspectDescriptor aspectDescriptor,
120 @Nullable BuildConfigurationValue.Key aspectConfigurationKey,
121 int hashCode) {
messac4157da2021-08-06 11:17:19 -0700122 super(baseConfiguredTargetKey, hashCode);
jcater40549e72020-04-16 10:52:40 -0700123 this.baseKeys = baseKeys;
jcater40549e72020-04-16 10:52:40 -0700124 this.aspectConfigurationKey = aspectConfigurationKey;
jcater40549e72020-04-16 10:52:40 -0700125 this.aspectDescriptor = aspectDescriptor;
jcater40549e72020-04-16 10:52:40 -0700126 }
127
128 @AutoCodec.VisibleForSerialization
129 @AutoCodec.Instantiator
130 static AspectKey createAspectKey(
jcater40549e72020-04-16 10:52:40 -0700131 ConfiguredTargetKey baseConfiguredTargetKey,
132 ImmutableList<AspectKey> baseKeys,
133 AspectDescriptor aspectDescriptor,
jcater02054bb2020-05-28 09:49:38 -0700134 @Nullable BuildConfigurationValue.Key aspectConfigurationKey) {
jcater40549e72020-04-16 10:52:40 -0700135 return aspectKeyInterner.intern(
jcater02054bb2020-05-28 09:49:38 -0700136 new AspectKey(
jhorvitz24b73662021-04-30 15:06:19 -0700137 baseConfiguredTargetKey,
138 baseKeys,
139 aspectDescriptor,
140 aspectConfigurationKey,
141 Objects.hashCode(
142 baseConfiguredTargetKey, baseKeys, aspectDescriptor, aspectConfigurationKey)));
jcater40549e72020-04-16 10:52:40 -0700143 }
144
145 @Override
146 public SkyFunctionName functionName() {
147 return SkyFunctions.ASPECT;
148 }
149
messac4157da2021-08-06 11:17:19 -0700150 /**
151 * Gets the name of the aspect that would be returned by the corresponding value's {@code
152 * aspectValue.getAspect().getAspectClass().getName()}, if the value could be produced.
153 *
154 * <p>Only needed for reporting errors in BEP when the key's AspectValue fails evaluation.
155 */
adgar24879202021-04-12 09:21:20 -0700156 public String getAspectName() {
157 return aspectDescriptor.getDescription();
158 }
jcater40549e72020-04-16 10:52:40 -0700159
160 @Override
161 public Label getLabel() {
messac4157da2021-08-06 11:17:19 -0700162 return getBaseConfiguredTargetKey().getLabel();
jcater40549e72020-04-16 10:52:40 -0700163 }
164
165 public AspectClass getAspectClass() {
166 return aspectDescriptor.getAspectClass();
167 }
168
169 @Nullable
170 public AspectParameters getParameters() {
171 return aspectDescriptor.getParameters();
172 }
173
jcater40549e72020-04-16 10:52:40 -0700174 public AspectDescriptor getAspectDescriptor() {
175 return aspectDescriptor;
176 }
177
178 @Nullable
jhorvitz5d349572021-09-21 09:34:12 -0700179 public ImmutableList<AspectKey> getBaseKeys() {
jcater40549e72020-04-16 10:52:40 -0700180 return baseKeys;
181 }
182
jcater40549e72020-04-16 10:52:40 -0700183 public String getDescription() {
184 if (baseKeys.isEmpty()) {
messa7649f612021-07-27 01:19:27 -0700185 return String.format("%s of %s", aspectDescriptor.getAspectClass().getName(), getLabel());
jcater40549e72020-04-16 10:52:40 -0700186 } else {
jhorvitz24b73662021-04-30 15:06:19 -0700187 return String.format(
188 "%s on top of %s", aspectDescriptor.getAspectClass().getName(), baseKeys);
jcater40549e72020-04-16 10:52:40 -0700189 }
190 }
191
jcater40549e72020-04-16 10:52:40 -0700192 /**
193 * Returns the key of the configured target of the aspect; that is, the configuration in which
194 * the aspect will be evaluated.
195 *
196 * <p>In trimmed configuration mode, the aspect may require more fragments than the target on
197 * which it is being evaluated; in addition to configuration fragments required by the target
198 * and its dependencies, an aspect has configuration fragment requirements of its own, as well
199 * as dependencies of its own with their own configuration fragment requirements.
200 *
201 * <p>The aspect configuration contains all of these fragments, and is used to create the
202 * aspect's RuleContext and to retrieve the dependencies. Note that dependencies will have their
203 * configurations trimmed from this one as normal.
204 *
205 * <p>Because of these properties, this configuration is always a superset of the base target's
206 * configuration. In untrimmed configuration mode, this configuration will be equivalent to the
207 * base target's configuration.
208 */
jcater02054bb2020-05-28 09:49:38 -0700209 @Nullable
jhorvitzc6509782021-08-23 15:43:24 -0700210 public BuildConfigurationValue.Key getAspectConfigurationKey() {
jcater40549e72020-04-16 10:52:40 -0700211 return aspectConfigurationKey;
212 }
213
jcater40549e72020-04-16 10:52:40 -0700214 @Override
215 public boolean equals(Object other) {
216 if (this == other) {
217 return true;
218 }
jcater40549e72020-04-16 10:52:40 -0700219 if (!(other instanceof AspectKey)) {
220 return false;
221 }
jcater40549e72020-04-16 10:52:40 -0700222 AspectKey that = (AspectKey) other;
messac4157da2021-08-06 11:17:19 -0700223 return hashCode() == that.hashCode()
jhorvitz24b73662021-04-30 15:06:19 -0700224 && Objects.equal(baseKeys, that.baseKeys)
jcater40549e72020-04-16 10:52:40 -0700225 && Objects.equal(aspectConfigurationKey, that.aspectConfigurationKey)
messac4157da2021-08-06 11:17:19 -0700226 && Objects.equal(getBaseConfiguredTargetKey(), that.getBaseConfiguredTargetKey())
jcater40549e72020-04-16 10:52:40 -0700227 && Objects.equal(aspectDescriptor, that.aspectDescriptor);
228 }
229
230 public String prettyPrint() {
janakrf15d08d2020-04-22 12:53:03 -0700231 if (getLabel() == null) {
jcater40549e72020-04-16 10:52:40 -0700232 return "null";
233 }
234
jhorvitz24b73662021-04-30 15:06:19 -0700235 String baseKeysString = baseKeys.isEmpty() ? "" : String.format(" (over %s)", baseKeys);
jcater40549e72020-04-16 10:52:40 -0700236 return String.format(
jcater02054bb2020-05-28 09:49:38 -0700237 "%s with aspect %s%s",
238 getLabel(), aspectDescriptor.getAspectClass().getName(), baseKeysString);
jcater40549e72020-04-16 10:52:40 -0700239 }
240
241 @Override
242 public String toString() {
janakrf15d08d2020-04-22 12:53:03 -0700243 return (baseKeys == null ? getLabel() : baseKeys.toString())
jcater40549e72020-04-16 10:52:40 -0700244 + "#"
245 + aspectDescriptor
246 + " "
247 + aspectConfigurationKey
248 + " "
messac4157da2021-08-06 11:17:19 -0700249 + getBaseConfiguredTargetKey()
jcater40549e72020-04-16 10:52:40 -0700250 + " "
jcater02054bb2020-05-28 09:49:38 -0700251 + aspectDescriptor.getParameters();
jcater40549e72020-04-16 10:52:40 -0700252 }
253
254 AspectKey withLabel(Label label) {
255 ImmutableList.Builder<AspectKey> newBaseKeys = ImmutableList.builder();
256 for (AspectKey baseKey : baseKeys) {
257 newBaseKeys.add(baseKey.withLabel(label));
258 }
259
260 return createAspectKey(
jcater3eb2d252020-05-28 10:39:12 -0700261 ConfiguredTargetKey.builder()
262 .setLabel(label)
messac4157da2021-08-06 11:17:19 -0700263 .setConfigurationKey(getBaseConfiguredTargetKey().getConfigurationKey())
jcater3eb2d252020-05-28 10:39:12 -0700264 .build(),
jcater40549e72020-04-16 10:52:40 -0700265 newBaseKeys.build(),
266 aspectDescriptor,
jcater02054bb2020-05-28 09:49:38 -0700267 aspectConfigurationKey);
jcater40549e72020-04-16 10:52:40 -0700268 }
269 }
270
messac4157da2021-08-06 11:17:19 -0700271 /** The key for top level aspects specified by --aspects option on a top level target. */
jhorvitz24b73662021-04-30 15:06:19 -0700272 @AutoCodec
messac4157da2021-08-06 11:17:19 -0700273 public static final class TopLevelAspectsKey extends AspectBaseKey {
274 private final ImmutableList<AspectClass> topLevelAspectsClasses;
jcater40549e72020-04-16 10:52:40 -0700275 private final Label targetLabel;
jhorvitz24b73662021-04-30 15:06:19 -0700276
277 @AutoCodec.Instantiator
278 @AutoCodec.VisibleForSerialization
messac4157da2021-08-06 11:17:19 -0700279 static TopLevelAspectsKey createInternal(
280 ImmutableList<AspectClass> topLevelAspectsClasses,
jhorvitz24b73662021-04-30 15:06:19 -0700281 Label targetLabel,
messac4157da2021-08-06 11:17:19 -0700282 ConfiguredTargetKey baseConfiguredTargetKey) {
283 return topLevelAspectsKeyInterner.intern(
284 new TopLevelAspectsKey(
285 topLevelAspectsClasses,
jhorvitz24b73662021-04-30 15:06:19 -0700286 targetLabel,
jhorvitz24b73662021-04-30 15:06:19 -0700287 baseConfiguredTargetKey,
messac4157da2021-08-06 11:17:19 -0700288 Objects.hashCode(topLevelAspectsClasses, targetLabel, baseConfiguredTargetKey)));
jhorvitz24b73662021-04-30 15:06:19 -0700289 }
jcater40549e72020-04-16 10:52:40 -0700290
messac4157da2021-08-06 11:17:19 -0700291 private TopLevelAspectsKey(
292 ImmutableList<AspectClass> topLevelAspectsClasses,
jcater40549e72020-04-16 10:52:40 -0700293 Label targetLabel,
jcater40549e72020-04-16 10:52:40 -0700294 ConfiguredTargetKey baseConfiguredTargetKey,
jhorvitz24b73662021-04-30 15:06:19 -0700295 int hashCode) {
messac4157da2021-08-06 11:17:19 -0700296 super(baseConfiguredTargetKey, hashCode);
297 this.topLevelAspectsClasses = topLevelAspectsClasses;
jcater40549e72020-04-16 10:52:40 -0700298 this.targetLabel = targetLabel;
jcater40549e72020-04-16 10:52:40 -0700299 }
300
301 @Override
302 public SkyFunctionName functionName() {
messac4157da2021-08-06 11:17:19 -0700303 return SkyFunctions.TOP_LEVEL_ASPECTS;
jcater40549e72020-04-16 10:52:40 -0700304 }
305
messac4157da2021-08-06 11:17:19 -0700306 ImmutableList<AspectClass> getTopLevelAspectsClasses() {
307 return topLevelAspectsClasses;
adgar24879202021-04-12 09:21:20 -0700308 }
309
310 @Override
jcater40549e72020-04-16 10:52:40 -0700311 public Label getLabel() {
312 return targetLabel;
313 }
314
messac4157da2021-08-06 11:17:19 -0700315 String getDescription() {
316 return topLevelAspectsClasses + " on " + getLabel();
messa943c83a2021-06-28 07:54:28 -0700317 }
318
jcater40549e72020-04-16 10:52:40 -0700319 @Override
jcater40549e72020-04-16 10:52:40 -0700320 public boolean equals(Object o) {
321 if (o == this) {
322 return true;
323 }
messac4157da2021-08-06 11:17:19 -0700324 if (!(o instanceof TopLevelAspectsKey)) {
jcater40549e72020-04-16 10:52:40 -0700325 return false;
326 }
messac4157da2021-08-06 11:17:19 -0700327 TopLevelAspectsKey that = (TopLevelAspectsKey) o;
328 return hashCode() == that.hashCode()
jhorvitz24b73662021-04-30 15:06:19 -0700329 && Objects.equal(targetLabel, that.targetLabel)
messac4157da2021-08-06 11:17:19 -0700330 && Objects.equal(getBaseConfiguredTargetKey(), that.getBaseConfiguredTargetKey())
331 && Objects.equal(topLevelAspectsClasses, that.topLevelAspectsClasses);
jcater40549e72020-04-16 10:52:40 -0700332 }
333 }
334}