Skip to content

Commit 7f475e2

Browse files
SibiSiddharthangitster
authored andcommitted
cmake: support for building git on windows with msvc and clang.
This patch adds support for Visual Studio and Clang builds The minimum required version of CMake is upgraded to 3.15 because this version offers proper support for Clang builds on Windows. Libintl is not searched for when building with Visual Studio or Clang because there is no binary compatible version available yet. NOTE: In the link options invalidcontinue.obj has to be included. The reason for this is because by default, Windows calls abort()'s instead of setting errno=EINVAL when invalid arguments are passed to standard functions. This commit explains it in detail: 4b623d8 On Windows the default generator is Visual Studio,so for Visual Studio builds do this: cmake `relative-path-to-srcdir` NOTE: Visual Studio generator is a multi config generator, which means that Debug and Release builds can be done on the same build directory. For Clang builds do this: On bash CC=clang cmake `relative-path-to-srcdir` -G Ninja -DCMAKE_BUILD_TYPE=[Debug or Release] On cmd set CC=Clang cmake `relative-path-to-srcdir` -G Ninja -DCMAKE_BUILD_TYPE=[Debug or Release] Signed-off-by: Sibi Siddharthan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f7adba4 commit 7f475e2

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

contrib/buildsystems/CMakeLists.txt

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ find_package(ZLIB REQUIRED)
108108
find_package(CURL)
109109
find_package(EXPAT)
110110
find_package(Iconv)
111-
find_package(Intl)
112111

112+
#Don't use libintl on Windows Visual Studio and Clang builds
113+
if(NOT (WIN32 AND (CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")))
114+
find_package(Intl)
115+
endif()
113116

114117
if(NOT Intl_FOUND)
115118
add_compile_definitions(NO_GETTEXT)
@@ -133,7 +136,7 @@ if(Intl_FOUND)
133136
endif()
134137

135138

136-
if(WIN32)
139+
if(WIN32 AND NOT MSVC)#not required for visual studio builds
137140
find_program(WINDRES_EXE windres)
138141
if(NOT WINDRES_EXE)
139142
message(FATAL_ERROR "Install windres on Windows for resource files")
@@ -145,6 +148,13 @@ if(NOT MSGFMT_EXE)
145148
message(WARNING "Text Translations won't be build")
146149
endif()
147150

151+
#Force all visual studio outputs to CMAKE_BINARY_DIR
152+
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
153+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR})
154+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR})
155+
add_compile_options(/MP)
156+
endif()
157+
148158
#default behaviour
149159
include_directories(${CMAKE_SOURCE_DIR})
150160
add_compile_definitions(GIT_HOST_CPU="${CMAKE_SYSTEM_PROCESSOR}")
@@ -182,6 +192,10 @@ endif()
182192

183193
#Platform Specific
184194
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
195+
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
196+
include_directories(${CMAKE_SOURCE_DIR}/compat/vcbuild/include)
197+
add_compile_definitions(_CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
198+
endif()
185199
include_directories(${CMAKE_SOURCE_DIR}/compat/win32)
186200
add_compile_definitions(HAVE_ALLOCA_H NO_POSIX_GOODIES NATIVE_CRLF NO_UNIX_SOCKETS WIN32
187201
_CONSOLE DETECT_MSYS_TTY STRIP_EXTENSION=".exe" NO_SYMLINK_HEAD UNRELIABLE_FSTAT
@@ -566,14 +580,22 @@ parse_makefile_for_sources(libvcs-svn_SOURCES "VCSSVN_OBJS")
566580
list(TRANSFORM libvcs-svn_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
567581
add_library(vcs-svn STATIC ${libvcs-svn_SOURCES})
568582

569-
#add git.rc for gcc
570583
if(WIN32)
571-
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
572-
COMMAND ${WINDRES_EXE} -O coff -DMAJOR=${PROJECT_VERSION_MAJOR} -DMINOR=${PROJECT_VERSION_MINOR}
573-
-DMICRO=${PROJECT_VERSION_PATCH} -DPATCHLEVEL=0 -DGIT_VERSION="\\\"${PROJECT_VERSION}.GIT\\\""
574-
-i ${CMAKE_SOURCE_DIR}/git.rc -o ${CMAKE_BINARY_DIR}/git.res
575-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
576-
VERBATIM)
584+
if(NOT MSVC)#use windres when compiling with gcc and clang
585+
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
586+
COMMAND ${WINDRES_EXE} -O coff -DMAJOR=${PROJECT_VERSION_MAJOR} -DMINOR=${PROJECT_VERSION_MINOR}
587+
-DMICRO=${PROJECT_VERSION_PATCH} -DPATCHLEVEL=0 -DGIT_VERSION="\\\"${PROJECT_VERSION}.GIT\\\""
588+
-i ${CMAKE_SOURCE_DIR}/git.rc -o ${CMAKE_BINARY_DIR}/git.res
589+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
590+
VERBATIM)
591+
else()#MSVC use rc
592+
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
593+
COMMAND ${CMAKE_RC_COMPILER} /d MAJOR=${PROJECT_VERSION_MAJOR} /d MINOR=${PROJECT_VERSION_MINOR}
594+
/d MICRO=${PROJECT_VERSION_PATCH} /d PATCHLEVEL=0 /d GIT_VERSION="${PROJECT_VERSION}.GIT"
595+
/fo ${CMAKE_BINARY_DIR}/git.res ${CMAKE_SOURCE_DIR}/git.rc
596+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
597+
VERBATIM)
598+
endif()
577599
add_custom_target(git-rc DEPENDS ${CMAKE_BINARY_DIR}/git.res)
578600
endif()
579601

@@ -590,7 +612,13 @@ endif()
590612
if(WIN32)
591613
target_link_libraries(common-main ws2_32 ntdll ${CMAKE_BINARY_DIR}/git.res)
592614
add_dependencies(common-main git-rc)
593-
target_link_options(common-main PUBLIC -municode -Wl,--nxcompat -Wl,--dynamicbase -Wl,--pic-executable,-e,mainCRTStartup)
615+
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
616+
target_link_options(common-main PUBLIC -municode -Wl,--nxcompat -Wl,--dynamicbase -Wl,--pic-executable,-e,mainCRTStartup)
617+
elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
618+
target_link_options(common-main PUBLIC -municode -Wl,-nxcompat -Wl,-dynamicbase -Wl,-entry:wmainCRTStartup -Wl,invalidcontinue.obj)
619+
elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
620+
target_link_options(common-main PUBLIC /IGNORE:4217 /IGNORE:4049 /NOLOGO /ENTRY:wmainCRTStartup /SUBSYSTEM:CONSOLE invalidcontinue.obj)
621+
endif()
594622
elseif(UNIX)
595623
target_link_libraries(common-main pthread rt)
596624
endif()
@@ -844,6 +872,13 @@ target_link_libraries(test-tool common-main)
844872
set_target_properties(test-fake-ssh test-line-buffer test-svn-fe test-tool
845873
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/helper)
846874

875+
if(MSVC)
876+
set_target_properties(test-fake-ssh test-line-buffer test-svn-fe test-tool
877+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/helper)
878+
set_target_properties(test-fake-ssh test-line-buffer test-svn-fe test-tool
879+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/helper)
880+
endif()
881+
847882
#wrapper scripts
848883
set(wrapper_scripts
849884
git git-upload-pack git-receive-pack git-upload-archive git-shell git-remote-ext)

0 commit comments

Comments
 (0)