John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 1 | // Copyright 2017 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. |
| 14 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | import static com.google.devtools.build.skyframe.EvaluationResultSubjectFactory.assertThatEvaluationResult; |
John Cater | 52eb53b | 2019-11-27 07:38:24 -0800 | [diff] [blame^] | 19 | import static org.mockito.Mockito.when; |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 20 | |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableCollection; |
John Cater | 96b4081 | 2017-07-18 18:08:03 +0200 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableMap; |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 24 | import com.google.common.testing.EqualsTester; |
janakr | 0175ce3 | 2018-02-26 15:54:57 -0800 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.Actions.GeneratingActions; |
janakr | e54491e | 2018-07-11 16:29:13 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.util.InjectedActionLookupKey; |
janakr | 9565666 | 2018-02-14 17:14:51 -0800 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 28 | import com.google.devtools.build.lib.analysis.TransitiveInfoProvider; |
John Cater | 96b4081 | 2017-07-18 18:08:03 +0200 | [diff] [blame] | 29 | import com.google.devtools.build.lib.analysis.platform.PlatformInfo; |
| 30 | import com.google.devtools.build.lib.cmdline.Label; |
janakr | 9565666 | 2018-02-14 17:14:51 -0800 | [diff] [blame] | 31 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 32 | import com.google.devtools.build.lib.collect.nestedset.Order; |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 33 | import com.google.devtools.build.lib.events.Location; |
| 34 | import com.google.devtools.build.lib.packages.InfoInterface; |
| 35 | import com.google.devtools.build.lib.packages.NativeProvider; |
| 36 | import com.google.devtools.build.lib.packages.Provider; |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 37 | import com.google.devtools.build.lib.rules.platform.ToolchainTestCase; |
janakr | e54491e | 2018-07-11 16:29:13 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 39 | import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils; |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 40 | import com.google.devtools.build.lib.syntax.Printer; |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 41 | import com.google.devtools.build.skyframe.EvaluationResult; |
| 42 | import com.google.devtools.build.skyframe.SkyKey; |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 43 | import javax.annotation.Nullable; |
John Cater | 52eb53b | 2019-11-27 07:38:24 -0800 | [diff] [blame^] | 44 | import org.junit.Before; |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.junit.runners.JUnit4; |
janakr | e54491e | 2018-07-11 16:29:13 -0700 | [diff] [blame] | 48 | import org.mockito.Mockito; |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 49 | |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Tests for {@link SingleToolchainResolutionValue} and {@link SingleToolchainResolutionFunction}. |
| 52 | */ |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 53 | @RunWith(JUnit4.class) |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 54 | public class SingleToolchainResolutionFunctionTest extends ToolchainTestCase { |
janakr | e54491e | 2018-07-11 16:29:13 -0700 | [diff] [blame] | 55 | @AutoCodec @AutoCodec.VisibleForSerialization |
| 56 | static final ConfiguredTargetKey LINUX_CTKEY = Mockito.mock(ConfiguredTargetKey.class); |
| 57 | |
| 58 | @AutoCodec @AutoCodec.VisibleForSerialization |
| 59 | static final ConfiguredTargetKey MAC_CTKEY = Mockito.mock(ConfiguredTargetKey.class); |
| 60 | |
John Cater | 52eb53b | 2019-11-27 07:38:24 -0800 | [diff] [blame^] | 61 | @Before |
| 62 | public void setUpKeys() { |
| 63 | when(LINUX_CTKEY.functionName()).thenReturn(InjectedActionLookupKey.INJECTED_ACTION_LOOKUP); |
| 64 | when(LINUX_CTKEY.getLabel()).thenReturn(Label.parseAbsoluteUnchecked("//platforms:linux")); |
| 65 | when(MAC_CTKEY.functionName()).thenReturn(InjectedActionLookupKey.INJECTED_ACTION_LOOKUP); |
| 66 | when(MAC_CTKEY.getLabel()).thenReturn(Label.parseAbsoluteUnchecked("//platforms:mac")); |
janakr | e54491e | 2018-07-11 16:29:13 -0700 | [diff] [blame] | 67 | } |
janakr | 9565666 | 2018-02-14 17:14:51 -0800 | [diff] [blame] | 68 | |
| 69 | private static ConfiguredTargetValue createConfiguredTargetValue( |
| 70 | ConfiguredTarget configuredTarget) { |
cparsons | e2d200f | 2018-03-06 16:15:11 -0800 | [diff] [blame] | 71 | return new NonRuleConfiguredTargetValue( |
janakr | a81bb95 | 2019-01-28 17:30:06 -0800 | [diff] [blame] | 72 | configuredTarget, |
| 73 | GeneratingActions.EMPTY, |
| 74 | NestedSetBuilder.emptySet(Order.STABLE_ORDER), |
| 75 | /*nonceVersion=*/ null); |
janakr | 9565666 | 2018-02-14 17:14:51 -0800 | [diff] [blame] | 76 | } |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 77 | |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 78 | private EvaluationResult<SingleToolchainResolutionValue> invokeToolchainResolution(SkyKey key) |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 79 | throws InterruptedException { |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 80 | ConfiguredTarget mockLinuxTarget = new SerializableConfiguredTarget(linuxPlatform); |
| 81 | ConfiguredTarget mockMacTarget = new SerializableConfiguredTarget(macPlatform); |
janakr | 9565666 | 2018-02-14 17:14:51 -0800 | [diff] [blame] | 82 | getSkyframeExecutor() |
| 83 | .getDifferencerForTesting() |
| 84 | .inject( |
| 85 | ImmutableMap.of( |
| 86 | LINUX_CTKEY, |
| 87 | createConfiguredTargetValue(mockLinuxTarget), |
| 88 | MAC_CTKEY, |
| 89 | createConfiguredTargetValue(mockMacTarget))); |
John Cater | 96b4081 | 2017-07-18 18:08:03 +0200 | [diff] [blame] | 90 | |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 91 | try { |
| 92 | getSkyframeExecutor().getSkyframeBuildView().enableAnalysis(true); |
| 93 | return SkyframeExecutorTestUtils.evaluate( |
| 94 | getSkyframeExecutor(), key, /*keepGoing=*/ false, reporter); |
| 95 | } finally { |
| 96 | getSkyframeExecutor().getSkyframeBuildView().enableAnalysis(false); |
| 97 | } |
| 98 | } |
| 99 | |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 100 | @Test |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 101 | public void testResolution_singleExecutionPlatform() throws Exception { |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 102 | SkyKey key = |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 103 | SingleToolchainResolutionValue.key( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 104 | targetConfigKey, testToolchainTypeLabel, LINUX_CTKEY, ImmutableList.of(MAC_CTKEY)); |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 105 | EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key); |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 106 | |
| 107 | assertThatEvaluationResult(result).hasNoError(); |
| 108 | |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 109 | SingleToolchainResolutionValue singleToolchainResolutionValue = result.get(key); |
| 110 | assertThat(singleToolchainResolutionValue.availableToolchainLabels()) |
John Cater | e959e44 | 2018-02-28 07:52:21 -0800 | [diff] [blame] | 111 | .containsExactly(MAC_CTKEY, makeLabel("//toolchain:toolchain_2_impl")); |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testResolution_multipleExecutionPlatforms() throws Exception { |
| 116 | addToolchain( |
| 117 | "extra", |
| 118 | "extra_toolchain", |
| 119 | ImmutableList.of("//constraints:linux"), |
| 120 | ImmutableList.of("//constraints:linux"), |
| 121 | "baz"); |
| 122 | rewriteWorkspace( |
| 123 | "register_toolchains(", |
| 124 | "'//toolchain:toolchain_1',", |
| 125 | "'//toolchain:toolchain_2',", |
| 126 | "'//extra:extra_toolchain')"); |
| 127 | |
| 128 | SkyKey key = |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 129 | SingleToolchainResolutionValue.key( |
janakr | 40d0077 | 2018-02-14 14:08:45 -0800 | [diff] [blame] | 130 | targetConfigKey, |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 131 | testToolchainTypeLabel, |
janakr | 9565666 | 2018-02-14 17:14:51 -0800 | [diff] [blame] | 132 | LINUX_CTKEY, |
| 133 | ImmutableList.of(LINUX_CTKEY, MAC_CTKEY)); |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 134 | EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key); |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 135 | |
| 136 | assertThatEvaluationResult(result).hasNoError(); |
| 137 | |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 138 | SingleToolchainResolutionValue singleToolchainResolutionValue = result.get(key); |
| 139 | assertThat(singleToolchainResolutionValue.availableToolchainLabels()) |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 140 | .containsExactly( |
John Cater | e959e44 | 2018-02-28 07:52:21 -0800 | [diff] [blame] | 141 | LINUX_CTKEY, |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 142 | makeLabel("//extra:extra_toolchain_impl"), |
John Cater | e959e44 | 2018-02-28 07:52:21 -0800 | [diff] [blame] | 143 | MAC_CTKEY, |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 144 | makeLabel("//toolchain:toolchain_2_impl")); |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testResolution_noneFound() throws Exception { |
| 149 | // Clear the toolchains. |
| 150 | rewriteWorkspace(); |
| 151 | |
| 152 | SkyKey key = |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 153 | SingleToolchainResolutionValue.key( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 154 | targetConfigKey, testToolchainTypeLabel, LINUX_CTKEY, ImmutableList.of(MAC_CTKEY)); |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 155 | EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key); |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 156 | |
| 157 | assertThatEvaluationResult(result) |
| 158 | .hasErrorEntryForKeyThat(key) |
| 159 | .hasExceptionThat() |
| 160 | .hasMessageThat() |
| 161 | .contains("no matching toolchain found for //toolchain:test_toolchain"); |
| 162 | } |
| 163 | |
| 164 | @Test |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 165 | public void testToolchainResolutionValue_equalsAndHashCode() { |
| 166 | new EqualsTester() |
| 167 | .addEqualityGroup( |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 168 | SingleToolchainResolutionValue.create( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 169 | testToolchainType, |
John Cater | e959e44 | 2018-02-28 07:52:21 -0800 | [diff] [blame] | 170 | ImmutableMap.of(LINUX_CTKEY, makeLabel("//test:toolchain_impl_1"))), |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 171 | SingleToolchainResolutionValue.create( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 172 | testToolchainType, |
John Cater | e959e44 | 2018-02-28 07:52:21 -0800 | [diff] [blame] | 173 | ImmutableMap.of(LINUX_CTKEY, makeLabel("//test:toolchain_impl_1")))) |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 174 | // Different execution platform, same label. |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 175 | .addEqualityGroup( |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 176 | SingleToolchainResolutionValue.create( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 177 | testToolchainType, |
John Cater | e959e44 | 2018-02-28 07:52:21 -0800 | [diff] [blame] | 178 | ImmutableMap.of(MAC_CTKEY, makeLabel("//test:toolchain_impl_1")))) |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 179 | // Same execution platform, different label. |
| 180 | .addEqualityGroup( |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 181 | SingleToolchainResolutionValue.create( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 182 | testToolchainType, |
John Cater | e959e44 | 2018-02-28 07:52:21 -0800 | [diff] [blame] | 183 | ImmutableMap.of(LINUX_CTKEY, makeLabel("//test:toolchain_impl_2")))) |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 184 | // Different execution platform, different label. |
| 185 | .addEqualityGroup( |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 186 | SingleToolchainResolutionValue.create( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 187 | testToolchainType, |
John Cater | e959e44 | 2018-02-28 07:52:21 -0800 | [diff] [blame] | 188 | ImmutableMap.of(MAC_CTKEY, makeLabel("//test:toolchain_impl_2")))) |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 189 | // Multiple execution platforms. |
| 190 | .addEqualityGroup( |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 191 | SingleToolchainResolutionValue.create( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 192 | testToolchainType, |
John Cater | e959e44 | 2018-02-28 07:52:21 -0800 | [diff] [blame] | 193 | ImmutableMap.<ConfiguredTargetKey, Label>builder() |
| 194 | .put(LINUX_CTKEY, makeLabel("//test:toolchain_impl_1")) |
| 195 | .put(MAC_CTKEY, makeLabel("//test:toolchain_impl_1")) |
Googler | 39e9b45 | 2018-08-08 07:38:28 -0700 | [diff] [blame] | 196 | .build())) |
| 197 | .testEquals(); |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 198 | } |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 199 | |
| 200 | /** Use custom class instead of mock to make sure that the dynamic codecs lookup is correct. */ |
| 201 | class SerializableConfiguredTarget implements ConfiguredTarget { |
| 202 | |
| 203 | private final PlatformInfo platform; |
| 204 | |
| 205 | SerializableConfiguredTarget(PlatformInfo platform) { |
| 206 | this.platform = platform; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public ImmutableCollection<String> getFieldNames() { |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | @Nullable |
| 215 | @Override |
| 216 | public String getErrorMessageForUnknownField(String field) { |
| 217 | return null; |
| 218 | } |
| 219 | |
| 220 | @Nullable |
| 221 | @Override |
| 222 | public Object getValue(String name) { |
| 223 | return null; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public Label getLabel() { |
| 228 | return null; |
| 229 | } |
| 230 | |
| 231 | @Nullable |
| 232 | @Override |
| 233 | public BuildConfigurationValue.Key getConfigurationKey() { |
| 234 | return null; |
| 235 | } |
| 236 | |
| 237 | @Nullable |
| 238 | @Override |
| 239 | public <P extends TransitiveInfoProvider> P getProvider(Class<P> provider) { |
| 240 | return null; |
| 241 | } |
| 242 | |
| 243 | @Nullable |
| 244 | @Override |
| 245 | public Object get(String providerKey) { |
| 246 | return null; |
| 247 | } |
| 248 | |
| 249 | @SuppressWarnings("unchecked") |
| 250 | @Override |
| 251 | public <T extends InfoInterface> T get(NativeProvider<T> provider) { |
| 252 | if (PlatformInfo.PROVIDER.equals(provider)) { |
| 253 | return (T) this.platform; |
| 254 | } |
| 255 | return provider.getValueClass().cast(get(provider.getKey())); |
| 256 | } |
| 257 | |
| 258 | @Nullable |
| 259 | @Override |
| 260 | public InfoInterface get(Provider.Key providerKey) { |
| 261 | |
| 262 | return null; |
| 263 | } |
| 264 | |
| 265 | @Override |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 266 | public void repr(Printer printer) {} |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 267 | |
| 268 | @Override |
Googler | 4ff2912 | 2019-09-04 21:14:38 -0700 | [diff] [blame] | 269 | public Object getIndex(Object key, Location loc) { |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 270 | return null; |
| 271 | } |
| 272 | |
| 273 | @Override |
Googler | 4ff2912 | 2019-09-04 21:14:38 -0700 | [diff] [blame] | 274 | public boolean containsKey(Object key, Location loc) { |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | } |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 278 | } |