diff options
author | Brad King <brad.king@kitware.com> | 2022-10-19 13:56:58 +0000 |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2022-10-19 09:57:08 -0400 |
commit | 177c56e3fa7827629bde4289ec3b7e8661e47650 (patch) | |
tree | 50df54bb7492d12362308912408c032f4044e542 | |
parent | 077e42fe7648f4ef5c9741e7a1e085ee6c8685ab (diff) | |
parent | 50e90e282847f1109e5559d49fc12214b3493fff (diff) | |
download | cmake-177c56e3fa7827629bde4289ec3b7e8661e47650.tar.gz |
Merge topic 'try_compile-CMP0128' into release-3.25
50e90e2828 try_compile: Honor CMP0128 setting in test project
Acked-by: Kitware Robot <kwrobot@kitware.com>
Reviewed-by: Raul Tambre <raul@tambre.ee>
Merge-request: !7803
-rw-r--r-- | Source/cmCoreTryCompile.cxx | 6 | ||||
-rw-r--r-- | Tests/RunCMake/try_compile/CMP0128-NEW.cmake | 7 | ||||
-rw-r--r-- | Tests/RunCMake/try_compile/CMP0128-WARN.cmake | 7 | ||||
-rw-r--r-- | Tests/RunCMake/try_compile/CMP0128-common.cmake | 31 | ||||
-rw-r--r-- | Tests/RunCMake/try_compile/Inspect.cmake | 4 | ||||
-rw-r--r-- | Tests/RunCMake/try_compile/RunCMakeTest.cmake | 17 |
6 files changed, 72 insertions, 0 deletions
diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 588f44a6fc..867f984fd2 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -630,6 +630,12 @@ bool cmCoreTryCompile::TryCompileCode(Arguments& arguments, fprintf(fout, "cmake_policy(SET CMP0126 OLD)\n"); } + /* Set language extensions policy to match outer project. */ + if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0128) != + cmPolicies::NEW) { + fprintf(fout, "cmake_policy(SET CMP0128 OLD)\n"); + } + std::string projectLangs; for (std::string const& li : testLangs) { projectLangs += " " + li; diff --git a/Tests/RunCMake/try_compile/CMP0128-NEW.cmake b/Tests/RunCMake/try_compile/CMP0128-NEW.cmake new file mode 100644 index 0000000000..20e389a7d8 --- /dev/null +++ b/Tests/RunCMake/try_compile/CMP0128-NEW.cmake @@ -0,0 +1,7 @@ +cmake_policy(SET CMP0128 NEW) +set(check_cxx_std " +#if __cplusplus > 199711L && __cplusplus <= 201103L +# error Compiler is incorrectly in C++11 mode. +#endif +") +include(CMP0128-common.cmake) diff --git a/Tests/RunCMake/try_compile/CMP0128-WARN.cmake b/Tests/RunCMake/try_compile/CMP0128-WARN.cmake new file mode 100644 index 0000000000..266bd22aee --- /dev/null +++ b/Tests/RunCMake/try_compile/CMP0128-WARN.cmake @@ -0,0 +1,7 @@ + +set(check_cxx_std " +#if __cplusplus <= 199711L || __cplusplus > 201103L +# error Compiler is incorrectly not in C++11 mode. +#endif +") +include(CMP0128-common.cmake) diff --git a/Tests/RunCMake/try_compile/CMP0128-common.cmake b/Tests/RunCMake/try_compile/CMP0128-common.cmake new file mode 100644 index 0000000000..0b8a12bc54 --- /dev/null +++ b/Tests/RunCMake/try_compile/CMP0128-common.cmake @@ -0,0 +1,31 @@ +cmake_policy(SET CMP0067 NEW) +enable_language(CXX) + +# Isolate the one try_compile below in the error log. +set(CMakeError_log "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeError.log") +file(REMOVE "${CMakeError_log}") + +# Add our own -std= flag to the try_compile check. +set(CMAKE_REQUIRED_FLAGS -std=c++11) + +# Tell CMP0128 NEW behavior to append a -std= flag (after ours). +if(CMAKE_CXX_EXTENSIONS_DEFAULT) + set(CMAKE_CXX_EXTENSIONS OFF) +else() + set(CMAKE_CXX_EXTENSIONS ON) +endif() + +include(CheckSourceCompiles) +check_source_compiles(CXX " +${check_cxx_std} +int main() +{ + return 0; +} +" SRC_COMPILED) +if(NOT SRC_COMPILED) + if(EXISTS "${CMakeError_log}") + file(READ "${CMakeError_log}" err_log) + endif() + message("${err_log}") +endif() diff --git a/Tests/RunCMake/try_compile/Inspect.cmake b/Tests/RunCMake/try_compile/Inspect.cmake new file mode 100644 index 0000000000..added410b1 --- /dev/null +++ b/Tests/RunCMake/try_compile/Inspect.cmake @@ -0,0 +1,4 @@ +enable_language(CXX) +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/info.cmake" " +set(CMAKE_CXX_EXTENSIONS_DEFAULT \"${CMAKE_CXX_EXTENSIONS_DEFAULT}\") +") diff --git a/Tests/RunCMake/try_compile/RunCMakeTest.cmake b/Tests/RunCMake/try_compile/RunCMakeTest.cmake index d63624c197..a3a3451baf 100644 --- a/Tests/RunCMake/try_compile/RunCMakeTest.cmake +++ b/Tests/RunCMake/try_compile/RunCMakeTest.cmake @@ -88,6 +88,23 @@ if(RunCMake_GENERATOR MATCHES "Make|Ninja") unset(RunCMake_TEST_NO_CLEAN) endif() +# Lookup CMAKE_CXX_EXTENSIONS_DEFAULT. +# FIXME: Someday we could move this to the top of the file and use it in +# place of some of the values passed by 'Tests/RunCMake/CMakeLists.txt'. +run_cmake(Inspect) +include("${RunCMake_BINARY_DIR}/Inspect-build/info.cmake") + +# FIXME: Support more compilers and default standard levels. +if (CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|AppleClang)$" + AND DEFINED CMAKE_CXX_STANDARD_DEFAULT + AND DEFINED CMAKE_CXX_EXTENSIONS_DEFAULT + ) + run_cmake(CMP0128-WARN) + if(NOT CMAKE_CXX_STANDARD_DEFAULT EQUAL 11) + run_cmake(CMP0128-NEW) + endif() +endif() + if(UNIX) run_cmake(CleanupNoFollowSymlink) endif() |