From 6940d26f2ee10ab7bfb851a2e961bd702b49e393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gallou?= Date: Fri, 11 Jun 2021 00:20:16 +0200 Subject: [PATCH 1/2] Add option to link with MSVC static runtime (OFF by default) --- CMakeLists.txt | 1 + src/CMakeLists.txt | 5 +++++ src/MSVCStaticRuntime.cmake | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 src/MSVCStaticRuntime.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f496471..07bfe4db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ option(BUILD_SHARED_LIBS "Build shared libraries" OFF) option(OATPP_INSTALL "Create installation target for oat++" ON) option(OATPP_BUILD_TESTS "Create test target for oat++" ON) option(OATPP_LINK_ATOMIC "Link atomic library for other platform than MSVC|MINGW|APPLE|FreeBSD" ON) +option(OATPP_MSVC_LINK_STATIC_RUNTIME "MSVC: Link with static runtime (/MT and /MTd)." OFF) ################################################################################################### ## COMPILATION CONFIG ############################################################################# diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 00cedd8f..ab157114 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -278,6 +278,11 @@ set_target_properties(oatpp PROPERTIES CXX_STANDARD_REQUIRED ON ) +if(OATPP_MSVC_LINK_STATIC_RUNTIME) + include(MSVCStaticRuntime.cmake) + configure_msvc_static_runtime() +endif() + set(CMAKE_THREAD_PREFER_PTHREAD ON) find_package(Threads REQUIRED) diff --git a/src/MSVCStaticRuntime.cmake b/src/MSVCStaticRuntime.cmake new file mode 100644 index 00000000..7d2dfdaf --- /dev/null +++ b/src/MSVCStaticRuntime.cmake @@ -0,0 +1,27 @@ +macro(configure_msvc_static_runtime) + if(MSVC) + # Set compiler options. + set(variables + CMAKE_C_FLAGS + CMAKE_C_FLAGS_DEBUG + CMAKE_C_FLAGS_MINSIZEREL + CMAKE_C_FLAGS_RELEASE + CMAKE_C_FLAGS_RELWITHDEBINFO + CMAKE_CXX_FLAGS + CMAKE_CXX_FLAGS_DEBUG + CMAKE_CXX_FLAGS_MINSIZEREL + CMAKE_CXX_FLAGS_RELEASE + CMAKE_CXX_FLAGS_RELWITHDEBINFO) + + message(STATUS "MSVC: using statically-linked runtime (/MT and /MTd).") + foreach(variable ${variables}) + if(${variable} MATCHES "/MD") + string(REGEX REPLACE "/MD" "/MT" ${variable} "${${variable}}") + endif() + endforeach() + + foreach(variable ${variables}) + set(${variable} "${${variable}}" CACHE STRING "MSVC_${variable}" FORCE) + endforeach() + endif() +endmacro(configure_msvc_static_runtime) \ No newline at end of file From 47b34ef74c3939faf2b0423464dcce2736a9128c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gallou?= Date: Fri, 11 Jun 2021 09:55:01 +0200 Subject: [PATCH 2/2] Make MSVC runtime setting available for all projects --- CMakeLists.txt | 3 +++ cmake/msvc-runtime.cmake | 36 ++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 5 ----- src/MSVCStaticRuntime.cmake | 27 --------------------------- 4 files changed, 39 insertions(+), 32 deletions(-) create mode 100644 cmake/msvc-runtime.cmake delete mode 100644 src/MSVCStaticRuntime.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 07bfe4db..af146aab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,6 +114,9 @@ message("oatpp version: '${OATPP_THIS_MODULE_VERSION}'") #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") +include(cmake/msvc-runtime.cmake) +configure_msvc_runtime() + add_subdirectory(src) if(OATPP_BUILD_TESTS) diff --git a/cmake/msvc-runtime.cmake b/cmake/msvc-runtime.cmake new file mode 100644 index 00000000..42338978 --- /dev/null +++ b/cmake/msvc-runtime.cmake @@ -0,0 +1,36 @@ +macro(configure_msvc_runtime) + if(MSVC) + # Set compiler options. + set(variables + CMAKE_C_FLAGS + CMAKE_C_FLAGS_DEBUG + CMAKE_C_FLAGS_MINSIZEREL + CMAKE_C_FLAGS_RELEASE + CMAKE_C_FLAGS_RELWITHDEBINFO + CMAKE_CXX_FLAGS + CMAKE_CXX_FLAGS_DEBUG + CMAKE_CXX_FLAGS_MINSIZEREL + CMAKE_CXX_FLAGS_RELEASE + CMAKE_CXX_FLAGS_RELWITHDEBINFO) + + if(OATPP_MSVC_LINK_STATIC_RUNTIME) + message(STATUS "MSVC: using statically-linked runtime (/MT and /MTd).") + foreach(variable ${variables}) + if(${variable} MATCHES "/MD") + string(REGEX REPLACE "/MD" "/MT" ${variable} "${${variable}}") + endif() + endforeach() + else() + message(STATUS "MSVC: using dynamically-linked runtime (/MD and /MDd).") + foreach(variable ${variables}) + if(${variable} MATCHES "/MT") + string(REGEX REPLACE "/MT" "/MD" ${variable} "${${variable}}") + endif() + endforeach() + endif() + + foreach(variable ${variables}) + set(${variable} "${${variable}}" CACHE STRING "MSVC_${variable}" FORCE) + endforeach() + endif() +endmacro(configure_msvc_runtime) \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ab157114..00cedd8f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -278,11 +278,6 @@ set_target_properties(oatpp PROPERTIES CXX_STANDARD_REQUIRED ON ) -if(OATPP_MSVC_LINK_STATIC_RUNTIME) - include(MSVCStaticRuntime.cmake) - configure_msvc_static_runtime() -endif() - set(CMAKE_THREAD_PREFER_PTHREAD ON) find_package(Threads REQUIRED) diff --git a/src/MSVCStaticRuntime.cmake b/src/MSVCStaticRuntime.cmake deleted file mode 100644 index 7d2dfdaf..00000000 --- a/src/MSVCStaticRuntime.cmake +++ /dev/null @@ -1,27 +0,0 @@ -macro(configure_msvc_static_runtime) - if(MSVC) - # Set compiler options. - set(variables - CMAKE_C_FLAGS - CMAKE_C_FLAGS_DEBUG - CMAKE_C_FLAGS_MINSIZEREL - CMAKE_C_FLAGS_RELEASE - CMAKE_C_FLAGS_RELWITHDEBINFO - CMAKE_CXX_FLAGS - CMAKE_CXX_FLAGS_DEBUG - CMAKE_CXX_FLAGS_MINSIZEREL - CMAKE_CXX_FLAGS_RELEASE - CMAKE_CXX_FLAGS_RELWITHDEBINFO) - - message(STATUS "MSVC: using statically-linked runtime (/MT and /MTd).") - foreach(variable ${variables}) - if(${variable} MATCHES "/MD") - string(REGEX REPLACE "/MD" "/MT" ${variable} "${${variable}}") - endif() - endforeach() - - foreach(variable ${variables}) - set(${variable} "${${variable}}" CACHE STRING "MSVC_${variable}" FORCE) - endforeach() - endif() -endmacro(configure_msvc_static_runtime) \ No newline at end of file