Some CMake fixes for static linking on Windows from the OpenFL project.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 28 Jan 2014 12:37:04 -0500
changeset 8136 175cac5d90a6
parent 8135 dd4729596096
child 8137 6dbd80731aa0
Some CMake fixes for static linking on Windows from the OpenFL project.
CMakeLists.txt
--- a/CMakeLists.txt	Tue Jan 28 11:39:37 2014 -0300
+++ b/CMakeLists.txt	Tue Jan 28 12:37:04 2014 -0500
@@ -149,6 +149,19 @@
   list(APPEND EXTRA_LDFLAGS "$ENV{LDFLAGS}")
 endif(NOT ("$ENV{CFLAGS}" STREQUAL ""))
 
+if(MSVC)
+  option(FORCE_STATIC_VCRT "Force /MT for static VC runtimes" OFF)
+  if(FORCE_STATIC_VCRT)
+    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(flag_var)
+  endif()
+endif(MSVC)
+
 # Those are used for pkg-config and friends, so that the SDL2.pc, sdl2-config,
 # etc. are created correctly.
 set(SDL_LIBS "-lSDL2")
@@ -1234,8 +1247,14 @@
 endif(SDL_SHARED)
 
 if(SDL_STATIC)
+  set (BUILD_SHARED_LIBS FALSE)
   add_library(SDL2-static STATIC ${SOURCE_FILES})
   set_target_properties(SDL2-static PROPERTIES OUTPUT_NAME "SDL2")
+  if(WINDOWS)
+    set_target_properties(SDL2-static PROPERTIES LINK_FLAGS_RELEASE "/NODEFAULTLIB")
+    set_target_properties(SDL2-static PROPERTIES LINK_FLAGS_DEBUG "/NODEFAULTLIB")
+    set_target_properties(SDL2-static PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
+  endif(WINDOWS)
   # TODO: Win32 platforms keep the same suffix .lib for import and static
   # libraries - do we need to consider this?
   set(_INSTALL_LIBS "SDL2-static" ${_INSTALL_LIBS})