Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 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.analysis; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
Florian Weikert | 0509b91 | 2015-11-26 13:18:23 +0000 | [diff] [blame] | 18 | import static org.junit.Assert.fail; |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 19 | |
| 20 | import com.google.common.collect.Iterables; |
| 21 | import com.google.devtools.build.lib.actions.Artifact; |
| 22 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
| 23 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil.UncheckedActionConflictException; |
| 24 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 25 | import com.google.devtools.build.lib.analysis.util.AnalysisTestUtil; |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
cpeyser | 58fd82d | 2017-10-31 10:07:15 -0400 | [diff] [blame] | 27 | import com.google.devtools.build.lib.rules.cpp.CcToolchainProvider; |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.rules.cpp.CppHelper; |
janakr | e2df6e2 | 2018-03-19 14:31:00 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 30 | import java.io.IOException; |
| 31 | import java.util.List; |
Florian Weikert | 0509b91 | 2015-11-26 13:18:23 +0000 | [diff] [blame] | 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | import org.junit.runners.JUnit4; |
| 36 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 37 | /** Unit tests for the {@link CompilationHelper} class. */ |
Florian Weikert | 0509b91 | 2015-11-26 13:18:23 +0000 | [diff] [blame] | 38 | @RunWith(JUnit4.class) |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 39 | public class CompilationHelperTest extends BuildViewTestCase { |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 40 | private AnalysisTestUtil.CollectingAnalysisEnvironment analysisEnvironment; |
| 41 | |
Florian Weikert | 0509b91 | 2015-11-26 13:18:23 +0000 | [diff] [blame] | 42 | @Before |
| 43 | public final void createAnalysisEnvironment() throws Exception { |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 44 | analysisEnvironment = |
| 45 | new AnalysisTestUtil.CollectingAnalysisEnvironment(getTestAnalysisEnvironment()); |
| 46 | } |
| 47 | |
| 48 | private List<Artifact> getAggregatingMiddleman( |
| 49 | ConfiguredTarget rule, BuildConfiguration configuration, boolean withSolib) throws Exception { |
cpeyser | 58fd82d | 2017-10-31 10:07:15 -0400 | [diff] [blame] | 50 | RuleContext ruleContext = getRuleContext(rule, analysisEnvironment); |
| 51 | CcToolchainProvider toolchain = |
| 52 | CppHelper.getToolchainUsingDefaultCcToolchainAttribute(ruleContext); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 53 | return CppHelper.getAggregatingMiddlemanForTesting( |
cpeyser | 58fd82d | 2017-10-31 10:07:15 -0400 | [diff] [blame] | 54 | ruleContext, |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 55 | ActionsTestUtil.NULL_ACTION_OWNER, |
| 56 | "middleman", |
Lukacs Berki | e3ed60d | 2016-09-09 15:20:49 +0000 | [diff] [blame] | 57 | rule.getProvider(FileProvider.class).getFilesToBuild(), |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 58 | withSolib, |
cpeyser | 58fd82d | 2017-10-31 10:07:15 -0400 | [diff] [blame] | 59 | toolchain.getSolibDirectory(), |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 60 | configuration); |
| 61 | } |
| 62 | |
janakr | e2df6e2 | 2018-03-19 14:31:00 -0700 | [diff] [blame] | 63 | private List<Artifact> getAggregatingMiddleman(ConfiguredTargetAndData rule, boolean withSolib) |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 64 | throws Exception { |
janakr | e2df6e2 | 2018-03-19 14:31:00 -0700 | [diff] [blame] | 65 | return getAggregatingMiddleman(rule.getConfiguredTarget(), rule.getConfiguration(), withSolib); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Tests that duplicate calls to |
| 70 | * {@link com.google.devtools.build.lib.analysis.CompilationHelper#getAggregatingMiddleman} |
| 71 | * with identical parameters return the same artifact. |
| 72 | */ |
Florian Weikert | 0509b91 | 2015-11-26 13:18:23 +0000 | [diff] [blame] | 73 | @Test |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 74 | public void testDuplicateCallsReturnSameObject() throws Exception { |
janakr | e2df6e2 | 2018-03-19 14:31:00 -0700 | [diff] [blame] | 75 | ConfiguredTargetAndData rule = |
| 76 | scratchConfiguredTargetAndData( |
| 77 | "package", "a", "cc_binary(name = 'a'," + " srcs = ['a.cc'])"); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 78 | List<Artifact> middleman1 = getAggregatingMiddleman(rule, false); |
| 79 | assertThat(middleman1).hasSize(1); |
| 80 | List<Artifact> middleman2 = getAggregatingMiddleman(rule, false); |
| 81 | assertThat(middleman2).hasSize(1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 82 | assertThat(middleman2.get(0)).isEqualTo(middleman1.get(0)); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Tests that |
| 87 | * {@link com.google.devtools.build.lib.analysis.CompilationHelper#getAggregatingMiddleman} |
| 88 | * returns distinct artifacts even when called with identical rules, depending on |
| 89 | * whether solib symlink are created. |
| 90 | */ |
Florian Weikert | 0509b91 | 2015-11-26 13:18:23 +0000 | [diff] [blame] | 91 | @Test |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 92 | public void testMiddlemanAndSolibMiddlemanAreDistinct() throws Exception { |
janakr | e2df6e2 | 2018-03-19 14:31:00 -0700 | [diff] [blame] | 93 | ConfiguredTargetAndData rule = |
| 94 | scratchConfiguredTargetAndData( |
| 95 | "package", "liba.so", "cc_binary(name = 'liba.so', srcs = ['a.cc'], linkshared = 1)"); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 96 | |
| 97 | List<Artifact> middleman = getAggregatingMiddleman(rule, false); |
| 98 | assertThat(middleman).hasSize(1); |
| 99 | List<Artifact> middlemanWithSymlinks = getAggregatingMiddleman(rule, true); |
| 100 | assertThat(middlemanWithSymlinks).hasSize(1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 101 | assertThat(middlemanWithSymlinks.get(0)).isNotSameAs(middleman.get(0)); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Regression test: tests that Python CPU configurations are taken into account |
| 106 | * when generating a rule's aggregating middleman, so that otherwise equivalent rules can sustain |
| 107 | * distinct middlemen. |
| 108 | */ |
Florian Weikert | 0509b91 | 2015-11-26 13:18:23 +0000 | [diff] [blame] | 109 | @Test |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 110 | public void testPythonCcConfigurations() throws Exception { |
| 111 | setupJavaPythonCcConfigurationFiles(); |
| 112 | |
| 113 | // Equivalent cc / Python configurations: |
| 114 | |
janakr | e2df6e2 | 2018-03-19 14:31:00 -0700 | [diff] [blame] | 115 | ConfiguredTargetAndData ccRuleA = getConfiguredTargetAndData("//foo:liba.so"); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 116 | List<Artifact> middleman1 = getAggregatingMiddleman(ccRuleA, true); |
| 117 | try { |
janakr | e2df6e2 | 2018-03-19 14:31:00 -0700 | [diff] [blame] | 118 | ConfiguredTargetAndData ccRuleB = getConfiguredTargetAndData("//foo:libb.so"); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 119 | getAggregatingMiddleman(ccRuleB, true); |
| 120 | analysisEnvironment.registerWith(getMutableActionGraph()); |
| 121 | fail("Expected ActionConflictException due to same middleman artifact with different files"); |
| 122 | } catch (UncheckedActionConflictException e) { |
| 123 | // Expected failure: same "purpose" and root directory sent to the middleman generator |
| 124 | // (which results in the same output artifact), but different rules / middleman inputs. |
| 125 | } |
| 126 | |
| 127 | // This should succeed because the py_binary's middleman is under the Python configuration's |
| 128 | // internal directory, while the cc_binary's middleman is under the cc config's directory, |
| 129 | // and both configurations are the same. |
janakr | e2df6e2 | 2018-03-19 14:31:00 -0700 | [diff] [blame] | 130 | ConfiguredTargetAndData pyRuleB = |
| 131 | getConfiguredTargetAndDataDirectPrerequisite( |
| 132 | getConfiguredTargetAndData("//foo:c"), "//foo:libb.so"); |
Lukacs Berki | 4e844f0 | 2016-08-22 08:48:37 +0000 | [diff] [blame] | 133 | |
| 134 | List<Artifact> middleman2 = getAggregatingMiddleman(pyRuleB, true); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 135 | assertThat(Iterables.getOnlyElement(middleman2).getExecPathString()) |
| 136 | .isEqualTo(Iterables.getOnlyElement(middleman1).getExecPathString()); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Regression test: tests that Java CPU configurations are taken into account when |
| 141 | * generating a rule's aggregating middleman, so that otherwise equivalent rules can sustain |
| 142 | * distinct middlemen. |
| 143 | */ |
Florian Weikert | 0509b91 | 2015-11-26 13:18:23 +0000 | [diff] [blame] | 144 | @Test |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 145 | public void testJavaCcConfigurations() throws Exception { |
| 146 | setupJavaPythonCcConfigurationFiles(); |
| 147 | |
| 148 | // Equivalent cc / Java configurations: |
| 149 | |
janakr | e2df6e2 | 2018-03-19 14:31:00 -0700 | [diff] [blame] | 150 | ConfiguredTargetAndData ccRuleA = getConfiguredTargetAndData("//foo:liba.so"); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 151 | List<Artifact> middleman1 = getAggregatingMiddleman(ccRuleA, true); |
| 152 | try { |
janakr | e2df6e2 | 2018-03-19 14:31:00 -0700 | [diff] [blame] | 153 | ConfiguredTargetAndData ccRuleB = getConfiguredTargetAndData("//foo:libb.so"); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 154 | getAggregatingMiddleman(ccRuleB, true); |
| 155 | analysisEnvironment.registerWith(getMutableActionGraph()); |
| 156 | fail("Expected ActionConflictException due to same middleman artifact with different files"); |
| 157 | } catch (UncheckedActionConflictException e) { |
| 158 | // Expected failure: same "purpose" and root directory sent to the middleman generator |
| 159 | // (which results in the same output artifact), but different rules / middleman inputs. |
| 160 | } |
| 161 | |
| 162 | // This should succeed because the java_binary's middleman is under the Java configuration's |
| 163 | // internal directory, while the cc_binary's middleman is under the cc config's directory. |
janakr | e2df6e2 | 2018-03-19 14:31:00 -0700 | [diff] [blame] | 164 | ConfiguredTargetAndData javaRuleB = |
| 165 | getConfiguredTargetAndDataDirectPrerequisite( |
| 166 | getConfiguredTargetAndData("//foo:d"), "//foo:libb.so"); |
Lukacs Berki | 4e844f0 | 2016-08-22 08:48:37 +0000 | [diff] [blame] | 167 | List<Artifact> middleman2 = getAggregatingMiddleman(javaRuleB, false); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 168 | assertThat( |
| 169 | Iterables.getOnlyElement(middleman1) |
| 170 | .getExecPathString() |
| 171 | .equals(Iterables.getOnlyElement(middleman2).getExecPathString())) |
| 172 | .isFalse(); |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private void setupJavaPythonCcConfigurationFiles() throws IOException { |
| 176 | scratch.file( |
| 177 | "foo/BUILD", |
Lukacs Berki | 4e844f0 | 2016-08-22 08:48:37 +0000 | [diff] [blame] | 178 | "cc_binary(name = 'liba.so',", |
| 179 | " linkshared = 1,", |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 180 | " srcs = ['a.cc'])", |
Lukacs Berki | 4e844f0 | 2016-08-22 08:48:37 +0000 | [diff] [blame] | 181 | "cc_binary(name = 'libb.so',", |
| 182 | " linkshared = 1,", |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 183 | " srcs = ['b.cc'])", |
| 184 | "py_binary(name = 'c',", |
Lukacs Berki | 4e844f0 | 2016-08-22 08:48:37 +0000 | [diff] [blame] | 185 | " data = [':libb.so'],", |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 186 | " srcs = ['c.py'])", |
| 187 | "java_binary(name = 'd',", |
| 188 | " srcs = ['d.java'],", |
Lukacs Berki | 4e844f0 | 2016-08-22 08:48:37 +0000 | [diff] [blame] | 189 | " data = [':libb.so'],", |
Dmitry Lomov | 18212f1 | 2015-11-23 18:07:34 +0000 | [diff] [blame] | 190 | " main_class = 'd')"); |
| 191 | } |
| 192 | } |