From c8b3ec17114ab0cd7f22b23ab576b1f8092d7b8a Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Mon, 17 Dec 2018 21:31:54 +0300 Subject: [PATCH] cmake: support static runtime (MSVC) Fixes: #737 (cherry picked from commit 246f44041e0782f728fa5ff2d39113005a1ab02d) --- CMakeLists.txt | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c192262..854c7e9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -221,6 +221,34 @@ else() message(FATAL_ERROR "${EVENT_LIBRARY_TYPE} is not supported") endif() +if (${MSVC}) + set(msvc_static_runtime OFF) + if ("${EVENT_LIBRARY_TYPE}" STREQUAL "STATIC") + set(msvc_static_runtime ON) + endif() + + # For more info: + # - https://docs.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library?view=vs-2017 + # - https://gitlab.kitware.com/cmake/community/wikis/FAQ#how-can-i-build-my-msvc-application-with-a-static-runtime + option(EVENT__MSVC_STATIC_RUNTIME + "Link static runtime libraries" + ${msvc_static_runtime}) + + if (EVENT__MSVC_STATIC_RUNTIME) + foreach (flag_var + CMAKE_C_FLAGS + CMAKE_C_FLAGS_DEBUG + CMAKE_C_FLAGS_RELEASE + CMAKE_C_FLAGS_MINSIZEREL + CMAKE_C_FLAGS_RELWITHDEBINFO + ) + if (${flag_var} MATCHES "/MD") + string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") + endif() + endforeach() + endif() +endif() + # GNUC specific options. if (${GNUC}) option(EVENT__DISABLE_GCC_WARNINGS "Disable verbose warnings with GCC" OFF)