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 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Interner; |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame^] | 19 | import com.google.devtools.build.lib.actions.ActionLookupKey; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
| 21 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.cmdline.Label; |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 23 | import com.google.devtools.build.lib.concurrent.BlazeInterners; |
janakr | 1b78769 | 2018-02-13 12:53:31 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
jcater | 0985ad7 | 2020-07-14 05:22:21 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import com.google.devtools.build.skyframe.SkyFunctionName; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import java.util.Objects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import javax.annotation.Nullable; |
| 29 | |
| 30 | /** |
janakr | 3863b53 | 2018-01-11 15:49:42 -0800 | [diff] [blame] | 31 | * A (Label, Configuration key) pair. Note that this pair may be used to look up the generating |
| 32 | * action of an artifact. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | */ |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame^] | 34 | // TODO(janakr): Intern deserialized instances. |
| 35 | public class ConfiguredTargetKey implements ActionLookupKey { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | private final Label label; |
janakr | 192c890 | 2018-01-19 22:29:42 -0800 | [diff] [blame] | 37 | @Nullable private final BuildConfigurationValue.Key configurationKey; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 39 | private transient int hashCode; |
| 40 | |
jcater | 8e08aa3f | 2020-05-29 11:27:59 -0700 | [diff] [blame] | 41 | ConfiguredTargetKey(Label label, @Nullable BuildConfigurationValue.Key configurationKey) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | this.label = Preconditions.checkNotNull(label); |
janakr | 3863b53 | 2018-01-11 15:49:42 -0800 | [diff] [blame] | 43 | this.configurationKey = configurationKey; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | @Override |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 47 | public final Label getLabel() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 48 | return label; |
| 49 | } |
| 50 | |
| 51 | @Override |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 52 | public final SkyFunctionName functionName() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | return SkyFunctions.CONFIGURED_TARGET; |
| 54 | } |
| 55 | |
| 56 | @Nullable |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 57 | final BuildConfigurationValue.Key getConfigurationKey() { |
janakr | 3863b53 | 2018-01-11 15:49:42 -0800 | [diff] [blame] | 58 | return configurationKey; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | } |
| 60 | |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 61 | @Nullable |
| 62 | ToolchainContextKey getToolchainContextKey() { |
| 63 | return null; |
| 64 | } |
| 65 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | @Override |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 67 | public final int hashCode() { |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 68 | // We use the hash code caching strategy employed by java.lang.String. There are three subtle |
| 69 | // things going on here: |
| 70 | // |
| 71 | // (1) We use a value of 0 to indicate that the hash code hasn't been computed and cached yet. |
| 72 | // Yes, this means that if the hash code is really 0 then we will "recompute" it each time. But |
| 73 | // this isn't a problem in practice since a hash code of 0 should be rare. |
| 74 | // |
| 75 | // (2) Since we have no synchronization, multiple threads can race here thinking there are the |
| 76 | // first one to compute and cache the hash code. |
| 77 | // |
| 78 | // (3) Moreover, since 'hashCode' is non-volatile, the cached hash code value written from one |
| 79 | // thread may not be visible by another. |
| 80 | // |
| 81 | // All three of these issues are benign from a correctness perspective; in the end we have no |
| 82 | // overhead from synchronization, at the cost of potentially computing the hash code more than |
| 83 | // once. |
| 84 | int h = hashCode; |
| 85 | if (h == 0) { |
| 86 | h = computeHashCode(); |
| 87 | hashCode = h; |
| 88 | } |
| 89 | return h; |
| 90 | } |
| 91 | |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame^] | 92 | private int computeHashCode() { |
janakr | 3863b53 | 2018-01-11 15:49:42 -0800 | [diff] [blame] | 93 | int configVal = configurationKey == null ? 79 : configurationKey.hashCode(); |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 94 | int toolchainContextVal = |
| 95 | getToolchainContextKey() == null ? 47 : getToolchainContextKey().hashCode(); |
| 96 | return 31 * label.hashCode() + configVal + toolchainContextVal; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 100 | public final boolean equals(Object obj) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | if (this == obj) { |
| 102 | return true; |
| 103 | } |
| 104 | if (obj == null) { |
| 105 | return false; |
| 106 | } |
| 107 | if (!(obj instanceof ConfiguredTargetKey)) { |
| 108 | return false; |
| 109 | } |
| 110 | ConfiguredTargetKey other = (ConfiguredTargetKey) obj; |
jcater | 6be6316 | 2020-05-28 09:29:22 -0700 | [diff] [blame] | 111 | return Objects.equals(label, other.label) |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 112 | && Objects.equals(configurationKey, other.configurationKey) |
| 113 | && Objects.equals(getToolchainContextKey(), other.getToolchainContextKey()); |
janakr | 3863b53 | 2018-01-11 15:49:42 -0800 | [diff] [blame] | 114 | } |
| 115 | |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 116 | public final String prettyPrint() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 117 | if (label == null) { |
| 118 | return "null"; |
| 119 | } |
jcater | 6be6316 | 2020-05-28 09:29:22 -0700 | [diff] [blame] | 120 | return label.toString(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 124 | public final String toString() { |
| 125 | if (getToolchainContextKey() != null) { |
| 126 | return String.format("%s %s %s", label, configurationKey, getToolchainContextKey()); |
| 127 | } |
jcater | 6be6316 | 2020-05-28 09:29:22 -0700 | [diff] [blame] | 128 | return String.format("%s %s", label, configurationKey); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | } |
jcater | 3eb2d25 | 2020-05-28 10:39:12 -0700 | [diff] [blame] | 130 | |
jcater | 0985ad7 | 2020-07-14 05:22:21 -0700 | [diff] [blame] | 131 | @AutoCodec |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 132 | static class ConfiguredTargetKeyWithToolchainContext extends ConfiguredTargetKey { |
| 133 | private final ToolchainContextKey toolchainContextKey; |
| 134 | |
jcater | 0985ad7 | 2020-07-14 05:22:21 -0700 | [diff] [blame] | 135 | @VisibleForSerialization |
| 136 | ConfiguredTargetKeyWithToolchainContext( |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 137 | Label label, |
| 138 | @Nullable BuildConfigurationValue.Key configurationKey, |
| 139 | ToolchainContextKey toolchainContextKey) { |
| 140 | super(label, configurationKey); |
| 141 | this.toolchainContextKey = toolchainContextKey; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | @Nullable |
| 146 | final ToolchainContextKey getToolchainContextKey() { |
| 147 | return toolchainContextKey; |
| 148 | } |
| 149 | } |
| 150 | |
jcater | 3eb2d25 | 2020-05-28 10:39:12 -0700 | [diff] [blame] | 151 | /** Returns a new {@link Builder} to create instances of {@link ConfiguredTargetKey}. */ |
| 152 | public static Builder builder() { |
| 153 | return new Builder(); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Caches so that the number of ConfiguredTargetKey instances is {@code O(configured targets)} and |
| 158 | * not {@code O(edges between configured targets)}. |
| 159 | */ |
| 160 | private static final Interner<ConfiguredTargetKey> interner = BlazeInterners.newWeakInterner(); |
| 161 | |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 162 | private static final Interner<ConfiguredTargetKeyWithToolchainContext> |
| 163 | withToolchainContextInterner = BlazeInterners.newWeakInterner(); |
| 164 | |
jcater | 3eb2d25 | 2020-05-28 10:39:12 -0700 | [diff] [blame] | 165 | /** A helper class to create instances of {@link ConfiguredTargetKey}. */ |
| 166 | public static class Builder { |
jcater | 3eb2d25 | 2020-05-28 10:39:12 -0700 | [diff] [blame] | 167 | private Label label = null; |
| 168 | private BuildConfigurationValue.Key configurationKey = null; |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 169 | private ToolchainContextKey toolchainContextKey = null; |
jcater | 3eb2d25 | 2020-05-28 10:39:12 -0700 | [diff] [blame] | 170 | |
| 171 | /** Sets the label for the target. */ |
| 172 | public Builder setLabel(Label label) { |
| 173 | this.label = label; |
| 174 | return this; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Sets the {@link ConfiguredTarget} that we want a key for. |
| 179 | * |
| 180 | * <p>This sets both the label and configurationKey data. |
| 181 | */ |
| 182 | public Builder setConfiguredTarget(ConfiguredTarget configuredTarget) { |
| 183 | setLabel(configuredTarget.getOriginalLabel()); |
| 184 | if (this.configurationKey == null) { |
| 185 | setConfigurationKey(configuredTarget.getConfigurationKey()); |
| 186 | } |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | /** Sets the {@link BuildConfiguration} for the configured target. */ |
| 191 | public Builder setConfiguration(@Nullable BuildConfiguration buildConfiguration) { |
| 192 | if (buildConfiguration == null) { |
| 193 | return setConfigurationKey(null); |
| 194 | } else { |
| 195 | return setConfigurationKey(BuildConfigurationValue.key(buildConfiguration)); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** Sets the configuration key for the configured target. */ |
| 200 | public Builder setConfigurationKey(@Nullable BuildConfigurationValue.Key configurationKey) { |
| 201 | this.configurationKey = configurationKey; |
| 202 | return this; |
| 203 | } |
| 204 | |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 205 | /** |
| 206 | * Sets the {@link ToolchainContextKey} this configured target should use for toolchain |
| 207 | * resolution. When present, this overrides the normally determined toolchain context. |
| 208 | */ |
| 209 | public Builder setToolchainContextKey(ToolchainContextKey toolchainContextKey) { |
| 210 | this.toolchainContextKey = toolchainContextKey; |
| 211 | return this; |
| 212 | } |
| 213 | |
jcater | 3eb2d25 | 2020-05-28 10:39:12 -0700 | [diff] [blame] | 214 | /** Builds a new {@link ConfiguredTargetKey} based on the supplied data. */ |
| 215 | public ConfiguredTargetKey build() { |
jcater | df1a96e | 2020-06-03 14:28:46 -0700 | [diff] [blame] | 216 | if (this.toolchainContextKey != null) { |
| 217 | return withToolchainContextInterner.intern( |
| 218 | new ConfiguredTargetKeyWithToolchainContext( |
| 219 | label, configurationKey, toolchainContextKey)); |
| 220 | } |
jcater | 3eb2d25 | 2020-05-28 10:39:12 -0700 | [diff] [blame] | 221 | return interner.intern(new ConfiguredTargetKey(label, configurationKey)); |
| 222 | } |
| 223 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 224 | } |