diff options
author | Raul Tambre <raul@tambre.ee> | 2021-11-09 18:34:06 (GMT) |
---|---|---|
committer | Raul Tambre <raul@tambre.ee> | 2021-11-11 19:55:04 (GMT) |
commit | e47dfce75d74de26ac6ee25625a44989312e29c1 (patch) | |
tree | b0e802226f6ac38f6f0e3992719f6807355e6577 | |
parent | 2ad300ebd73c6aca7189a9f8ea8d8ad61e9c3223 (diff) | |
download | CMake-e47dfce75d74de26ac6ee25625a44989312e29c1.zip CMake-e47dfce75d74de26ac6ee25625a44989312e29c1.tar.gz CMake-e47dfce75d74de26ac6ee25625a44989312e29c1.tar.bz2 |
CMP0128: Enable/disable extensions if standard same as default
This was intended to be part of the initial MR (!6177), but accidentally went
missing when debugging nightly failures on less common systems. Noticed during
!6711 review as the comment about this behaviour didn't match the code.
Documentation for CMP0128 is updated to remove a false case and note the two
cases related to this.
Fixes #22224.
-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 b894ce1..604a146 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 957f4ca..61416e0 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 0000000..4e85397 --- /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 0000000..32578d1 --- /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 abe293c..abe293c 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 99bb3f0..99bb3f0 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 3bfd211..ebd981b 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() |