From: Azat Khuzhin Date: Mon, 17 Dec 2018 18:31:54 +0000 (+0300) Subject: cmake: support static runtime (MSVC) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=246f44041e0782f728fa5ff2d39113005a1ab02d;p=libevent cmake: support static runtime (MSVC) Fixes: #737 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d149d7b..22b5fcd2 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)