summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorRaul Tambre <raul@tambre.ee>2021-05-29 15:34:18 (GMT)
committerRaul Tambre <raul@tambre.ee>2021-09-29 19:28:40 (GMT)
commit4a0485be7f4ab06201c478f5a46111ab1e8e773e (patch)
treea4a89916f051e70a7fac32fbba0daaefbcc4ac0d /Source
parent29e2b8517126389b2c4b2f3479c4634a8260bea3 (diff)
downloadCMake-4a0485be7f4ab06201c478f5a46111ab1e8e773e.zip
CMake-4a0485be7f4ab06201c478f5a46111ab1e8e773e.tar.gz
CMake-4a0485be7f4ab06201c478f5a46111ab1e8e773e.tar.bz2
cmStandardLevelResolver: Avoid unnecessary flags, fix unset level logic
The changes are part of CMP0128. When the standard level is unset: * Flags are added if extension mode doesn't match the compiler's default. Previously logic only worked if LANG_EXTENSIONS was ON. Fixes #22224. * The full flag is used. Previously CMAKE_LANG_EXTENSION_COMPILE_OPTION was used. This was only supported for IAR. Otherwise: * Avoid adding flags if not necessary per the detected compiler defaults. * Fixed check for when the requested standard is older. It now matches the nearby comments. I reworded the fallback comment as its logic was a bit difficult to wrap my head around.
Diffstat (limited to 'Source')
-rw-r--r--Source/cmPolicies.h5
-rw-r--r--Source/cmStandardLevelResolver.cxx87
2 files changed, 76 insertions, 16 deletions
diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h
index a98e6c6..ce04117 100644
--- a/Source/cmPolicies.h
+++ b/Source/cmPolicies.h
@@ -382,7 +382,10 @@ class cmMakefile;
21, 0, cmPolicies::WARN) \
SELECT(POLICY, CMP0127, \
"cmake_dependent_option() supports full Condition Syntax.", 3, 22, \
- 0, cmPolicies::WARN)
+ 0, cmPolicies::WARN) \
+ SELECT(POLICY, CMP0128, \
+ "Selection of language standard and extension flags improved.", 3, \
+ 22, 0, cmPolicies::WARN)
#define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1)
#define CM_FOR_EACH_POLICY_ID(POLICY) \
diff --git a/Source/cmStandardLevelResolver.cxx b/Source/cmStandardLevelResolver.cxx
index c73f53a..957f4ca 100644
--- a/Source/cmStandardLevelResolver.cxx
+++ b/Source/cmStandardLevelResolver.cxx
@@ -20,6 +20,7 @@
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
+#include "cmPolicies.h"
#include "cmStringAlgorithms.h"
#include "cmTarget.h"
#include "cmValue.h"
@@ -83,25 +84,62 @@ struct StandardLevelComputer
return std::string{};
}
+ cmPolicies::PolicyStatus const cmp0128{ makefile->GetPolicyStatus(
+ cmPolicies::CMP0128) };
+ bool const defaultExt{ cmIsOn(*makefile->GetDefinition(
+ cmStrCat("CMAKE_", this->Language, "_EXTENSIONS_DEFAULT"))) };
bool ext = true;
+
+ if (cmp0128 == cmPolicies::NEW) {
+ ext = defaultExt;
+ }
+
if (cmValue extPropValue = target->GetLanguageExtensions(this->Language)) {
- if (cmIsOff(*extPropValue)) {
- ext = false;
- }
+ ext = cmIsOn(*extPropValue);
}
+ std::string const type{ ext ? "EXTENSION" : "STANDARD" };
+
cmValue standardProp = target->GetLanguageStandard(this->Language, config);
if (!standardProp) {
- if (ext) {
- // No language standard is specified and extensions are not disabled.
- // Check if this compiler needs a flag to enable extensions.
- return cmStrCat("CMAKE_", this->Language, "_EXTENSION_COMPILE_OPTION");
+ if (cmp0128 == cmPolicies::NEW) {
+ // Add extension flag if compiler's default doesn't match.
+ if (ext != defaultExt) {
+ return cmStrCat("CMAKE_", this->Language, *defaultStd, "_", type,
+ "_COMPILE_OPTION");
+ }
+ } else {
+ if (cmp0128 == cmPolicies::WARN &&
+ makefile->PolicyOptionalWarningEnabled(
+ "CMAKE_POLICY_WARNING_CMP0128") &&
+ ext != defaultExt) {
+ const char* state{};
+ if (ext) {
+ if (!makefile->GetDefinition(cmStrCat(
+ "CMAKE_", this->Language, "_EXTENSION_COMPILE_OPTION"))) {
+ state = "enabled";
+ }
+ } else {
+ state = "disabled";
+ }
+ if (state) {
+ makefile->IssueMessage(
+ MessageType::AUTHOR_WARNING,
+ cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0128),
+ "\nFor compatibility with older versions of CMake, "
+ "compiler extensions won't be ",
+ state, "."));
+ }
+ }
+
+ if (ext) {
+ return cmStrCat("CMAKE_", this->Language,
+ "_EXTENSION_COMPILE_OPTION");
+ }
}
return std::string{};
}
- std::string const type = ext ? "EXTENSION" : "STANDARD";
-
if (target->GetLanguageStandardRequired(this->Language)) {
std::string option_flag = cmStrCat(
"CMAKE_", this->Language, *standardProp, "_", type, "_COMPILE_OPTION");
@@ -121,6 +159,25 @@ struct StandardLevelComputer
return option_flag;
}
+ // If the request matches the compiler's defaults we don't need to add
+ // anything.
+ if (*standardProp == *defaultStd && ext == defaultExt) {
+ if (cmp0128 == cmPolicies::NEW) {
+ return std::string{};
+ }
+
+ if (cmp0128 == cmPolicies::WARN &&
+ makefile->PolicyOptionalWarningEnabled(
+ "CMAKE_POLICY_WARNING_CMP0128")) {
+ makefile->IssueMessage(
+ MessageType::AUTHOR_WARNING,
+ cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0128),
+ "\nFor compatibility with older versions of CMake, "
+ "unnecessary flags for language standard or compiler "
+ "extensions may be added."));
+ }
+ }
+
std::string standardStr(*standardProp);
if (this->Language == "CUDA" && standardStr == "98") {
standardStr = "03";
@@ -147,17 +204,17 @@ struct StandardLevelComputer
return std::string{};
}
- // If the standard requested is older than the compiler's default
- // then we need to use a flag to change it.
- if (stdIt <= defaultStdIt) {
+ // If the standard requested is older than the compiler's default or the
+ // extension mode doesn't match then we need to use a flag.
+ if (stdIt < defaultStdIt) {
auto offset = std::distance(cm::cbegin(stds), stdIt);
return cmStrCat("CMAKE_", this->Language, stdsStrings[offset], "_", type,
"_COMPILE_OPTION");
}
- // The standard requested is at least as new as the compiler's default,
- // and the standard request is not required. Decay to the newest standard
- // for which a flag is defined.
+ // The compiler's default is at least as new as the requested standard,
+ // and the requested standard is not required. Decay to the newest
+ // standard for which a flag is defined.
for (; defaultStdIt < stdIt; --stdIt) {
auto offset = std::distance(cm::cbegin(stds), stdIt);
std::string option_flag =