diff options
author | Brad King <brad.king@kitware.com> | 2019-04-10 17:38:41 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2019-04-17 15:00:44 (GMT) |
commit | fb3370b6a1681190ffd8daf63975c44ce8fc1c49 (patch) | |
tree | 1e837d6bb039320c6f38c60b69c9439abc720e5f /Tests/MSVCRuntimeLibrary | |
parent | f621e7fa5df8d35cc379f9f7825f3d75b8489876 (diff) | |
download | CMake-fb3370b6a1681190ffd8daf63975c44ce8fc1c49.zip CMake-fb3370b6a1681190ffd8daf63975c44ce8fc1c49.tar.gz CMake-fb3370b6a1681190ffd8daf63975c44ce8fc1c49.tar.bz2 |
MSVC: Add abstraction for runtime library selection
Replace our hard-coded defaults for `/MD` and `/MDd` with a first-class
abstraction to select the runtime library from an enumeration of logical
names. We've long hesitated to do this because the idea of "runtime
library selection" touches on related concepts on several platforms.
Avoid that scope creep by simply defining an abstraction that applies
only when targeting the MSVC ABI on Windows.
Removing the old default flags requires a policy because existing
projects may rely on string processing to edit them and choose a runtime
library under the old behavior. Add policy CMP0091 to provide
compatibility.
Fixes: #19108
Diffstat (limited to 'Tests/MSVCRuntimeLibrary')
-rw-r--r-- | Tests/MSVCRuntimeLibrary/CMakeLists.txt | 49 | ||||
-rw-r--r-- | Tests/MSVCRuntimeLibrary/Fortran/CMakeLists.txt | 50 | ||||
-rw-r--r-- | Tests/MSVCRuntimeLibrary/Fortran/verify.F90 | 1 | ||||
-rw-r--r-- | Tests/MSVCRuntimeLibrary/verify.c | 1 | ||||
-rw-r--r-- | Tests/MSVCRuntimeLibrary/verify.cxx | 1 | ||||
-rw-r--r-- | Tests/MSVCRuntimeLibrary/verify.h | 29 |
6 files changed, 131 insertions, 0 deletions
diff --git a/Tests/MSVCRuntimeLibrary/CMakeLists.txt b/Tests/MSVCRuntimeLibrary/CMakeLists.txt new file mode 100644 index 0000000..b7a6e86 --- /dev/null +++ b/Tests/MSVCRuntimeLibrary/CMakeLists.txt @@ -0,0 +1,49 @@ +cmake_minimum_required(VERSION 3.14) +cmake_policy(SET CMP0091 NEW) +project(MSVCRuntimeLibrary) + +function(verify_combinations threads lang src) + set(verify_tc_config_ Release) + set(verify_tc_config_Debug Debug) + set(verify_def_MultiThreaded -DVERIFY_MT) + set(verify_def_Debug -DVERIFY_DEBUG) + set(verify_def_DLL -DVERIFY_DLL) + foreach(dbg "" Debug) + foreach(dll "" DLL) + # Construct the name of this runtime library combination. + set(rtl "${threads}${dbg}${dll}") + + # Test that try_compile builds with this RTL. + set(CMAKE_MSVC_RUNTIME_LIBRARY "${rtl}") + set(CMAKE_TRY_COMPILE_CONFIGURATION "${verify_tc_config_${dbg}}") + set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY") + try_compile(${rtl}_COMPILES + ${CMAKE_CURRENT_BINARY_DIR}/try_compile/${rtl} + ${CMAKE_CURRENT_SOURCE_DIR}/${src} + COMPILE_DEFINITIONS ${verify_def_${threads}} ${verify_def_${dbg}} ${verify_def_${dll}} + OUTPUT_VARIABLE ${rtl}_OUTPUT + ) + if(${rtl}_COMPILES) + message(STATUS "try_compile with ${rtl} worked") + else() + string(REPLACE "\n" "\n " ${rtl}_OUTPUT " ${${rtl}_OUTPUT}") + message(SEND_ERROR "try_compile with ${rtl} failed:\n${${rtl}_OUTPUT}") + endif() + + # Test that targets build with this RTL. + set(CMAKE_MSVC_RUNTIME_LIBRARY "$<$<BOOL:$<TARGET_PROPERTY:BOOL_TRUE>>:${rtl}>$<$<BOOL:$<TARGET_PROPERTY:BOOL_FALSE>>:BadContent>") + add_library(${rtl}-${lang} ${src}) + set_property(TARGET ${rtl}-${lang} PROPERTY BOOL_TRUE TRUE) + target_compile_definitions(${rtl}-${lang} PRIVATE ${verify_def_${threads}} ${verify_def_${dbg}} ${verify_def_${dll}}) + endforeach() + endforeach() +endfunction() + +function(verify lang src) + add_library(default-${lang} ${src}) + target_compile_definitions(default-${lang} PRIVATE VERIFY_MT VERIFY_DLL "$<$<CONFIG:Debug>:VERIFY_DEBUG>") + verify_combinations(MultiThreaded ${lang} ${src}) +endfunction() + +verify(C verify.c) +verify(CXX verify.cxx) diff --git a/Tests/MSVCRuntimeLibrary/Fortran/CMakeLists.txt b/Tests/MSVCRuntimeLibrary/Fortran/CMakeLists.txt new file mode 100644 index 0000000..169ba07 --- /dev/null +++ b/Tests/MSVCRuntimeLibrary/Fortran/CMakeLists.txt @@ -0,0 +1,50 @@ +cmake_minimum_required(VERSION 3.14) +cmake_policy(SET CMP0091 NEW) +project(MSVCRuntimeLibraryFortran Fortran) + +foreach(t MultiThreaded SingleThreaded) + foreach(dbg "" Debug) + foreach(dll "" DLL) + set(var "CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_${t}${dbg}${dll}") + # ifort does not actually define these, so inject them + string(REPLACE "-threads" "-threads;-D_MT" "${var}" "${${var}}") + string(REPLACE "-dbglibs" "-dbglibs;-D_DEBUG" "${var}" "${${var}}") + endforeach() + endforeach() +endforeach() +string(APPEND CMAKE_Fortran_FLAGS " -w") + +function(verify_combinations threads lang src) + set(verify_tc_config_ Release) + set(verify_tc_config_Debug Debug) + set(verify_def_MultiThreaded -DVERIFY_MT) + set(verify_def_Debug -DVERIFY_DEBUG) + set(verify_def_DLL -DVERIFY_DLL) + foreach(dbg "" Debug) + foreach(dll "" DLL) + # Construct the name of this runtime library combination. + set(rtl "${threads}${dbg}${dll}") + + # Test that targets build with this RTL. + set(CMAKE_MSVC_RUNTIME_LIBRARY "$<$<BOOL:$<TARGET_PROPERTY:BOOL_TRUE>>:${rtl}>$<$<BOOL:$<TARGET_PROPERTY:BOOL_FALSE>>:BadContent>") + add_library(${rtl}-${lang} ${src}) + set_property(TARGET ${rtl}-${lang} PROPERTY BOOL_TRUE TRUE) + target_compile_definitions(${rtl}-${lang} PRIVATE ${verify_def_${threads}} ${verify_def_${dbg}} ${verify_def_${dll}}) + endforeach() + endforeach() +endfunction() + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + +function(verify lang src) + add_library(default-${lang} ${src}) + target_compile_definitions(default-${lang} PRIVATE VERIFY_MT VERIFY_DLL "$<$<CONFIG:Debug>:VERIFY_DEBUG>") + verify_combinations(MultiThreaded ${lang} ${src}) +endfunction() + +verify(Fortran verify.F90) +# Intel Fortran for Windows supports single-threaded RTL but it is +# not implemented by the Visual Studio integration. +if(NOT CMAKE_GENERATOR MATCHES "Visual Studio") + verify_combinations(SingleThreaded Fortran verify.F90) +endif() diff --git a/Tests/MSVCRuntimeLibrary/Fortran/verify.F90 b/Tests/MSVCRuntimeLibrary/Fortran/verify.F90 new file mode 100644 index 0000000..6fe5e05 --- /dev/null +++ b/Tests/MSVCRuntimeLibrary/Fortran/verify.F90 @@ -0,0 +1 @@ +#include "../verify.h" diff --git a/Tests/MSVCRuntimeLibrary/verify.c b/Tests/MSVCRuntimeLibrary/verify.c new file mode 100644 index 0000000..741bca6 --- /dev/null +++ b/Tests/MSVCRuntimeLibrary/verify.c @@ -0,0 +1 @@ +#include "verify.h" diff --git a/Tests/MSVCRuntimeLibrary/verify.cxx b/Tests/MSVCRuntimeLibrary/verify.cxx new file mode 100644 index 0000000..741bca6 --- /dev/null +++ b/Tests/MSVCRuntimeLibrary/verify.cxx @@ -0,0 +1 @@ +#include "verify.h" diff --git a/Tests/MSVCRuntimeLibrary/verify.h b/Tests/MSVCRuntimeLibrary/verify.h new file mode 100644 index 0000000..58d65fe --- /dev/null +++ b/Tests/MSVCRuntimeLibrary/verify.h @@ -0,0 +1,29 @@ +#ifdef VERIFY_DEBUG +# ifndef _DEBUG +# error "_DEBUG not defined by debug runtime library selection" +# endif +#else +# ifdef _DEBUG +# error "_DEBUG defined by non-debug runtime library selection" +# endif +#endif + +#ifdef VERIFY_DLL +# ifndef _DLL +# error "_DLL not defined by DLL runtime library selection" +# endif +#else +# ifdef _DLL +# error "_DLL defined by non-DLL runtime library selection" +# endif +#endif + +#ifdef VERIFY_MT +# ifndef _MT +# error "_MT not defined by multi-threaded runtime library selection" +# endif +#else +# ifdef _MT +# error "_MT defined by single-threaded runtime library selection" +# endif +#endif |