diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java index eeb2bfafc52808..e2446cdd500b20 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java @@ -872,10 +872,12 @@ public static FeatureConfiguration configureFeaturesOrThrowEvalException( .addAll(requestedFeatures) .addAll(toolchain.getFeatures().getDefaultFeaturesAndActionConfigs()); - if (toolchain.isHostConfiguration()) { - allFeatures.add("host"); - } else { - allFeatures.add("nonhost"); + if (!cppConfiguration.dontEnableHostNonhost()) { + if (toolchain.isHostConfiguration()) { + allFeatures.add("host"); + } else { + allFeatures.add("nonhost"); + } } if (toolchain.useFission() && !cppConfiguration.disableLegacyCrosstoolFields()) { diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java index 27979403351ec4..5a614b43b33770 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java @@ -597,4 +597,8 @@ public boolean enableLegacyCcProvider() { public boolean disableCrosstool() { return cppOptions.disableCrosstool; } + + public boolean dontEnableHostNonhost() { + return cppOptions.dontEnableHostNonhost; + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java index 314919bc13ba0c..6e6415c767ebe9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java @@ -685,6 +685,20 @@ public Label getFdoPrefetchHintsLabel() { ) public boolean useLLVMCoverageMapFormat; + @Option( + name = "incompatible_dont_enable_host_nonhost_crosstool_features", + defaultValue = "false", + documentationCategory = OptionDocumentationCategory.TOOLCHAIN, + effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS}, + metadataTags = { + OptionMetadataTag.INCOMPATIBLE_CHANGE, + OptionMetadataTag.TRIGGERED_BY_ALL_INCOMPATIBLE_CHANGES + }, + help = + "If true, Bazel will not enable 'host' and 'nonhost' features in the c++ toolchain " + + "(see https://github.com/bazelbuild/bazel/issues/7407 for more information).") + public boolean dontEnableHostNonhost; + @Option( name = "incompatible_disable_legacy_crosstool_fields", oldName = "experimental_disable_legacy_crosstool_fields", @@ -886,6 +900,7 @@ public FragmentOptions getHost() { host.disableCrosstool = disableCrosstool; host.enableCcToolchainResolution = enableCcToolchainResolution; host.removeLegacyWholeArchive = removeLegacyWholeArchive; + host.dontEnableHostNonhost = dontEnableHostNonhost; return host; } diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java index ebf1bf525df7c9..b219d81f65b50f 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java @@ -1230,12 +1230,17 @@ public void testCompilationModeFeatures() throws Exception { assertThat(flags).containsNoneOf("-fastbuild", "-opt"); } - private List getHostAndTargetFlags(boolean useHost) throws Exception { + private List getHostAndTargetFlags(boolean useHost, boolean isDisabledByFlag) + throws Exception { AnalysisMock.get() .ccSupport() .setupCrosstool(mockToolsConfig, MockCcSupport.HOST_AND_NONHOST_CONFIGURATION); scratch.overwriteFile("mode/BUILD", "cc_library(name = 'a', srcs = ['a.cc'])"); - useConfiguration("--cpu=k8"); + useConfiguration( + "--cpu=k8", + isDisabledByFlag + ? "--incompatible_dont_enable_host_nonhost_crosstool_features" + : "--noincompatible_dont_enable_host_nonhost_crosstool_features"); ConfiguredTarget target; String objectPath; if (useHost) { @@ -1255,15 +1260,28 @@ private List getHostAndTargetFlags(boolean useHost) throws Exception { public void testHostAndNonHostFeatures() throws Exception { List flags; - flags = getHostAndTargetFlags(true); + flags = getHostAndTargetFlags(/* useHost= */ true, /* isDisabledByFlag= */ false); assertThat(flags).contains("-host"); assertThat(flags).doesNotContain("-nonhost"); - flags = getHostAndTargetFlags(false); + flags = getHostAndTargetFlags(/* useHost= */ false, /* isDisabledByFlag= */ false); assertThat(flags).contains("-nonhost"); assertThat(flags).doesNotContain("-host"); } + @Test + public void testHostAndNonHostFeaturesDisabledByTheFlag() throws Exception { + List flags; + + flags = getHostAndTargetFlags(/* useHost= */ true, /* isDisabledByFlag= */ true); + assertThat(flags).doesNotContain("-host"); + assertThat(flags).doesNotContain("-nonhost"); + + flags = getHostAndTargetFlags(/* useHost= */ false, /* isDisabledByFlag= */ true); + assertThat(flags).doesNotContain("-nonhost"); + assertThat(flags).doesNotContain("-host"); + } + @Test public void testIncludePathsOutsideExecutionRoot() throws Exception { scratchRule(