diff options
Diffstat (limited to 'src/test/java/com/google/devtools/build')
4 files changed, 139 insertions, 2 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockCcSupport.java b/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockCcSupport.java index b5b9c4b182..38266f3105 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockCcSupport.java +++ b/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockCcSupport.java @@ -104,7 +104,8 @@ public final class BazelMockCcSupport extends MockCcSupport { " module_map = 'crosstool.cppmap', supports_header_parsing = 1,", " objcopy_files = ':empty', static_runtime_libs = [':empty'], strip_files = ':empty',", ")", - "cc_toolchain(name = 'cc-compiler-piii', all_files = ':empty', compiler_files = ':empty',", + "cc_toolchain(name = 'cc-compiler-piii', all_files = ':all-files-piii',", + " compiler_files = ':compiler-files-piii',", " cpu = 'local', dwp_files = ':empty', dynamic_runtime_libs = [':empty'], ", " linker_files = ':empty',", " module_map = 'crosstool.cppmap', supports_header_parsing = 1,", @@ -176,6 +177,7 @@ public final class BazelMockCcSupport extends MockCcSupport { } MockObjcSupport.setup(config); MockPlatformSupport.setup(config, "/bazel_tools_workspace/platforms"); + MockPlatformSupport.setup(config, "/bazel_tools_workspace/"); } @Override diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/MockPlatformSupport.java b/src/test/java/com/google/devtools/build/lib/packages/util/MockPlatformSupport.java index f2d89fbbe4..5aa0fbd4ac 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/util/MockPlatformSupport.java +++ b/src/test/java/com/google/devtools/build/lib/packages/util/MockPlatformSupport.java @@ -13,6 +13,8 @@ // limitations under the License. package com.google.devtools.build.lib.packages.util; +import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.testutil.TestConstants; import java.io.IOException; /** Mocking support for platforms and toolchains. */ @@ -33,4 +35,27 @@ public class MockPlatformSupport { " host_platform = True,", ")"); } + + /** Adds a mock piii platform. */ + public static void addMockPiiiPlatform(MockToolsConfig mockToolsConfig, Label crosstoolLabel) + throws Exception { + mockToolsConfig.create( + "mock_platform/BUILD", + "package(default_visibility=['//visibility:public'])", + "constraint_setting(name = 'mock_setting')", + "constraint_value(name = 'mock_value', constraint_setting = ':mock_setting')", + "platform(", + " name = 'mock-piii-platform',", + " constraint_values = [':mock_value'],", + ")", + "toolchain(", + " name = 'toolchain_cc-compiler-piii',", + " toolchain_type = '" + + TestConstants.TOOLS_REPOSITORY + + "//tools/cpp:toolchain_category',", + " toolchain = '" + crosstoolLabel.getRelative("cc-compiler-piii") + "',", + // " toolchain = '//third_party/crosstool/mock:cc-compiler-piii',", + " target_compatible_with = [':mock_value'],", + ")"); + } } 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(); } } |