blob: f2a5b621f12d21353afadf985577f09b3621a2b7 [file] [log] [blame]
John Cater98375a22017-07-13 19:18:50 +02001// 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
15package com.google.devtools.build.lib.skyframe;
16
17import static com.google.common.truth.Truth.assertThat;
18import static com.google.devtools.build.skyframe.EvaluationResultSubjectFactory.assertThatEvaluationResult;
John Cater52eb53b2019-11-27 07:38:24 -080019import static org.mockito.Mockito.when;
John Cater98375a22017-07-13 19:18:50 +020020
Googler7e7675f2019-02-12 08:45:13 -080021import com.google.common.collect.ImmutableCollection;
John Cater96b40812017-07-18 18:08:03 +020022import com.google.common.collect.ImmutableList;
John Cater71dbed42018-02-06 11:04:17 -080023import com.google.common.collect.ImmutableMap;
John Cater98375a22017-07-13 19:18:50 +020024import com.google.common.testing.EqualsTester;
janakr0175ce32018-02-26 15:54:57 -080025import com.google.devtools.build.lib.actions.Actions.GeneratingActions;
janakre54491e2018-07-11 16:29:13 -070026import com.google.devtools.build.lib.actions.util.InjectedActionLookupKey;
janakr95656662018-02-14 17:14:51 -080027import com.google.devtools.build.lib.analysis.ConfiguredTarget;
Googler7e7675f2019-02-12 08:45:13 -080028import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
John Cater96b40812017-07-18 18:08:03 +020029import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
30import com.google.devtools.build.lib.cmdline.Label;
janakr95656662018-02-14 17:14:51 -080031import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
32import com.google.devtools.build.lib.collect.nestedset.Order;
Googler7e7675f2019-02-12 08:45:13 -080033import com.google.devtools.build.lib.events.Location;
34import com.google.devtools.build.lib.packages.InfoInterface;
35import com.google.devtools.build.lib.packages.NativeProvider;
36import com.google.devtools.build.lib.packages.Provider;
John Cater98375a22017-07-13 19:18:50 +020037import com.google.devtools.build.lib.rules.platform.ToolchainTestCase;
janakre54491e2018-07-11 16:29:13 -070038import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
John Cater98375a22017-07-13 19:18:50 +020039import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils;
Googler34f70582019-11-25 12:27:34 -080040import com.google.devtools.build.lib.syntax.Printer;
John Cater98375a22017-07-13 19:18:50 +020041import com.google.devtools.build.skyframe.EvaluationResult;
42import com.google.devtools.build.skyframe.SkyKey;
Googler7e7675f2019-02-12 08:45:13 -080043import javax.annotation.Nullable;
John Cater52eb53b2019-11-27 07:38:24 -080044import org.junit.Before;
John Cater98375a22017-07-13 19:18:50 +020045import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.junit.runners.JUnit4;
janakre54491e2018-07-11 16:29:13 -070048import org.mockito.Mockito;
John Cater98375a22017-07-13 19:18:50 +020049
John Catera3df53c2019-04-16 11:15:28 -070050/**
51 * Tests for {@link SingleToolchainResolutionValue} and {@link SingleToolchainResolutionFunction}.
52 */
John Cater98375a22017-07-13 19:18:50 +020053@RunWith(JUnit4.class)
John Catera3df53c2019-04-16 11:15:28 -070054public class SingleToolchainResolutionFunctionTest extends ToolchainTestCase {
janakre54491e2018-07-11 16:29:13 -070055 @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 Cater52eb53b2019-11-27 07:38:24 -080061 @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"));
janakre54491e2018-07-11 16:29:13 -070067 }
janakr95656662018-02-14 17:14:51 -080068
69 private static ConfiguredTargetValue createConfiguredTargetValue(
70 ConfiguredTarget configuredTarget) {
cparsonse2d200f2018-03-06 16:15:11 -080071 return new NonRuleConfiguredTargetValue(
janakra81bb952019-01-28 17:30:06 -080072 configuredTarget,
73 GeneratingActions.EMPTY,
74 NestedSetBuilder.emptySet(Order.STABLE_ORDER),
75 /*nonceVersion=*/ null);
janakr95656662018-02-14 17:14:51 -080076 }
John Cater98375a22017-07-13 19:18:50 +020077
John Catera3df53c2019-04-16 11:15:28 -070078 private EvaluationResult<SingleToolchainResolutionValue> invokeToolchainResolution(SkyKey key)
John Cater98375a22017-07-13 19:18:50 +020079 throws InterruptedException {
Googler7e7675f2019-02-12 08:45:13 -080080 ConfiguredTarget mockLinuxTarget = new SerializableConfiguredTarget(linuxPlatform);
81 ConfiguredTarget mockMacTarget = new SerializableConfiguredTarget(macPlatform);
janakr95656662018-02-14 17:14:51 -080082 getSkyframeExecutor()
83 .getDifferencerForTesting()
84 .inject(
85 ImmutableMap.of(
86 LINUX_CTKEY,
87 createConfiguredTargetValue(mockLinuxTarget),
88 MAC_CTKEY,
89 createConfiguredTargetValue(mockMacTarget)));
John Cater96b40812017-07-18 18:08:03 +020090
John Cater98375a22017-07-13 19:18:50 +020091 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 Cater98375a22017-07-13 19:18:50 +0200100 @Test
John Cater71dbed42018-02-06 11:04:17 -0800101 public void testResolution_singleExecutionPlatform() throws Exception {
John Cater98375a22017-07-13 19:18:50 +0200102 SkyKey key =
John Catera3df53c2019-04-16 11:15:28 -0700103 SingleToolchainResolutionValue.key(
John Cater2f948562018-11-07 08:17:48 -0800104 targetConfigKey, testToolchainTypeLabel, LINUX_CTKEY, ImmutableList.of(MAC_CTKEY));
John Catera3df53c2019-04-16 11:15:28 -0700105 EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key);
John Cater98375a22017-07-13 19:18:50 +0200106
107 assertThatEvaluationResult(result).hasNoError();
108
John Catera3df53c2019-04-16 11:15:28 -0700109 SingleToolchainResolutionValue singleToolchainResolutionValue = result.get(key);
110 assertThat(singleToolchainResolutionValue.availableToolchainLabels())
John Catere959e442018-02-28 07:52:21 -0800111 .containsExactly(MAC_CTKEY, makeLabel("//toolchain:toolchain_2_impl"));
John Cater71dbed42018-02-06 11:04:17 -0800112 }
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 Catera3df53c2019-04-16 11:15:28 -0700129 SingleToolchainResolutionValue.key(
janakr40d00772018-02-14 14:08:45 -0800130 targetConfigKey,
John Cater2f948562018-11-07 08:17:48 -0800131 testToolchainTypeLabel,
janakr95656662018-02-14 17:14:51 -0800132 LINUX_CTKEY,
133 ImmutableList.of(LINUX_CTKEY, MAC_CTKEY));
John Catera3df53c2019-04-16 11:15:28 -0700134 EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key);
John Cater71dbed42018-02-06 11:04:17 -0800135
136 assertThatEvaluationResult(result).hasNoError();
137
John Catera3df53c2019-04-16 11:15:28 -0700138 SingleToolchainResolutionValue singleToolchainResolutionValue = result.get(key);
139 assertThat(singleToolchainResolutionValue.availableToolchainLabels())
John Cater71dbed42018-02-06 11:04:17 -0800140 .containsExactly(
John Catere959e442018-02-28 07:52:21 -0800141 LINUX_CTKEY,
John Cater71dbed42018-02-06 11:04:17 -0800142 makeLabel("//extra:extra_toolchain_impl"),
John Catere959e442018-02-28 07:52:21 -0800143 MAC_CTKEY,
John Cater71dbed42018-02-06 11:04:17 -0800144 makeLabel("//toolchain:toolchain_2_impl"));
John Cater98375a22017-07-13 19:18:50 +0200145 }
146
147 @Test
148 public void testResolution_noneFound() throws Exception {
149 // Clear the toolchains.
150 rewriteWorkspace();
151
152 SkyKey key =
John Catera3df53c2019-04-16 11:15:28 -0700153 SingleToolchainResolutionValue.key(
John Cater2f948562018-11-07 08:17:48 -0800154 targetConfigKey, testToolchainTypeLabel, LINUX_CTKEY, ImmutableList.of(MAC_CTKEY));
John Catera3df53c2019-04-16 11:15:28 -0700155 EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key);
John Cater98375a22017-07-13 19:18:50 +0200156
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 Cater98375a22017-07-13 19:18:50 +0200165 public void testToolchainResolutionValue_equalsAndHashCode() {
166 new EqualsTester()
167 .addEqualityGroup(
John Catera3df53c2019-04-16 11:15:28 -0700168 SingleToolchainResolutionValue.create(
John Cater2f948562018-11-07 08:17:48 -0800169 testToolchainType,
John Catere959e442018-02-28 07:52:21 -0800170 ImmutableMap.of(LINUX_CTKEY, makeLabel("//test:toolchain_impl_1"))),
John Catera3df53c2019-04-16 11:15:28 -0700171 SingleToolchainResolutionValue.create(
John Cater2f948562018-11-07 08:17:48 -0800172 testToolchainType,
John Catere959e442018-02-28 07:52:21 -0800173 ImmutableMap.of(LINUX_CTKEY, makeLabel("//test:toolchain_impl_1"))))
John Cater71dbed42018-02-06 11:04:17 -0800174 // Different execution platform, same label.
John Cater98375a22017-07-13 19:18:50 +0200175 .addEqualityGroup(
John Catera3df53c2019-04-16 11:15:28 -0700176 SingleToolchainResolutionValue.create(
John Cater2f948562018-11-07 08:17:48 -0800177 testToolchainType,
John Catere959e442018-02-28 07:52:21 -0800178 ImmutableMap.of(MAC_CTKEY, makeLabel("//test:toolchain_impl_1"))))
John Cater71dbed42018-02-06 11:04:17 -0800179 // Same execution platform, different label.
180 .addEqualityGroup(
John Catera3df53c2019-04-16 11:15:28 -0700181 SingleToolchainResolutionValue.create(
John Cater2f948562018-11-07 08:17:48 -0800182 testToolchainType,
John Catere959e442018-02-28 07:52:21 -0800183 ImmutableMap.of(LINUX_CTKEY, makeLabel("//test:toolchain_impl_2"))))
John Cater71dbed42018-02-06 11:04:17 -0800184 // Different execution platform, different label.
185 .addEqualityGroup(
John Catera3df53c2019-04-16 11:15:28 -0700186 SingleToolchainResolutionValue.create(
John Cater2f948562018-11-07 08:17:48 -0800187 testToolchainType,
John Catere959e442018-02-28 07:52:21 -0800188 ImmutableMap.of(MAC_CTKEY, makeLabel("//test:toolchain_impl_2"))))
John Cater71dbed42018-02-06 11:04:17 -0800189 // Multiple execution platforms.
190 .addEqualityGroup(
John Catera3df53c2019-04-16 11:15:28 -0700191 SingleToolchainResolutionValue.create(
John Cater2f948562018-11-07 08:17:48 -0800192 testToolchainType,
John Catere959e442018-02-28 07:52:21 -0800193 ImmutableMap.<ConfiguredTargetKey, Label>builder()
194 .put(LINUX_CTKEY, makeLabel("//test:toolchain_impl_1"))
195 .put(MAC_CTKEY, makeLabel("//test:toolchain_impl_1"))
Googler39e9b452018-08-08 07:38:28 -0700196 .build()))
197 .testEquals();
John Cater98375a22017-07-13 19:18:50 +0200198 }
Googler7e7675f2019-02-12 08:45:13 -0800199
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
Googler34f70582019-11-25 12:27:34 -0800266 public void repr(Printer printer) {}
Googler7e7675f2019-02-12 08:45:13 -0800267
268 @Override
Googler4ff29122019-09-04 21:14:38 -0700269 public Object getIndex(Object key, Location loc) {
Googler7e7675f2019-02-12 08:45:13 -0800270 return null;
271 }
272
273 @Override
Googler4ff29122019-09-04 21:14:38 -0700274 public boolean containsKey(Object key, Location loc) {
Googler7e7675f2019-02-12 08:45:13 -0800275 return false;
276 }
277 }
John Cater98375a22017-07-13 19:18:50 +0200278}