diff options
author | Brad King <brad.king@kitware.com> | 2021-11-12 14:02:23 +0000 |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2021-11-12 09:02:40 -0500 |
commit | 9740573673ba9e26108dad83c7b368e09b8b7810 (patch) | |
tree | efdc75421ac1439babbf5dfb3d7f26d016baa9c7 | |
parent | 5154dd2450679e4c2d265d12f7e71656cf0830cf (diff) | |
parent | e47dfce75d74de26ac6ee25625a44989312e29c1 (diff) | |
download | cmake-9740573673ba9e26108dad83c7b368e09b8b7810.tar.gz |
Merge topic 'cmp0128-fixup' into release-3.22
e47dfce75d CMP0128: Enable/disable extensions if standard same as default
Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !6723
-rw-r--r-- | Help/policy/CMP0128.rst | 35 | ||||
-rw-r--r-- | Source/cmStandardLevelResolver.cxx | 3 | ||||
-rw-r--r-- | Tests/RunCMake/CompileFeatures/ExtensionsStandardDefault-build-check.cmake | 12 | ||||
-rw-r--r-- | Tests/RunCMake/CompileFeatures/ExtensionsStandardDefault.cmake | 9 | ||||
-rw-r--r-- | Tests/RunCMake/CompileFeatures/ExtensionsStandardUnset-build-check.cmake (renamed from Tests/RunCMake/CompileFeatures/UnsetStandard-build-check.cmake) | 0 | ||||
-rw-r--r-- | Tests/RunCMake/CompileFeatures/ExtensionsStandardUnset.cmake (renamed from Tests/RunCMake/CompileFeatures/UnsetStandard.cmake) | 0 | ||||
-rw-r--r-- | Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake | 16 |
7 files changed, 47 insertions, 28 deletions
diff --git a/Help/policy/CMP0128.rst b/Help/policy/CMP0128.rst index b894ce1fd7..604a146a16 100644 --- a/Help/policy/CMP0128.rst +++ b/Help/policy/CMP0128.rst @@ -9,8 +9,8 @@ When this policy is set to ``NEW``: :variable:`CMAKE_<LANG>_EXTENSIONS` if set, otherwise falling back to :variable:`CMAKE_<LANG>_EXTENSIONS_DEFAULT`. -* Extensions are correctly disabled/enabled if :prop_tgt:`<LANG>_STANDARD` is - unset. +* Extensions are correctly enabled/disabled if :prop_tgt:`<LANG>_STANDARD` is + unset or satisfied by the default. * Standard mode-affecting flags aren't added unless necessary to achieve the specified mode. @@ -33,26 +33,6 @@ The ``OLD`` behavior: Code may need to be updated for the ``NEW`` behavior in the following cases: -* If :prop_tgt:`<LANG>_EXTENSIONS` matches - :variable:`CMAKE_<LANG>_EXTENSIONS_DEFAULT` or is unset and the compiler's - default satisfies :prop_tgt:`<LANG>_STANDARD` but the compiled code requires - the exact standard specified. - Such code should set :prop_tgt:`<LANG>_STANDARD_REQUIRED` to ``ON``. - - For example: - - .. code-block:: cmake - - cmake_minimum_required(VERSION |release|) - project(example C) - - add_executable(exe main.c) - set_property(TARGET exe PROPERTY C_STANDARD 99) - - If the compiler defaults to C11 then the standard specification for C99 is - satisfied and CMake will pass no flags. ``main.c`` will no longer compile if - it is incompatible with C11. - * If a standard mode flag previously overridden by CMake's and not used during compiler detection now takes effect due to CMake no longer adding one as the default detected is appropriate. @@ -64,6 +44,17 @@ Code may need to be updated for the ``NEW`` behavior in the following cases: * Or ensure the manually-specified flags are used during compiler detection. +* If extensions were disabled without :prop_tgt:`<LANG>_STANDARD` being set + CMake previously wouldn't actually disable extensions. + + Such code should be updated to not disable extensions if they are required. + +* If extensions were enabled/disabled when :prop_tgt:`<LANG>_STANDARD` was + satisfied by the compiler's default CMake previously wouldn't actually + enable/disable extensions. + + Such code should be updated to set the correct extensions mode. + If compiler flags affecting the standard mode are used during compiler detection (for example in :manual:`a toolchain file <cmake-toolchains(7)>` using :variable:`CMAKE_<LANG>_FLAGS_INIT`) then they will affect the detected diff --git a/Source/cmStandardLevelResolver.cxx b/Source/cmStandardLevelResolver.cxx index 957f4ca2df..61416e063f 100644 --- a/Source/cmStandardLevelResolver.cxx +++ b/Source/cmStandardLevelResolver.cxx @@ -206,7 +206,8 @@ 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) { + if (stdIt < defaultStdIt || + (cmp0128 == cmPolicies::NEW && ext != defaultExt)) { auto offset = std::distance(cm::cbegin(stds), stdIt); return cmStrCat("CMAKE_", this->Language, stdsStrings[offset], "_", type, "_COMPILE_OPTION"); diff --git a/Tests/RunCMake/CompileFeatures/ExtensionsStandardDefault-build-check.cmake b/Tests/RunCMake/CompileFeatures/ExtensionsStandardDefault-build-check.cmake new file mode 100644 index 0000000000..4e85397f4a --- /dev/null +++ b/Tests/RunCMake/CompileFeatures/ExtensionsStandardDefault-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 and @lang@_EXTENSIONS=@extensions_opposite@.") +endif() diff --git a/Tests/RunCMake/CompileFeatures/ExtensionsStandardDefault.cmake b/Tests/RunCMake/CompileFeatures/ExtensionsStandardDefault.cmake new file mode 100644 index 0000000000..32578d1f96 --- /dev/null +++ b/Tests/RunCMake/CompileFeatures/ExtensionsStandardDefault.cmake @@ -0,0 +1,9 @@ +enable_language(@lang@) + +# Make sure the compile command is not hidden. +string(REPLACE "${CMAKE_START_TEMP_FILE}" "" CMAKE_@lang@_COMPILE_OBJECT "${CMAKE_@lang@_COMPILE_OBJECT}") +string(REPLACE "${CMAKE_END_TEMP_FILE}" "" CMAKE_@lang@_COMPILE_OBJECT "${CMAKE_@lang@_COMPILE_OBJECT}") + +set(CMAKE_@lang@_EXTENSIONS @extensions_opposite@) +set(CMAKE_@lang@_STANDARD @standard_default@) +add_library(foo "@RunCMake_SOURCE_DIR@/empty.@ext@") diff --git a/Tests/RunCMake/CompileFeatures/UnsetStandard-build-check.cmake b/Tests/RunCMake/CompileFeatures/ExtensionsStandardUnset-build-check.cmake index abe293c34a..abe293c34a 100644 --- a/Tests/RunCMake/CompileFeatures/UnsetStandard-build-check.cmake +++ b/Tests/RunCMake/CompileFeatures/ExtensionsStandardUnset-build-check.cmake diff --git a/Tests/RunCMake/CompileFeatures/UnsetStandard.cmake b/Tests/RunCMake/CompileFeatures/ExtensionsStandardUnset.cmake index 99bb3f069a..99bb3f069a 100644 --- a/Tests/RunCMake/CompileFeatures/UnsetStandard.cmake +++ b/Tests/RunCMake/CompileFeatures/ExtensionsStandardUnset.cmake diff --git a/Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake b/Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake index 3bfd2112a1..ebd981b3d5 100644 --- a/Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake +++ b/Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake @@ -36,7 +36,7 @@ endif() configure_file("${RunCMake_SOURCE_DIR}/CMakeLists.txt" "${RunCMake_BINARY_DIR}/CMakeLists.txt" COPYONLY) -macro(test_build) +function(test_build) set(test ${name}-${lang}) configure_file("${RunCMake_SOURCE_DIR}/${name}.cmake" "${RunCMake_BINARY_DIR}/${test}.cmake" @ONLY) @@ -52,7 +52,7 @@ macro(test_build) run_cmake(${test}) set(RunCMake_TEST_NO_CLEAN 1) run_cmake_command(${test}-build ${CMAKE_COMMAND} --build . ${ARGN}) -endmacro() +endfunction() # Mangle flags such as they're in verbose build output. macro(mangle_flags variable) @@ -68,7 +68,7 @@ macro(mangle_flags variable) list(APPEND flags "${result}") endmacro() -function(test_unset_standard) +function(test_extensions_opposite) if(extensions_opposite) set(flag_ext "_EXT") endif() @@ -81,9 +81,15 @@ function(test_unset_standard) mangle_flags(flag) - set(name UnsetStandard) + # Make sure we enable/disable extensions when: + # 1. LANG_STANDARD is unset. + set(name ExtensionsStandardUnset) set(RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0128=NEW) test_build(--verbose) + + # 2. LANG_STANDARD matches CMAKE_LANG_STANDARD_DEFAULT. + set(name ExtensionsStandardDefault) + test_build(--verbose) endfunction() function(test_no_unnecessary_flag) @@ -138,7 +144,7 @@ function(test_lang lang ext) set(extensions_opposite ON) endif() - test_unset_standard() + test_extensions_opposite() test_no_unnecessary_flag() test_cmp0128_warn_match() test_cmp0128_warn_unset() |