blob: b2114a3d4fd551590906a0610a3b3ab7db9f9bda [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;
19
Googler7e7675f2019-02-12 08:45:13 -080020import com.google.common.collect.ImmutableCollection;
John Cater96b40812017-07-18 18:08:03 +020021import com.google.common.collect.ImmutableList;
John Cater71dbed42018-02-06 11:04:17 -080022import com.google.common.collect.ImmutableMap;
John Cater98375a22017-07-13 19:18:50 +020023import com.google.common.testing.EqualsTester;
janakr95656662018-02-14 17:14:51 -080024import com.google.devtools.build.lib.analysis.ConfiguredTarget;
Googler7e7675f2019-02-12 08:45:13 -080025import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
John Cater96b40812017-07-18 18:08:03 +020026import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
27import com.google.devtools.build.lib.cmdline.Label;
jcater0d019de2020-01-21 11:20:12 -080028import com.google.devtools.build.lib.packages.BuiltinProvider;
adonovana11e2d02019-12-06 07:11:35 -080029import com.google.devtools.build.lib.packages.Info;
Googler7e7675f2019-02-12 08:45:13 -080030import com.google.devtools.build.lib.packages.Provider;
John Cater98375a22017-07-13 19:18:50 +020031import com.google.devtools.build.lib.rules.platform.ToolchainTestCase;
32import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils;
Googler34f70582019-11-25 12:27:34 -080033import com.google.devtools.build.lib.syntax.Printer;
adonovan47bf59e2020-01-13 14:46:59 -080034import com.google.devtools.build.lib.syntax.StarlarkSemantics;
John Cater98375a22017-07-13 19:18:50 +020035import com.google.devtools.build.skyframe.EvaluationResult;
36import com.google.devtools.build.skyframe.SkyKey;
Googler7e7675f2019-02-12 08:45:13 -080037import javax.annotation.Nullable;
John Cater52eb53b2019-11-27 07:38:24 -080038import org.junit.Before;
John Cater98375a22017-07-13 19:18:50 +020039import org.junit.Test;
40import org.junit.runner.RunWith;
41import org.junit.runners.JUnit4;
42
John Catera3df53c2019-04-16 11:15:28 -070043/**
44 * Tests for {@link SingleToolchainResolutionValue} and {@link SingleToolchainResolutionFunction}.
45 */
John Cater98375a22017-07-13 19:18:50 +020046@RunWith(JUnit4.class)
John Catera3df53c2019-04-16 11:15:28 -070047public class SingleToolchainResolutionFunctionTest extends ToolchainTestCase {
jcater9b794ac2020-06-03 14:25:55 -070048 ConfiguredTargetKey linuxCtkey;
49 ConfiguredTargetKey macCtkey;
janakre54491e2018-07-11 16:29:13 -070050
John Cater52eb53b2019-11-27 07:38:24 -080051 @Before
52 public void setUpKeys() {
jcater9b794ac2020-06-03 14:25:55 -070053 // This has to happen here so that targetConfiguration is populated.
54 linuxCtkey =
55 ConfiguredTargetKey.builder()
56 .setLabel(Label.parseAbsoluteUnchecked("//platforms:linux"))
57 .setConfiguration(getTargetConfiguration())
58 .build();
59 macCtkey =
60 ConfiguredTargetKey.builder()
61 .setLabel(Label.parseAbsoluteUnchecked("//platforms:mac"))
62 .setConfiguration(getTargetConfiguration())
63 .build();
janakr95656662018-02-14 17:14:51 -080064 }
John Cater98375a22017-07-13 19:18:50 +020065
John Catera3df53c2019-04-16 11:15:28 -070066 private EvaluationResult<SingleToolchainResolutionValue> invokeToolchainResolution(SkyKey key)
John Cater98375a22017-07-13 19:18:50 +020067 throws InterruptedException {
68 try {
69 getSkyframeExecutor().getSkyframeBuildView().enableAnalysis(true);
70 return SkyframeExecutorTestUtils.evaluate(
71 getSkyframeExecutor(), key, /*keepGoing=*/ false, reporter);
72 } finally {
73 getSkyframeExecutor().getSkyframeBuildView().enableAnalysis(false);
74 }
75 }
76
John Cater98375a22017-07-13 19:18:50 +020077 @Test
John Cater71dbed42018-02-06 11:04:17 -080078 public void testResolution_singleExecutionPlatform() throws Exception {
John Cater98375a22017-07-13 19:18:50 +020079 SkyKey key =
John Catera3df53c2019-04-16 11:15:28 -070080 SingleToolchainResolutionValue.key(
jcater9b794ac2020-06-03 14:25:55 -070081 targetConfigKey, testToolchainTypeLabel, linuxCtkey, ImmutableList.of(macCtkey));
John Catera3df53c2019-04-16 11:15:28 -070082 EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key);
John Cater98375a22017-07-13 19:18:50 +020083
84 assertThatEvaluationResult(result).hasNoError();
85
John Catera3df53c2019-04-16 11:15:28 -070086 SingleToolchainResolutionValue singleToolchainResolutionValue = result.get(key);
87 assertThat(singleToolchainResolutionValue.availableToolchainLabels())
jcater9b794ac2020-06-03 14:25:55 -070088 .containsExactly(macCtkey, makeLabel("//toolchain:toolchain_2_impl"));
John Cater71dbed42018-02-06 11:04:17 -080089 }
90
91 @Test
92 public void testResolution_multipleExecutionPlatforms() throws Exception {
93 addToolchain(
94 "extra",
95 "extra_toolchain",
96 ImmutableList.of("//constraints:linux"),
97 ImmutableList.of("//constraints:linux"),
98 "baz");
99 rewriteWorkspace(
100 "register_toolchains(",
101 "'//toolchain:toolchain_1',",
102 "'//toolchain:toolchain_2',",
103 "'//extra:extra_toolchain')");
104
105 SkyKey key =
John Catera3df53c2019-04-16 11:15:28 -0700106 SingleToolchainResolutionValue.key(
janakr40d00772018-02-14 14:08:45 -0800107 targetConfigKey,
John Cater2f948562018-11-07 08:17:48 -0800108 testToolchainTypeLabel,
jcater9b794ac2020-06-03 14:25:55 -0700109 linuxCtkey,
110 ImmutableList.of(linuxCtkey, macCtkey));
John Catera3df53c2019-04-16 11:15:28 -0700111 EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key);
John Cater71dbed42018-02-06 11:04:17 -0800112
113 assertThatEvaluationResult(result).hasNoError();
114
John Catera3df53c2019-04-16 11:15:28 -0700115 SingleToolchainResolutionValue singleToolchainResolutionValue = result.get(key);
116 assertThat(singleToolchainResolutionValue.availableToolchainLabels())
John Cater71dbed42018-02-06 11:04:17 -0800117 .containsExactly(
jcater9b794ac2020-06-03 14:25:55 -0700118 linuxCtkey,
John Cater71dbed42018-02-06 11:04:17 -0800119 makeLabel("//extra:extra_toolchain_impl"),
jcater9b794ac2020-06-03 14:25:55 -0700120 macCtkey,
John Cater71dbed42018-02-06 11:04:17 -0800121 makeLabel("//toolchain:toolchain_2_impl"));
John Cater98375a22017-07-13 19:18:50 +0200122 }
123
124 @Test
125 public void testResolution_noneFound() throws Exception {
126 // Clear the toolchains.
127 rewriteWorkspace();
128
129 SkyKey key =
John Catera3df53c2019-04-16 11:15:28 -0700130 SingleToolchainResolutionValue.key(
jcater9b794ac2020-06-03 14:25:55 -0700131 targetConfigKey, testToolchainTypeLabel, linuxCtkey, ImmutableList.of(macCtkey));
John Catera3df53c2019-04-16 11:15:28 -0700132 EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key);
John Cater98375a22017-07-13 19:18:50 +0200133
134 assertThatEvaluationResult(result)
135 .hasErrorEntryForKeyThat(key)
136 .hasExceptionThat()
137 .hasMessageThat()
138 .contains("no matching toolchain found for //toolchain:test_toolchain");
139 }
140
141 @Test
John Cater98375a22017-07-13 19:18:50 +0200142 public void testToolchainResolutionValue_equalsAndHashCode() {
143 new EqualsTester()
144 .addEqualityGroup(
John Catera3df53c2019-04-16 11:15:28 -0700145 SingleToolchainResolutionValue.create(
John Cater2f948562018-11-07 08:17:48 -0800146 testToolchainType,
jcater9b794ac2020-06-03 14:25:55 -0700147 ImmutableMap.of(linuxCtkey, makeLabel("//test:toolchain_impl_1"))),
John Catera3df53c2019-04-16 11:15:28 -0700148 SingleToolchainResolutionValue.create(
John Cater2f948562018-11-07 08:17:48 -0800149 testToolchainType,
jcater9b794ac2020-06-03 14:25:55 -0700150 ImmutableMap.of(linuxCtkey, makeLabel("//test:toolchain_impl_1"))))
John Cater71dbed42018-02-06 11:04:17 -0800151 // Different execution platform, same label.
John Cater98375a22017-07-13 19:18:50 +0200152 .addEqualityGroup(
John Catera3df53c2019-04-16 11:15:28 -0700153 SingleToolchainResolutionValue.create(
jcater9b794ac2020-06-03 14:25:55 -0700154 testToolchainType, ImmutableMap.of(macCtkey, makeLabel("//test:toolchain_impl_1"))))
John Cater71dbed42018-02-06 11:04:17 -0800155 // Same execution platform, different label.
156 .addEqualityGroup(
John Catera3df53c2019-04-16 11:15:28 -0700157 SingleToolchainResolutionValue.create(
John Cater2f948562018-11-07 08:17:48 -0800158 testToolchainType,
jcater9b794ac2020-06-03 14:25:55 -0700159 ImmutableMap.of(linuxCtkey, makeLabel("//test:toolchain_impl_2"))))
John Cater71dbed42018-02-06 11:04:17 -0800160 // Different execution platform, different label.
161 .addEqualityGroup(
John Catera3df53c2019-04-16 11:15:28 -0700162 SingleToolchainResolutionValue.create(
jcater9b794ac2020-06-03 14:25:55 -0700163 testToolchainType, ImmutableMap.of(macCtkey, makeLabel("//test:toolchain_impl_2"))))
John Cater71dbed42018-02-06 11:04:17 -0800164 // Multiple execution platforms.
165 .addEqualityGroup(
John Catera3df53c2019-04-16 11:15:28 -0700166 SingleToolchainResolutionValue.create(
John Cater2f948562018-11-07 08:17:48 -0800167 testToolchainType,
John Catere959e442018-02-28 07:52:21 -0800168 ImmutableMap.<ConfiguredTargetKey, Label>builder()
jcater9b794ac2020-06-03 14:25:55 -0700169 .put(linuxCtkey, makeLabel("//test:toolchain_impl_1"))
170 .put(macCtkey, makeLabel("//test:toolchain_impl_1"))
Googler39e9b452018-08-08 07:38:28 -0700171 .build()))
172 .testEquals();
John Cater98375a22017-07-13 19:18:50 +0200173 }
Googler7e7675f2019-02-12 08:45:13 -0800174
175 /** Use custom class instead of mock to make sure that the dynamic codecs lookup is correct. */
176 class SerializableConfiguredTarget implements ConfiguredTarget {
177
178 private final PlatformInfo platform;
179
180 SerializableConfiguredTarget(PlatformInfo platform) {
181 this.platform = platform;
182 }
183
184 @Override
185 public ImmutableCollection<String> getFieldNames() {
186 return null;
187 }
188
189 @Nullable
190 @Override
191 public String getErrorMessageForUnknownField(String field) {
192 return null;
193 }
194
195 @Nullable
196 @Override
197 public Object getValue(String name) {
198 return null;
199 }
200
201 @Override
202 public Label getLabel() {
203 return null;
204 }
205
206 @Nullable
207 @Override
208 public BuildConfigurationValue.Key getConfigurationKey() {
209 return null;
210 }
211
212 @Nullable
213 @Override
214 public <P extends TransitiveInfoProvider> P getProvider(Class<P> provider) {
215 return null;
216 }
217
218 @Nullable
219 @Override
220 public Object get(String providerKey) {
221 return null;
222 }
223
224 @SuppressWarnings("unchecked")
225 @Override
jcater0d019de2020-01-21 11:20:12 -0800226 public <T extends Info> T get(BuiltinProvider<T> provider) {
Googler7e7675f2019-02-12 08:45:13 -0800227 if (PlatformInfo.PROVIDER.equals(provider)) {
228 return (T) this.platform;
229 }
230 return provider.getValueClass().cast(get(provider.getKey()));
231 }
232
233 @Nullable
234 @Override
adonovana11e2d02019-12-06 07:11:35 -0800235 public Info get(Provider.Key providerKey) {
Googler7e7675f2019-02-12 08:45:13 -0800236
237 return null;
238 }
239
240 @Override
Googler34f70582019-11-25 12:27:34 -0800241 public void repr(Printer printer) {}
Googler7e7675f2019-02-12 08:45:13 -0800242
243 @Override
adonovan47bf59e2020-01-13 14:46:59 -0800244 public Object getIndex(StarlarkSemantics semantics, Object key) {
Googler7e7675f2019-02-12 08:45:13 -0800245 return null;
246 }
247
248 @Override
adonovan47bf59e2020-01-13 14:46:59 -0800249 public boolean containsKey(StarlarkSemantics semantics, Object key) {
Googler7e7675f2019-02-12 08:45:13 -0800250 return false;
251 }
252 }
John Cater98375a22017-07-13 19:18:50 +0200253}