Skip to content

Commit 99516ea

Browse files
gmabeylws-team
authored andcommitted
win32: cmake: adds LWS_MSVC_STATIC_RUNTIME option
#3354
1 parent 1007814 commit 99516ea

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ if (POLICY CMP0075)
5151
cmake_policy(SET CMP0075 NEW)
5252
endif()
5353

54+
if(POLICY CMP0091)
55+
cmake_policy(SET CMP0091 NEW)
56+
endif()
57+
5458
# General Advice
5559
#
5660
# For selecting between DEBUG / RELEASE, use -DCMAKE_BUILD_TYPE=DEBUG or =RELEASE
@@ -387,6 +391,9 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
387391
set(LWS_SUPPRESS_DEPRECATED_API_WARNINGS 1)
388392
endif()
389393

394+
if (MSVC)
395+
option(LWS_MSVC_STATIC_RUNTIME "Link to static MSVC runtime" OFF)
396+
endif()
390397

391398
#
392399
# to use miniz, enable both LWS_WITH_ZLIB and LWS_WITH_MINIZ
@@ -945,6 +952,31 @@ if (MSVC)
945952
else()
946953
add_compile_options(/experimental:preprocessor /wd5105)
947954
endif()
955+
if (LWS_MSVC_STATIC_RUNTIME AND NOT LWS_WITH_STATIC)
956+
message(FATAL_ERROR "Incompatible options: when LWS_MSVC_STATIC_RUNTIME=ON, must also set LWS_WITH_STATIC=ON")
957+
endif()
958+
if (LWS_MSVC_STATIC_RUNTIME)
959+
if (LWS_WITH_SHARED)
960+
message(FATAL_ERROR "Incompatible options: when LWS_MSVC_STATIC_RUNTIME=ON, must also set LWS_WITH_SHARED=OFF")
961+
endif()
962+
if (${CMAKE_VERSION} VERSION_GREATER 3.15 OR ${CMAKE_VERSION} VERSION_EQUAL 3.15)
963+
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>)
964+
else()
965+
# Link the runtime library statically so that MSVCR*.DLL is not required at runtime.
966+
# https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
967+
# This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
968+
# http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
969+
foreach (flag_var
970+
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
971+
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
972+
if(${flag_var} MATCHES "/MD")
973+
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
974+
endif()
975+
endforeach()
976+
endif()
977+
else()
978+
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>DLL)
979+
endif()
948980
endif(MSVC)
949981

950982
if (MINGW)

0 commit comments

Comments
 (0)