Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [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 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
janakr | 171a7eb | 2018-03-26 09:26:53 -0700 | [diff] [blame] | 16 | import com.google.common.annotations.VisibleForTesting; |
janakr | 29bd80d | 2017-12-28 12:59:48 -0800 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableSortedSet; |
| 18 | import com.google.common.collect.Interner; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 20 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
janakr | a39a562 | 2018-01-24 08:43:35 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.analysis.config.FragmentClassSet; |
janakr | 29bd80d | 2017-12-28 12:59:48 -0800 | [diff] [blame] | 22 | import com.google.devtools.build.lib.concurrent.BlazeInterners; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
shahan | 9694cae | 2018-01-10 18:02:30 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
janakr | 129c3e2 | 2018-07-27 10:50:45 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; |
janakr | 34e1b3c | 2017-12-20 14:27:55 -0800 | [diff] [blame] | 26 | import com.google.devtools.build.skyframe.SkyFunctionName; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.skyframe.SkyKey; |
| 28 | import com.google.devtools.build.skyframe.SkyValue; |
schmitt | 3e77024 | 2019-04-22 14:12:24 -0700 | [diff] [blame] | 29 | import com.google.devtools.common.options.OptionsParsingException; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 30 | import java.io.Serializable; |
| 31 | import java.util.Objects; |
| 32 | import java.util.Set; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 33 | |
shahan | 9694cae | 2018-01-10 18:02:30 -0800 | [diff] [blame] | 34 | /** A Skyframe value representing a {@link BuildConfiguration}. */ |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 35 | // TODO(bazel-team): mark this immutable when BuildConfiguration is immutable. |
| 36 | // @Immutable |
shahan | fae34b9 | 2018-02-13 10:08:47 -0800 | [diff] [blame] | 37 | @AutoCodec |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 38 | @ThreadSafe |
| 39 | public class BuildConfigurationValue implements SkyValue { |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 40 | private final BuildConfiguration configuration; |
| 41 | |
| 42 | BuildConfigurationValue(BuildConfiguration configuration) { |
| 43 | this.configuration = configuration; |
| 44 | } |
| 45 | |
| 46 | public BuildConfiguration getConfiguration() { |
| 47 | return configuration; |
| 48 | } |
| 49 | |
| 50 | /** |
schmitt | 3e77024 | 2019-04-22 14:12:24 -0700 | [diff] [blame] | 51 | * Creates a new configuration key based on the given diff, after applying a platform mapping |
| 52 | * transformation. |
| 53 | * |
| 54 | * @param platformMappingValue sky value that can transform a configuration key based on a |
| 55 | * platform mapping |
| 56 | * @param defaultBuildOptions set of native build options without modifications based on parsing |
| 57 | * flags |
| 58 | * @param fragments set of options fragments this configuration should cover |
| 59 | * @param optionsDiff diff between the default options and the desired configuration |
| 60 | * @throws OptionsParsingException if the platform mapping cannot be parsed |
| 61 | */ |
| 62 | public static Key keyWithPlatformMapping( |
| 63 | PlatformMappingValue platformMappingValue, |
| 64 | BuildOptions defaultBuildOptions, |
| 65 | FragmentClassSet fragments, |
| 66 | BuildOptions.OptionsDiffForReconstruction optionsDiff) |
| 67 | throws OptionsParsingException { |
| 68 | return platformMappingValue.map( |
| 69 | keyWithoutPlatformMapping(fragments, optionsDiff), defaultBuildOptions); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Creates a new configuration key based on the given diff, after applying a platform mapping |
| 74 | * transformation. |
| 75 | * |
| 76 | * @param platformMappingValue sky value that can transform a configuration key based on a |
| 77 | * platform mapping |
| 78 | * @param defaultBuildOptions set of native build options without modifications based on parsing |
| 79 | * flags |
| 80 | * @param fragments set of options fragments this configuration should cover |
| 81 | * @param optionsDiff diff between the default options and the desired configuration |
| 82 | * @throws OptionsParsingException if the platform mapping cannot be parsed |
| 83 | */ |
| 84 | public static Key keyWithPlatformMapping( |
| 85 | PlatformMappingValue platformMappingValue, |
| 86 | BuildOptions defaultBuildOptions, |
| 87 | Set<Class<? extends BuildConfiguration.Fragment>> fragments, |
| 88 | BuildOptions.OptionsDiffForReconstruction optionsDiff) |
| 89 | throws OptionsParsingException { |
| 90 | return platformMappingValue.map( |
| 91 | keyWithoutPlatformMapping(fragments, optionsDiff), defaultBuildOptions); |
| 92 | } |
| 93 | |
| 94 | /** |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 95 | * Returns the key for a requested configuration. |
| 96 | * |
schmitt | 3e77024 | 2019-04-22 14:12:24 -0700 | [diff] [blame] | 97 | * <p>Callers are responsible for applying the platform mapping or ascertaining that a platform |
| 98 | * mapping is not required. |
| 99 | * |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 100 | * @param fragments the fragments the configuration should contain |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 101 | * @param optionsDiff the {@link BuildOptions.OptionsDiffForReconstruction} object the {@link |
| 102 | * BuildOptions} should be rebuilt from |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 103 | */ |
| 104 | @ThreadSafe |
schmitt | 3e77024 | 2019-04-22 14:12:24 -0700 | [diff] [blame] | 105 | static Key keyWithoutPlatformMapping( |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 106 | Set<Class<? extends BuildConfiguration.Fragment>> fragments, |
| 107 | BuildOptions.OptionsDiffForReconstruction optionsDiff) { |
janakr | 129c3e2 | 2018-07-27 10:50:45 -0700 | [diff] [blame] | 108 | return Key.create( |
janakr | a39a562 | 2018-01-24 08:43:35 -0800 | [diff] [blame] | 109 | FragmentClassSet.of( |
| 110 | ImmutableSortedSet.copyOf(BuildConfiguration.lexicalFragmentSorter, fragments)), |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 111 | optionsDiff); |
janakr | a39a562 | 2018-01-24 08:43:35 -0800 | [diff] [blame] | 112 | } |
| 113 | |
schmitt | 3e77024 | 2019-04-22 14:12:24 -0700 | [diff] [blame] | 114 | private static Key keyWithoutPlatformMapping( |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 115 | FragmentClassSet fragmentClassSet, BuildOptions.OptionsDiffForReconstruction optionsDiff) { |
| 116 | return Key.create(fragmentClassSet, optionsDiff); |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 117 | } |
| 118 | |
schmitt | 3e77024 | 2019-04-22 14:12:24 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Returns a configuration key for the given configuration. |
| 121 | * |
| 122 | * <p>Note that this key creation method does not apply a platform mapping, it is assumed that the |
| 123 | * passed configuration was created with one such and thus its key does not need to be mapped |
| 124 | * again. |
| 125 | * |
| 126 | * @param buildConfiguration configuration whose key is requested |
| 127 | */ |
janakr | 9c4fa4a | 2018-03-28 11:37:34 -0700 | [diff] [blame] | 128 | public static Key key(BuildConfiguration buildConfiguration) { |
schmitt | 3e77024 | 2019-04-22 14:12:24 -0700 | [diff] [blame] | 129 | return keyWithoutPlatformMapping( |
| 130 | buildConfiguration.fragmentClasses(), buildConfiguration.getBuildOptionsDiff()); |
janakr | 9c4fa4a | 2018-03-28 11:37:34 -0700 | [diff] [blame] | 131 | } |
| 132 | |
janakr | 269d9bd | 2018-01-24 10:15:56 -0800 | [diff] [blame] | 133 | /** {@link SkyKey} for {@link BuildConfigurationValue}. */ |
janakr | 129c3e2 | 2018-07-27 10:50:45 -0700 | [diff] [blame] | 134 | @AutoCodec |
janakr | 269d9bd | 2018-01-24 10:15:56 -0800 | [diff] [blame] | 135 | public static final class Key implements SkyKey, Serializable { |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 136 | private static final Interner<Key> keyInterner = BlazeInterners.newWeakInterner(); |
| 137 | |
janakr | a39a562 | 2018-01-24 08:43:35 -0800 | [diff] [blame] | 138 | private final FragmentClassSet fragments; |
janakr | 129c3e2 | 2018-07-27 10:50:45 -0700 | [diff] [blame] | 139 | final BuildOptions.OptionsDiffForReconstruction optionsDiff; |
janakr | 29bd80d | 2017-12-28 12:59:48 -0800 | [diff] [blame] | 140 | // If hashCode really is -1, we'll recompute it from scratch each time. Oh well. |
| 141 | private volatile int hashCode = -1; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 142 | |
janakr | 129c3e2 | 2018-07-27 10:50:45 -0700 | [diff] [blame] | 143 | @AutoCodec.Instantiator |
| 144 | @VisibleForSerialization |
| 145 | static Key create( |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 146 | FragmentClassSet fragments, BuildOptions.OptionsDiffForReconstruction optionsDiff) { |
| 147 | return keyInterner.intern(new Key(fragments, optionsDiff)); |
| 148 | } |
| 149 | |
janakr | 56d7852 | 2018-03-28 09:58:15 -0700 | [diff] [blame] | 150 | private Key(FragmentClassSet fragments, BuildOptions.OptionsDiffForReconstruction optionsDiff) { |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 151 | this.fragments = fragments; |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 152 | this.optionsDiff = optionsDiff; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 153 | } |
| 154 | |
janakr | 171a7eb | 2018-03-26 09:26:53 -0700 | [diff] [blame] | 155 | @VisibleForTesting |
| 156 | public ImmutableSortedSet<Class<? extends BuildConfiguration.Fragment>> getFragments() { |
janakr | a39a562 | 2018-01-24 08:43:35 -0800 | [diff] [blame] | 157 | return fragments.fragmentClasses(); |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 158 | } |
| 159 | |
janakr | 56d7852 | 2018-03-28 09:58:15 -0700 | [diff] [blame] | 160 | public BuildOptions.OptionsDiffForReconstruction getOptionsDiff() { |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 161 | return optionsDiff; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 164 | @Override |
janakr | 34e1b3c | 2017-12-20 14:27:55 -0800 | [diff] [blame] | 165 | public SkyFunctionName functionName() { |
| 166 | return SkyFunctions.BUILD_CONFIGURATION; |
| 167 | } |
| 168 | |
| 169 | @Override |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 170 | public boolean equals(Object o) { |
janakr | 29bd80d | 2017-12-28 12:59:48 -0800 | [diff] [blame] | 171 | if (this == o) { |
| 172 | return true; |
| 173 | } |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 174 | if (!(o instanceof Key)) { |
| 175 | return false; |
| 176 | } |
| 177 | Key otherConfig = (Key) o; |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 178 | return optionsDiff.equals(otherConfig.optionsDiff) |
janakr | 534191b | 2018-01-11 12:24:49 -0800 | [diff] [blame] | 179 | && Objects.equals(fragments, otherConfig.fragments); |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public int hashCode() { |
janakr | 29bd80d | 2017-12-28 12:59:48 -0800 | [diff] [blame] | 184 | if (hashCode == -1) { |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 185 | hashCode = Objects.hash(fragments, optionsDiff); |
janakr | 29bd80d | 2017-12-28 12:59:48 -0800 | [diff] [blame] | 186 | } |
| 187 | return hashCode; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 188 | } |
janakr | 192c890 | 2018-01-19 22:29:42 -0800 | [diff] [blame] | 189 | |
mjhalupka | 6febc73 | 2018-04-11 11:23:44 -0700 | [diff] [blame] | 190 | @Override |
| 191 | public String toString() { |
shahan | a961266 | 2018-05-17 07:54:17 -0700 | [diff] [blame] | 192 | return "BuildConfigurationValue.Key[" + optionsDiff.getChecksum() + "]"; |
mjhalupka | 6febc73 | 2018-04-11 11:23:44 -0700 | [diff] [blame] | 193 | } |
janakr | 5217712 | 2018-05-14 12:06:36 -0700 | [diff] [blame] | 194 | } |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 195 | } |