Merge pull request #435 from Yadoms/LinkWithMsvcStaticRuntime

Add option to link with MSVC static runtime (OFF by default)
This commit is contained in:
Leonid Stryzhevskyi 2021-06-11 12:53:41 +03:00 committed by GitHub
commit 45996bf7fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -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 #############################################################################
@ -113,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)

36
cmake/msvc-runtime.cmake Normal file
View File

@ -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)