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 36ecb13eab3de6..89413e38d27292 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 @@ -525,6 +525,14 @@ private static Pattern getNoCoptsPattern(RuleContext ruleContext) { if (Strings.isNullOrEmpty(nocoptsValue)) { return null; } + + if (ruleContext.getConfiguration().getFragment(CppConfiguration.class).disableNoCopts()) { + ruleContext.attributeError( + NO_COPTS_ATTRIBUTE, + "This attribute was removed. See https://github.com/bazelbuild/bazel/issues/8706 for" + + " details."); + } + String nocoptsAttr = ruleContext.getExpander().expand(NO_COPTS_ATTRIBUTE, nocoptsValue); try { return Pattern.compile(nocoptsAttr); 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 6d7798ef1a3a46..0fa59043c1b5ca 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 @@ -694,4 +694,8 @@ public boolean useStandaloneLtoIndexingCommandLines() { public boolean useSpecificToolFiles() { return cppOptions.useSpecificToolFiles; } + + public boolean disableNoCopts() { + return cppOptions.disableNoCopts; + } } 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 eb27d8a5e8c951..a2b285155a94f5 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 @@ -903,6 +903,20 @@ public Label getFdoPrefetchHintsLabel() { + "See https://github.com/bazelbuild/bazel/issues/8546.") public boolean disableStaticCcToolchains; + @Option( + name = "incompatible_disable_nocopts", + defaultValue = "false", + documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, + effectTags = {OptionEffectTag.ACTION_COMMAND_LINES}, + metadataTags = { + OptionMetadataTag.INCOMPATIBLE_CHANGE, + OptionMetadataTag.TRIGGERED_BY_ALL_INCOMPATIBLE_CHANGES, + }, + help = + "When enabled, it removes nocopts attribute from C++ rules. See" + + " https://github.com/bazelbuild/bazel/issues/8706 for details.") + public boolean disableNoCopts; + @Override public FragmentOptions getHost() { CppOptions host = (CppOptions) getDefault(); @@ -959,6 +973,7 @@ public FragmentOptions getHost() { host.useStandaloneLtoIndexingCommandLines = useStandaloneLtoIndexingCommandLines; host.useSpecificToolFiles = useSpecificToolFiles; host.disableStaticCcToolchains = disableStaticCcToolchains; + host.disableNoCopts = disableNoCopts; // Save host options for further use. host.hostCoptList = hostCoptList; diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java index 0d4f1f8d6dfe65..675b84eaf7e7e6 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java @@ -1060,4 +1060,15 @@ public void testCompilationParameterFile() throws Exception { private String removeOutDirectory(String s) { return s.replace("blaze-out", "").replace("bazel-out", ""); } + + @Test + public void testNoCoptsDisabled() throws Exception { + reporter.removeHandler(failFastHandler); + scratch.file("x/BUILD", "cc_library(name = 'foo', srcs = ['a.cc'], nocopts = 'abc')"); + useConfiguration("--incompatible_disable_nocopts"); + getConfiguredTarget("//x:foo"); + assertContainsEvent( + "This attribute was removed. See https://github.com/bazelbuild/bazel/issues/8706 for" + + " details."); + } }