blob: f552b112c6bf930193fbb8b0cca165d444158a0b [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.lib.skyframe;
16
janakrb9788e12018-01-24 11:46:25 -080017import com.google.common.base.MoreObjects;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.common.base.Objects;
janakrdb4dec22017-03-28 22:39:35 +000019import com.google.common.base.Preconditions;
Dmitry Lomove851fe22017-02-14 23:11:23 +000020import com.google.common.collect.ImmutableList;
janakr573807d2018-01-11 14:02:35 -080021import com.google.common.collect.Interner;
cparsonse2d200f2018-03-06 16:15:11 -080022import com.google.devtools.build.lib.actions.BasicActionLookupValue;
Dmitry Lomovb487ac62015-11-09 13:09:12 +000023import com.google.devtools.build.lib.analysis.ConfiguredAspect;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
Lukacs Berki6e91eb92015-09-21 09:12:37 +000025import com.google.devtools.build.lib.cmdline.Label;
Marian Loburc62faba2015-09-09 10:08:06 +000026import com.google.devtools.build.lib.collect.nestedset.NestedSet;
janakr573807d2018-01-11 14:02:35 -080027import com.google.devtools.build.lib.concurrent.BlazeInterners;
Dmitry Lomove2033b12015-08-19 16:57:49 +000028import com.google.devtools.build.lib.events.Location;
Dmitry Lomovb487ac62015-11-09 13:09:12 +000029import com.google.devtools.build.lib.packages.Aspect;
Dmitry Lomov8ff06452015-10-21 19:16:30 +000030import com.google.devtools.build.lib.packages.AspectClass;
Dmitry Lomov15756522016-12-16 16:52:37 +000031import com.google.devtools.build.lib.packages.AspectDescriptor;
Marian Lobur702cad72015-09-02 09:53:58 +000032import com.google.devtools.build.lib.packages.AspectParameters;
Marian Loburc62faba2015-09-09 10:08:06 +000033import com.google.devtools.build.lib.packages.Package;
cpeysera8a61ee2018-01-26 09:20:37 -080034import com.google.devtools.build.lib.skyframe.BuildConfigurationValue.Key;
35import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey.KeyAndHost;
janakr649d2b12018-02-13 15:37:30 -080036import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
Dmitry Lomov51aafc12016-11-18 14:02:54 +000037import com.google.devtools.build.lib.syntax.SkylarkImport;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010038import com.google.devtools.build.skyframe.SkyFunctionName;
janakra81bb952019-01-28 17:30:06 -080039import java.math.BigInteger;
Marian Lobur702cad72015-09-02 09:53:58 +000040import javax.annotation.Nullable;
41
janakr9bad8402018-03-23 15:32:37 -070042/** An aspect in the context of the Skyframe graph. */
cparsonse2d200f2018-03-06 16:15:11 -080043public final class AspectValue extends BasicActionLookupValue {
Dmitry Lomov0b832ce2015-10-20 10:03:14 +000044 /**
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +000045 * A base class for keys that have AspectValue as a Sky value.
46 */
47 public abstract static class AspectValueKey extends ActionLookupKey {
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +000048 public abstract String getDescription();
janakr20984742019-01-28 10:36:35 -080049
50 @Override
51 public abstract Label getLabel();
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +000052 }
53
janakrb9788e12018-01-24 11:46:25 -080054 /** A base class for a key representing an aspect applied to a particular target. */
janakr649d2b12018-02-13 15:37:30 -080055 @AutoCodec
janakrb9788e12018-01-24 11:46:25 -080056 public static class AspectKey extends AspectValueKey {
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +000057 private final Label label;
Dmitry Lomove851fe22017-02-14 23:11:23 +000058 private final ImmutableList<AspectKey> baseKeys;
cpeysera8a61ee2018-01-26 09:20:37 -080059 private final BuildConfigurationValue.Key aspectConfigurationKey;
60 private final ConfiguredTargetKey baseConfiguredTargetKey;
Dmitry Lomove851fe22017-02-14 23:11:23 +000061 private final AspectDescriptor aspectDescriptor;
janakr573807d2018-01-11 14:02:35 -080062 private int hashCode;
Dmitry Lomov0b832ce2015-10-20 10:03:14 +000063
Dmitry Lomovca9bfa42016-11-15 13:22:36 +000064 private AspectKey(
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +000065 Label label,
cpeysera8a61ee2018-01-26 09:20:37 -080066 BuildConfigurationValue.Key aspectConfigurationKey,
67 ConfiguredTargetKey baseConfiguredTargetKey,
Dmitry Lomove851fe22017-02-14 23:11:23 +000068 ImmutableList<AspectKey> baseKeys,
cpeysera8a61ee2018-01-26 09:20:37 -080069 AspectDescriptor aspectDescriptor) {
Dmitry Lomove851fe22017-02-14 23:11:23 +000070 this.baseKeys = baseKeys;
71 this.label = label;
cpeysera8a61ee2018-01-26 09:20:37 -080072 this.aspectConfigurationKey = aspectConfigurationKey;
73 this.baseConfiguredTargetKey = baseConfiguredTargetKey;
Dmitry Lomove851fe22017-02-14 23:11:23 +000074 this.aspectDescriptor = aspectDescriptor;
Dmitry Lomovca9bfa42016-11-15 13:22:36 +000075 }
76
janakr649d2b12018-02-13 15:37:30 -080077 @AutoCodec.VisibleForSerialization
78 @AutoCodec.Instantiator
79 static AspectKey createAspectKey(
80 Label label,
81 ConfiguredTargetKey baseConfiguredTargetKey,
82 ImmutableList<AspectKey> baseKeys,
83 AspectDescriptor aspectDescriptor,
84 BuildConfigurationValue.Key aspectConfigurationKey,
85 boolean aspectConfigurationIsHost) {
86 return aspectKeyInterner.intern(
87 aspectConfigurationIsHost
88 ? new HostAspectKey(
89 label,
90 aspectConfigurationKey,
91 baseConfiguredTargetKey,
92 baseKeys,
93 aspectDescriptor)
94 : new AspectKey(
95 label,
96 aspectConfigurationKey,
97 baseConfiguredTargetKey,
98 baseKeys,
99 aspectDescriptor));
100 }
101
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100102 @Override
janakr573807d2018-01-11 14:02:35 -0800103 public SkyFunctionName functionName() {
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000104 return SkyFunctions.ASPECT;
105 }
106
107
108 @Override
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100109 public Label getLabel() {
110 return label;
111 }
112
Dmitry Lomovb487ac62015-11-09 13:09:12 +0000113 public AspectClass getAspectClass() {
Dmitry Lomove851fe22017-02-14 23:11:23 +0000114 return aspectDescriptor.getAspectClass();
Marian Lobur702cad72015-09-02 09:53:58 +0000115 }
116
117 @Nullable
118 public AspectParameters getParameters() {
Dmitry Lomove851fe22017-02-14 23:11:23 +0000119 return aspectDescriptor.getParameters();
120 }
121
122 public AspectDescriptor getAspectDescriptor() {
123 return aspectDescriptor;
Dmitry Lomov6231d082015-11-02 17:17:20 +0000124 }
125
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000126 @Nullable
janakr649d2b12018-02-13 15:37:30 -0800127 ImmutableList<AspectKey> getBaseKeys() {
Dmitry Lomove851fe22017-02-14 23:11:23 +0000128 return baseKeys;
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000129 }
130
John Fielda97e17f2015-11-13 02:19:52 +0000131 @Override
Dmitry Lomov2aa1a982015-10-20 12:18:36 +0000132 public String getDescription() {
Dmitry Lomove851fe22017-02-14 23:11:23 +0000133 if (baseKeys.isEmpty()) {
134 return String.format("%s of %s",
135 aspectDescriptor.getAspectClass().getName(), getLabel());
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000136 } else {
Dmitry Lomove851fe22017-02-14 23:11:23 +0000137 return String.format("%s on top of %s",
138 aspectDescriptor.getAspectClass().getName(), baseKeys.toString());
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000139 }
Dmitry Lomov2aa1a982015-10-20 12:18:36 +0000140 }
141
janakr649d2b12018-02-13 15:37:30 -0800142 // Note that this does not factor into equality/hash-code computations because its value is
143 // already encoded in the aspectConfigurationKey, albeit in an opaque way.
cpeysera8a61ee2018-01-26 09:20:37 -0800144 protected boolean aspectConfigurationIsHost() {
145 return false;
146 }
147
Michael Staib04f6f242016-03-01 15:40:29 +0000148 /**
cpeysera8a61ee2018-01-26 09:20:37 -0800149 * Returns the key of the configured target of the aspect; that is, the configuration in which
150 * the aspect will be evaluated.
Michael Staib04f6f242016-03-01 15:40:29 +0000151 *
gregcee0bbe752017-09-12 23:58:34 +0200152 * <p>In trimmed configuration mode, the aspect may require more fragments than the target on
Michael Staib04f6f242016-03-01 15:40:29 +0000153 * which it is being evaluated; in addition to configuration fragments required by the target
154 * and its dependencies, an aspect has configuration fragment requirements of its own, as well
155 * as dependencies of its own with their own configuration fragment requirements.
156 *
157 * <p>The aspect configuration contains all of these fragments, and is used to create the
158 * aspect's RuleContext and to retrieve the dependencies. Note that dependencies will have their
159 * configurations trimmed from this one as normal.
160 *
janakrb9788e12018-01-24 11:46:25 -0800161 * <p>Because of these properties, this configuration is always a superset of the base target's
162 * configuration. In untrimmed configuration mode, this configuration will be equivalent to the
cpeysera8a61ee2018-01-26 09:20:37 -0800163 * base target's configuration.
Michael Staib04f6f242016-03-01 15:40:29 +0000164 */
cpeysera8a61ee2018-01-26 09:20:37 -0800165 BuildConfigurationValue.Key getAspectConfigurationKey() {
166 return aspectConfigurationKey;
Michael Staib04f6f242016-03-01 15:40:29 +0000167 }
168
janakrb9788e12018-01-24 11:46:25 -0800169 /** Returns the key for the base configured target for this aspect. */
cpeysera8a61ee2018-01-26 09:20:37 -0800170 ConfiguredTargetKey getBaseConfiguredTargetKey() {
171 return baseConfiguredTargetKey;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100172 }
173
174 @Override
175 public int hashCode() {
janakr573807d2018-01-11 14:02:35 -0800176 // We use the hash code caching strategy employed by java.lang.String. There are three subtle
177 // things going on here:
178 //
179 // (1) We use a value of 0 to indicate that the hash code hasn't been computed and cached yet.
180 // Yes, this means that if the hash code is really 0 then we will "recompute" it each time.
181 // But this isn't a problem in practice since a hash code of 0 should be rare.
182 //
183 // (2) Since we have no synchronization, multiple threads can race here thinking there are the
184 // first one to compute and cache the hash code.
185 //
186 // (3) Moreover, since 'hashCode' is non-volatile, the cached hash code value written from one
187 // thread may not be visible by another.
188 //
189 // All three of these issues are benign from a correctness perspective; in the end we have no
190 // overhead from synchronization, at the cost of potentially computing the hash code more than
191 // once.
192 int h = hashCode;
193 if (h == 0) {
194 h = computeHashCode();
195 hashCode = h;
196 }
197 return h;
198 }
199
200 private int computeHashCode() {
Dmitry Lomovf86ad5f2016-04-06 10:01:15 +0000201 return Objects.hashCode(
cpeysera8a61ee2018-01-26 09:20:37 -0800202 label, baseKeys, aspectConfigurationKey, baseConfiguredTargetKey, aspectDescriptor);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100203 }
204
205 @Override
206 public boolean equals(Object other) {
207 if (this == other) {
208 return true;
209 }
210
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000211 if (!(other instanceof AspectKey)) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100212 return false;
213 }
214
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000215 AspectKey that = (AspectKey) other;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100216 return Objects.equal(label, that.label)
Dmitry Lomove851fe22017-02-14 23:11:23 +0000217 && Objects.equal(baseKeys, that.baseKeys)
cpeysera8a61ee2018-01-26 09:20:37 -0800218 && Objects.equal(aspectConfigurationKey, that.aspectConfigurationKey)
219 && Objects.equal(baseConfiguredTargetKey, that.baseConfiguredTargetKey)
Dmitry Lomove851fe22017-02-14 23:11:23 +0000220 && Objects.equal(aspectDescriptor, that.aspectDescriptor);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100221 }
222
Lukacs Berkibba75d82016-06-14 09:08:29 +0000223 public String prettyPrint() {
224 if (label == null) {
225 return "null";
226 }
Dmitry Lomove851fe22017-02-14 23:11:23 +0000227
228 String baseKeysString =
229 baseKeys.isEmpty()
230 ? ""
231 : String.format(" (over %s)", baseKeys.toString());
cpeysera8a61ee2018-01-26 09:20:37 -0800232 return String.format(
233 "%s with aspect %s%s%s",
Dmitry Lomove851fe22017-02-14 23:11:23 +0000234 label.toString(),
235 aspectDescriptor.getAspectClass().getName(),
cpeysera8a61ee2018-01-26 09:20:37 -0800236 (aspectConfigurationKey != null && aspectConfigurationIsHost()) ? "(host) " : "",
237 baseKeysString);
Lukacs Berkibba75d82016-06-14 09:08:29 +0000238 }
239
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100240 @Override
241 public String toString() {
Dmitry Lomove851fe22017-02-14 23:11:23 +0000242 return (baseKeys == null ? label : baseKeys.toString())
Dmitry Lomov6231d082015-11-02 17:17:20 +0000243 + "#"
janakr649d2b12018-02-13 15:37:30 -0800244 + aspectDescriptor
Dmitry Lomov6231d082015-11-02 17:17:20 +0000245 + " "
cpeysera8a61ee2018-01-26 09:20:37 -0800246 + aspectConfigurationKey
Michael Staib04f6f242016-03-01 15:40:29 +0000247 + " "
cpeysera8a61ee2018-01-26 09:20:37 -0800248 + baseConfiguredTargetKey
Dmitry Lomov6231d082015-11-02 17:17:20 +0000249 + " "
janakr649d2b12018-02-13 15:37:30 -0800250 + aspectDescriptor.getParameters()
251 + (aspectConfigurationIsHost() ? " (host)" : "");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100252 }
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000253
janakrb9788e12018-01-24 11:46:25 -0800254 AspectKey withLabel(Label label) {
Dmitry Lomove851fe22017-02-14 23:11:23 +0000255 ImmutableList.Builder<AspectKey> newBaseKeys = ImmutableList.builder();
256 for (AspectKey baseKey : baseKeys) {
257 newBaseKeys.add(baseKey.withLabel(label));
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000258 }
Dmitry Lomove851fe22017-02-14 23:11:23 +0000259
janakrb9788e12018-01-24 11:46:25 -0800260 return createAspectKey(
261 label,
262 ConfiguredTargetKey.of(
263 label,
cpeysera8a61ee2018-01-26 09:20:37 -0800264 baseConfiguredTargetKey.getConfigurationKey(),
265 baseConfiguredTargetKey.isHostConfiguration()),
janakrb9788e12018-01-24 11:46:25 -0800266 newBaseKeys.build(),
267 aspectDescriptor,
cpeysera8a61ee2018-01-26 09:20:37 -0800268 aspectConfigurationKey,
269 aspectConfigurationIsHost());
270 }
271 }
272
273 /** An {@link AspectKey} for an aspect in the host configuration. */
janakr64d9a4d2018-02-02 12:38:58 -0800274 static class HostAspectKey extends AspectKey {
cpeysera8a61ee2018-01-26 09:20:37 -0800275 private HostAspectKey(
276 Label label,
277 Key aspectConfigurationKey,
278 ConfiguredTargetKey baseConfiguredTargetKey,
279 ImmutableList<AspectKey> baseKeys,
280 AspectDescriptor aspectDescriptor) {
281 super(label, aspectConfigurationKey, baseConfiguredTargetKey, baseKeys, aspectDescriptor);
282 }
283
284 @Override
285 protected boolean aspectConfigurationIsHost() {
286 return true;
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000287 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100288 }
289
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000290 /**
Dmitry Lomovb487ac62015-11-09 13:09:12 +0000291 * The key for a skylark aspect.
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000292 */
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000293 public static class SkylarkAspectLoadingKey extends AspectValueKey {
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000294
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000295 private final Label targetLabel;
cpeysera8a61ee2018-01-26 09:20:37 -0800296 private final BuildConfigurationValue.Key aspectConfigurationKey;
297 private final ConfiguredTargetKey baseConfiguredTargetKey;
Dmitry Lomov51aafc12016-11-18 14:02:54 +0000298 private final SkylarkImport skylarkImport;
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000299 private final String skylarkValueName;
janakr573807d2018-01-11 14:02:35 -0800300 private int hashCode;
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000301
302 private SkylarkAspectLoadingKey(
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000303 Label targetLabel,
cpeysera8a61ee2018-01-26 09:20:37 -0800304 BuildConfigurationValue.Key aspectConfigurationKey,
305 ConfiguredTargetKey baseConfiguredTargetKey,
Dmitry Lomov51aafc12016-11-18 14:02:54 +0000306 SkylarkImport skylarkImport,
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000307 String skylarkFunctionName) {
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000308 this.targetLabel = targetLabel;
cpeysera8a61ee2018-01-26 09:20:37 -0800309 this.aspectConfigurationKey = aspectConfigurationKey;
310 this.baseConfiguredTargetKey = baseConfiguredTargetKey;
Dmitry Lomov51aafc12016-11-18 14:02:54 +0000311 this.skylarkImport = skylarkImport;
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000312 this.skylarkValueName = skylarkFunctionName;
313 }
314
315 @Override
janakr573807d2018-01-11 14:02:35 -0800316 public SkyFunctionName functionName() {
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000317 return SkyFunctions.LOAD_SKYLARK_ASPECT;
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000318 }
319
janakr20984742019-01-28 10:36:35 -0800320 String getSkylarkValueName() {
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000321 return skylarkValueName;
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000322 }
323
janakr20984742019-01-28 10:36:35 -0800324 SkylarkImport getSkylarkImport() {
Dmitry Lomov51aafc12016-11-18 14:02:54 +0000325 return skylarkImport;
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000326 }
327
cpeysera8a61ee2018-01-26 09:20:37 -0800328 protected boolean isAspectConfigurationHost() {
329 return false;
330 }
331
John Fielda97e17f2015-11-13 02:19:52 +0000332 @Override
janakr20984742019-01-28 10:36:35 -0800333 public Label getLabel() {
334 return targetLabel;
335 }
336
337 @Override
Dmitry Lomov2aa1a982015-10-20 12:18:36 +0000338 public String getDescription() {
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000339 // Skylark aspects are referred to on command line with <file>%<value ame>
Dmitry Lomov51aafc12016-11-18 14:02:54 +0000340 return String.format("%s%%%s of %s", skylarkImport.getImportString(),
341 skylarkValueName, targetLabel);
342 }
343
344 @Override
345 public int hashCode() {
janakr573807d2018-01-11 14:02:35 -0800346 // We use the hash code caching strategy employed by java.lang.String. There are three subtle
347 // things going on here:
348 //
349 // (1) We use a value of 0 to indicate that the hash code hasn't been computed and cached yet.
350 // Yes, this means that if the hash code is really 0 then we will "recompute" it each time.
351 // But this isn't a problem in practice since a hash code of 0 should be rare.
352 //
353 // (2) Since we have no synchronization, multiple threads can race here thinking there are the
354 // first one to compute and cache the hash code.
355 //
356 // (3) Moreover, since 'hashCode' is non-volatile, the cached hash code value written from one
357 // thread may not be visible by another.
358 //
359 // All three of these issues are benign from a correctness perspective; in the end we have no
360 // overhead from synchronization, at the cost of potentially computing the hash code more than
361 // once.
362 int h = hashCode;
363 if (h == 0) {
364 h = computeHashCode();
365 hashCode = h;
366 }
367 return h;
368 }
369
370 private int computeHashCode() {
janakrb9788e12018-01-24 11:46:25 -0800371 return Objects.hashCode(
cpeysera8a61ee2018-01-26 09:20:37 -0800372 targetLabel,
373 aspectConfigurationKey,
374 baseConfiguredTargetKey,
375 skylarkImport,
376 skylarkValueName);
Dmitry Lomov51aafc12016-11-18 14:02:54 +0000377 }
378
379 @Override
380 public boolean equals(Object o) {
381 if (o == this) {
382 return true;
383 }
384
385 if (!(o instanceof SkylarkAspectLoadingKey)) {
386 return false;
387 }
388 SkylarkAspectLoadingKey that = (SkylarkAspectLoadingKey) o;
389 return Objects.equal(targetLabel, that.targetLabel)
cpeysera8a61ee2018-01-26 09:20:37 -0800390 && Objects.equal(aspectConfigurationKey, that.aspectConfigurationKey)
391 && Objects.equal(baseConfiguredTargetKey, that.baseConfiguredTargetKey)
Dmitry Lomov51aafc12016-11-18 14:02:54 +0000392 && Objects.equal(skylarkImport, that.skylarkImport)
393 && Objects.equal(skylarkValueName, that.skylarkValueName);
janakrb9788e12018-01-24 11:46:25 -0800394 }
Dmitry Lomov51aafc12016-11-18 14:02:54 +0000395
janakrb9788e12018-01-24 11:46:25 -0800396 AspectKey toAspectKey(AspectClass aspectClass) {
janakr649d2b12018-02-13 15:37:30 -0800397 return AspectKey.createAspectKey(
janakrb9788e12018-01-24 11:46:25 -0800398 targetLabel,
cpeysera8a61ee2018-01-26 09:20:37 -0800399 baseConfiguredTargetKey,
janakrb9788e12018-01-24 11:46:25 -0800400 ImmutableList.of(),
401 new AspectDescriptor(aspectClass, AspectParameters.EMPTY),
cpeysera8a61ee2018-01-26 09:20:37 -0800402 aspectConfigurationKey,
403 isAspectConfigurationHost());
404 }
405 }
406
407 /** A {@link SkylarkAspectLoadingKey} for an aspect in the host configuration. */
408 private static class HostSkylarkAspectLoadingKey extends SkylarkAspectLoadingKey {
409
410 private HostSkylarkAspectLoadingKey(
411 Label targetLabel,
412 Key aspectConfigurationKey,
413 ConfiguredTargetKey baseConfiguredTargetKey,
414 SkylarkImport skylarkImport,
415 String skylarkFunctionName) {
416 super(
417 targetLabel,
418 aspectConfigurationKey,
419 baseConfiguredTargetKey,
420 skylarkImport,
421 skylarkFunctionName);
422 }
423
424 @Override
425 protected boolean isAspectConfigurationHost() {
426 return true;
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000427 }
428 }
429
janakrdb4dec22017-03-28 22:39:35 +0000430 // These variables are only non-final because they may be clear()ed to save memory. They are null
janakr931d2852017-12-15 13:48:29 -0800431 // only after they are cleared except for transitivePackagesForPackageRootResolution.
janakrdb4dec22017-03-28 22:39:35 +0000432 @Nullable private Label label;
433 @Nullable private Aspect aspect;
434 @Nullable private Location location;
435 @Nullable private AspectKey key;
436 @Nullable private ConfiguredAspect configuredAspect;
janakr931d2852017-12-15 13:48:29 -0800437 // May be null either after clearing or because transitive packages are not tracked.
janakr907ca4a2019-01-25 13:37:24 -0800438 @Nullable private transient NestedSet<Package> transitivePackagesForPackageRootResolution;
janakr9bad8402018-03-23 15:32:37 -0700439
Dmitry Lomove2033b12015-08-19 16:57:49 +0000440 public AspectValue(
Dmitry Lomovb487ac62015-11-09 13:09:12 +0000441 AspectKey key,
Dmitry Lomov2eb8bdd2016-04-06 08:47:30 +0000442 Aspect aspect,
Dmitry Lomovb487ac62015-11-09 13:09:12 +0000443 Label label,
444 Location location,
445 ConfiguredAspect configuredAspect,
janakra81bb952019-01-28 17:30:06 -0800446 NestedSet<Package> transitivePackagesForPackageRootResolution,
447 BigInteger nonceVersion) {
448 super(configuredAspect.getActions(), configuredAspect.getGeneratingActionIndex(), nonceVersion);
janakrdb4dec22017-03-28 22:39:35 +0000449 this.label = Preconditions.checkNotNull(label, actions);
450 this.aspect = Preconditions.checkNotNull(aspect, label);
451 this.location = Preconditions.checkNotNull(location, label);
452 this.key = Preconditions.checkNotNull(key, label);
453 this.configuredAspect = Preconditions.checkNotNull(configuredAspect, label);
janakr931d2852017-12-15 13:48:29 -0800454 this.transitivePackagesForPackageRootResolution = transitivePackagesForPackageRootResolution;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100455 }
456
Dmitry Lomovb487ac62015-11-09 13:09:12 +0000457 public ConfiguredAspect getConfiguredAspect() {
janakrdb4dec22017-03-28 22:39:35 +0000458 return Preconditions.checkNotNull(configuredAspect);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100459 }
460
Dmitry Lomove2033b12015-08-19 16:57:49 +0000461 public Label getLabel() {
janakrdb4dec22017-03-28 22:39:35 +0000462 return Preconditions.checkNotNull(label);
Dmitry Lomove2033b12015-08-19 16:57:49 +0000463 }
464
465 public Location getLocation() {
janakrdb4dec22017-03-28 22:39:35 +0000466 return Preconditions.checkNotNull(location);
Dmitry Lomove2033b12015-08-19 16:57:49 +0000467 }
468
469 public AspectKey getKey() {
janakrdb4dec22017-03-28 22:39:35 +0000470 return Preconditions.checkNotNull(key);
Dmitry Lomove2033b12015-08-19 16:57:49 +0000471 }
472
Dmitry Lomov2eb8bdd2016-04-06 08:47:30 +0000473 public Aspect getAspect() {
janakrdb4dec22017-03-28 22:39:35 +0000474 return Preconditions.checkNotNull(aspect);
Dmitry Lomov2eb8bdd2016-04-06 08:47:30 +0000475 }
476
janakrdb4dec22017-03-28 22:39:35 +0000477 void clear(boolean clearEverything) {
478 Preconditions.checkNotNull(label, this);
479 Preconditions.checkNotNull(aspect, this);
480 Preconditions.checkNotNull(location, this);
481 Preconditions.checkNotNull(key, this);
482 Preconditions.checkNotNull(configuredAspect, this);
janakrdb4dec22017-03-28 22:39:35 +0000483 if (clearEverything) {
484 label = null;
485 aspect = null;
486 location = null;
487 key = null;
488 configuredAspect = null;
489 }
janakr931d2852017-12-15 13:48:29 -0800490 transitivePackagesForPackageRootResolution = null;
Marian Loburc62faba2015-09-09 10:08:06 +0000491 }
492
janakra81bb952019-01-28 17:30:06 -0800493 @Override
494 public final boolean mustBeReferenceComparedOnRecomputation() {
495 return true;
496 }
497
janakr931d2852017-12-15 13:48:29 -0800498 /**
499 * Returns the set of packages transitively loaded by this value. Must only be used for
500 * constructing the package -> source root map needed for some builds. If the caller has not
501 * specified that this map needs to be constructed (via the constructor argument in {@link
502 * AspectFunction#AspectFunction}), calling this will crash.
503 */
504 public NestedSet<Package> getTransitivePackagesForPackageRootResolution() {
505 return Preconditions.checkNotNull(transitivePackagesForPackageRootResolution);
janakrdb4dec22017-03-28 22:39:35 +0000506 }
507
janakrb9788e12018-01-24 11:46:25 -0800508 @Override
509 public String toString() {
510 return MoreObjects.toStringHelper(this)
511 .add("label", label)
512 .add("key", key)
513 .add("location", location)
514 .add("aspect", aspect)
515 .add("configuredAspect", configuredAspect)
516 .toString();
517 }
janakrdb4dec22017-03-28 22:39:35 +0000518
Dmitry Lomove851fe22017-02-14 23:11:23 +0000519 public static AspectKey createAspectKey(
janakrdb4dec22017-03-28 22:39:35 +0000520 Label label,
521 BuildConfiguration baseConfiguration,
522 ImmutableList<AspectKey> baseKeys,
523 AspectDescriptor aspectDescriptor,
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000524 BuildConfiguration aspectConfiguration) {
cpeysera8a61ee2018-01-26 09:20:37 -0800525 KeyAndHost aspectKeyAndHost = ConfiguredTargetKey.keyFromConfiguration(aspectConfiguration);
janakr649d2b12018-02-13 15:37:30 -0800526 return AspectKey.createAspectKey(
janakrb9788e12018-01-24 11:46:25 -0800527 label,
528 ConfiguredTargetKey.of(label, baseConfiguration),
Dmitry Lomove851fe22017-02-14 23:11:23 +0000529 baseKeys,
530 aspectDescriptor,
cpeysera8a61ee2018-01-26 09:20:37 -0800531 aspectKeyAndHost.key,
532 aspectKeyAndHost.isHost);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100533 }
Dmitry Lomove2033b12015-08-19 16:57:49 +0000534
janakr573807d2018-01-11 14:02:35 -0800535 private static final Interner<AspectKey> aspectKeyInterner = BlazeInterners.newWeakInterner();
Dmitry Lomove2033b12015-08-19 16:57:49 +0000536
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000537 public static AspectKey createAspectKey(
Michael Staib04f6f242016-03-01 15:40:29 +0000538 Label label,
janakrb9788e12018-01-24 11:46:25 -0800539 BuildConfiguration baseConfiguration,
540 AspectDescriptor aspectDescriptor,
541 BuildConfiguration aspectConfiguration) {
cpeysera8a61ee2018-01-26 09:20:37 -0800542 KeyAndHost aspectKeyAndHost = ConfiguredTargetKey.keyFromConfiguration(aspectConfiguration);
janakr649d2b12018-02-13 15:37:30 -0800543 return AspectKey.createAspectKey(
janakrb9788e12018-01-24 11:46:25 -0800544 label,
545 ConfiguredTargetKey.of(label, baseConfiguration),
546 ImmutableList.of(),
547 aspectDescriptor,
cpeysera8a61ee2018-01-26 09:20:37 -0800548 aspectKeyAndHost.key,
549 aspectKeyAndHost.isHost);
janakrb9788e12018-01-24 11:46:25 -0800550 }
551
janakr573807d2018-01-11 14:02:35 -0800552 private static final Interner<SkylarkAspectLoadingKey> skylarkAspectKeyInterner =
553 BlazeInterners.newWeakInterner();
Dmitry Lomovca9bfa42016-11-15 13:22:36 +0000554
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000555 public static SkylarkAspectLoadingKey createSkylarkAspectKey(
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000556 Label targetLabel,
Michael Staib04f6f242016-03-01 15:40:29 +0000557 BuildConfiguration aspectConfiguration,
Dmitry Lomov0b832ce2015-10-20 10:03:14 +0000558 BuildConfiguration targetConfiguration,
Dmitry Lomov51aafc12016-11-18 14:02:54 +0000559 SkylarkImport skylarkImport,
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +0000560 String skylarkExportName) {
cpeysera8a61ee2018-01-26 09:20:37 -0800561 KeyAndHost keyAndHost = ConfiguredTargetKey.keyFromConfiguration(aspectConfiguration);
562 SkylarkAspectLoadingKey key =
563 keyAndHost.isHost
564 ? new HostSkylarkAspectLoadingKey(
565 targetLabel,
566 keyAndHost.key,
567 ConfiguredTargetKey.of(targetLabel, targetConfiguration),
568 skylarkImport,
569 skylarkExportName)
570 : new SkylarkAspectLoadingKey(
571 targetLabel,
572 keyAndHost.key,
573 ConfiguredTargetKey.of(targetLabel, targetConfiguration),
574 skylarkImport,
575 skylarkExportName);
576
577 return skylarkAspectKeyInterner.intern(key);
Dmitry Lomove2033b12015-08-19 16:57:49 +0000578 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100579}