]> granicus.if.org Git - libevent/commitdiff
cmake: support static runtime (MSVC)
authorAzat Khuzhin <azat@libevent.org>
Mon, 17 Dec 2018 18:31:54 +0000 (21:31 +0300)
committerAzat Khuzhin <azat@libevent.org>
Mon, 17 Dec 2018 18:49:00 +0000 (21:49 +0300)
Fixes: #737
CMakeLists.txt

index 9d149d7bdb34243c650f6de1fc9d65cd1905ad39..22b5fcd2aa5558377ce8031dd3fef01dc3953a02 100644 (file)
@@ -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)