Commit 15a63e2158cd42ffcd5d30d8ed3caac735ae78dc

Vicent Martí 2013-03-18T13:55:18

Merge pull request #1418 from QbProg/qb/cmake-msvc MSVC cmake improvements

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 37a5830..dfca736 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -35,6 +35,10 @@ IF(MSVC)
 	# - Turn this off by invoking CMake with the "-DSTDCALL=Off" argument.
 	#
 	OPTION( STDCALL			"Build libgit2 with the __stdcall convention"	ON  )
+
+	# This option must match the settings used in your program, in particular if you
+	# are linking statically
+	OPTION( STATIC_CRT		"Link the static CRT libraries"	ON  )
 ENDIF()
 
 # Installation paths
@@ -148,26 +152,36 @@ IF (MSVC)
 		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
 	ENDIF ()
 
+	IF (STATIC_CRT)
+		SET(CRT_FLAG_DEBUG "/MTd")
+		SET(CRT_FLAG_RELEASE "/MT")
+	ELSE()
+	    SET(CRT_FLAG_DEBUG "/MDd")
+		SET(CRT_FLAG_RELEASE "/MD")
+	ENDIF()
+
 	# /Zi - Create debugging information
 	# /Od - Disable optimization
 	# /D_DEBUG - #define _DEBUG
 	# /MTd - Statically link the multithreaded debug version of the CRT
+	# /MDd - Dynamically link the multithreaded debug version of the CRT
 	# /RTC1 - Run time checks
-	SET(CMAKE_C_FLAGS_DEBUG "/Zi /Od /D_DEBUG /MTd /RTC1")
+	SET(CMAKE_C_FLAGS_DEBUG "/Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}")
 
 	# /DNDEBUG - Disables asserts
 	# /MT - Statically link the multithreaded release version of the CRT
+	# /MD - Dynamically link the multithreaded release version of the CRT
 	# /O2 - Optimize for speed
 	# /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off)
 	# /GL - Link time code generation (whole program optimization)
 	# /Gy - Function-level linking
-	SET(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /MT /O2 /Oy /GL /Gy")
+	SET(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
 
 	# /Oy- - Disable frame pointer omission (FPO)
-	SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /MT /O2 /Oy- /GL /Gy")
+	SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}")
 
 	# /O1 - Optimize for size
-	SET(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /MT /O1 /Oy /GL /Gy")
+	SET(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
 
 	# /DYNAMICBASE - Address space load randomization (ASLR)
 	# /NXCOMPAT - Data execution prevention (DEP)
@@ -268,6 +282,12 @@ ADD_LIBRARY(git2 ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SR
 TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
 TARGET_OS_LIBRARIES(git2)
 
+# Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
+# Win64+MSVC+static libs = linker error
+IF(MSVC AND NOT BUILD_SHARED_LIBS AND (${CMAKE_SIZEOF_VOID_P} MATCHES "8") )
+  SET_TARGET_PROPERTIES(git2 PROPERTIES STATIC_LIBRARY_FLAGS "/MACHINE:x64")
+ENDIF()
+
 MSVC_SPLIT_SOURCES(git2)
 
 IF (SONAME)