Merge pull request #5714 from daverodgman/k-stachowiak_static-runtime-option-msvc
Enable static linking of the common runtime in MSVC
diff --git a/ChangeLog.d/static_link_common_runtime_msvc.txt b/ChangeLog.d/static_link_common_runtime_msvc.txt
new file mode 100644
index 0000000..6104943
--- /dev/null
+++ b/ChangeLog.d/static_link_common_runtime_msvc.txt
@@ -0,0 +1,3 @@
+Changes
+ * Add a CMake option that enables static linking of the runtime library
+ in Microsoft Visual C++ compiler. Contributed by Microplankton.
diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
index a726e11..886fc68 100644
--- a/library/CMakeLists.txt
+++ b/library/CMakeLists.txt
@@ -187,6 +187,18 @@
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
endif(CMAKE_COMPILER_IS_CLANG)
+if(CMAKE_COMPILER_IS_MSVC)
+ option(MSVC_STATIC_RUNTIME "Build the libraries with /MT compiler flag" OFF)
+ if(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
+ CMAKE_C_FLAGS_CHECK)
+ string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
+ endforeach(flag_var)
+ endif()
+endif()
+
if(WIN32)
set(libs ${libs} ws2_32)
endif(WIN32)