2 # Copyright (c) 2020 Sibi Siddharthan
7 Instructions how to use this in Visual Studio:
9 Open the worktree as a folder. Visual Studio 2019 and later will detect
10 the CMake configuration automatically and set everything up for you,
11 ready to build. You can then run the tests in `t/` via a regular Git Bash.
13 Note: Visual Studio also has the option of opening `CMakeLists.txt`
14 directly; Using this option, Visual Studio will not find the source code,
15 though, therefore the `File>Open>Folder...` option is preferred.
17 Instructions to run CMake manually:
19 mkdir -p contrib/buildsystems/out
20 cd contrib/buildsystems/out
21 cmake ../ -DCMAKE_BUILD_TYPE=Release
23 This will build the git binaries in contrib/buildsystems/out
24 directory (our top-level .gitignore file knows to ignore contents of
27 Possible build configurations(-DCMAKE_BUILD_TYPE) with corresponding
31 RelWithDebInfo : -O2 -g
35 NOTE: -DCMAKE_BUILD_TYPE is optional. For multi-config generators like Visual Studio
36 this option is ignored
38 This process generates a Makefile(Linux/*BSD/MacOS) , Visual Studio solution(Windows) by default.
39 Run `make` to build Git on Linux/*BSD/MacOS.
40 Open git.sln on Windows and build Git.
42 NOTE: By default CMake uses Makefile as the build tool on Linux and Visual Studio in Windows,
43 to use another tool say `ninja` add this to the command line when configuring.
47 cmake_minimum_required(VERSION 3.14)
49 #set the source directory to root of git
50 set(CMAKE_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
52 set(VCPKG_DIR "${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg")
53 if(MSVC AND NOT EXISTS ${VCPKG_DIR})
54 message("Initializing vcpkg and building the Git's dependencies (this will take a while...)")
55 execute_process(COMMAND ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg_install.bat)
57 list(APPEND CMAKE_PREFIX_PATH "${VCPKG_DIR}/installed/x64-windows")
59 # In the vcpkg edition, we need this to be able to link to libcurl
60 set(CURL_NO_CURL_CMAKE ON)
63 find_program(SH_EXE sh PATHS "C:/Program Files/Git/bin")
65 message(FATAL_ERROR "sh: shell interpreter was not found in your path, please install one."
66 "On Windows, you can get it as part of 'Git for Windows' install at https://gitforwindows.org/")
69 #Create GIT-VERSION-FILE using GIT-VERSION-GEN
70 if(NOT EXISTS ${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE)
71 message("Generating GIT-VERSION-FILE")
72 execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN
73 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
76 #Parse GIT-VERSION-FILE to get the version
77 file(STRINGS ${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE git_version REGEX "GIT_VERSION = (.*)")
78 string(REPLACE "GIT_VERSION = " "" git_version ${git_version})
79 string(FIND ${git_version} "GIT" location)
81 string(REGEX MATCH "[0-9]*\\.[0-9]*\\.[0-9]*" git_version ${git_version})
83 string(REGEX MATCH "[0-9]*\\.[0-9]*" git_version ${git_version})
84 string(APPEND git_version ".0") #for building from a snapshot
88 VERSION ${git_version}
92 #TODO gitk git-gui gitweb
93 #TODO Enable NLS on windows natively
94 #TODO Add pcre support
96 #macros for parsing the Makefile for sources and scripts
97 macro(parse_makefile_for_sources list_var regex)
98 file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+=(.*)")
99 string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
100 string(REPLACE "$(COMPAT_OBJS)" "" ${list_var} ${${list_var}}) #remove "$(COMPAT_OBJS)" This is only for libgit.
101 string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
102 string(REPLACE ".o" ".c;" ${list_var} ${${list_var}}) #change .o to .c, ; is for converting the string into a list
103 list(TRANSFORM ${list_var} STRIP) #remove trailing/leading whitespaces for each element in list
104 list(REMOVE_ITEM ${list_var} "") #remove empty list elements
107 macro(parse_makefile_for_scripts list_var regex lang)
108 file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+=(.*)")
109 string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
110 string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
111 string(REPLACE " " ";" ${list_var} ${${list_var}}) #convert string to a list
112 if(NOT ${lang}) #exclude for SCRIPT_LIB
113 list(TRANSFORM ${list_var} REPLACE "${lang}" "") #do the replacement
117 macro(parse_makefile_for_executables list_var regex)
118 file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+= git-(.*)")
119 string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
120 string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
121 string(REPLACE "git-" "" ${list_var} ${${list_var}}) #strip `git-` prefix
122 string(REPLACE "\$X" ";" ${list_var} ${${list_var}}) #strip $X, ; is for converting the string into a list
123 list(TRANSFORM ${list_var} STRIP) #remove trailing/leading whitespaces for each element in list
124 list(REMOVE_ITEM ${list_var} "") #remove empty list elements
127 include(CheckTypeSize)
128 include(CheckCSourceRuns)
129 include(CheckCSourceCompiles)
130 include(CheckIncludeFile)
131 include(CheckFunctionExists)
132 include(CheckSymbolExists)
133 include(CheckStructHasMember)
136 find_package(ZLIB REQUIRED)
141 #Don't use libintl on Windows Visual Studio and Clang builds
142 if(NOT (WIN32 AND (CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")))
147 add_compile_definitions(NO_GETTEXT)
149 add_compile_definitions(NO_ICONV)
153 include_directories(SYSTEM ${ZLIB_INCLUDE_DIRS})
155 include_directories(SYSTEM ${CURL_INCLUDE_DIRS})
158 include_directories(SYSTEM ${EXPAT_INCLUDE_DIRS})
161 include_directories(SYSTEM ${Iconv_INCLUDE_DIRS})
164 include_directories(SYSTEM ${Intl_INCLUDE_DIRS})
168 if(WIN32 AND NOT MSVC)#not required for visual studio builds
169 find_program(WINDRES_EXE windres)
171 message(FATAL_ERROR "Install windres on Windows for resource files")
175 find_program(MSGFMT_EXE msgfmt)
177 set(MSGFMT_EXE ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg/downloads/tools/msys2/msys64/usr/bin/msgfmt.exe)
178 if(NOT EXISTS ${MSGFMT_EXE})
179 message(WARNING "Text Translations won't be built")
184 #Force all visual studio outputs to CMAKE_BINARY_DIR
185 if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
186 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR})
187 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR})
188 add_compile_options(/MP)
192 include_directories(${CMAKE_SOURCE_DIR})
193 add_compile_definitions(GIT_HOST_CPU="${CMAKE_SYSTEM_PROCESSOR}")
194 add_compile_definitions(SHA256_BLK INTERNAL_QSORT RUNTIME_PREFIX)
195 add_compile_definitions(NO_OPENSSL SHA1_DC SHA1DC_NO_STANDARD_INCLUDES
196 SHA1DC_INIT_SAFE_HASH_DEFAULT=0
197 SHA1DC_CUSTOM_INCLUDE_SHA1_C="cache.h"
198 SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C="git-compat-util.h" )
199 list(APPEND compat_SOURCES sha1dc_git.c sha1dc/sha1.c sha1dc/ubc_check.c block-sha1/sha1.c sha256/block/sha256.c compat/qsort_s.c)
202 add_compile_definitions(PAGER_ENV="LESS=FRX LV=-c"
203 ETC_GITATTRIBUTES="etc/gitattributes"
204 ETC_GITCONFIG="etc/gitconfig"
205 GIT_EXEC_PATH="libexec/git-core"
206 GIT_LOCALE_PATH="share/locale"
207 GIT_MAN_PATH="share/man"
208 GIT_INFO_PATH="share/info"
209 GIT_HTML_PATH="share/doc/git-doc"
210 DEFAULT_HELP_FORMAT="html"
211 DEFAULT_GIT_TEMPLATE_DIR="share/git-core/templates"
212 GIT_VERSION="${PROJECT_VERSION}.GIT"
213 GIT_USER_AGENT="git/${PROJECT_VERSION}.GIT"
215 GIT_BUILT_FROM_COMMIT="")
218 set(FALLBACK_RUNTIME_PREFIX /mingw64)
219 add_compile_definitions(FALLBACK_RUNTIME_PREFIX="${FALLBACK_RUNTIME_PREFIX}")
221 set(FALLBACK_RUNTIME_PREFIX /home/$ENV{USER})
222 add_compile_definitions(FALLBACK_RUNTIME_PREFIX="${FALLBACK_RUNTIME_PREFIX}")
227 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
228 if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
229 include_directories(${CMAKE_SOURCE_DIR}/compat/vcbuild/include)
230 add_compile_definitions(_CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
232 include_directories(${CMAKE_SOURCE_DIR}/compat/win32)
233 add_compile_definitions(HAVE_ALLOCA_H NO_POSIX_GOODIES NATIVE_CRLF NO_UNIX_SOCKETS WIN32
234 _CONSOLE DETECT_MSYS_TTY STRIP_EXTENSION=".exe" NO_SYMLINK_HEAD UNRELIABLE_FSTAT
235 NOGDI OBJECT_CREATION_MODE=1 __USE_MINGW_ANSI_STDIO=0
236 USE_NED_ALLOCATOR OVERRIDE_STRDUP MMAP_PREVENTS_DELETE USE_WIN32_MMAP
237 UNICODE _UNICODE HAVE_WPGMPTR ENSURE_MSYSTEM_IS_SET)
238 list(APPEND compat_SOURCES compat/mingw.c compat/winansi.c compat/win32/path-utils.c
239 compat/win32/pthread.c compat/win32mmap.c compat/win32/syslog.c
240 compat/win32/trace2_win32_process_info.c compat/win32/dirent.c
241 compat/nedmalloc/nedmalloc.c compat/strdup.c)
242 set(NO_UNIX_SOCKETS 1)
244 elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
245 add_compile_definitions(PROCFS_EXECUTABLE_PATH="/proc/self/exe" HAVE_DEV_TTY )
246 list(APPEND compat_SOURCES unix-socket.c unix-stream-server.c)
249 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
250 list(APPEND compat_SOURCES compat/simple-ipc/ipc-shared.c compat/simple-ipc/ipc-win32.c)
252 list(APPEND compat_SOURCES compat/simple-ipc/ipc-shared.c compat/simple-ipc/ipc-unix-socket.c)
255 set(EXE_EXTENSION ${CMAKE_EXECUTABLE_SUFFIX})
258 check_include_file(libgen.h HAVE_LIBGEN_H)
259 if(NOT HAVE_LIBGEN_H)
260 add_compile_definitions(NO_LIBGEN_H)
261 list(APPEND compat_SOURCES compat/basename.c)
264 check_include_file(sys/sysinfo.h HAVE_SYSINFO)
266 add_compile_definitions(HAVE_SYSINFO)
269 check_c_source_compiles("
274 char *p = (char *) alloca(2 * sizeof(int));
282 add_compile_definitions(HAVE_ALLOCA_H)
285 check_include_file(strings.h HAVE_STRINGS_H)
287 add_compile_definitions(HAVE_STRINGS_H)
290 check_include_file(sys/select.h HAVE_SYS_SELECT_H)
291 if(NOT HAVE_SYS_SELECT_H)
292 add_compile_definitions(NO_SYS_SELECT_H)
295 check_include_file(sys/poll.h HAVE_SYS_POLL_H)
296 if(NOT HAVE_SYS_POLL_H)
297 add_compile_definitions(NO_SYS_POLL_H)
300 check_include_file(poll.h HAVE_POLL_H)
302 add_compile_definitions(NO_POLL_H)
305 check_include_file(inttypes.h HAVE_INTTYPES_H)
306 if(NOT HAVE_INTTYPES_H)
307 add_compile_definitions(NO_INTTYPES_H)
310 check_include_file(paths.h HAVE_PATHS_H)
312 add_compile_definitions(HAVE_PATHS_H)
317 strcasestr memmem strlcpy strtoimax strtoumax strtoull
318 setenv mkdtemp poll pread memmem)
320 #unsetenv,hstrerror are incompatible with windows build
322 list(APPEND function_checks unsetenv hstrerror)
325 foreach(f ${function_checks})
326 string(TOUPPER ${f} uf)
327 check_function_exists(${f} HAVE_${uf})
329 add_compile_definitions(NO_${uf})
333 if(NOT HAVE_POLL_H OR NOT HAVE_SYS_POLL_H OR NOT HAVE_POLL)
334 include_directories(${CMAKE_SOURCE_DIR}/compat/poll)
335 add_compile_definitions(NO_POLL)
336 list(APPEND compat_SOURCES compat/poll/poll.c)
339 if(NOT HAVE_STRCASESTR)
340 list(APPEND compat_SOURCES compat/strcasestr.c)
344 list(APPEND compat_SOURCES compat/strlcpy.c)
347 if(NOT HAVE_STRTOUMAX)
348 list(APPEND compat_SOURCES compat/strtoumax.c compat/strtoimax.c)
352 list(APPEND compat_SOURCES compat/setenv.c)
356 list(APPEND compat_SOURCES compat/mkdtemp.c)
360 list(APPEND compat_SOURCES compat/pread.c)
364 list(APPEND compat_SOURCES compat/memmem.c)
368 if(NOT HAVE_UNSETENV)
369 list(APPEND compat_SOURCES compat/unsetenv.c)
372 if(NOT HAVE_HSTRERROR)
373 list(APPEND compat_SOURCES compat/hstrerror.c)
377 check_function_exists(getdelim HAVE_GETDELIM)
379 add_compile_definitions(HAVE_GETDELIM)
382 check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
383 check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_CLOCK_MONOTONIC)
384 if(HAVE_CLOCK_GETTIME)
385 add_compile_definitions(HAVE_CLOCK_GETTIME)
387 if(HAVE_CLOCK_MONOTONIC)
388 add_compile_definitions(HAVE_CLOCK_MONOTONIC)
391 #check for st_blocks in struct stat
392 check_struct_has_member("struct stat" st_blocks "sys/stat.h" STRUCT_STAT_HAS_ST_BLOCKS)
393 if(NOT STRUCT_STAT_HAS_ST_BLOCKS)
394 add_compile_definitions(NO_ST_BLOCKS_IN_STRUCT_STAT)
398 check_c_source_runs("
404 int test_vsnprintf(char *str, size_t maxsize, const char *format, ...)
409 va_start(ap, format);
410 ret = vsnprintf(str, maxsize, format, ap);
419 if (test_vsnprintf(buf, 3, \"%s\", \"12345\") != 5
420 || strcmp(buf, \"12\"))
422 if (snprintf(buf, 3, \"%s\", \"12345\") != 5
423 || strcmp(buf, \"12\"))
429 add_compile_definitions(SNPRINTF_RETURNS_BOGUS)
430 list(APPEND compat_SOURCES compat/snprintf.c)
433 check_c_source_runs("
438 FILE *f = fopen(\".\", \"r\");
442 FREAD_READS_DIRECTORIES_NO)
443 if(NOT FREAD_READS_DIRECTORIES_NO)
444 add_compile_definitions(FREAD_READS_DIRECTORIES)
445 list(APPEND compat_SOURCES compat/fopen.c)
448 check_c_source_compiles("
451 #error oops we dont have it
460 include_directories(${CMAKE_SOURCE_DIR}/compat/regex)
461 list(APPEND compat_SOURCES compat/regex/regex.c )
462 add_compile_definitions(NO_REGEX NO_MBSUPPORT GAWK)
466 check_c_source_compiles("
468 #include <sys/types.h>
469 #include <sys/sysctl.h>
479 return sysctl(mib, 2, &val, &len, NULL, 0) ? 1 : 0;
483 add_compile_definitions(HAVE_BSD_SYSCTL)
486 set(CMAKE_REQUIRED_LIBRARIES ${Iconv_LIBRARIES})
487 set(CMAKE_REQUIRED_INCLUDES ${Iconv_INCLUDE_DIRS})
489 check_c_source_compiles("
492 extern size_t iconv(iconv_t cd,
493 char **inbuf, size_t *inbytesleft,
494 char **outbuf, size_t *outbytesleft);
502 set(HAVE_OLD_ICONV 0)
504 set(HAVE_OLD_ICONV 1)
507 check_c_source_runs("
509 #if ${HAVE_OLD_ICONV}
510 typedef const char *iconv_ibp;
512 typedef char *iconv_ibp;
523 size_t isz = sizeof(in);
524 size_t osz = sizeof(out);
526 conv = iconv_open(\"UTF-16\", \"UTF-8\");
527 iconv(conv, &pin, &isz, &pout, &osz);
529 v = (unsigned char)(out[0]) + (unsigned char)(out[1]);
530 return v != 0xfe + 0xff;
532 ICONV_DOESNOT_OMIT_BOM)
533 if(NOT ICONV_DOESNOT_OMIT_BOM)
534 add_compile_definitions(ICONV_OMITS_BOM)
537 unset(CMAKE_REQUIRED_LIBRARIES)
538 unset(CMAKE_REQUIRED_INCLUDES)
543 git git-daemon git-http-backend git-sh-i18n--envsubst
547 list(APPEND excluded_progs git-http-fetch git-http-push)
548 add_compile_definitions(NO_CURL)
549 message(WARNING "git-http-push and git-http-fetch will not be built")
551 list(APPEND PROGRAMS_BUILT git-http-fetch git-http-push git-imap-send git-remote-http)
552 if(CURL_VERSION_STRING VERSION_GREATER_EQUAL 7.34.0)
553 add_compile_definitions(USE_CURL_FOR_IMAP_SEND)
558 list(APPEND excluded_progs git-http-push)
559 add_compile_definitions(NO_EXPAT)
561 list(APPEND PROGRAMS_BUILT git-http-push)
562 if(EXPAT_VERSION_STRING VERSION_LESS_EQUAL 1.2)
563 add_compile_definitions(EXPAT_NEEDS_XMLPARSE_H)
567 list(REMOVE_DUPLICATES excluded_progs)
568 list(REMOVE_DUPLICATES PROGRAMS_BUILT)
571 foreach(p ${excluded_progs})
572 list(APPEND EXCLUSION_PROGS --exclude-program ${p} )
575 #for comparing null values
576 list(APPEND EXCLUSION_PROGS empty)
577 set(EXCLUSION_PROGS_CACHE ${EXCLUSION_PROGS} CACHE STRING "Programs not built" FORCE)
579 if(NOT EXISTS ${CMAKE_BINARY_DIR}/command-list.h OR NOT EXCLUSION_PROGS_CACHE STREQUAL EXCLUSION_PROGS)
580 list(REMOVE_ITEM EXCLUSION_PROGS empty)
581 message("Generating command-list.h")
582 execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-cmdlist.sh ${EXCLUSION_PROGS} command-list.txt
583 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
584 OUTPUT_FILE ${CMAKE_BINARY_DIR}/command-list.h)
587 if(NOT EXISTS ${CMAKE_BINARY_DIR}/config-list.h)
588 message("Generating config-list.h")
589 execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-configlist.sh
590 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
591 OUTPUT_FILE ${CMAKE_BINARY_DIR}/config-list.h)
594 include_directories(${CMAKE_BINARY_DIR})
598 parse_makefile_for_sources(libgit_SOURCES "LIB_OBJS")
600 list(TRANSFORM libgit_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
601 list(TRANSFORM compat_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
602 add_library(libgit ${libgit_SOURCES} ${compat_SOURCES})
605 parse_makefile_for_sources(libxdiff_SOURCES "XDIFF_OBJS")
607 list(TRANSFORM libxdiff_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
608 add_library(xdiff STATIC ${libxdiff_SOURCES})
611 if(NOT MSVC)#use windres when compiling with gcc and clang
612 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
613 COMMAND ${WINDRES_EXE} -O coff -DMAJOR=${PROJECT_VERSION_MAJOR} -DMINOR=${PROJECT_VERSION_MINOR}
614 -DMICRO=${PROJECT_VERSION_PATCH} -DPATCHLEVEL=0 -DGIT_VERSION="\\\"${PROJECT_VERSION}.GIT\\\""
615 -i ${CMAKE_SOURCE_DIR}/git.rc -o ${CMAKE_BINARY_DIR}/git.res
616 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
619 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
620 COMMAND ${CMAKE_RC_COMPILER} /d MAJOR=${PROJECT_VERSION_MAJOR} /d MINOR=${PROJECT_VERSION_MINOR}
621 /d MICRO=${PROJECT_VERSION_PATCH} /d PATCHLEVEL=0 /d GIT_VERSION="${PROJECT_VERSION}.GIT"
622 /fo ${CMAKE_BINARY_DIR}/git.res ${CMAKE_SOURCE_DIR}/git.rc
623 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
626 add_custom_target(git-rc DEPENDS ${CMAKE_BINARY_DIR}/git.res)
629 #link all required libraries to common-main
630 add_library(common-main OBJECT ${CMAKE_SOURCE_DIR}/common-main.c)
632 target_link_libraries(common-main libgit xdiff ${ZLIB_LIBRARIES})
634 target_link_libraries(common-main ${Intl_LIBRARIES})
637 target_link_libraries(common-main ${Iconv_LIBRARIES})
640 target_link_libraries(common-main ws2_32 ntdll ${CMAKE_BINARY_DIR}/git.res)
641 add_dependencies(common-main git-rc)
642 if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
643 target_link_options(common-main PUBLIC -municode -Wl,--nxcompat -Wl,--dynamicbase -Wl,--pic-executable,-e,mainCRTStartup)
644 elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
645 target_link_options(common-main PUBLIC -municode -Wl,-nxcompat -Wl,-dynamicbase -Wl,-entry:wmainCRTStartup -Wl,invalidcontinue.obj)
646 elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
647 target_link_options(common-main PUBLIC /IGNORE:4217 /IGNORE:4049 /NOLOGO /ENTRY:wmainCRTStartup /SUBSYSTEM:CONSOLE invalidcontinue.obj)
649 message(FATAL_ERROR "Unhandled compiler: ${CMAKE_C_COMPILER_ID}")
652 target_link_libraries(common-main pthread rt)
656 parse_makefile_for_sources(git_SOURCES "BUILTIN_OBJS")
658 list(TRANSFORM git_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
659 add_executable(git ${CMAKE_SOURCE_DIR}/git.c ${git_SOURCES})
660 target_link_libraries(git common-main)
662 add_executable(git-daemon ${CMAKE_SOURCE_DIR}/daemon.c)
663 target_link_libraries(git-daemon common-main)
665 add_executable(git-http-backend ${CMAKE_SOURCE_DIR}/http-backend.c)
666 target_link_libraries(git-http-backend common-main)
668 add_executable(git-sh-i18n--envsubst ${CMAKE_SOURCE_DIR}/sh-i18n--envsubst.c)
669 target_link_libraries(git-sh-i18n--envsubst common-main)
671 add_executable(git-shell ${CMAKE_SOURCE_DIR}/shell.c)
672 target_link_libraries(git-shell common-main)
675 add_library(http_obj OBJECT ${CMAKE_SOURCE_DIR}/http.c)
677 add_executable(git-imap-send ${CMAKE_SOURCE_DIR}/imap-send.c)
678 target_link_libraries(git-imap-send http_obj common-main ${CURL_LIBRARIES})
680 add_executable(git-http-fetch ${CMAKE_SOURCE_DIR}/http-walker.c ${CMAKE_SOURCE_DIR}/http-fetch.c)
681 target_link_libraries(git-http-fetch http_obj common-main ${CURL_LIBRARIES})
683 add_executable(git-remote-http ${CMAKE_SOURCE_DIR}/http-walker.c ${CMAKE_SOURCE_DIR}/remote-curl.c)
684 target_link_libraries(git-remote-http http_obj common-main ${CURL_LIBRARIES} )
687 add_executable(git-http-push ${CMAKE_SOURCE_DIR}/http-push.c)
688 target_link_libraries(git-http-push http_obj common-main ${CURL_LIBRARIES} ${EXPAT_LIBRARIES})
692 parse_makefile_for_executables(git_builtin_extra "BUILT_INS")
695 foreach(s ${git_SOURCES} ${git_builtin_extra})
696 string(REPLACE "${CMAKE_SOURCE_DIR}/builtin/" "" s ${s})
697 string(REPLACE ".c" "" s ${s})
698 file(APPEND ${CMAKE_BINARY_DIR}/CreateLinks.cmake "file(CREATE_LINK git${EXE_EXTENSION} git-${s}${EXE_EXTENSION})\n")
699 list(APPEND git_links ${CMAKE_BINARY_DIR}/git-${s}${EXE_EXTENSION})
704 git-remote-https git-remote-ftp git-remote-ftps)
705 foreach(s ${remote_exes})
706 file(APPEND ${CMAKE_BINARY_DIR}/CreateLinks.cmake "file(CREATE_LINK git-remote-http${EXE_EXTENSION} ${s}${EXE_EXTENSION})\n")
707 list(APPEND git_http_links ${CMAKE_BINARY_DIR}/${s}${EXE_EXTENSION})
711 add_custom_command(OUTPUT ${git_links} ${git_http_links}
712 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/CreateLinks.cmake
713 DEPENDS git git-remote-http)
714 add_custom_target(git-links ALL DEPENDS ${git_links} ${git_http_links})
717 #creating required scripts
718 set(SHELL_PATH /bin/sh)
719 set(PERL_PATH /usr/bin/perl)
720 set(LOCALEDIR ${FALLBACK_RUNTIME_PREFIX}/share/locale)
721 set(GITWEBDIR ${FALLBACK_RUNTIME_PREFIX}/share/locale)
722 set(INSTLIBDIR ${FALLBACK_RUNTIME_PREFIX}/share/perl5)
725 parse_makefile_for_scripts(git_sh_scripts "SCRIPT_SH" ".sh")
726 parse_makefile_for_scripts(git_shlib_scripts "SCRIPT_LIB" "")
727 set(git_shell_scripts
728 ${git_sh_scripts} ${git_shlib_scripts} git-instaweb)
730 foreach(script ${git_shell_scripts})
731 file(STRINGS ${CMAKE_SOURCE_DIR}/${script}.sh content NEWLINE_CONSUME)
732 string(REPLACE "@SHELL_PATH@" "${SHELL_PATH}" content "${content}")
733 string(REPLACE "@@DIFF@@" "diff" content "${content}")
734 string(REPLACE "@LOCALEDIR@" "${LOCALEDIR}" content "${content}")
735 string(REPLACE "@GITWEBDIR@" "${GITWEBDIR}" content "${content}")
736 string(REPLACE "@@NO_CURL@@" "" content "${content}")
737 string(REPLACE "@@USE_GETTEXT_SCHEME@@" "" content "${content}")
738 string(REPLACE "# @@BROKEN_PATH_FIX@@" "" content "${content}")
739 string(REPLACE "@@PERL@@" "${PERL_PATH}" content "${content}")
740 string(REPLACE "@@SANE_TEXT_GREP@@" "-a" content "${content}")
741 string(REPLACE "@@PAGER_ENV@@" "LESS=FRX LV=-c" content "${content}")
742 file(WRITE ${CMAKE_BINARY_DIR}/${script} ${content})
746 parse_makefile_for_scripts(git_perl_scripts "SCRIPT_PERL" ".perl")
749 file(STRINGS ${CMAKE_SOURCE_DIR}/perl/header_templates/fixed_prefix.template.pl perl_header )
750 string(REPLACE "@@PATHSEP@@" ":" perl_header "${perl_header}")
751 string(REPLACE "@@INSTLIBDIR@@" "${INSTLIBDIR}" perl_header "${perl_header}")
753 foreach(script ${git_perl_scripts})
754 file(STRINGS ${CMAKE_SOURCE_DIR}/${script}.perl content NEWLINE_CONSUME)
755 string(REPLACE "#!/usr/bin/perl" "#!/usr/bin/perl\n${perl_header}\n" content "${content}")
756 string(REPLACE "@@GIT_VERSION@@" "${PROJECT_VERSION}" content "${content}")
757 file(WRITE ${CMAKE_BINARY_DIR}/${script} ${content})
761 file(STRINGS ${CMAKE_SOURCE_DIR}/git-p4.py content NEWLINE_CONSUME)
762 string(REPLACE "#!/usr/bin/env python" "#!/usr/bin/python" content "${content}")
763 file(WRITE ${CMAKE_BINARY_DIR}/git-p4 ${content})
766 file(GLOB_RECURSE perl_modules "${CMAKE_SOURCE_DIR}/perl/*.pm")
768 foreach(pm ${perl_modules})
769 string(REPLACE "${CMAKE_SOURCE_DIR}/perl/" "" file_path ${pm})
770 file(STRINGS ${pm} content NEWLINE_CONSUME)
771 string(REPLACE "@@LOCALEDIR@@" "${LOCALEDIR}" content "${content}")
772 string(REPLACE "@@NO_PERL_CPAN_FALLBACKS@@" "" content "${content}")
773 file(WRITE ${CMAKE_BINARY_DIR}/perl/build/lib/${file_path} ${content})
774 #test-lib.sh requires perl/build/lib to be the build directory of perl modules
779 file(GLOB templates "${CMAKE_SOURCE_DIR}/templates/*")
780 list(TRANSFORM templates REPLACE "${CMAKE_SOURCE_DIR}/templates/" "")
781 list(REMOVE_ITEM templates ".gitignore")
782 list(REMOVE_ITEM templates "Makefile")
783 list(REMOVE_ITEM templates "blt")# Prevents an error when reconfiguring for in source builds
785 list(REMOVE_ITEM templates "branches--")
786 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/templates/blt/branches) #create branches
788 #templates have @.*@ replacement so use configure_file instead
789 foreach(tm ${templates})
790 string(REPLACE "--" "/" blt_tm ${tm})
791 string(REPLACE "this" "" blt_tm ${blt_tm})# for this--
792 configure_file(${CMAKE_SOURCE_DIR}/templates/${tm} ${CMAKE_BINARY_DIR}/templates/blt/${blt_tm} @ONLY)
798 file(GLOB po_files "${CMAKE_SOURCE_DIR}/po/*.po")
799 list(TRANSFORM po_files REPLACE "${CMAKE_SOURCE_DIR}/po/" "")
800 list(TRANSFORM po_files REPLACE ".po" "")
801 foreach(po ${po_files})
802 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES)
803 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo
804 COMMAND ${MSGFMT_EXE} --check --statistics -o ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo ${CMAKE_SOURCE_DIR}/po/${po}.po)
805 list(APPEND po_gen ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo)
807 add_custom_target(po-gen ALL DEPENDS ${po_gen})
811 #to help with the install
812 list(TRANSFORM git_shell_scripts PREPEND "${CMAKE_BINARY_DIR}/")
813 list(TRANSFORM git_perl_scripts PREPEND "${CMAKE_BINARY_DIR}/")
816 install(TARGETS git git-shell
817 RUNTIME DESTINATION bin)
818 install(PROGRAMS ${CMAKE_BINARY_DIR}/git-cvsserver
821 list(REMOVE_ITEM PROGRAMS_BUILT git git-shell)
822 install(TARGETS ${PROGRAMS_BUILT}
823 RUNTIME DESTINATION libexec/git-core)
826 git-receive-pack git-upload-archive git-upload-pack)
828 foreach(b ${bin_links})
829 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/bin/${b}${EXE_EXTENSION})")
832 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git${EXE_EXTENSION})")
833 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git-shell${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git-shell${EXE_EXTENSION})")
835 foreach(b ${git_links})
836 string(REPLACE "${CMAKE_BINARY_DIR}" "" b ${b})
837 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/${b}${EXE_EXTENSION})")
840 foreach(b ${git_http_links})
841 string(REPLACE "${CMAKE_BINARY_DIR}" "" b ${b})
842 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git-remote-http${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/${b}${EXE_EXTENSION})")
845 install(PROGRAMS ${git_shell_scripts} ${git_perl_scripts} ${CMAKE_BINARY_DIR}/git-p4
846 DESTINATION libexec/git-core)
848 install(DIRECTORY ${CMAKE_SOURCE_DIR}/mergetools DESTINATION libexec/git-core)
849 install(DIRECTORY ${CMAKE_BINARY_DIR}/perl/build/lib/ DESTINATION share/perl5
850 FILES_MATCHING PATTERN "*.pm")
851 install(DIRECTORY ${CMAKE_BINARY_DIR}/templates/blt/ DESTINATION share/git-core/templates)
854 install(DIRECTORY ${CMAKE_BINARY_DIR}/po/build/locale DESTINATION share)
861 add_executable(test-fake-ssh ${CMAKE_SOURCE_DIR}/t/helper/test-fake-ssh.c)
862 target_link_libraries(test-fake-ssh common-main)
865 parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")
867 list(TRANSFORM test-tool_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/t/helper/")
868 add_executable(test-tool ${CMAKE_SOURCE_DIR}/t/helper/test-tool.c ${test-tool_SOURCES})
869 target_link_libraries(test-tool common-main)
871 set_target_properties(test-fake-ssh test-tool
872 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/helper)
875 set_target_properties(test-fake-ssh test-tool
876 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/helper)
877 set_target_properties(test-fake-ssh test-tool
878 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/helper)
883 git git-upload-pack git-receive-pack git-upload-archive git-shell git-remote-ext)
885 set(wrapper_test_scripts
886 test-fake-ssh test-tool)
889 foreach(script ${wrapper_scripts})
890 file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
891 string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
892 string(REPLACE "@@PROG@@" "${script}${EXE_EXTENSION}" content "${content}")
893 file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/${script} ${content})
896 foreach(script ${wrapper_test_scripts})
897 file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
898 string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
899 string(REPLACE "@@PROG@@" "t/helper/${script}${EXE_EXTENSION}" content "${content}")
900 file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/${script} ${content})
903 file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
904 string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
905 string(REPLACE "@@PROG@@" "git-cvsserver" content "${content}")
906 file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/git-cvsserver ${content})
908 #options for configuring test options
909 option(PERL_TESTS "Perform tests that use perl" ON)
910 option(PYTHON_TESTS "Perform tests that use python" ON)
913 set(TEST_SHELL_PATH ${SHELL_PATH})
915 set(PYTHON_PATH /usr/bin/python)
923 set(PAGER_ENV "LESS=FRX LV=-c")
924 set(DC_SHA1 YesPlease)
925 set(RUNTIME_PREFIX true)
948 file(WRITE ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "SHELL_PATH='${SHELL_PATH}'\n")
949 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "TEST_SHELL_PATH='${TEST_SHELL_PATH}'\n")
950 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PERL_PATH='${PERL_PATH}'\n")
951 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "DIFF='${DIFF}'\n")
952 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PYTHON_PATH='${PYTHON_PATH}'\n")
953 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "TAR='${TAR}'\n")
954 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_CURL='${NO_CURL}'\n")
955 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_EXPAT='${NO_EXPAT}'\n")
956 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PERL='${NO_PERL}'\n")
957 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PTHREADS='${NO_PTHREADS}'\n")
958 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_UNIX_SOCKETS='${NO_UNIX_SOCKETS}'\n")
959 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PAGER_ENV='${PAGER_ENV}'\n")
960 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "DC_SHA1='${DC_SHA1}'\n")
961 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "X='${EXE_EXTENSION}'\n")
962 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_GETTEXT='${NO_GETTEXT}'\n")
963 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "RUNTIME_PREFIX='${RUNTIME_PREFIX}'\n")
964 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PYTHON='${NO_PYTHON}'\n")
966 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PATH=\"$PATH:$TEST_DIRECTORY/../compat/vcbuild/vcpkg/installed/x64-windows/bin\"\n")
969 #Make the tests work when building out of the source tree
970 get_filename_component(CACHE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../CMakeCache.txt ABSOLUTE)
971 if(NOT ${CMAKE_BINARY_DIR}/CMakeCache.txt STREQUAL ${CACHE_PATH})
972 file(RELATIVE_PATH BUILD_DIR_RELATIVE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}/CMakeCache.txt)
973 string(REPLACE "/CMakeCache.txt" "" BUILD_DIR_RELATIVE ${BUILD_DIR_RELATIVE})
974 #Setting the build directory in test-lib.sh before running tests
975 file(WRITE ${CMAKE_BINARY_DIR}/CTestCustom.cmake
976 "file(STRINGS ${CMAKE_SOURCE_DIR}/t/test-lib.sh GIT_BUILD_DIR_REPL REGEX \"GIT_BUILD_DIR=(.*)\")\n"
977 "file(STRINGS ${CMAKE_SOURCE_DIR}/t/test-lib.sh content NEWLINE_CONSUME)\n"
978 "string(REPLACE \"\${GIT_BUILD_DIR_REPL}\" \"GIT_BUILD_DIR=\\\"$TEST_DIRECTORY/../${BUILD_DIR_RELATIVE}\\\"\" content \"\${content}\")\n"
979 "file(WRITE ${CMAKE_SOURCE_DIR}/t/test-lib.sh \${content})")
981 file(COPY ${CMAKE_SOURCE_DIR}/t/chainlint.sed DESTINATION ${CMAKE_BINARY_DIR}/t/)
982 file(COPY ${CMAKE_SOURCE_DIR}/po/is.po DESTINATION ${CMAKE_BINARY_DIR}/po/)
983 file(COPY ${CMAKE_SOURCE_DIR}/mergetools/tkdiff DESTINATION ${CMAKE_BINARY_DIR}/mergetools/)
984 file(COPY ${CMAKE_SOURCE_DIR}/contrib/completion/git-prompt.sh DESTINATION ${CMAKE_BINARY_DIR}/contrib/completion/)
985 file(COPY ${CMAKE_SOURCE_DIR}/contrib/completion/git-completion.bash DESTINATION ${CMAKE_BINARY_DIR}/contrib/completion/)
988 file(GLOB test_scipts "${CMAKE_SOURCE_DIR}/t/t[0-9]*.sh")
991 foreach(tsh ${test_scipts})
993 COMMAND ${SH_EXE} ${tsh}
994 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t)
997 endif()#BUILD_TESTING