Skip to content

Commit

Permalink
Fix C++ unit test to use C++ toolchains
Browse files Browse the repository at this point in the history
BEGIN_PUBLIC
Fix C++ unit test to use C++ toolchains

This is preparation to make --incompatible_enable_cc_toolchain_resolution a no-op. All the unit tests need to work with the flag enabled.
END_PUBLIC

PiperOrigin-RevId: 586254632
Change-Id: I55452515e571f12eefa376718743b1f23cbdbde8
  • Loading branch information
comius authored and copybara-github committed Nov 29, 2023
1 parent e791618 commit 18b9da9
Showing 1 changed file with 16 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,16 @@ void testCCFlagsContainsSysroot(

CcToolchainProvider getCcToolchainProvider(BuildConfigurationValue configuration)
throws Exception {
CppConfiguration cppConfiguration = configuration.getFragment(CppConfiguration.class);
return Preconditions.checkNotNull(
getConfiguredTarget(cppConfiguration.getRuleProvidingCcToolchainProvider(), configuration)
.get(CcToolchainProvider.PROVIDER));
// use dummy library to get C++ toolchain from toolchain resolution
RuleContext ruleContext =
getRuleContext(getConfiguredTarget(Label.parseCanonical("//dummy:library"), configuration));
return Preconditions.checkNotNull(CppHelper.getToolchain(ruleContext));
}

@Test
public void testHostGrteTop() throws Exception {
scratch.file("a/grte/top/BUILD", "filegroup(name='everything')", "cc_library(name='library')");
useConfiguration(
"--host_grte_top=//a/grte/top", "--noincompatible_enable_cc_toolchain_resolution");
useConfiguration("--host_grte_top=//a/grte/top");
BuildConfigurationValue target = getTargetConfiguration();
CcToolchainProvider targetCcProvider = getCcToolchainProvider(target);
BuildConfigurationValue exec = getExecConfiguration();
Expand All @@ -82,10 +81,7 @@ public void testHostGrteTop() throws Exception {
public void testOverrideHostGrteTop() throws Exception {
scratch.file("a/grte/top/BUILD", "filegroup(name='everything')");
scratch.file("b/grte/top/BUILD", "filegroup(name='everything')");
useConfiguration(
"--grte_top=//a/grte/top",
"--host_grte_top=//b/grte/top",
"--noincompatible_enable_cc_toolchain_resolution");
useConfiguration("--grte_top=//a/grte/top", "--host_grte_top=//b/grte/top");
BuildConfigurationValue target = getTargetConfiguration();
CcToolchainProvider targetCcProvider = getCcToolchainProvider(target);
BuildConfigurationValue exec = getExecConfiguration();
Expand All @@ -104,7 +100,7 @@ public void testOverrideHostGrteTop() throws Exception {
public void testGrteTopAlias() throws Exception {
scratch.file("a/grte/top/BUILD", "filegroup(name='everything')");
scratch.file("b/grte/top/BUILD", "alias(name='everything', actual='//a/grte/top:everything')");
useConfiguration("--grte_top=//b/grte/top", "--noincompatible_enable_cc_toolchain_resolution");
useConfiguration("--grte_top=//b/grte/top");
BuildConfigurationValue target = getTargetConfiguration();
CcToolchainProvider targetCcProvider = getCcToolchainProvider(target);

Expand All @@ -117,16 +113,13 @@ public void testGrteTopAlias() throws Exception {
@Test
public void testSysroot() throws Exception {
// BuildConfigurationValue shouldn't provide a sysroot option by default.
useConfiguration("--cpu=k8", "--noincompatible_enable_cc_toolchain_resolution");
useConfiguration("--cpu=k8");
BuildConfigurationValue config = getTargetConfiguration();
testCCFlagsContainsSysroot(config, "/usr/grte/v1", true);

scratch.file("a/grte/top/BUILD", "filegroup(name='everything')");
// BuildConfigurationValue should work with label grte_top options.
useConfiguration(
"--cpu=k8",
"--grte_top=//a/grte/top:everything",
"--noincompatible_enable_cc_toolchain_resolution");
useConfiguration("--cpu=k8", "--grte_top=//a/grte/top:everything");
config = getTargetConfiguration();
testCCFlagsContainsSysroot(config, "a/grte/top", true);
}
Expand All @@ -139,8 +132,7 @@ public void testSysrootInFeatureConfigBlocksLegacySysroot() throws Exception {
mockToolsConfig,
CcToolchainConfig.builder().withActionConfigs("sysroot_in_action_config"));
scratch.overwriteFile("a/grte/top/BUILD", "filegroup(name='everything')");
useConfiguration(
"--grte_top=//a/grte/top:everything", "--noincompatible_enable_cc_toolchain_resolution");
useConfiguration("--grte_top=//a/grte/top:everything");
RuleContext ruleContext =
getRuleContext(getConfiguredTarget(Label.parseCanonical("//dummy:library"), targetConfig));
ConfigurationMakeVariableContext context =
Expand All @@ -158,26 +150,20 @@ public void testSysrootInFeatureConfigBlocksLegacySysroot() throws Exception {
public void testSysrootWithExecConfig() throws Exception {
// The exec BuildConfigurationValue shouldn't provide a sysroot option by default.
for (String cpu : new String[] {"piii", "k8"}) {
useConfiguration("--cpu=" + cpu, "--noincompatible_enable_cc_toolchain_resolution");
useConfiguration("--cpu=" + cpu);
BuildConfigurationValue config = getExecConfiguration();
testCCFlagsContainsSysroot(config, "/usr/grte/v1", true);
}
// The exec BuildConfigurationValue should work with label grte_top options.
scratch.file("a/grte/top/BUILD", "filegroup(name='everything')");
for (String cpu : new String[] {"piii", "k8"}) {
useConfiguration(
"--cpu=" + cpu,
"--host_grte_top=//a/grte/top",
"--noincompatible_enable_cc_toolchain_resolution");
useConfiguration("--cpu=" + cpu, "--host_grte_top=//a/grte/top");
BuildConfigurationValue config = getExecConfiguration();
testCCFlagsContainsSysroot(config, "a/grte/top", true);

// "--grte_top" does *not* set the exec grte_top,
// so we don't get "a/grte/top" here, but instead the default "/usr/grte/v1"
useConfiguration(
"--cpu=" + cpu,
"--grte_top=//a/grte/top",
"--noincompatible_enable_cc_toolchain_resolution");
useConfiguration("--cpu=" + cpu, "--grte_top=//a/grte/top");
config = getExecConfiguration();
testCCFlagsContainsSysroot(config, "/usr/grte/v1", true);

Expand All @@ -186,8 +172,7 @@ public void testSysrootWithExecConfig() throws Exception {
useConfiguration(
"--cpu=" + cpu,
"--grte_top=//a/grte/top",
"--host_crosstool_top=" + analysisMock.ccSupport().getMockCrosstoolLabel(),
"--noincompatible_enable_cc_toolchain_resolution");
"--host_crosstool_top=" + analysisMock.ccSupport().getMockCrosstoolLabel());
config = getExecConfiguration();
testCCFlagsContainsSysroot(config, "/usr/grte/v1", true);
}
Expand All @@ -209,15 +194,11 @@ public void testConfigurableSysroot() throws Exception {
" '//conditions:default' : '//b/grte/top:everything'}",
" )",
")");
useConfiguration(
"--grte_top=//c/grte/top:everything", "--noincompatible_enable_cc_toolchain_resolution");
useConfiguration("--grte_top=//c/grte/top:everything");
CcToolchainProvider ccProvider = getCcToolchainProvider(getTargetConfiguration());
assertThat(ccProvider.getSysroot()).isEqualTo("b/grte/top");

useConfiguration(
"--grte_top=//c/grte/top:everything",
"--define=override_grte_top=1",
"--noincompatible_enable_cc_toolchain_resolution");
useConfiguration("--grte_top=//c/grte/top:everything", "--define=override_grte_top=1");
ccProvider = getCcToolchainProvider(getTargetConfiguration());
assertThat(ccProvider.getSysroot()).isEqualTo("a/grte/top");
}
Expand Down

0 comments on commit 18b9da9

Please sign in to comment.