Browse Source

CMP0128: Add flag in OLD mode even when standard matches the default

Commit 4a0485be (cmStandardLevelResolver: Avoid unnecessary flags, fix unset
level logic, 2021-04-29) unintentionally changed the behavior by modifying the
code to match a pre-existing comment. The resulting behavior change however
matches the intentions of CMP0128, so we simply need to guard it.

Fixes #23122.
Raul Tambre 3 years ago
parent
commit
ee1396e29e

+ 6 - 0
Help/release/3.22.rst

@@ -142,3 +142,9 @@ Other Changes
   This became available as of VS 16.10 (toolchain version 14.29.30037).
 
 * The :cpack_gen:`CPack NSIS Generator` now requires NSIS 3.03 or later.
+
+3.22.2
+------
+
+* The ``OLD`` behavior of :policy:`CMP0128` was fixed to add flags even when
+  the specified standard matches the compiler default.

+ 3 - 2
Source/cmStandardLevelResolver.cxx

@@ -206,8 +206,9 @@ struct StandardLevelComputer
 
     // 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 ||
-        (cmp0128 == cmPolicies::NEW && ext != defaultExt)) {
+    if ((cmp0128 != cmPolicies::NEW && stdIt <= defaultStdIt) ||
+        (cmp0128 == cmPolicies::NEW &&
+         (stdIt < defaultStdIt || ext != defaultExt))) {
       auto offset = std::distance(cm::cbegin(stds), stdIt);
       return cmStrCat("CMAKE_", this->Language, stdsStrings[offset], "_", type,
                       "_COMPILE_OPTION");

+ 12 - 0
Tests/RunCMake/CompileFeatures/CMP0128OldSameStandard-build-check.cmake

@@ -0,0 +1,12 @@
+foreach(flag @flags@)
+  string(FIND "${actual_stdout}" "${flag}" position)
+
+  if(NOT position EQUAL -1)
+    set(found TRUE)
+    break()
+  endif()
+endforeach()
+
+if(NOT found)
+  set(RunCMake_TEST_FAILED "No compile flags from \"@flags@\" found for LANG_STANDARD=default.")
+endif()

+ 1 - 0
Tests/RunCMake/CompileFeatures/CMP0128OldSameStandard.cmake

@@ -0,0 +1 @@
+set(CMAKE_@lang@_STANDARD @standard_default@)

+ 18 - 0
Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake

@@ -70,6 +70,23 @@ macro(mangle_flags variable)
   list(APPEND flags "${result}")
 endmacro()
 
+function(test_cmp0128_old_same_standard)
+  if(extensions_default)
+    set(flag_ext "_EXT")
+  endif()
+
+  set(flag "${${lang}${${lang}_STANDARD_DEFAULT}${flag_ext}_FLAG}")
+
+  if(NOT flag)
+    return()
+  endif()
+
+  mangle_flags(flag)
+
+  set(name CMP0128OldSameStandard)
+  test_build(--verbose)
+endfunction()
+
 function(test_cmp0128_new_extensions_opposite)
   if(extensions_opposite)
     set(flag_ext "_EXT")
@@ -148,6 +165,7 @@ function(test_lang lang ext)
 
   test_cmp0128_new_extensions_opposite()
   test_cmp0128_new_no_unnecessary_flag()
+  test_cmp0128_old_same_standard()
   test_cmp0128_warn_match()
   test_cmp0128_warn_unset()
 endfunction()