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; |
| 19 | |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableCollection; |
John Cater | 96b4081 | 2017-07-18 18:08:03 +0200 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableMap; |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 23 | import com.google.common.testing.EqualsTester; |
janakr | 9565666 | 2018-02-14 17:14:51 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.TransitiveInfoProvider; |
John Cater | 96b4081 | 2017-07-18 18:08:03 +0200 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.platform.PlatformInfo; |
| 27 | import com.google.devtools.build.lib.cmdline.Label; |
jcater | 0d019de | 2020-01-21 11:20:12 -0800 | [diff] [blame] | 28 | import com.google.devtools.build.lib.packages.BuiltinProvider; |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 29 | import com.google.devtools.build.lib.packages.Info; |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 30 | import com.google.devtools.build.lib.packages.Provider; |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 31 | import com.google.devtools.build.lib.rules.platform.ToolchainTestCase; |
| 32 | import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils; |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 33 | import com.google.devtools.build.lib.syntax.Printer; |
adonovan | 47bf59e | 2020-01-13 14:46:59 -0800 | [diff] [blame] | 34 | import com.google.devtools.build.lib.syntax.StarlarkSemantics; |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 35 | import com.google.devtools.build.skyframe.EvaluationResult; |
| 36 | import com.google.devtools.build.skyframe.SkyKey; |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 37 | import javax.annotation.Nullable; |
John Cater | 52eb53b | 2019-11-27 07:38:24 -0800 | [diff] [blame] | 38 | import org.junit.Before; |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.junit.runners.JUnit4; |
| 42 | |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Tests for {@link SingleToolchainResolutionValue} and {@link SingleToolchainResolutionFunction}. |
| 45 | */ |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 46 | @RunWith(JUnit4.class) |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 47 | public class SingleToolchainResolutionFunctionTest extends ToolchainTestCase { |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 48 | ConfiguredTargetKey linuxCtkey; |
| 49 | ConfiguredTargetKey macCtkey; |
janakr | e54491e | 2018-07-11 16:29:13 -0700 | [diff] [blame] | 50 | |
John Cater | 52eb53b | 2019-11-27 07:38:24 -0800 | [diff] [blame] | 51 | @Before |
| 52 | public void setUpKeys() { |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 53 | // 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(); |
janakr | 9565666 | 2018-02-14 17:14:51 -0800 | [diff] [blame] | 64 | } |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 65 | |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 66 | private EvaluationResult<SingleToolchainResolutionValue> invokeToolchainResolution(SkyKey key) |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 67 | 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 Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 77 | @Test |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 78 | public void testResolution_singleExecutionPlatform() throws Exception { |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 79 | SkyKey key = |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 80 | SingleToolchainResolutionValue.key( |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 81 | targetConfigKey, testToolchainTypeLabel, linuxCtkey, ImmutableList.of(macCtkey)); |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 82 | EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key); |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 83 | |
| 84 | assertThatEvaluationResult(result).hasNoError(); |
| 85 | |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 86 | SingleToolchainResolutionValue singleToolchainResolutionValue = result.get(key); |
| 87 | assertThat(singleToolchainResolutionValue.availableToolchainLabels()) |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 88 | .containsExactly(macCtkey, makeLabel("//toolchain:toolchain_2_impl")); |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 89 | } |
| 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 Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 106 | SingleToolchainResolutionValue.key( |
janakr | 40d0077 | 2018-02-14 14:08:45 -0800 | [diff] [blame] | 107 | targetConfigKey, |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 108 | testToolchainTypeLabel, |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 109 | linuxCtkey, |
| 110 | ImmutableList.of(linuxCtkey, macCtkey)); |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 111 | EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key); |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 112 | |
| 113 | assertThatEvaluationResult(result).hasNoError(); |
| 114 | |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 115 | SingleToolchainResolutionValue singleToolchainResolutionValue = result.get(key); |
| 116 | assertThat(singleToolchainResolutionValue.availableToolchainLabels()) |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 117 | .containsExactly( |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 118 | linuxCtkey, |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 119 | makeLabel("//extra:extra_toolchain_impl"), |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 120 | macCtkey, |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 121 | makeLabel("//toolchain:toolchain_2_impl")); |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testResolution_noneFound() throws Exception { |
| 126 | // Clear the toolchains. |
| 127 | rewriteWorkspace(); |
| 128 | |
| 129 | SkyKey key = |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 130 | SingleToolchainResolutionValue.key( |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 131 | targetConfigKey, testToolchainTypeLabel, linuxCtkey, ImmutableList.of(macCtkey)); |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 132 | EvaluationResult<SingleToolchainResolutionValue> result = invokeToolchainResolution(key); |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 133 | |
| 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 Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 142 | public void testToolchainResolutionValue_equalsAndHashCode() { |
| 143 | new EqualsTester() |
| 144 | .addEqualityGroup( |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 145 | SingleToolchainResolutionValue.create( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 146 | testToolchainType, |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 147 | ImmutableMap.of(linuxCtkey, makeLabel("//test:toolchain_impl_1"))), |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 148 | SingleToolchainResolutionValue.create( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 149 | testToolchainType, |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 150 | ImmutableMap.of(linuxCtkey, makeLabel("//test:toolchain_impl_1")))) |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 151 | // Different execution platform, same label. |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 152 | .addEqualityGroup( |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 153 | SingleToolchainResolutionValue.create( |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 154 | testToolchainType, ImmutableMap.of(macCtkey, makeLabel("//test:toolchain_impl_1")))) |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 155 | // Same execution platform, different label. |
| 156 | .addEqualityGroup( |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 157 | SingleToolchainResolutionValue.create( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 158 | testToolchainType, |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 159 | ImmutableMap.of(linuxCtkey, makeLabel("//test:toolchain_impl_2")))) |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 160 | // Different execution platform, different label. |
| 161 | .addEqualityGroup( |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 162 | SingleToolchainResolutionValue.create( |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 163 | testToolchainType, ImmutableMap.of(macCtkey, makeLabel("//test:toolchain_impl_2")))) |
John Cater | 71dbed4 | 2018-02-06 11:04:17 -0800 | [diff] [blame] | 164 | // Multiple execution platforms. |
| 165 | .addEqualityGroup( |
John Cater | a3df53c | 2019-04-16 11:15:28 -0700 | [diff] [blame] | 166 | SingleToolchainResolutionValue.create( |
John Cater | 2f94856 | 2018-11-07 08:17:48 -0800 | [diff] [blame] | 167 | testToolchainType, |
John Cater | e959e44 | 2018-02-28 07:52:21 -0800 | [diff] [blame] | 168 | ImmutableMap.<ConfiguredTargetKey, Label>builder() |
jcater | 9b794ac | 2020-06-03 14:25:55 -0700 | [diff] [blame^] | 169 | .put(linuxCtkey, makeLabel("//test:toolchain_impl_1")) |
| 170 | .put(macCtkey, makeLabel("//test:toolchain_impl_1")) |
Googler | 39e9b45 | 2018-08-08 07:38:28 -0700 | [diff] [blame] | 171 | .build())) |
| 172 | .testEquals(); |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 173 | } |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 174 | |
| 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 |
jcater | 0d019de | 2020-01-21 11:20:12 -0800 | [diff] [blame] | 226 | public <T extends Info> T get(BuiltinProvider<T> provider) { |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 227 | 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 |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 235 | public Info get(Provider.Key providerKey) { |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 236 | |
| 237 | return null; |
| 238 | } |
| 239 | |
| 240 | @Override |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 241 | public void repr(Printer printer) {} |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 242 | |
| 243 | @Override |
adonovan | 47bf59e | 2020-01-13 14:46:59 -0800 | [diff] [blame] | 244 | public Object getIndex(StarlarkSemantics semantics, Object key) { |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 245 | return null; |
| 246 | } |
| 247 | |
| 248 | @Override |
adonovan | 47bf59e | 2020-01-13 14:46:59 -0800 | [diff] [blame] | 249 | public boolean containsKey(StarlarkSemantics semantics, Object key) { |
Googler | 7e7675f | 2019-02-12 08:45:13 -0800 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | } |
John Cater | 98375a2 | 2017-07-13 19:18:50 +0200 | [diff] [blame] | 253 | } |