diff options
author | 2017-09-19 18:19:27 +0200 | |
---|---|---|
committer | 2017-09-20 09:02:42 +0200 | |
commit | a399b7cb92955897c745a1b813210c6168b80c59 (patch) | |
tree | 3f8874275e9f71bfa6c6512fb4a8ecd95c32eb3c /src/test/java/com/google/devtools/build/lib/rules/cpp | |
parent | c034dfd487bdba6b6b2f3f7851415a312ae10f53 (diff) |
Introduce --enabled_toolchain_types, which when set to the cpp toolchain causes the cc_toolchain dependency of cc targets to be selected using the platforms/toolchains constraint solving system.
PiperOrigin-RevId: 169250621
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/rules/cpp')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSelectionTest.java | 109 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java | 3 |
2 files changed, 111 insertions, 1 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSelectionTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSelectionTest.java new file mode 100644 index 0000000000..43aa71bbfa --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSelectionTest.java @@ -0,0 +1,109 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.rules.cpp; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import com.google.devtools.build.lib.analysis.ConfiguredTarget; +import com.google.devtools.build.lib.analysis.ToolchainContext.ResolvedToolchainProviders; +import com.google.devtools.build.lib.analysis.platform.ToolchainInfo; +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import com.google.devtools.build.lib.analysis.util.ScratchAttributeWriter; +import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.packages.util.MockPlatformSupport; +import com.google.devtools.build.lib.testutil.TestConstants; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for platform-based toolchain selection in the c++ rules. */ +@RunWith(JUnit4.class) +public class CcToolchainSelectionTest extends BuildViewTestCase { + + @Before + public void setup() throws Exception { + MockPlatformSupport.addMockPiiiPlatform( + mockToolsConfig, analysisMock.ccSupport().getMockCrosstoolLabel()); + } + + private CppCompileAction getCppCompileAction(String label) throws Exception { + ConfiguredTarget target = getConfiguredTarget(label); + List<CppCompileAction> compilationSteps = + actionsTestUtil() + .findTransitivePrerequisitesOf( + getFilesToBuild(target).iterator().next(), CppCompileAction.class); + return compilationSteps.get(0); + } + + private static String CPP_TOOLCHAIN_TYPE = + TestConstants.TOOLS_REPOSITORY + "//tools/cpp:toolchain_category"; + + @Test + public void testResolvedCcToolchain() throws Exception { + useConfiguration( + "--experimental_platforms=//mock_platform:mock-piii-platform", + "--extra_toolchains=//mock_platform:toolchain_cc-compiler-piii"); + ConfiguredTarget target = + ScratchAttributeWriter.fromLabelString(this, "cc_library", "//lib") + .setList("srcs", "a.cc") + .write(); + ResolvedToolchainProviders providers = + (ResolvedToolchainProviders) + getRuleContext(target).getToolchainContext().getResolvedToolchainProviders(); + CcToolchainProvider toolchain = + (CcToolchainProvider) + providers.getForToolchainType(Label.parseAbsolute(CPP_TOOLCHAIN_TYPE)); + assertThat(Iterables.getOnlyElement(toolchain.getCompile()).getExecPathString()) + .endsWith("piii"); + } + + @Test + public void testToolchainSelectionWithPlatforms() throws Exception { + useConfiguration( + "--enabled_toolchain_types=" + CPP_TOOLCHAIN_TYPE, + "--experimental_platforms=//mock_platform:mock-piii-platform", + "--extra_toolchains=//mock_platform:toolchain_cc-compiler-piii"); + ScratchAttributeWriter.fromLabelString(this, "cc_library", "//lib") + .setList("srcs", "a.cc") + .write(); + CppCompileAction compileAction = getCppCompileAction("//lib"); + System.err.println("!!!!!!!!!!!!!!!!!!!!!!"); + System.err.println(compileAction.getInputs()); + boolean isPiii = + ImmutableList.copyOf(compileAction.getInputs()) + .stream() + .anyMatch(artifact -> artifact.getExecPathString().endsWith("piii")); + assertThat(isPiii).isTrue(); + } + + @Test + public void testToolchainSelectionWithoutPlatforms() throws Exception { + useConfiguration("--experimental_platforms=//mock_platform:mock-piii-platform"); + ConfiguredTarget target = + ScratchAttributeWriter.fromLabelString(this, "cc_library", "//lib") + .setList("srcs", "a.cc") + .write(); + ResolvedToolchainProviders providers = + (ResolvedToolchainProviders) + getRuleContext(target).getToolchainContext().getResolvedToolchainProviders(); + ToolchainInfo toolchain = + providers.getForToolchainType(Label.parseAbsolute(CPP_TOOLCHAIN_TYPE)); + assertThat(toolchain.getKeys()).isEmpty(); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java index fa12efd127..6b66a15fec 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java @@ -17,6 +17,7 @@ package com.google.devtools.build.lib.rules.cpp; import static com.google.common.truth.Truth.assertThat; import com.google.devtools.build.lib.analysis.ConfiguredTarget; +import com.google.devtools.build.lib.analysis.platform.ToolchainInfo; import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig; import org.junit.Test; @@ -114,6 +115,6 @@ public class CcToolchainTest extends BuildViewTestCase { public void testToolchainAlias() throws Exception { ConfiguredTarget reference = scratchConfiguredTarget("a", "ref", "cc_toolchain_alias(name='ref')"); - assertThat(reference.get(CcToolchainProvider.PROVIDER.getKey())).isNotNull(); + assertThat(reference.get(ToolchainInfo.PROVIDER.getKey())).isNotNull(); } } |