cache.h: remove this no-longer-used header
[alt-git.git] / contrib / buildsystems / CMakeLists.txt
blob4faa419cc3d506cedaf1b0bc1773eeef58f31dab
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" "$ENV{LOCALAPPDATA}/Programs/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
112 #macros for parsing the Makefile for sources and scripts
113 macro(parse_makefile_for_sources list_var regex)
114         file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+=(.*)")
115         string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
116         string(REPLACE "$(COMPAT_OBJS)" "" ${list_var} ${${list_var}}) #remove "$(COMPAT_OBJS)" This is only for libgit.
117         string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
118         string(REPLACE ".o" ".c;" ${list_var} ${${list_var}}) #change .o to .c, ; is for converting the string into a list
119         list(TRANSFORM ${list_var} STRIP) #remove trailing/leading whitespaces for each element in list
120         list(REMOVE_ITEM ${list_var} "") #remove empty list elements
121 endmacro()
123 macro(parse_makefile_for_scripts list_var regex lang)
124         file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+=(.*)")
125         string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
126         string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
127         string(REPLACE " " ";" ${list_var} ${${list_var}}) #convert string to a list
128         if(NOT ${lang}) #exclude for SCRIPT_LIB
129                 list(TRANSFORM ${list_var} REPLACE "${lang}" "") #do the replacement
130         endif()
131 endmacro()
133 macro(parse_makefile_for_executables list_var regex)
134         file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+= git-(.*)")
135         string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
136         string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
137         string(REPLACE "git-" "" ${list_var} ${${list_var}}) #strip `git-` prefix
138         string(REPLACE "\$X" ";" ${list_var} ${${list_var}}) #strip $X, ; is for converting the string into a list
139         list(TRANSFORM ${list_var} STRIP) #remove trailing/leading whitespaces for each element in list
140         list(REMOVE_ITEM ${list_var} "") #remove empty list elements
141 endmacro()
143 include(CheckTypeSize)
144 include(CheckCSourceRuns)
145 include(CheckCSourceCompiles)
146 include(CheckIncludeFile)
147 include(CheckFunctionExists)
148 include(CheckSymbolExists)
149 include(CheckStructHasMember)
150 include(CTest)
152 find_package(ZLIB REQUIRED)
153 find_package(CURL)
154 find_package(EXPAT)
155 find_package(Iconv)
157 #Don't use libintl on Windows Visual Studio and Clang builds
158 if(NOT (WIN32 AND (CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")))
159         find_package(Intl)
160 endif()
162 find_package(PkgConfig)
163 if(PkgConfig_FOUND)
164         pkg_check_modules(PCRE2 libpcre2-8)
165         if(PCRE2_FOUND)
166                 add_compile_definitions(USE_LIBPCRE2)
167         endif()
168 endif()
170 if(NOT Intl_FOUND)
171         add_compile_definitions(NO_GETTEXT)
172         if(NOT Iconv_FOUND)
173                 add_compile_definitions(NO_ICONV)
174         endif()
175 endif()
177 include_directories(SYSTEM ${ZLIB_INCLUDE_DIRS})
178 if(CURL_FOUND)
179         include_directories(SYSTEM ${CURL_INCLUDE_DIRS})
180 endif()
181 if(EXPAT_FOUND)
182         include_directories(SYSTEM ${EXPAT_INCLUDE_DIRS})
183 endif()
184 if(Iconv_FOUND)
185         include_directories(SYSTEM ${Iconv_INCLUDE_DIRS})
186 endif()
187 if(Intl_FOUND)
188         include_directories(SYSTEM ${Intl_INCLUDE_DIRS})
189 endif()
190 if(PCRE2_FOUND)
191         include_directories(SYSTEM ${PCRE2_INCLUDE_DIRS})
192 endif()
195 if(WIN32 AND NOT MSVC)#not required for visual studio builds
196         find_program(WINDRES_EXE windres)
197         if(NOT WINDRES_EXE)
198                 message(FATAL_ERROR "Install windres on Windows for resource files")
199         endif()
200 endif()
202 if(NO_GETTEXT)
203         message(STATUS "msgfmt not used under NO_GETTEXT")
204 else()
205         find_program(MSGFMT_EXE msgfmt)
206         if(NOT MSGFMT_EXE)
207                 if(USE_VCPKG)
208                         set(MSGFMT_EXE ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg/downloads/tools/msys2/msys64/usr/bin/msgfmt.exe)
209                 endif()
210                 if(NOT EXISTS ${MSGFMT_EXE})
211                         message(WARNING "Text Translations won't be built")
212                         unset(MSGFMT_EXE)
213                 endif()
214         endif()
215 endif()
217 #Force all visual studio outputs to CMAKE_BINARY_DIR
218 if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
219         set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR})
220         set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR})
221         add_compile_options(/MP /std:c11)
222 endif()
224 #default behaviour
225 include_directories(${CMAKE_SOURCE_DIR})
226 add_compile_definitions(GIT_HOST_CPU="${CMAKE_SYSTEM_PROCESSOR}")
227 add_compile_definitions(SHA256_BLK INTERNAL_QSORT RUNTIME_PREFIX)
228 add_compile_definitions(NO_OPENSSL SHA1_DC SHA1DC_NO_STANDARD_INCLUDES
229                         SHA1DC_INIT_SAFE_HASH_DEFAULT=0
230                         SHA1DC_CUSTOM_INCLUDE_SHA1_C="git-compat-util.h"
231                         SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C="git-compat-util.h" )
232 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)
235 add_compile_definitions(PAGER_ENV="LESS=FRX LV=-c"
236                         GIT_EXEC_PATH="libexec/git-core"
237                         GIT_LOCALE_PATH="share/locale"
238                         GIT_MAN_PATH="share/man"
239                         GIT_INFO_PATH="share/info"
240                         GIT_HTML_PATH="share/doc/git-doc"
241                         DEFAULT_HELP_FORMAT="html"
242                         DEFAULT_GIT_TEMPLATE_DIR="share/git-core/templates"
243                         GIT_VERSION="${PROJECT_VERSION}.GIT"
244                         GIT_USER_AGENT="git/${PROJECT_VERSION}.GIT"
245                         BINDIR="bin"
246                         GIT_BUILT_FROM_COMMIT="")
248 if(WIN32)
249         set(FALLBACK_RUNTIME_PREFIX /mingw64)
250         # Move system config into top-level /etc/
251         add_compile_definitions(FALLBACK_RUNTIME_PREFIX="${FALLBACK_RUNTIME_PREFIX}"
252                 ETC_GITATTRIBUTES="../etc/gitattributes"
253                 ETC_GITCONFIG="../etc/gitconfig")
254 else()
255         set(FALLBACK_RUNTIME_PREFIX /home/$ENV{USER})
256         add_compile_definitions(FALLBACK_RUNTIME_PREFIX="${FALLBACK_RUNTIME_PREFIX}"
257                 ETC_GITATTRIBUTES="etc/gitattributes"
258                 ETC_GITCONFIG="etc/gitconfig")
259 endif()
262 #Platform Specific
263 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
264         if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
265                 include_directories(${CMAKE_SOURCE_DIR}/compat/vcbuild/include)
266                 add_compile_definitions(_CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
267         endif()
268         include_directories(${CMAKE_SOURCE_DIR}/compat/win32)
269         add_compile_definitions(HAVE_ALLOCA_H NO_POSIX_GOODIES NATIVE_CRLF NO_UNIX_SOCKETS WIN32
270                                 _CONSOLE DETECT_MSYS_TTY STRIP_EXTENSION=".exe"  NO_SYMLINK_HEAD UNRELIABLE_FSTAT
271                                 NOGDI OBJECT_CREATION_MODE=1 __USE_MINGW_ANSI_STDIO=0
272                                 USE_NED_ALLOCATOR OVERRIDE_STRDUP MMAP_PREVENTS_DELETE USE_WIN32_MMAP
273                                 HAVE_WPGMPTR ENSURE_MSYSTEM_IS_SET HAVE_RTLGENRANDOM)
274         list(APPEND compat_SOURCES
275                 compat/mingw.c
276                 compat/winansi.c
277                 compat/win32/flush.c
278                 compat/win32/path-utils.c
279                 compat/win32/pthread.c
280                 compat/win32mmap.c
281                 compat/win32/syslog.c
282                 compat/win32/trace2_win32_process_info.c
283                 compat/win32/dirent.c
284                 compat/nedmalloc/nedmalloc.c
285                 compat/strdup.c)
286         set(NO_UNIX_SOCKETS 1)
288 elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
289         add_compile_definitions(PROCFS_EXECUTABLE_PATH="/proc/self/exe" HAVE_DEV_TTY )
290         list(APPEND compat_SOURCES unix-socket.c unix-stream-server.c compat/linux/procinfo.c)
291 endif()
293 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
294         list(APPEND compat_SOURCES compat/simple-ipc/ipc-shared.c compat/simple-ipc/ipc-win32.c)
295         add_compile_definitions(SUPPORTS_SIMPLE_IPC)
296         set(SUPPORTS_SIMPLE_IPC 1)
297 else()
298         # Simple IPC requires both Unix sockets and pthreads on Unix-based systems.
299         if(NOT NO_UNIX_SOCKETS AND NOT NO_PTHREADS)
300                 list(APPEND compat_SOURCES compat/simple-ipc/ipc-shared.c compat/simple-ipc/ipc-unix-socket.c)
301                 add_compile_definitions(SUPPORTS_SIMPLE_IPC)
302                 set(SUPPORTS_SIMPLE_IPC 1)
303         endif()
304 endif()
306 if(SUPPORTS_SIMPLE_IPC)
307         if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
308                 add_compile_definitions(HAVE_FSMONITOR_DAEMON_BACKEND)
309                 list(APPEND compat_SOURCES compat/fsmonitor/fsm-listen-win32.c)
310                 list(APPEND compat_SOURCES compat/fsmonitor/fsm-health-win32.c)
311                 list(APPEND compat_SOURCES compat/fsmonitor/fsm-ipc-win32.c)
312                 list(APPEND compat_SOURCES compat/fsmonitor/fsm-path-utils-win32.c)
314                 add_compile_definitions(HAVE_FSMONITOR_OS_SETTINGS)
315                 list(APPEND compat_SOURCES compat/fsmonitor/fsm-settings-win32.c)
316         elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
317                 add_compile_definitions(HAVE_FSMONITOR_DAEMON_BACKEND)
318                 list(APPEND compat_SOURCES compat/fsmonitor/fsm-listen-darwin.c)
319                 list(APPEND compat_SOURCES compat/fsmonitor/fsm-health-darwin.c)
320                 list(APPEND compat_SOURCES compat/fsmonitor/fsm-ipc-darwin.c)
321                 list(APPEND compat_SOURCES compat/fsmonitor/fsm-path-utils-darwin.c)
323                 add_compile_definitions(HAVE_FSMONITOR_OS_SETTINGS)
324                 list(APPEND compat_SOURCES compat/fsmonitor/fsm-settings-darwin.c)
325         endif()
326 endif()
328 set(EXE_EXTENSION ${CMAKE_EXECUTABLE_SUFFIX})
330 #header checks
331 check_include_file(libgen.h HAVE_LIBGEN_H)
332 if(NOT HAVE_LIBGEN_H)
333         add_compile_definitions(NO_LIBGEN_H)
334         list(APPEND compat_SOURCES compat/basename.c)
335 endif()
337 check_include_file(sys/sysinfo.h HAVE_SYSINFO)
338 if(HAVE_SYSINFO)
339         add_compile_definitions(HAVE_SYSINFO)
340 endif()
342 check_c_source_compiles("
343 #include <alloca.h>
345 int main(void)
347         char *p = (char *) alloca(2 * sizeof(int));
349         if (p)
350                 return 0;
351         return 0;
353 HAVE_ALLOCA_H)
354 if(HAVE_ALLOCA_H)
355         add_compile_definitions(HAVE_ALLOCA_H)
356 endif()
358 check_include_file(strings.h HAVE_STRINGS_H)
359 if(HAVE_STRINGS_H)
360         add_compile_definitions(HAVE_STRINGS_H)
361 endif()
363 check_include_file(sys/select.h HAVE_SYS_SELECT_H)
364 if(NOT HAVE_SYS_SELECT_H)
365         add_compile_definitions(NO_SYS_SELECT_H)
366 endif()
368 check_include_file(sys/poll.h HAVE_SYS_POLL_H)
369 if(NOT HAVE_SYS_POLL_H)
370         add_compile_definitions(NO_SYS_POLL_H)
371 endif()
373 check_include_file(poll.h HAVE_POLL_H)
374 if(NOT HAVE_POLL_H)
375         add_compile_definitions(NO_POLL_H)
376 endif()
378 check_include_file(inttypes.h HAVE_INTTYPES_H)
379 if(NOT HAVE_INTTYPES_H)
380         add_compile_definitions(NO_INTTYPES_H)
381 endif()
383 check_include_file(paths.h HAVE_PATHS_H)
384 if(HAVE_PATHS_H)
385         add_compile_definitions(HAVE_PATHS_H)
386 endif()
388 #function checks
389 set(function_checks
390         strcasestr memmem strlcpy strtoimax strtoumax strtoull
391         setenv mkdtemp poll pread memmem)
393 #unsetenv,hstrerror are incompatible with windows build
394 if(NOT WIN32)
395         list(APPEND function_checks unsetenv hstrerror)
396 endif()
398 foreach(f ${function_checks})
399         string(TOUPPER ${f} uf)
400         check_function_exists(${f} HAVE_${uf})
401         if(NOT HAVE_${uf})
402                 add_compile_definitions(NO_${uf})
403         endif()
404 endforeach()
406 if(NOT HAVE_POLL_H OR NOT HAVE_SYS_POLL_H OR NOT HAVE_POLL)
407         include_directories(${CMAKE_SOURCE_DIR}/compat/poll)
408         add_compile_definitions(NO_POLL)
409         list(APPEND compat_SOURCES compat/poll/poll.c)
410 endif()
412 if(NOT HAVE_STRCASESTR)
413         list(APPEND compat_SOURCES compat/strcasestr.c)
414 endif()
416 if(NOT HAVE_STRLCPY)
417         list(APPEND compat_SOURCES compat/strlcpy.c)
418 endif()
420 if(NOT HAVE_STRTOUMAX)
421         list(APPEND compat_SOURCES compat/strtoumax.c compat/strtoimax.c)
422 endif()
424 if(NOT HAVE_SETENV)
425         list(APPEND compat_SOURCES compat/setenv.c)
426 endif()
428 if(NOT HAVE_MKDTEMP)
429         list(APPEND compat_SOURCES compat/mkdtemp.c)
430 endif()
432 if(NOT HAVE_PREAD)
433         list(APPEND compat_SOURCES compat/pread.c)
434 endif()
436 if(NOT HAVE_MEMMEM)
437         list(APPEND compat_SOURCES compat/memmem.c)
438 endif()
440 if(NOT WIN32)
441         if(NOT HAVE_UNSETENV)
442                 list(APPEND compat_SOURCES compat/unsetenv.c)
443         endif()
445         if(NOT HAVE_HSTRERROR)
446                 list(APPEND compat_SOURCES compat/hstrerror.c)
447         endif()
448 endif()
450 check_function_exists(getdelim HAVE_GETDELIM)
451 if(HAVE_GETDELIM)
452         add_compile_definitions(HAVE_GETDELIM)
453 endif()
455 check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
456 check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_CLOCK_MONOTONIC)
457 if(HAVE_CLOCK_GETTIME)
458         add_compile_definitions(HAVE_CLOCK_GETTIME)
459 endif()
460 if(HAVE_CLOCK_MONOTONIC)
461         add_compile_definitions(HAVE_CLOCK_MONOTONIC)
462 endif()
464 #check for st_blocks in struct stat
465 check_struct_has_member("struct stat" st_blocks "sys/stat.h" STRUCT_STAT_HAS_ST_BLOCKS)
466 if(NOT STRUCT_STAT_HAS_ST_BLOCKS)
467         add_compile_definitions(NO_ST_BLOCKS_IN_STRUCT_STAT)
468 endif()
470 #compile checks
471 check_c_source_runs("
472 #include<stdio.h>
473 #include<stdarg.h>
474 #include<string.h>
475 #include<stdlib.h>
477 int test_vsnprintf(char *str, size_t maxsize, const char *format, ...)
479         int ret;
480         va_list ap;
482         va_start(ap, format);
483         ret = vsnprintf(str, maxsize, format, ap);
484         va_end(ap);
485         return ret;
488 int main(void)
490         char buf[6];
492         if (test_vsnprintf(buf, 3, \"%s\", \"12345\") != 5
493                 || strcmp(buf, \"12\"))
494                         return 1;
495         if (snprintf(buf, 3, \"%s\", \"12345\") != 5
496                 || strcmp(buf, \"12\"))
497                         return 1;
498         return 0;
500 SNPRINTF_OK)
501 if(NOT SNPRINTF_OK)
502         add_compile_definitions(SNPRINTF_RETURNS_BOGUS)
503         list(APPEND compat_SOURCES compat/snprintf.c)
504 endif()
506 check_c_source_runs("
507 #include<stdio.h>
509 int main(void)
511         FILE *f = fopen(\".\", \"r\");
513         return f != NULL;
515 FREAD_READS_DIRECTORIES_NO)
516 if(NOT FREAD_READS_DIRECTORIES_NO)
517         add_compile_definitions(FREAD_READS_DIRECTORIES)
518         list(APPEND compat_SOURCES compat/fopen.c)
519 endif()
521 check_c_source_compiles("
522 #include <regex.h>
523 #ifndef REG_STARTEND
524 #error oops we dont have it
525 #endif
527 int main(void)
529         return 0;
531 HAVE_REGEX)
532 if(NOT HAVE_REGEX)
533         include_directories(${CMAKE_SOURCE_DIR}/compat/regex)
534         list(APPEND compat_SOURCES compat/regex/regex.c )
535         add_compile_definitions(NO_REGEX NO_MBSUPPORT GAWK)
536 endif()
539 check_c_source_compiles("
540 #include <stddef.h>
541 #include <sys/types.h>
542 #include <sys/sysctl.h>
544 int main(void)
546         int val, mib[2];
547         size_t len;
549         mib[0] = CTL_HW;
550         mib[1] = 1;
551         len = sizeof(val);
552         return sysctl(mib, 2, &val, &len, NULL, 0) ? 1 : 0;
554 HAVE_BSD_SYSCTL)
555 if(HAVE_BSD_SYSCTL)
556         add_compile_definitions(HAVE_BSD_SYSCTL)
557 endif()
559 set(CMAKE_REQUIRED_LIBRARIES ${Iconv_LIBRARIES})
560 set(CMAKE_REQUIRED_INCLUDES ${Iconv_INCLUDE_DIRS})
562 check_c_source_compiles("
563 #include <iconv.h>
565 extern size_t iconv(iconv_t cd,
566                 char **inbuf, size_t *inbytesleft,
567                 char **outbuf, size_t *outbytesleft);
569 int main(void)
571         return 0;
573 HAVE_NEW_ICONV)
574 if(HAVE_NEW_ICONV)
575         set(HAVE_OLD_ICONV 0)
576 else()
577         set(HAVE_OLD_ICONV 1)
578 endif()
580 check_c_source_runs("
581 #include <iconv.h>
582 #if ${HAVE_OLD_ICONV}
583 typedef const char *iconv_ibp;
584 #else
585 typedef char *iconv_ibp;
586 #endif
588 int main(void)
590         int v;
591         iconv_t conv;
592         char in[] = \"a\";
593         iconv_ibp pin = in;
594         char out[20] = \"\";
595         char *pout = out;
596         size_t isz = sizeof(in);
597         size_t osz = sizeof(out);
599         conv = iconv_open(\"UTF-16\", \"UTF-8\");
600         iconv(conv, &pin, &isz, &pout, &osz);
601         iconv_close(conv);
602         v = (unsigned char)(out[0]) + (unsigned char)(out[1]);
603         return v != 0xfe + 0xff;
605 ICONV_DOESNOT_OMIT_BOM)
606 if(NOT ICONV_DOESNOT_OMIT_BOM)
607         add_compile_definitions(ICONV_OMITS_BOM)
608 endif()
610 unset(CMAKE_REQUIRED_LIBRARIES)
611 unset(CMAKE_REQUIRED_INCLUDES)
614 #programs
615 set(PROGRAMS_BUILT
616         git git-daemon git-http-backend git-sh-i18n--envsubst
617         git-shell scalar)
619 if(NOT CURL_FOUND)
620         list(APPEND excluded_progs git-http-fetch git-http-push)
621         add_compile_definitions(NO_CURL)
622         message(WARNING "git-http-push and git-http-fetch will not be built")
623 else()
624         list(APPEND PROGRAMS_BUILT git-http-fetch git-http-push git-imap-send git-remote-http)
625         if(CURL_VERSION_STRING VERSION_GREATER_EQUAL 7.34.0)
626                 add_compile_definitions(USE_CURL_FOR_IMAP_SEND)
627         endif()
628 endif()
630 if(NOT EXPAT_FOUND)
631         list(APPEND excluded_progs git-http-push)
632         add_compile_definitions(NO_EXPAT)
633 else()
634         list(APPEND PROGRAMS_BUILT git-http-push)
635         if(EXPAT_VERSION_STRING VERSION_LESS_EQUAL 1.2)
636                 add_compile_definitions(EXPAT_NEEDS_XMLPARSE_H)
637         endif()
638 endif()
640 list(REMOVE_DUPLICATES excluded_progs)
641 list(REMOVE_DUPLICATES PROGRAMS_BUILT)
644 foreach(p ${excluded_progs})
645         list(APPEND EXCLUSION_PROGS --exclude-program ${p} )
646 endforeach()
648 #for comparing null values
649 list(APPEND EXCLUSION_PROGS empty)
650 set(EXCLUSION_PROGS_CACHE ${EXCLUSION_PROGS} CACHE STRING "Programs not built" FORCE)
652 if(NOT EXISTS ${CMAKE_BINARY_DIR}/command-list.h OR NOT EXCLUSION_PROGS_CACHE STREQUAL EXCLUSION_PROGS)
653         list(REMOVE_ITEM EXCLUSION_PROGS empty)
654         message("Generating command-list.h")
655         execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-cmdlist.sh ${EXCLUSION_PROGS} command-list.txt
656                         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
657                         OUTPUT_FILE ${CMAKE_BINARY_DIR}/command-list.h)
658 endif()
660 if(NOT EXISTS ${CMAKE_BINARY_DIR}/config-list.h)
661         message("Generating config-list.h")
662         execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-configlist.sh
663                         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
664                         OUTPUT_FILE ${CMAKE_BINARY_DIR}/config-list.h)
665 endif()
667 if(NOT EXISTS ${CMAKE_BINARY_DIR}/hook-list.h)
668         message("Generating hook-list.h")
669         execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-hooklist.sh
670                         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
671                         OUTPUT_FILE ${CMAKE_BINARY_DIR}/hook-list.h)
672 endif()
674 include_directories(${CMAKE_BINARY_DIR})
676 #build
677 #libgit
678 parse_makefile_for_sources(libgit_SOURCES "LIB_OBJS")
680 list(TRANSFORM libgit_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
681 list(TRANSFORM compat_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
682 add_library(libgit ${libgit_SOURCES} ${compat_SOURCES})
684 #libxdiff
685 parse_makefile_for_sources(libxdiff_SOURCES "XDIFF_OBJS")
687 list(TRANSFORM libxdiff_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
688 add_library(xdiff STATIC ${libxdiff_SOURCES})
690 #reftable
691 parse_makefile_for_sources(reftable_SOURCES "REFTABLE_OBJS")
693 list(TRANSFORM reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
694 add_library(reftable STATIC ${reftable_SOURCES})
696 if(WIN32)
697         if(NOT MSVC)#use windres when compiling with gcc and clang
698                 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
699                                 COMMAND ${WINDRES_EXE} -O coff -DMAJOR=${PROJECT_VERSION_MAJOR} -DMINOR=${PROJECT_VERSION_MINOR}
700                                         -DMICRO=${PROJECT_VERSION_PATCH} -DPATCHLEVEL=0 -DGIT_VERSION="\\\"${PROJECT_VERSION}.GIT\\\""
701                                         -i ${CMAKE_SOURCE_DIR}/git.rc -o ${CMAKE_BINARY_DIR}/git.res
702                                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
703                                 VERBATIM)
704         else()#MSVC use rc
705                 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
706                                 COMMAND ${CMAKE_RC_COMPILER} /d MAJOR=${PROJECT_VERSION_MAJOR} /d MINOR=${PROJECT_VERSION_MINOR}
707                                         /d MICRO=${PROJECT_VERSION_PATCH} /d PATCHLEVEL=0 /d GIT_VERSION="${PROJECT_VERSION}.GIT"
708                                         /fo ${CMAKE_BINARY_DIR}/git.res ${CMAKE_SOURCE_DIR}/git.rc
709                                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
710                                 VERBATIM)
711         endif()
712         add_custom_target(git-rc DEPENDS ${CMAKE_BINARY_DIR}/git.res)
713 endif()
715 #link all required libraries to common-main
716 add_library(common-main OBJECT ${CMAKE_SOURCE_DIR}/common-main.c)
718 target_link_libraries(common-main libgit xdiff reftable ${ZLIB_LIBRARIES})
719 if(Intl_FOUND)
720         target_link_libraries(common-main ${Intl_LIBRARIES})
721 endif()
722 if(Iconv_FOUND)
723         target_link_libraries(common-main ${Iconv_LIBRARIES})
724 endif()
725 if(PCRE2_FOUND)
726         target_link_libraries(common-main ${PCRE2_LIBRARIES})
727         target_link_directories(common-main PUBLIC ${PCRE2_LIBRARY_DIRS})
728 endif()
729 if(WIN32)
730         target_link_libraries(common-main ws2_32 ntdll ${CMAKE_BINARY_DIR}/git.res)
731         add_dependencies(common-main git-rc)
732         if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
733                 target_link_options(common-main PUBLIC -municode -Wl,--nxcompat -Wl,--dynamicbase -Wl,--pic-executable,-e,mainCRTStartup)
734         elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
735                 target_link_options(common-main PUBLIC -municode -Wl,-nxcompat -Wl,-dynamicbase -Wl,-entry:wmainCRTStartup -Wl,invalidcontinue.obj)
736         elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
737                 target_link_options(common-main PUBLIC /IGNORE:4217 /IGNORE:4049 /NOLOGO /ENTRY:wmainCRTStartup /SUBSYSTEM:CONSOLE invalidcontinue.obj)
738         else()
739                 message(FATAL_ERROR "Unhandled compiler: ${CMAKE_C_COMPILER_ID}")
740         endif()
741 elseif(UNIX)
742         target_link_libraries(common-main pthread rt)
743 endif()
745 #git
746 parse_makefile_for_sources(git_SOURCES "BUILTIN_OBJS")
748 list(TRANSFORM git_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
749 add_executable(git ${CMAKE_SOURCE_DIR}/git.c ${git_SOURCES})
750 target_link_libraries(git common-main)
752 add_executable(git-daemon ${CMAKE_SOURCE_DIR}/daemon.c)
753 target_link_libraries(git-daemon common-main)
755 add_executable(git-http-backend ${CMAKE_SOURCE_DIR}/http-backend.c)
756 target_link_libraries(git-http-backend common-main)
758 add_executable(git-sh-i18n--envsubst ${CMAKE_SOURCE_DIR}/sh-i18n--envsubst.c)
759 target_link_libraries(git-sh-i18n--envsubst common-main)
761 add_executable(git-shell ${CMAKE_SOURCE_DIR}/shell.c)
762 target_link_libraries(git-shell common-main)
764 add_executable(scalar ${CMAKE_SOURCE_DIR}/scalar.c)
765 target_link_libraries(scalar common-main)
767 if(CURL_FOUND)
768         add_library(http_obj OBJECT ${CMAKE_SOURCE_DIR}/http.c)
770         add_executable(git-imap-send ${CMAKE_SOURCE_DIR}/imap-send.c)
771         target_link_libraries(git-imap-send http_obj common-main ${CURL_LIBRARIES})
773         add_executable(git-http-fetch ${CMAKE_SOURCE_DIR}/http-walker.c ${CMAKE_SOURCE_DIR}/http-fetch.c)
774         target_link_libraries(git-http-fetch http_obj common-main ${CURL_LIBRARIES})
776         add_executable(git-remote-http ${CMAKE_SOURCE_DIR}/http-walker.c ${CMAKE_SOURCE_DIR}/remote-curl.c)
777         target_link_libraries(git-remote-http http_obj common-main ${CURL_LIBRARIES} )
779         if(EXPAT_FOUND)
780                 add_executable(git-http-push ${CMAKE_SOURCE_DIR}/http-push.c)
781                 target_link_libraries(git-http-push http_obj common-main ${CURL_LIBRARIES} ${EXPAT_LIBRARIES})
782         endif()
783 endif()
785 parse_makefile_for_executables(git_builtin_extra "BUILT_INS")
787 option(SKIP_DASHED_BUILT_INS "Skip hardlinking the dashed versions of the built-ins")
789 #Creating hardlinks
790 if(NOT SKIP_DASHED_BUILT_INS)
791 foreach(s ${git_SOURCES} ${git_builtin_extra})
792         string(REPLACE "${CMAKE_SOURCE_DIR}/builtin/" "" s ${s})
793         string(REPLACE ".c" "" s ${s})
794         file(APPEND ${CMAKE_BINARY_DIR}/CreateLinks.cmake "file(CREATE_LINK git${EXE_EXTENSION} git-${s}${EXE_EXTENSION})\n")
795         list(APPEND git_links ${CMAKE_BINARY_DIR}/git-${s}${EXE_EXTENSION})
796 endforeach()
797 endif()
799 if(CURL_FOUND)
800         set(remote_exes
801                 git-remote-https git-remote-ftp git-remote-ftps)
802         foreach(s ${remote_exes})
803                 file(APPEND ${CMAKE_BINARY_DIR}/CreateLinks.cmake "file(CREATE_LINK git-remote-http${EXE_EXTENSION} ${s}${EXE_EXTENSION})\n")
804                 list(APPEND git_http_links ${CMAKE_BINARY_DIR}/${s}${EXE_EXTENSION})
805         endforeach()
806 endif()
808 add_custom_command(OUTPUT ${git_links} ${git_http_links}
809                 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/CreateLinks.cmake
810                 DEPENDS git git-remote-http)
811 add_custom_target(git-links ALL DEPENDS ${git_links} ${git_http_links})
814 #creating required scripts
815 set(SHELL_PATH /bin/sh)
816 set(PERL_PATH /usr/bin/perl)
817 set(LOCALEDIR ${FALLBACK_RUNTIME_PREFIX}/share/locale)
818 set(GITWEBDIR ${FALLBACK_RUNTIME_PREFIX}/share/locale)
819 set(INSTLIBDIR ${FALLBACK_RUNTIME_PREFIX}/share/perl5)
821 #shell scripts
822 parse_makefile_for_scripts(git_sh_scripts "SCRIPT_SH" ".sh")
823 parse_makefile_for_scripts(git_shlib_scripts "SCRIPT_LIB" "")
824 set(git_shell_scripts
825         ${git_sh_scripts} ${git_shlib_scripts} git-instaweb)
827 foreach(script ${git_shell_scripts})
828         file(STRINGS ${CMAKE_SOURCE_DIR}/${script}.sh content NEWLINE_CONSUME)
829         string(REPLACE "@SHELL_PATH@" "${SHELL_PATH}" content "${content}")
830         string(REPLACE "@@DIFF@@" "diff" content "${content}")
831         string(REPLACE "@LOCALEDIR@" "${LOCALEDIR}" content "${content}")
832         string(REPLACE "@GITWEBDIR@" "${GITWEBDIR}" content "${content}")
833         string(REPLACE "@@NO_CURL@@" "" content "${content}")
834         string(REPLACE "@@USE_GETTEXT_SCHEME@@" "" content "${content}")
835         string(REPLACE "# @@BROKEN_PATH_FIX@@" "" content "${content}")
836         string(REPLACE "@@PERL@@" "${PERL_PATH}" content "${content}")
837         string(REPLACE "@@PAGER_ENV@@" "LESS=FRX LV=-c" content "${content}")
838         file(WRITE ${CMAKE_BINARY_DIR}/${script} ${content})
839 endforeach()
841 #perl scripts
842 parse_makefile_for_scripts(git_perl_scripts "SCRIPT_PERL" ".perl")
844 #create perl header
845 file(STRINGS ${CMAKE_SOURCE_DIR}/perl/header_templates/fixed_prefix.template.pl perl_header )
846 string(REPLACE "@@PATHSEP@@" ":" perl_header "${perl_header}")
847 string(REPLACE "@@INSTLIBDIR@@" "${INSTLIBDIR}" perl_header "${perl_header}")
849 foreach(script ${git_perl_scripts})
850         file(STRINGS ${CMAKE_SOURCE_DIR}/${script}.perl content NEWLINE_CONSUME)
851         string(REPLACE "#!/usr/bin/perl" "#!/usr/bin/perl\n${perl_header}\n" content "${content}")
852         string(REPLACE "@@GIT_VERSION@@" "${PROJECT_VERSION}" content "${content}")
853         file(WRITE ${CMAKE_BINARY_DIR}/${script} ${content})
854 endforeach()
856 #python script
857 file(STRINGS ${CMAKE_SOURCE_DIR}/git-p4.py content NEWLINE_CONSUME)
858 string(REPLACE "#!/usr/bin/env python" "#!/usr/bin/python" content "${content}")
859 file(WRITE ${CMAKE_BINARY_DIR}/git-p4 ${content})
861 #perl modules
862 file(GLOB_RECURSE perl_modules "${CMAKE_SOURCE_DIR}/perl/*.pm")
864 foreach(pm ${perl_modules})
865         string(REPLACE "${CMAKE_SOURCE_DIR}/perl/" "" file_path ${pm})
866         file(STRINGS ${pm} content NEWLINE_CONSUME)
867         string(REPLACE "@@LOCALEDIR@@" "${LOCALEDIR}" content "${content}")
868         string(REPLACE "@@NO_PERL_CPAN_FALLBACKS@@" "" content "${content}")
869         file(WRITE ${CMAKE_BINARY_DIR}/perl/build/lib/${file_path} ${content})
870 #test-lib.sh requires perl/build/lib to be the build directory of perl modules
871 endforeach()
874 #templates
875 file(GLOB templates "${CMAKE_SOURCE_DIR}/templates/*")
876 list(TRANSFORM templates REPLACE "${CMAKE_SOURCE_DIR}/templates/" "")
877 list(REMOVE_ITEM templates ".gitignore")
878 list(REMOVE_ITEM templates "Makefile")
879 list(REMOVE_ITEM templates "blt")# Prevents an error when reconfiguring for in source builds
881 list(REMOVE_ITEM templates "branches--")
882 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/templates/blt/branches) #create branches
884 #templates have @.*@ replacement so use configure_file instead
885 foreach(tm ${templates})
886         string(REPLACE "--" "/" blt_tm ${tm})
887         string(REPLACE "this" "" blt_tm ${blt_tm})# for this--
888         configure_file(${CMAKE_SOURCE_DIR}/templates/${tm} ${CMAKE_BINARY_DIR}/templates/blt/${blt_tm} @ONLY)
889 endforeach()
892 #translations
893 if(MSGFMT_EXE)
894         file(GLOB po_files "${CMAKE_SOURCE_DIR}/po/*.po")
895         list(TRANSFORM po_files REPLACE "${CMAKE_SOURCE_DIR}/po/" "")
896         list(TRANSFORM po_files REPLACE ".po" "")
897         foreach(po ${po_files})
898                 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES)
899                 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo
900                                 COMMAND ${MSGFMT_EXE} --check --statistics -o ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo ${CMAKE_SOURCE_DIR}/po/${po}.po)
901                 list(APPEND po_gen ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo)
902         endforeach()
903         add_custom_target(po-gen ALL DEPENDS ${po_gen})
904 endif()
907 #to help with the install
908 list(TRANSFORM git_shell_scripts PREPEND "${CMAKE_BINARY_DIR}/")
909 list(TRANSFORM git_perl_scripts PREPEND "${CMAKE_BINARY_DIR}/")
911 #install
912 foreach(program ${PROGRAMS_BUILT})
913 if(program MATCHES "^(git|git-shell|scalar)$")
914 install(TARGETS ${program}
915         RUNTIME DESTINATION bin)
916 else()
917 install(TARGETS ${program}
918         RUNTIME DESTINATION libexec/git-core)
919 endif()
920 endforeach()
922 install(PROGRAMS ${CMAKE_BINARY_DIR}/git-cvsserver
923         DESTINATION bin)
925 set(bin_links
926         git-receive-pack git-upload-archive git-upload-pack)
928 foreach(b ${bin_links})
929 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/bin/${b}${EXE_EXTENSION})")
930 endforeach()
932 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git${EXE_EXTENSION})")
933 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git-shell${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git-shell${EXE_EXTENSION})")
935 foreach(b ${git_links})
936         string(REPLACE "${CMAKE_BINARY_DIR}" "" b ${b})
937         install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/${b})")
938 endforeach()
940 foreach(b ${git_http_links})
941         string(REPLACE "${CMAKE_BINARY_DIR}" "" b ${b})
942         install(CODE "file(CREATE_LINK  ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git-remote-http${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/${b})")
943 endforeach()
945 install(PROGRAMS ${git_shell_scripts} ${git_perl_scripts} ${CMAKE_BINARY_DIR}/git-p4
946         DESTINATION libexec/git-core)
948 install(DIRECTORY ${CMAKE_SOURCE_DIR}/mergetools DESTINATION libexec/git-core)
949 install(DIRECTORY ${CMAKE_BINARY_DIR}/perl/build/lib/ DESTINATION share/perl5
950         FILES_MATCHING PATTERN "*.pm")
951 install(DIRECTORY ${CMAKE_BINARY_DIR}/templates/blt/ DESTINATION share/git-core/templates)
953 if(MSGFMT_EXE)
954         install(DIRECTORY ${CMAKE_BINARY_DIR}/po/build/locale DESTINATION share)
955 endif()
958 if(BUILD_TESTING)
960 #tests-helpers
961 add_executable(test-fake-ssh ${CMAKE_SOURCE_DIR}/t/helper/test-fake-ssh.c)
962 target_link_libraries(test-fake-ssh common-main)
964 #reftable-tests
965 parse_makefile_for_sources(test-reftable_SOURCES "REFTABLE_TEST_OBJS")
966 list(TRANSFORM test-reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
968 #test-tool
969 parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")
971 list(TRANSFORM test-tool_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/t/helper/")
972 add_executable(test-tool ${CMAKE_SOURCE_DIR}/t/helper/test-tool.c ${test-tool_SOURCES} ${test-reftable_SOURCES})
973 target_link_libraries(test-tool common-main)
975 set_target_properties(test-fake-ssh test-tool
976                         PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/helper)
978 if(MSVC)
979         set_target_properties(test-fake-ssh test-tool
980                                 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/helper)
981         set_target_properties(test-fake-ssh test-tool
982                                 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/helper)
983 endif()
985 #wrapper scripts
986 set(wrapper_scripts
987         git git-upload-pack git-receive-pack git-upload-archive git-shell git-remote-ext scalar)
989 set(wrapper_test_scripts
990         test-fake-ssh test-tool)
993 foreach(script ${wrapper_scripts})
994         file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
995         string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
996         string(REPLACE "@@PROG@@" "${script}${EXE_EXTENSION}" content "${content}")
997         file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/${script} ${content})
998 endforeach()
1000 foreach(script ${wrapper_test_scripts})
1001         file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
1002         string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
1003         string(REPLACE "@@PROG@@" "t/helper/${script}${EXE_EXTENSION}" content "${content}")
1004         file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/${script} ${content})
1005 endforeach()
1007 file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
1008 string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
1009 string(REPLACE "@@PROG@@" "git-cvsserver" content "${content}")
1010 file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/git-cvsserver ${content})
1012 #options for configuring test options
1013 option(PERL_TESTS "Perform tests that use perl" ON)
1014 option(PYTHON_TESTS "Perform tests that use python" ON)
1016 #GIT-BUILD-OPTIONS
1017 set(TEST_SHELL_PATH ${SHELL_PATH})
1018 set(DIFF diff)
1019 set(PYTHON_PATH /usr/bin/python)
1020 set(TAR tar)
1021 set(NO_CURL )
1022 set(NO_EXPAT )
1023 set(USE_LIBPCRE2 )
1024 set(NO_PERL )
1025 set(NO_PTHREADS )
1026 set(NO_PYTHON )
1027 set(PAGER_ENV "LESS=FRX LV=-c")
1028 set(RUNTIME_PREFIX true)
1029 set(NO_GETTEXT )
1031 if(NOT CURL_FOUND)
1032         set(NO_CURL 1)
1033 endif()
1035 if(NOT EXPAT_FOUND)
1036         set(NO_EXPAT 1)
1037 endif()
1039 if(NOT Intl_FOUND)
1040         set(NO_GETTEXT 1)
1041 endif()
1043 if(NOT PERL_TESTS)
1044         set(NO_PERL 1)
1045 endif()
1047 if(NOT PYTHON_TESTS)
1048         set(NO_PYTHON 1)
1049 endif()
1051 file(WRITE ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "SHELL_PATH='${SHELL_PATH}'\n")
1052 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "TEST_SHELL_PATH='${TEST_SHELL_PATH}'\n")
1053 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PERL_PATH='${PERL_PATH}'\n")
1054 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "DIFF='${DIFF}'\n")
1055 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PYTHON_PATH='${PYTHON_PATH}'\n")
1056 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "TAR='${TAR}'\n")
1057 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_CURL='${NO_CURL}'\n")
1058 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_EXPAT='${NO_EXPAT}'\n")
1059 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PERL='${NO_PERL}'\n")
1060 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PTHREADS='${NO_PTHREADS}'\n")
1061 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_UNIX_SOCKETS='${NO_UNIX_SOCKETS}'\n")
1062 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PAGER_ENV='${PAGER_ENV}'\n")
1063 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "X='${EXE_EXTENSION}'\n")
1064 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_GETTEXT='${NO_GETTEXT}'\n")
1065 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "RUNTIME_PREFIX='${RUNTIME_PREFIX}'\n")
1066 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PYTHON='${NO_PYTHON}'\n")
1067 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "SUPPORTS_SIMPLE_IPC='${SUPPORTS_SIMPLE_IPC}'\n")
1068 if(USE_VCPKG)
1069         file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PATH=\"$PATH:$TEST_DIRECTORY/../compat/vcbuild/vcpkg/installed/x64-windows/bin\"\n")
1070 endif()
1072 #Make the tests work when building out of the source tree
1073 get_filename_component(CACHE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../CMakeCache.txt ABSOLUTE)
1074 if(NOT ${CMAKE_BINARY_DIR}/CMakeCache.txt STREQUAL ${CACHE_PATH})
1075         #Setting the build directory in test-lib.sh before running tests
1076         file(WRITE ${CMAKE_BINARY_DIR}/CTestCustom.cmake
1077                 "file(WRITE ${CMAKE_SOURCE_DIR}/GIT-BUILD-DIR \"${CMAKE_BINARY_DIR}\")")
1078         #misc copies
1079         file(COPY ${CMAKE_SOURCE_DIR}/t/chainlint.pl DESTINATION ${CMAKE_BINARY_DIR}/t/)
1080         file(COPY ${CMAKE_SOURCE_DIR}/po/is.po DESTINATION ${CMAKE_BINARY_DIR}/po/)
1081         file(GLOB mergetools "${CMAKE_SOURCE_DIR}/mergetools/*")
1082         file(COPY ${mergetools} DESTINATION ${CMAKE_BINARY_DIR}/mergetools/)
1083         file(COPY ${CMAKE_SOURCE_DIR}/contrib/completion/git-prompt.sh DESTINATION ${CMAKE_BINARY_DIR}/contrib/completion/)
1084         file(COPY ${CMAKE_SOURCE_DIR}/contrib/completion/git-completion.bash DESTINATION ${CMAKE_BINARY_DIR}/contrib/completion/)
1085 endif()
1087 file(GLOB test_scipts "${CMAKE_SOURCE_DIR}/t/t[0-9]*.sh")
1089 #test
1090 foreach(tsh ${test_scipts})
1091         add_test(NAME ${tsh}
1092                 COMMAND ${SH_EXE} ${tsh} --no-bin-wrappers --no-chain-lint -vx
1093                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t)
1094 endforeach()
1096 # This test script takes an extremely long time and is known to time out even
1097 # on fast machines because it requires in excess of one hour to run
1098 set_tests_properties("${CMAKE_SOURCE_DIR}/t/t7112-reset-submodule.sh" PROPERTIES TIMEOUT 4000)
1100 endif()#BUILD_TESTING