Merge branch 'hn/reftable'
[git/debian.git] / contrib / buildsystems / CMakeLists.txt
blob5100f56bb37a41f82f895cb14f6311d5081a3482
2 #       Copyright (c) 2020 Sibi Siddharthan
5 #[[
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
25 this directory).
27 Possible build configurations(-DCMAKE_BUILD_TYPE) with corresponding
28 compiler flags
29 Debug : -g
30 Release: -O3
31 RelWithDebInfo : -O2 -g
32 MinSizeRel : -Os
33 empty(default) :
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.
44 `-G Ninja`
46 NOTE: By default CMake will install vcpkg locally to your source tree on configuration,
47 to avoid this, add `-DNO_VCPKG=TRUE` to the command line when configuring.
50 cmake_minimum_required(VERSION 3.14)
52 #set the source directory to root of git
53 set(CMAKE_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
55 option(USE_VCPKG "Whether or not to use vcpkg for obtaining dependencies.  Only applicable to Windows platforms" ON)
56 if(NOT WIN32)
57         set(USE_VCPKG OFF CACHE BOOL FORCE)
58 endif()
60 if(NOT DEFINED CMAKE_EXPORT_COMPILE_COMMANDS)
61         set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
62 endif()
64 if(USE_VCPKG)
65         set(VCPKG_DIR "${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg")
66         if(NOT EXISTS ${VCPKG_DIR})
67                 message("Initializing vcpkg and building the Git's dependencies (this will take a while...)")
68                 execute_process(COMMAND ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg_install.bat)
69         endif()
70         list(APPEND CMAKE_PREFIX_PATH "${VCPKG_DIR}/installed/x64-windows")
72         # In the vcpkg edition, we need this to be able to link to libcurl
73         set(CURL_NO_CURL_CMAKE ON)
75         # Copy the necessary vcpkg DLLs (like iconv) to the install dir
76         set(X_VCPKG_APPLOCAL_DEPS_INSTALL ON)
77         set(CMAKE_TOOLCHAIN_FILE ${VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file")
78 endif()
80 find_program(SH_EXE sh PATHS "C:/Program Files/Git/bin")
81 if(NOT SH_EXE)
82         message(FATAL_ERROR "sh: shell interpreter was not found in your path, please install one."
83                         "On Windows, you can get it as part of 'Git for Windows' install at https://gitforwindows.org/")
84 endif()
86 #Create GIT-VERSION-FILE using GIT-VERSION-GEN
87 if(NOT EXISTS ${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE)
88         message("Generating GIT-VERSION-FILE")
89         execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN
90                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
91 endif()
93 #Parse GIT-VERSION-FILE to get the version
94 file(STRINGS ${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE git_version REGEX "GIT_VERSION = (.*)")
95 string(REPLACE "GIT_VERSION = " "" git_version ${git_version})
96 string(FIND ${git_version} "GIT" location)
97 if(location EQUAL -1)
98         string(REGEX MATCH "[0-9]*\\.[0-9]*\\.[0-9]*" git_version ${git_version})
99 else()
100         string(REGEX MATCH "[0-9]*\\.[0-9]*" git_version ${git_version})
101         string(APPEND git_version ".0") #for building from a snapshot
102 endif()
104 project(git
105         VERSION ${git_version}
106         LANGUAGES C)
109 #TODO gitk git-gui gitweb
110 #TODO Enable NLS on windows natively
111 #TODO Add pcre support
113 #macros for parsing the Makefile for sources and scripts
114 macro(parse_makefile_for_sources list_var regex)
115         file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+=(.*)")
116         string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
117         string(REPLACE "$(COMPAT_OBJS)" "" ${list_var} ${${list_var}}) #remove "$(COMPAT_OBJS)" This is only for libgit.
118         string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
119         string(REPLACE ".o" ".c;" ${list_var} ${${list_var}}) #change .o to .c, ; is for converting the string into a list
120         list(TRANSFORM ${list_var} STRIP) #remove trailing/leading whitespaces for each element in list
121         list(REMOVE_ITEM ${list_var} "") #remove empty list elements
122 endmacro()
124 macro(parse_makefile_for_scripts list_var regex lang)
125         file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+=(.*)")
126         string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
127         string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
128         string(REPLACE " " ";" ${list_var} ${${list_var}}) #convert string to a list
129         if(NOT ${lang}) #exclude for SCRIPT_LIB
130                 list(TRANSFORM ${list_var} REPLACE "${lang}" "") #do the replacement
131         endif()
132 endmacro()
134 macro(parse_makefile_for_executables list_var regex)
135         file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+= git-(.*)")
136         string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
137         string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
138         string(REPLACE "git-" "" ${list_var} ${${list_var}}) #strip `git-` prefix
139         string(REPLACE "\$X" ";" ${list_var} ${${list_var}}) #strip $X, ; is for converting the string into a list
140         list(TRANSFORM ${list_var} STRIP) #remove trailing/leading whitespaces for each element in list
141         list(REMOVE_ITEM ${list_var} "") #remove empty list elements
142 endmacro()
144 include(CheckTypeSize)
145 include(CheckCSourceRuns)
146 include(CheckCSourceCompiles)
147 include(CheckIncludeFile)
148 include(CheckFunctionExists)
149 include(CheckSymbolExists)
150 include(CheckStructHasMember)
151 include(CTest)
153 find_package(ZLIB REQUIRED)
154 find_package(CURL)
155 find_package(EXPAT)
156 find_package(Iconv)
158 #Don't use libintl on Windows Visual Studio and Clang builds
159 if(NOT (WIN32 AND (CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")))
160         find_package(Intl)
161 endif()
163 if(NOT Intl_FOUND)
164         add_compile_definitions(NO_GETTEXT)
165         if(NOT Iconv_FOUND)
166                 add_compile_definitions(NO_ICONV)
167         endif()
168 endif()
170 include_directories(SYSTEM ${ZLIB_INCLUDE_DIRS})
171 if(CURL_FOUND)
172         include_directories(SYSTEM ${CURL_INCLUDE_DIRS})
173 endif()
174 if(EXPAT_FOUND)
175         include_directories(SYSTEM ${EXPAT_INCLUDE_DIRS})
176 endif()
177 if(Iconv_FOUND)
178         include_directories(SYSTEM ${Iconv_INCLUDE_DIRS})
179 endif()
180 if(Intl_FOUND)
181         include_directories(SYSTEM ${Intl_INCLUDE_DIRS})
182 endif()
185 if(WIN32 AND NOT MSVC)#not required for visual studio builds
186         find_program(WINDRES_EXE windres)
187         if(NOT WINDRES_EXE)
188                 message(FATAL_ERROR "Install windres on Windows for resource files")
189         endif()
190 endif()
192 if(NO_GETTEXT)
193         message(STATUS "msgfmt not used under NO_GETTEXT")
194 else()
195         find_program(MSGFMT_EXE msgfmt)
196         if(NOT MSGFMT_EXE)
197                 if(USE_VCPKG)
198                         set(MSGFMT_EXE ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg/downloads/tools/msys2/msys64/usr/bin/msgfmt.exe)
199                 endif()
200                 if(NOT EXISTS ${MSGFMT_EXE})
201                         message(WARNING "Text Translations won't be built")
202                         unset(MSGFMT_EXE)
203                 endif()
204         endif()
205 endif()
207 #Force all visual studio outputs to CMAKE_BINARY_DIR
208 if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
209         set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR})
210         set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR})
211         add_compile_options(/MP /std:c11)
212 endif()
214 #default behaviour
215 include_directories(${CMAKE_SOURCE_DIR})
216 add_compile_definitions(GIT_HOST_CPU="${CMAKE_SYSTEM_PROCESSOR}")
217 add_compile_definitions(SHA256_BLK INTERNAL_QSORT RUNTIME_PREFIX)
218 add_compile_definitions(NO_OPENSSL SHA1_DC SHA1DC_NO_STANDARD_INCLUDES
219                         SHA1DC_INIT_SAFE_HASH_DEFAULT=0
220                         SHA1DC_CUSTOM_INCLUDE_SHA1_C="cache.h"
221                         SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C="git-compat-util.h" )
222 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)
225 add_compile_definitions(PAGER_ENV="LESS=FRX LV=-c"
226                         GIT_EXEC_PATH="libexec/git-core"
227                         GIT_LOCALE_PATH="share/locale"
228                         GIT_MAN_PATH="share/man"
229                         GIT_INFO_PATH="share/info"
230                         GIT_HTML_PATH="share/doc/git-doc"
231                         DEFAULT_HELP_FORMAT="html"
232                         DEFAULT_GIT_TEMPLATE_DIR="share/git-core/templates"
233                         GIT_VERSION="${PROJECT_VERSION}.GIT"
234                         GIT_USER_AGENT="git/${PROJECT_VERSION}.GIT"
235                         BINDIR="bin"
236                         GIT_BUILT_FROM_COMMIT="")
238 if(WIN32)
239         set(FALLBACK_RUNTIME_PREFIX /mingw64)
240         # Move system config into top-level /etc/
241         add_compile_definitions(FALLBACK_RUNTIME_PREFIX="${FALLBACK_RUNTIME_PREFIX}"
242                 ETC_GITATTRIBUTES="../etc/gitattributes"
243                 ETC_GITCONFIG="../etc/gitconfig")
244 else()
245         set(FALLBACK_RUNTIME_PREFIX /home/$ENV{USER})
246         add_compile_definitions(FALLBACK_RUNTIME_PREFIX="${FALLBACK_RUNTIME_PREFIX}"
247                 ETC_GITATTRIBUTES="etc/gitattributes"
248                 ETC_GITCONFIG="etc/gitconfig")
249 endif()
252 #Platform Specific
253 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
254         if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
255                 include_directories(${CMAKE_SOURCE_DIR}/compat/vcbuild/include)
256                 add_compile_definitions(_CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
257         endif()
258         include_directories(${CMAKE_SOURCE_DIR}/compat/win32)
259         add_compile_definitions(HAVE_ALLOCA_H NO_POSIX_GOODIES NATIVE_CRLF NO_UNIX_SOCKETS WIN32
260                                 _CONSOLE DETECT_MSYS_TTY STRIP_EXTENSION=".exe"  NO_SYMLINK_HEAD UNRELIABLE_FSTAT
261                                 NOGDI OBJECT_CREATION_MODE=1 __USE_MINGW_ANSI_STDIO=0
262                                 USE_NED_ALLOCATOR OVERRIDE_STRDUP MMAP_PREVENTS_DELETE USE_WIN32_MMAP
263                                 UNICODE _UNICODE HAVE_WPGMPTR ENSURE_MSYSTEM_IS_SET)
264         list(APPEND compat_SOURCES compat/mingw.c compat/winansi.c compat/win32/path-utils.c
265                 compat/win32/pthread.c compat/win32mmap.c compat/win32/syslog.c
266                 compat/win32/trace2_win32_process_info.c compat/win32/dirent.c
267                 compat/nedmalloc/nedmalloc.c compat/strdup.c)
268         set(NO_UNIX_SOCKETS 1)
270 elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
271         add_compile_definitions(PROCFS_EXECUTABLE_PATH="/proc/self/exe" HAVE_DEV_TTY )
272         list(APPEND compat_SOURCES unix-socket.c unix-stream-server.c)
273 endif()
275 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
276         list(APPEND compat_SOURCES compat/simple-ipc/ipc-shared.c compat/simple-ipc/ipc-win32.c)
277         add_compile_definitions(SUPPORTS_SIMPLE_IPC)
278         set(SUPPORTS_SIMPLE_IPC 1)
279 else()
280         # Simple IPC requires both Unix sockets and pthreads on Unix-based systems.
281         if(NOT NO_UNIX_SOCKETS AND NOT NO_PTHREADS)
282                 list(APPEND compat_SOURCES compat/simple-ipc/ipc-shared.c compat/simple-ipc/ipc-unix-socket.c)
283                 add_compile_definitions(SUPPORTS_SIMPLE_IPC)
284                 set(SUPPORTS_SIMPLE_IPC 1)
285         endif()
286 endif()
288 set(EXE_EXTENSION ${CMAKE_EXECUTABLE_SUFFIX})
290 #header checks
291 check_include_file(libgen.h HAVE_LIBGEN_H)
292 if(NOT HAVE_LIBGEN_H)
293         add_compile_definitions(NO_LIBGEN_H)
294         list(APPEND compat_SOURCES compat/basename.c)
295 endif()
297 check_include_file(sys/sysinfo.h HAVE_SYSINFO)
298 if(HAVE_SYSINFO)
299         add_compile_definitions(HAVE_SYSINFO)
300 endif()
302 check_c_source_compiles("
303 #include <alloca.h>
305 int main(void)
307         char *p = (char *) alloca(2 * sizeof(int));
309         if (p)
310                 return 0;
311         return 0;
313 HAVE_ALLOCA_H)
314 if(HAVE_ALLOCA_H)
315         add_compile_definitions(HAVE_ALLOCA_H)
316 endif()
318 check_include_file(strings.h HAVE_STRINGS_H)
319 if(HAVE_STRINGS_H)
320         add_compile_definitions(HAVE_STRINGS_H)
321 endif()
323 check_include_file(sys/select.h HAVE_SYS_SELECT_H)
324 if(NOT HAVE_SYS_SELECT_H)
325         add_compile_definitions(NO_SYS_SELECT_H)
326 endif()
328 check_include_file(sys/poll.h HAVE_SYS_POLL_H)
329 if(NOT HAVE_SYS_POLL_H)
330         add_compile_definitions(NO_SYS_POLL_H)
331 endif()
333 check_include_file(poll.h HAVE_POLL_H)
334 if(NOT HAVE_POLL_H)
335         add_compile_definitions(NO_POLL_H)
336 endif()
338 check_include_file(inttypes.h HAVE_INTTYPES_H)
339 if(NOT HAVE_INTTYPES_H)
340         add_compile_definitions(NO_INTTYPES_H)
341 endif()
343 check_include_file(paths.h HAVE_PATHS_H)
344 if(HAVE_PATHS_H)
345         add_compile_definitions(HAVE_PATHS_H)
346 endif()
348 #function checks
349 set(function_checks
350         strcasestr memmem strlcpy strtoimax strtoumax strtoull
351         setenv mkdtemp poll pread memmem)
353 #unsetenv,hstrerror are incompatible with windows build
354 if(NOT WIN32)
355         list(APPEND function_checks unsetenv hstrerror)
356 endif()
358 foreach(f ${function_checks})
359         string(TOUPPER ${f} uf)
360         check_function_exists(${f} HAVE_${uf})
361         if(NOT HAVE_${uf})
362                 add_compile_definitions(NO_${uf})
363         endif()
364 endforeach()
366 if(NOT HAVE_POLL_H OR NOT HAVE_SYS_POLL_H OR NOT HAVE_POLL)
367         include_directories(${CMAKE_SOURCE_DIR}/compat/poll)
368         add_compile_definitions(NO_POLL)
369         list(APPEND compat_SOURCES compat/poll/poll.c)
370 endif()
372 if(NOT HAVE_STRCASESTR)
373         list(APPEND compat_SOURCES compat/strcasestr.c)
374 endif()
376 if(NOT HAVE_STRLCPY)
377         list(APPEND compat_SOURCES compat/strlcpy.c)
378 endif()
380 if(NOT HAVE_STRTOUMAX)
381         list(APPEND compat_SOURCES compat/strtoumax.c compat/strtoimax.c)
382 endif()
384 if(NOT HAVE_SETENV)
385         list(APPEND compat_SOURCES compat/setenv.c)
386 endif()
388 if(NOT HAVE_MKDTEMP)
389         list(APPEND compat_SOURCES compat/mkdtemp.c)
390 endif()
392 if(NOT HAVE_PREAD)
393         list(APPEND compat_SOURCES compat/pread.c)
394 endif()
396 if(NOT HAVE_MEMMEM)
397         list(APPEND compat_SOURCES compat/memmem.c)
398 endif()
400 if(NOT WIN32)
401         if(NOT HAVE_UNSETENV)
402                 list(APPEND compat_SOURCES compat/unsetenv.c)
403         endif()
405         if(NOT HAVE_HSTRERROR)
406                 list(APPEND compat_SOURCES compat/hstrerror.c)
407         endif()
408 endif()
410 check_function_exists(getdelim HAVE_GETDELIM)
411 if(HAVE_GETDELIM)
412         add_compile_definitions(HAVE_GETDELIM)
413 endif()
415 check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
416 check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_CLOCK_MONOTONIC)
417 if(HAVE_CLOCK_GETTIME)
418         add_compile_definitions(HAVE_CLOCK_GETTIME)
419 endif()
420 if(HAVE_CLOCK_MONOTONIC)
421         add_compile_definitions(HAVE_CLOCK_MONOTONIC)
422 endif()
424 #check for st_blocks in struct stat
425 check_struct_has_member("struct stat" st_blocks "sys/stat.h" STRUCT_STAT_HAS_ST_BLOCKS)
426 if(NOT STRUCT_STAT_HAS_ST_BLOCKS)
427         add_compile_definitions(NO_ST_BLOCKS_IN_STRUCT_STAT)
428 endif()
430 #compile checks
431 check_c_source_runs("
432 #include<stdio.h>
433 #include<stdarg.h>
434 #include<string.h>
435 #include<stdlib.h>
437 int test_vsnprintf(char *str, size_t maxsize, const char *format, ...)
439         int ret;
440         va_list ap;
442         va_start(ap, format);
443         ret = vsnprintf(str, maxsize, format, ap);
444         va_end(ap);
445         return ret;
448 int main(void)
450         char buf[6];
452         if (test_vsnprintf(buf, 3, \"%s\", \"12345\") != 5
453                 || strcmp(buf, \"12\"))
454                         return 1;
455         if (snprintf(buf, 3, \"%s\", \"12345\") != 5
456                 || strcmp(buf, \"12\"))
457                         return 1;
458         return 0;
460 SNPRINTF_OK)
461 if(NOT SNPRINTF_OK)
462         add_compile_definitions(SNPRINTF_RETURNS_BOGUS)
463         list(APPEND compat_SOURCES compat/snprintf.c)
464 endif()
466 check_c_source_runs("
467 #include<stdio.h>
469 int main(void)
471         FILE *f = fopen(\".\", \"r\");
473         return f != NULL;
475 FREAD_READS_DIRECTORIES_NO)
476 if(NOT FREAD_READS_DIRECTORIES_NO)
477         add_compile_definitions(FREAD_READS_DIRECTORIES)
478         list(APPEND compat_SOURCES compat/fopen.c)
479 endif()
481 check_c_source_compiles("
482 #include <regex.h>
483 #ifndef REG_STARTEND
484 #error oops we dont have it
485 #endif
487 int main(void)
489         return 0;
491 HAVE_REGEX)
492 if(NOT HAVE_REGEX)
493         include_directories(${CMAKE_SOURCE_DIR}/compat/regex)
494         list(APPEND compat_SOURCES compat/regex/regex.c )
495         add_compile_definitions(NO_REGEX NO_MBSUPPORT GAWK)
496 endif()
499 check_c_source_compiles("
500 #include <stddef.h>
501 #include <sys/types.h>
502 #include <sys/sysctl.h>
504 int main(void)
506         int val, mib[2];
507         size_t len;
509         mib[0] = CTL_HW;
510         mib[1] = 1;
511         len = sizeof(val);
512         return sysctl(mib, 2, &val, &len, NULL, 0) ? 1 : 0;
514 HAVE_BSD_SYSCTL)
515 if(HAVE_BSD_SYSCTL)
516         add_compile_definitions(HAVE_BSD_SYSCTL)
517 endif()
519 set(CMAKE_REQUIRED_LIBRARIES ${Iconv_LIBRARIES})
520 set(CMAKE_REQUIRED_INCLUDES ${Iconv_INCLUDE_DIRS})
522 check_c_source_compiles("
523 #include <iconv.h>
525 extern size_t iconv(iconv_t cd,
526                 char **inbuf, size_t *inbytesleft,
527                 char **outbuf, size_t *outbytesleft);
529 int main(void)
531         return 0;
533 HAVE_NEW_ICONV)
534 if(HAVE_NEW_ICONV)
535         set(HAVE_OLD_ICONV 0)
536 else()
537         set(HAVE_OLD_ICONV 1)
538 endif()
540 check_c_source_runs("
541 #include <iconv.h>
542 #if ${HAVE_OLD_ICONV}
543 typedef const char *iconv_ibp;
544 #else
545 typedef char *iconv_ibp;
546 #endif
548 int main(void)
550         int v;
551         iconv_t conv;
552         char in[] = \"a\";
553         iconv_ibp pin = in;
554         char out[20] = \"\";
555         char *pout = out;
556         size_t isz = sizeof(in);
557         size_t osz = sizeof(out);
559         conv = iconv_open(\"UTF-16\", \"UTF-8\");
560         iconv(conv, &pin, &isz, &pout, &osz);
561         iconv_close(conv);
562         v = (unsigned char)(out[0]) + (unsigned char)(out[1]);
563         return v != 0xfe + 0xff;
565 ICONV_DOESNOT_OMIT_BOM)
566 if(NOT ICONV_DOESNOT_OMIT_BOM)
567         add_compile_definitions(ICONV_OMITS_BOM)
568 endif()
570 unset(CMAKE_REQUIRED_LIBRARIES)
571 unset(CMAKE_REQUIRED_INCLUDES)
574 #programs
575 set(PROGRAMS_BUILT
576         git git-daemon git-http-backend git-sh-i18n--envsubst
577         git-shell)
579 if(NOT CURL_FOUND)
580         list(APPEND excluded_progs git-http-fetch git-http-push)
581         add_compile_definitions(NO_CURL)
582         message(WARNING "git-http-push and git-http-fetch will not be built")
583 else()
584         list(APPEND PROGRAMS_BUILT git-http-fetch git-http-push git-imap-send git-remote-http)
585         if(CURL_VERSION_STRING VERSION_GREATER_EQUAL 7.34.0)
586                 add_compile_definitions(USE_CURL_FOR_IMAP_SEND)
587         endif()
588 endif()
590 if(NOT EXPAT_FOUND)
591         list(APPEND excluded_progs git-http-push)
592         add_compile_definitions(NO_EXPAT)
593 else()
594         list(APPEND PROGRAMS_BUILT git-http-push)
595         if(EXPAT_VERSION_STRING VERSION_LESS_EQUAL 1.2)
596                 add_compile_definitions(EXPAT_NEEDS_XMLPARSE_H)
597         endif()
598 endif()
600 list(REMOVE_DUPLICATES excluded_progs)
601 list(REMOVE_DUPLICATES PROGRAMS_BUILT)
604 foreach(p ${excluded_progs})
605         list(APPEND EXCLUSION_PROGS --exclude-program ${p} )
606 endforeach()
608 #for comparing null values
609 list(APPEND EXCLUSION_PROGS empty)
610 set(EXCLUSION_PROGS_CACHE ${EXCLUSION_PROGS} CACHE STRING "Programs not built" FORCE)
612 if(NOT EXISTS ${CMAKE_BINARY_DIR}/command-list.h OR NOT EXCLUSION_PROGS_CACHE STREQUAL EXCLUSION_PROGS)
613         list(REMOVE_ITEM EXCLUSION_PROGS empty)
614         message("Generating command-list.h")
615         execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-cmdlist.sh ${EXCLUSION_PROGS} command-list.txt
616                         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
617                         OUTPUT_FILE ${CMAKE_BINARY_DIR}/command-list.h)
618 endif()
620 if(NOT EXISTS ${CMAKE_BINARY_DIR}/config-list.h)
621         message("Generating config-list.h")
622         execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-configlist.sh
623                         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
624                         OUTPUT_FILE ${CMAKE_BINARY_DIR}/config-list.h)
625 endif()
627 if(NOT EXISTS ${CMAKE_BINARY_DIR}/hook-list.h)
628         message("Generating hook-list.h")
629         execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-hooklist.sh
630                         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
631                         OUTPUT_FILE ${CMAKE_BINARY_DIR}/hook-list.h)
632 endif()
634 include_directories(${CMAKE_BINARY_DIR})
636 #build
637 #libgit
638 parse_makefile_for_sources(libgit_SOURCES "LIB_OBJS")
640 list(TRANSFORM libgit_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
641 list(TRANSFORM compat_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
642 add_library(libgit ${libgit_SOURCES} ${compat_SOURCES})
644 #libxdiff
645 parse_makefile_for_sources(libxdiff_SOURCES "XDIFF_OBJS")
647 list(TRANSFORM libxdiff_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
648 add_library(xdiff STATIC ${libxdiff_SOURCES})
650 #reftable
651 parse_makefile_for_sources(reftable_SOURCES "REFTABLE_OBJS")
653 list(TRANSFORM reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
654 add_library(reftable STATIC ${reftable_SOURCES})
656 if(WIN32)
657         if(NOT MSVC)#use windres when compiling with gcc and clang
658                 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
659                                 COMMAND ${WINDRES_EXE} -O coff -DMAJOR=${PROJECT_VERSION_MAJOR} -DMINOR=${PROJECT_VERSION_MINOR}
660                                         -DMICRO=${PROJECT_VERSION_PATCH} -DPATCHLEVEL=0 -DGIT_VERSION="\\\"${PROJECT_VERSION}.GIT\\\""
661                                         -i ${CMAKE_SOURCE_DIR}/git.rc -o ${CMAKE_BINARY_DIR}/git.res
662                                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
663                                 VERBATIM)
664         else()#MSVC use rc
665                 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
666                                 COMMAND ${CMAKE_RC_COMPILER} /d MAJOR=${PROJECT_VERSION_MAJOR} /d MINOR=${PROJECT_VERSION_MINOR}
667                                         /d MICRO=${PROJECT_VERSION_PATCH} /d PATCHLEVEL=0 /d GIT_VERSION="${PROJECT_VERSION}.GIT"
668                                         /fo ${CMAKE_BINARY_DIR}/git.res ${CMAKE_SOURCE_DIR}/git.rc
669                                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
670                                 VERBATIM)
671         endif()
672         add_custom_target(git-rc DEPENDS ${CMAKE_BINARY_DIR}/git.res)
673 endif()
675 #link all required libraries to common-main
676 add_library(common-main OBJECT ${CMAKE_SOURCE_DIR}/common-main.c)
678 target_link_libraries(common-main libgit xdiff reftable ${ZLIB_LIBRARIES})
679 if(Intl_FOUND)
680         target_link_libraries(common-main ${Intl_LIBRARIES})
681 endif()
682 if(Iconv_FOUND)
683         target_link_libraries(common-main ${Iconv_LIBRARIES})
684 endif()
685 if(WIN32)
686         target_link_libraries(common-main ws2_32 ntdll ${CMAKE_BINARY_DIR}/git.res)
687         add_dependencies(common-main git-rc)
688         if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
689                 target_link_options(common-main PUBLIC -municode -Wl,--nxcompat -Wl,--dynamicbase -Wl,--pic-executable,-e,mainCRTStartup)
690         elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
691                 target_link_options(common-main PUBLIC -municode -Wl,-nxcompat -Wl,-dynamicbase -Wl,-entry:wmainCRTStartup -Wl,invalidcontinue.obj)
692         elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
693                 target_link_options(common-main PUBLIC /IGNORE:4217 /IGNORE:4049 /NOLOGO /ENTRY:wmainCRTStartup /SUBSYSTEM:CONSOLE invalidcontinue.obj)
694         else()
695                 message(FATAL_ERROR "Unhandled compiler: ${CMAKE_C_COMPILER_ID}")
696         endif()
697 elseif(UNIX)
698         target_link_libraries(common-main pthread rt)
699 endif()
701 #git
702 parse_makefile_for_sources(git_SOURCES "BUILTIN_OBJS")
704 list(TRANSFORM git_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
705 add_executable(git ${CMAKE_SOURCE_DIR}/git.c ${git_SOURCES})
706 target_link_libraries(git common-main)
708 add_executable(git-daemon ${CMAKE_SOURCE_DIR}/daemon.c)
709 target_link_libraries(git-daemon common-main)
711 add_executable(git-http-backend ${CMAKE_SOURCE_DIR}/http-backend.c)
712 target_link_libraries(git-http-backend common-main)
714 add_executable(git-sh-i18n--envsubst ${CMAKE_SOURCE_DIR}/sh-i18n--envsubst.c)
715 target_link_libraries(git-sh-i18n--envsubst common-main)
717 add_executable(git-shell ${CMAKE_SOURCE_DIR}/shell.c)
718 target_link_libraries(git-shell common-main)
720 if(CURL_FOUND)
721         add_library(http_obj OBJECT ${CMAKE_SOURCE_DIR}/http.c)
723         add_executable(git-imap-send ${CMAKE_SOURCE_DIR}/imap-send.c)
724         target_link_libraries(git-imap-send http_obj common-main ${CURL_LIBRARIES})
726         add_executable(git-http-fetch ${CMAKE_SOURCE_DIR}/http-walker.c ${CMAKE_SOURCE_DIR}/http-fetch.c)
727         target_link_libraries(git-http-fetch http_obj common-main ${CURL_LIBRARIES})
729         add_executable(git-remote-http ${CMAKE_SOURCE_DIR}/http-walker.c ${CMAKE_SOURCE_DIR}/remote-curl.c)
730         target_link_libraries(git-remote-http http_obj common-main ${CURL_LIBRARIES} )
732         if(EXPAT_FOUND)
733                 add_executable(git-http-push ${CMAKE_SOURCE_DIR}/http-push.c)
734                 target_link_libraries(git-http-push http_obj common-main ${CURL_LIBRARIES} ${EXPAT_LIBRARIES})
735         endif()
736 endif()
738 parse_makefile_for_executables(git_builtin_extra "BUILT_INS")
740 option(SKIP_DASHED_BUILT_INS "Skip hardlinking the dashed versions of the built-ins")
742 #Creating hardlinks
743 if(NOT SKIP_DASHED_BUILT_INS)
744 foreach(s ${git_SOURCES} ${git_builtin_extra})
745         string(REPLACE "${CMAKE_SOURCE_DIR}/builtin/" "" s ${s})
746         string(REPLACE ".c" "" s ${s})
747         file(APPEND ${CMAKE_BINARY_DIR}/CreateLinks.cmake "file(CREATE_LINK git${EXE_EXTENSION} git-${s}${EXE_EXTENSION})\n")
748         list(APPEND git_links ${CMAKE_BINARY_DIR}/git-${s}${EXE_EXTENSION})
749 endforeach()
750 endif()
752 if(CURL_FOUND)
753         set(remote_exes
754                 git-remote-https git-remote-ftp git-remote-ftps)
755         foreach(s ${remote_exes})
756                 file(APPEND ${CMAKE_BINARY_DIR}/CreateLinks.cmake "file(CREATE_LINK git-remote-http${EXE_EXTENSION} ${s}${EXE_EXTENSION})\n")
757                 list(APPEND git_http_links ${CMAKE_BINARY_DIR}/${s}${EXE_EXTENSION})
758         endforeach()
759 endif()
761 add_custom_command(OUTPUT ${git_links} ${git_http_links}
762                 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/CreateLinks.cmake
763                 DEPENDS git git-remote-http)
764 add_custom_target(git-links ALL DEPENDS ${git_links} ${git_http_links})
767 #creating required scripts
768 set(SHELL_PATH /bin/sh)
769 set(PERL_PATH /usr/bin/perl)
770 set(LOCALEDIR ${FALLBACK_RUNTIME_PREFIX}/share/locale)
771 set(GITWEBDIR ${FALLBACK_RUNTIME_PREFIX}/share/locale)
772 set(INSTLIBDIR ${FALLBACK_RUNTIME_PREFIX}/share/perl5)
774 #shell scripts
775 parse_makefile_for_scripts(git_sh_scripts "SCRIPT_SH" ".sh")
776 parse_makefile_for_scripts(git_shlib_scripts "SCRIPT_LIB" "")
777 set(git_shell_scripts
778         ${git_sh_scripts} ${git_shlib_scripts} git-instaweb)
780 foreach(script ${git_shell_scripts})
781         file(STRINGS ${CMAKE_SOURCE_DIR}/${script}.sh content NEWLINE_CONSUME)
782         string(REPLACE "@SHELL_PATH@" "${SHELL_PATH}" content "${content}")
783         string(REPLACE "@@DIFF@@" "diff" content "${content}")
784         string(REPLACE "@LOCALEDIR@" "${LOCALEDIR}" content "${content}")
785         string(REPLACE "@GITWEBDIR@" "${GITWEBDIR}" content "${content}")
786         string(REPLACE "@@NO_CURL@@" "" content "${content}")
787         string(REPLACE "@@USE_GETTEXT_SCHEME@@" "" content "${content}")
788         string(REPLACE "# @@BROKEN_PATH_FIX@@" "" content "${content}")
789         string(REPLACE "@@PERL@@" "${PERL_PATH}" content "${content}")
790         string(REPLACE "@@PAGER_ENV@@" "LESS=FRX LV=-c" content "${content}")
791         file(WRITE ${CMAKE_BINARY_DIR}/${script} ${content})
792 endforeach()
794 #perl scripts
795 parse_makefile_for_scripts(git_perl_scripts "SCRIPT_PERL" ".perl")
797 #create perl header
798 file(STRINGS ${CMAKE_SOURCE_DIR}/perl/header_templates/fixed_prefix.template.pl perl_header )
799 string(REPLACE "@@PATHSEP@@" ":" perl_header "${perl_header}")
800 string(REPLACE "@@INSTLIBDIR@@" "${INSTLIBDIR}" perl_header "${perl_header}")
802 foreach(script ${git_perl_scripts})
803         file(STRINGS ${CMAKE_SOURCE_DIR}/${script}.perl content NEWLINE_CONSUME)
804         string(REPLACE "#!/usr/bin/perl" "#!/usr/bin/perl\n${perl_header}\n" content "${content}")
805         string(REPLACE "@@GIT_VERSION@@" "${PROJECT_VERSION}" content "${content}")
806         file(WRITE ${CMAKE_BINARY_DIR}/${script} ${content})
807 endforeach()
809 #python script
810 file(STRINGS ${CMAKE_SOURCE_DIR}/git-p4.py content NEWLINE_CONSUME)
811 string(REPLACE "#!/usr/bin/env python" "#!/usr/bin/python" content "${content}")
812 file(WRITE ${CMAKE_BINARY_DIR}/git-p4 ${content})
814 #perl modules
815 file(GLOB_RECURSE perl_modules "${CMAKE_SOURCE_DIR}/perl/*.pm")
817 foreach(pm ${perl_modules})
818         string(REPLACE "${CMAKE_SOURCE_DIR}/perl/" "" file_path ${pm})
819         file(STRINGS ${pm} content NEWLINE_CONSUME)
820         string(REPLACE "@@LOCALEDIR@@" "${LOCALEDIR}" content "${content}")
821         string(REPLACE "@@NO_PERL_CPAN_FALLBACKS@@" "" content "${content}")
822         file(WRITE ${CMAKE_BINARY_DIR}/perl/build/lib/${file_path} ${content})
823 #test-lib.sh requires perl/build/lib to be the build directory of perl modules
824 endforeach()
827 #templates
828 file(GLOB templates "${CMAKE_SOURCE_DIR}/templates/*")
829 list(TRANSFORM templates REPLACE "${CMAKE_SOURCE_DIR}/templates/" "")
830 list(REMOVE_ITEM templates ".gitignore")
831 list(REMOVE_ITEM templates "Makefile")
832 list(REMOVE_ITEM templates "blt")# Prevents an error when reconfiguring for in source builds
834 list(REMOVE_ITEM templates "branches--")
835 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/templates/blt/branches) #create branches
837 #templates have @.*@ replacement so use configure_file instead
838 foreach(tm ${templates})
839         string(REPLACE "--" "/" blt_tm ${tm})
840         string(REPLACE "this" "" blt_tm ${blt_tm})# for this--
841         configure_file(${CMAKE_SOURCE_DIR}/templates/${tm} ${CMAKE_BINARY_DIR}/templates/blt/${blt_tm} @ONLY)
842 endforeach()
845 #translations
846 if(MSGFMT_EXE)
847         file(GLOB po_files "${CMAKE_SOURCE_DIR}/po/*.po")
848         list(TRANSFORM po_files REPLACE "${CMAKE_SOURCE_DIR}/po/" "")
849         list(TRANSFORM po_files REPLACE ".po" "")
850         foreach(po ${po_files})
851                 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES)
852                 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo
853                                 COMMAND ${MSGFMT_EXE} --check --statistics -o ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo ${CMAKE_SOURCE_DIR}/po/${po}.po)
854                 list(APPEND po_gen ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo)
855         endforeach()
856         add_custom_target(po-gen ALL DEPENDS ${po_gen})
857 endif()
860 #to help with the install
861 list(TRANSFORM git_shell_scripts PREPEND "${CMAKE_BINARY_DIR}/")
862 list(TRANSFORM git_perl_scripts PREPEND "${CMAKE_BINARY_DIR}/")
864 #install
865 foreach(program ${PROGRAMS_BUILT})
866 if(program STREQUAL "git" OR program STREQUAL "git-shell")
867 install(TARGETS ${program}
868         RUNTIME DESTINATION bin)
869 else()
870 install(TARGETS ${program}
871         RUNTIME DESTINATION libexec/git-core)
872 endif()
873 endforeach()
875 install(PROGRAMS ${CMAKE_BINARY_DIR}/git-cvsserver
876         DESTINATION bin)
878 set(bin_links
879         git-receive-pack git-upload-archive git-upload-pack)
881 foreach(b ${bin_links})
882 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/bin/${b}${EXE_EXTENSION})")
883 endforeach()
885 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git${EXE_EXTENSION})")
886 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git-shell${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git-shell${EXE_EXTENSION})")
888 foreach(b ${git_links})
889         string(REPLACE "${CMAKE_BINARY_DIR}" "" b ${b})
890         install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/${b})")
891 endforeach()
893 foreach(b ${git_http_links})
894         string(REPLACE "${CMAKE_BINARY_DIR}" "" b ${b})
895         install(CODE "file(CREATE_LINK  ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git-remote-http${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/${b})")
896 endforeach()
898 install(PROGRAMS ${git_shell_scripts} ${git_perl_scripts} ${CMAKE_BINARY_DIR}/git-p4
899         DESTINATION libexec/git-core)
901 install(DIRECTORY ${CMAKE_SOURCE_DIR}/mergetools DESTINATION libexec/git-core)
902 install(DIRECTORY ${CMAKE_BINARY_DIR}/perl/build/lib/ DESTINATION share/perl5
903         FILES_MATCHING PATTERN "*.pm")
904 install(DIRECTORY ${CMAKE_BINARY_DIR}/templates/blt/ DESTINATION share/git-core/templates)
906 if(MSGFMT_EXE)
907         install(DIRECTORY ${CMAKE_BINARY_DIR}/po/build/locale DESTINATION share)
908 endif()
911 if(BUILD_TESTING)
913 #tests-helpers
914 add_executable(test-fake-ssh ${CMAKE_SOURCE_DIR}/t/helper/test-fake-ssh.c)
915 target_link_libraries(test-fake-ssh common-main)
917 #reftable-tests
918 parse_makefile_for_sources(test-reftable_SOURCES "REFTABLE_TEST_OBJS")
919 list(TRANSFORM test-reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
921 #test-tool
922 parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")
924 list(TRANSFORM test-tool_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/t/helper/")
925 add_executable(test-tool ${CMAKE_SOURCE_DIR}/t/helper/test-tool.c ${test-tool_SOURCES} ${test-reftable_SOURCES})
926 target_link_libraries(test-tool common-main)
928 set_target_properties(test-fake-ssh test-tool
929                         PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/helper)
931 if(MSVC)
932         set_target_properties(test-fake-ssh test-tool
933                                 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/helper)
934         set_target_properties(test-fake-ssh test-tool
935                                 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/helper)
936 endif()
938 #wrapper scripts
939 set(wrapper_scripts
940         git git-upload-pack git-receive-pack git-upload-archive git-shell git-remote-ext)
942 set(wrapper_test_scripts
943         test-fake-ssh test-tool)
946 foreach(script ${wrapper_scripts})
947         file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
948         string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
949         string(REPLACE "@@PROG@@" "${script}${EXE_EXTENSION}" content "${content}")
950         file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/${script} ${content})
951 endforeach()
953 foreach(script ${wrapper_test_scripts})
954         file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
955         string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
956         string(REPLACE "@@PROG@@" "t/helper/${script}${EXE_EXTENSION}" content "${content}")
957         file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/${script} ${content})
958 endforeach()
960 file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
961 string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
962 string(REPLACE "@@PROG@@" "git-cvsserver" content "${content}")
963 file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/git-cvsserver ${content})
965 #options for configuring test options
966 option(PERL_TESTS "Perform tests that use perl" ON)
967 option(PYTHON_TESTS "Perform tests that use python" ON)
969 #GIT-BUILD-OPTIONS
970 set(TEST_SHELL_PATH ${SHELL_PATH})
971 set(DIFF diff)
972 set(PYTHON_PATH /usr/bin/python)
973 set(TAR tar)
974 set(NO_CURL )
975 set(NO_EXPAT )
976 set(USE_LIBPCRE2 )
977 set(NO_PERL )
978 set(NO_PTHREADS )
979 set(NO_PYTHON )
980 set(PAGER_ENV "LESS=FRX LV=-c")
981 set(DC_SHA1 YesPlease)
982 set(RUNTIME_PREFIX true)
983 set(NO_GETTEXT )
985 if(NOT CURL_FOUND)
986         set(NO_CURL 1)
987 endif()
989 if(NOT EXPAT_FOUND)
990         set(NO_EXPAT 1)
991 endif()
993 if(NOT Intl_FOUND)
994         set(NO_GETTEXT 1)
995 endif()
997 if(NOT PERL_TESTS)
998         set(NO_PERL 1)
999 endif()
1001 if(NOT PYTHON_TESTS)
1002         set(NO_PYTHON 1)
1003 endif()
1005 file(WRITE ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "SHELL_PATH='${SHELL_PATH}'\n")
1006 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "TEST_SHELL_PATH='${TEST_SHELL_PATH}'\n")
1007 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PERL_PATH='${PERL_PATH}'\n")
1008 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "DIFF='${DIFF}'\n")
1009 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PYTHON_PATH='${PYTHON_PATH}'\n")
1010 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "TAR='${TAR}'\n")
1011 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_CURL='${NO_CURL}'\n")
1012 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_EXPAT='${NO_EXPAT}'\n")
1013 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PERL='${NO_PERL}'\n")
1014 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PTHREADS='${NO_PTHREADS}'\n")
1015 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_UNIX_SOCKETS='${NO_UNIX_SOCKETS}'\n")
1016 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PAGER_ENV='${PAGER_ENV}'\n")
1017 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "DC_SHA1='${DC_SHA1}'\n")
1018 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "X='${EXE_EXTENSION}'\n")
1019 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_GETTEXT='${NO_GETTEXT}'\n")
1020 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "RUNTIME_PREFIX='${RUNTIME_PREFIX}'\n")
1021 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PYTHON='${NO_PYTHON}'\n")
1022 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "SUPPORTS_SIMPLE_IPC='${SUPPORTS_SIMPLE_IPC}'\n")
1023 if(USE_VCPKG)
1024         file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PATH=\"$PATH:$TEST_DIRECTORY/../compat/vcbuild/vcpkg/installed/x64-windows/bin\"\n")
1025 endif()
1027 #Make the tests work when building out of the source tree
1028 get_filename_component(CACHE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../CMakeCache.txt ABSOLUTE)
1029 if(NOT ${CMAKE_BINARY_DIR}/CMakeCache.txt STREQUAL ${CACHE_PATH})
1030         file(RELATIVE_PATH BUILD_DIR_RELATIVE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}/CMakeCache.txt)
1031         string(REPLACE "/CMakeCache.txt" "" BUILD_DIR_RELATIVE ${BUILD_DIR_RELATIVE})
1032         #Setting the build directory in test-lib.sh before running tests
1033         file(WRITE ${CMAKE_BINARY_DIR}/CTestCustom.cmake
1034                 "file(STRINGS ${CMAKE_SOURCE_DIR}/t/test-lib.sh GIT_BUILD_DIR_REPL REGEX \"GIT_BUILD_DIR=(.*)\")\n"
1035                 "file(STRINGS ${CMAKE_SOURCE_DIR}/t/test-lib.sh content NEWLINE_CONSUME)\n"
1036                 "string(REPLACE \"\${GIT_BUILD_DIR_REPL}\" \"GIT_BUILD_DIR=\\\"$TEST_DIRECTORY/../${BUILD_DIR_RELATIVE}\\\"\" content \"\${content}\")\n"
1037                 "file(WRITE ${CMAKE_SOURCE_DIR}/t/test-lib.sh \${content})")
1038         #misc copies
1039         file(COPY ${CMAKE_SOURCE_DIR}/t/chainlint.sed DESTINATION ${CMAKE_BINARY_DIR}/t/)
1040         file(COPY ${CMAKE_SOURCE_DIR}/po/is.po DESTINATION ${CMAKE_BINARY_DIR}/po/)
1041         file(COPY ${CMAKE_SOURCE_DIR}/mergetools/tkdiff DESTINATION ${CMAKE_BINARY_DIR}/mergetools/)
1042         file(COPY ${CMAKE_SOURCE_DIR}/contrib/completion/git-prompt.sh DESTINATION ${CMAKE_BINARY_DIR}/contrib/completion/)
1043         file(COPY ${CMAKE_SOURCE_DIR}/contrib/completion/git-completion.bash DESTINATION ${CMAKE_BINARY_DIR}/contrib/completion/)
1044 endif()
1046 file(GLOB test_scipts "${CMAKE_SOURCE_DIR}/t/t[0-9]*.sh")
1048 #test
1049 foreach(tsh ${test_scipts})
1050         add_test(NAME ${tsh}
1051                 COMMAND ${SH_EXE} ${tsh}
1052                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t)
1053 endforeach()
1055 endif()#BUILD_TESTING