@matheusbsbs / matheus-bagliano / commits / 7f475e2780
Fork 0

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: 4b623d80f73528a632576990ca51e34c333d5dd6 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 <sibisiddharthan.github@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sibi Siddharthan committed Jun 26, 2020 at 16:11 UTC 7f475e2780fe3bf753372af9e8f33cff5df2e589
1 file changed +45 -10
contrib/buildsystems/CMakeLists.txt
+45 -10
@@ -108,8 +108,11 @@ find_package(ZLIB REQUIRED)
108 find_package(CURL)
109 find_package(EXPAT)
110 find_package(Iconv)
111 -find_package(Intl)
111
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()
116
117 if(NOT Intl_FOUND)
118 add_compile_definitions(NO_GETTEXT)
@@ -133,7 +136,7 @@ if(Intl_FOUND)
136 endif()
137
138
136 -if(WIN32)
139 +if(WIN32 AND NOT MSVC)#not required for visual studio builds
140 find_program(WINDRES_EXE windres)
141 if(NOT WINDRES_EXE)
142 message(FATAL_ERROR "Install windres on Windows for resource files")
@@ -145,6 +148,13 @@ if(NOT MSGFMT_EXE)
148 message(WARNING "Text Translations won't be build")
149 endif()
150
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 +
158 #default behaviour
159 include_directories(${CMAKE_SOURCE_DIR})
160 add_compile_definitions(GIT_HOST_CPU="${CMAKE_SYSTEM_PROCESSOR}")
@@ -182,6 +192,10 @@ endif()
192
193 #Platform Specific
194 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()
199 include_directories(${CMAKE_SOURCE_DIR}/compat/win32)
200 add_compile_definitions(HAVE_ALLOCA_H NO_POSIX_GOODIES NATIVE_CRLF NO_UNIX_SOCKETS WIN32
201 _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")
580 list(TRANSFORM libvcs-svn_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
581 add_library(vcs-svn STATIC ${libvcs-svn_SOURCES})
582
569 -#add git.rc for gcc
583 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()
599 add_custom_target(git-rc DEPENDS ${CMAKE_BINARY_DIR}/git.res)
600 endif()
601
@@ -590,7 +612,13 @@ endif()
612 if(WIN32)
613 target_link_libraries(common-main ws2_32 ntdll ${CMAKE_BINARY_DIR}/git.res)
614 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()
622 elseif(UNIX)
623 target_link_libraries(common-main pthread rt)
624 endif()
@@ -844,6 +872,13 @@ target_link_libraries(test-tool common-main)
872 set_target_properties(test-fake-ssh test-line-buffer test-svn-fe test-tool
873 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/helper)
874
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 +
882 #wrapper scripts
883 set(wrapper_scripts
884 git git-upload-pack git-receive-pack git-upload-archive git-shell git-remote-ext)