From 08f32085ab31643c1e26f8401bba91582aed770f Mon Sep 17 00:00:00 2001 From: QbProg Date: Sat, 16 Mar 2013 17:38:27 +0100 Subject: [PATCH 1/2] Adds an option to select the CRT link mode ( static or dynamic ). This is useful when linking libgit2 statically, as the setting must match the linking program's one. --- CMakeLists.txt | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37a583099..1ab254f10 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) From d66a7c061a3b0809589de05eeac00725444db3aa Mon Sep 17 00:00:00 2001 From: QbProg Date: Sat, 16 Mar 2013 17:48:24 +0100 Subject: [PATCH 2/2] Fix for a cmake bug when using MSVC + Win64 + static libraries (see http://public.kitware.com/Bug/view.php?id=11240) --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ab254f10..dfca73630 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -282,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)