Merge branch 'ab/config-based-hooks-base' into seen
[git] / contrib / buildsystems / CMakeLists.txt
1 #
2 #       Copyright (c) 2020 Sibi Siddharthan
3 #
4
5 #[[
6
7 Instructions how to use this in Visual Studio:
8
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.
12
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.
16
17 Instructions to run CMake manually:
18
19     mkdir -p contrib/buildsystems/out
20     cd contrib/buildsystems/out
21     cmake ../ -DCMAKE_BUILD_TYPE=Release
22
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).
26
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) :
34
35 NOTE: -DCMAKE_BUILD_TYPE is optional. For multi-config generators like Visual Studio
36 this option is ignored
37
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.
41
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`
45
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.
48
49 ]]
50 cmake_minimum_required(VERSION 3.14)
51
52 #set the source directory to root of git
53 set(CMAKE_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
54
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()
59
60 if(NOT DEFINED CMAKE_EXPORT_COMPILE_COMMANDS)
61         set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
62 endif()
63
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")
71
72         # In the vcpkg edition, we need this to be able to link to libcurl
73         set(CURL_NO_CURL_CMAKE ON)
74
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()
79
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()
85
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()
92
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()
103
104 project(git
105         VERSION ${git_version}
106         LANGUAGES C)
107
108
109 #TODO gitk git-gui gitweb
110 #TODO Enable NLS on windows natively
111 #TODO Add pcre support
112
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()
123
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()
133
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()
143
144 include(CheckTypeSize)
145 include(CheckCSourceRuns)
146 include(CheckCSourceCompiles)
147 include(CheckIncludeFile)
148 include(CheckFunctionExists)
149 include(CheckSymbolExists)
150 include(CheckStructHasMember)
151 include(CTest)
152
153 find_package(ZLIB REQUIRED)
154 find_package(CURL)
155 find_package(EXPAT)
156 find_package(Iconv)
157
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()
162
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()
169
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()
183
184
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()
191
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()
206
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)
212 endif()
213
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)
223
224
225 add_compile_definitions(PAGER_ENV="LESS=FRX LV=-c"
226                         ETC_GITATTRIBUTES="etc/gitattributes"
227                         ETC_GITCONFIG="etc/gitconfig"
228                         GIT_EXEC_PATH="libexec/git-core"
229                         GIT_LOCALE_PATH="share/locale"
230                         GIT_MAN_PATH="share/man"
231                         GIT_INFO_PATH="share/info"
232                         GIT_HTML_PATH="share/doc/git-doc"
233                         DEFAULT_HELP_FORMAT="html"
234                         DEFAULT_GIT_TEMPLATE_DIR="share/git-core/templates"
235                         GIT_VERSION="${PROJECT_VERSION}.GIT"
236                         GIT_USER_AGENT="git/${PROJECT_VERSION}.GIT"
237                         BINDIR="bin"
238                         GIT_BUILT_FROM_COMMIT="")
239
240 if(WIN32)
241         set(FALLBACK_RUNTIME_PREFIX /mingw64)
242         add_compile_definitions(FALLBACK_RUNTIME_PREFIX="${FALLBACK_RUNTIME_PREFIX}")
243 else()
244         set(FALLBACK_RUNTIME_PREFIX /home/$ENV{USER})
245         add_compile_definitions(FALLBACK_RUNTIME_PREFIX="${FALLBACK_RUNTIME_PREFIX}")
246 endif()
247
248
249 #Platform Specific
250 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
251         if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
252                 include_directories(${CMAKE_SOURCE_DIR}/compat/vcbuild/include)
253                 add_compile_definitions(_CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
254         endif()
255         include_directories(${CMAKE_SOURCE_DIR}/compat/win32)
256         add_compile_definitions(HAVE_ALLOCA_H NO_POSIX_GOODIES NATIVE_CRLF NO_UNIX_SOCKETS WIN32
257                                 _CONSOLE DETECT_MSYS_TTY STRIP_EXTENSION=".exe"  NO_SYMLINK_HEAD UNRELIABLE_FSTAT
258                                 NOGDI OBJECT_CREATION_MODE=1 __USE_MINGW_ANSI_STDIO=0
259                                 USE_NED_ALLOCATOR OVERRIDE_STRDUP MMAP_PREVENTS_DELETE USE_WIN32_MMAP
260                                 UNICODE _UNICODE HAVE_WPGMPTR ENSURE_MSYSTEM_IS_SET)
261         list(APPEND compat_SOURCES compat/mingw.c compat/winansi.c compat/win32/path-utils.c
262                 compat/win32/pthread.c compat/win32mmap.c compat/win32/syslog.c
263                 compat/win32/trace2_win32_process_info.c compat/win32/dirent.c
264                 compat/nedmalloc/nedmalloc.c compat/strdup.c)
265         set(NO_UNIX_SOCKETS 1)
266
267 elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
268         add_compile_definitions(PROCFS_EXECUTABLE_PATH="/proc/self/exe" HAVE_DEV_TTY )
269         list(APPEND compat_SOURCES unix-socket.c unix-stream-server.c)
270 endif()
271
272 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
273         list(APPEND compat_SOURCES compat/simple-ipc/ipc-shared.c compat/simple-ipc/ipc-win32.c)
274         add_compile_definitions(SUPPORTS_SIMPLE_IPC)
275         set(SUPPORTS_SIMPLE_IPC 1)
276 else()
277         # Simple IPC requires both Unix sockets and pthreads on Unix-based systems.
278         if(NOT NO_UNIX_SOCKETS AND NOT NO_PTHREADS)
279                 list(APPEND compat_SOURCES compat/simple-ipc/ipc-shared.c compat/simple-ipc/ipc-unix-socket.c)
280                 add_compile_definitions(SUPPORTS_SIMPLE_IPC)
281                 set(SUPPORTS_SIMPLE_IPC 1)
282         endif()
283 endif()
284
285 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
286         add_compile_definitions(HAVE_FSMONITOR_DAEMON_BACKEND)
287         list(APPEND compat_SOURCES compat/fsmonitor/fsmonitor-fs-listen-win32.c)
288 elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
289         add_compile_definitions(HAVE_FSMONITOR_DAEMON_BACKEND)
290         list(APPEND compat_SOURCES compat/fsmonitor/fsmonitor-fs-listen-macos.c)
291 endif()
292
293 set(EXE_EXTENSION ${CMAKE_EXECUTABLE_SUFFIX})
294
295 #header checks
296 check_include_file(libgen.h HAVE_LIBGEN_H)
297 if(NOT HAVE_LIBGEN_H)
298         add_compile_definitions(NO_LIBGEN_H)
299         list(APPEND compat_SOURCES compat/basename.c)
300 endif()
301
302 check_include_file(sys/sysinfo.h HAVE_SYSINFO)
303 if(HAVE_SYSINFO)
304         add_compile_definitions(HAVE_SYSINFO)
305 endif()
306
307 check_c_source_compiles("
308 #include <alloca.h>
309
310 int main(void)
311 {
312         char *p = (char *) alloca(2 * sizeof(int));
313
314         if (p)
315                 return 0;
316         return 0;
317 }"
318 HAVE_ALLOCA_H)
319 if(HAVE_ALLOCA_H)
320         add_compile_definitions(HAVE_ALLOCA_H)
321 endif()
322
323 check_include_file(strings.h HAVE_STRINGS_H)
324 if(HAVE_STRINGS_H)
325         add_compile_definitions(HAVE_STRINGS_H)
326 endif()
327
328 check_include_file(sys/select.h HAVE_SYS_SELECT_H)
329 if(NOT HAVE_SYS_SELECT_H)
330         add_compile_definitions(NO_SYS_SELECT_H)
331 endif()
332
333 check_include_file(sys/poll.h HAVE_SYS_POLL_H)
334 if(NOT HAVE_SYS_POLL_H)
335         add_compile_definitions(NO_SYS_POLL_H)
336 endif()
337
338 check_include_file(poll.h HAVE_POLL_H)
339 if(NOT HAVE_POLL_H)
340         add_compile_definitions(NO_POLL_H)
341 endif()
342
343 check_include_file(inttypes.h HAVE_INTTYPES_H)
344 if(NOT HAVE_INTTYPES_H)
345         add_compile_definitions(NO_INTTYPES_H)
346 endif()
347
348 check_include_file(paths.h HAVE_PATHS_H)
349 if(HAVE_PATHS_H)
350         add_compile_definitions(HAVE_PATHS_H)
351 endif()
352
353 #function checks
354 set(function_checks
355         strcasestr memmem strlcpy strtoimax strtoumax strtoull
356         setenv mkdtemp poll pread memmem)
357
358 #unsetenv,hstrerror are incompatible with windows build
359 if(NOT WIN32)
360         list(APPEND function_checks unsetenv hstrerror)
361 endif()
362
363 foreach(f ${function_checks})
364         string(TOUPPER ${f} uf)
365         check_function_exists(${f} HAVE_${uf})
366         if(NOT HAVE_${uf})
367                 add_compile_definitions(NO_${uf})
368         endif()
369 endforeach()
370
371 if(NOT HAVE_POLL_H OR NOT HAVE_SYS_POLL_H OR NOT HAVE_POLL)
372         include_directories(${CMAKE_SOURCE_DIR}/compat/poll)
373         add_compile_definitions(NO_POLL)
374         list(APPEND compat_SOURCES compat/poll/poll.c)
375 endif()
376
377 if(NOT HAVE_STRCASESTR)
378         list(APPEND compat_SOURCES compat/strcasestr.c)
379 endif()
380
381 if(NOT HAVE_STRLCPY)
382         list(APPEND compat_SOURCES compat/strlcpy.c)
383 endif()
384
385 if(NOT HAVE_STRTOUMAX)
386         list(APPEND compat_SOURCES compat/strtoumax.c compat/strtoimax.c)
387 endif()
388
389 if(NOT HAVE_SETENV)
390         list(APPEND compat_SOURCES compat/setenv.c)
391 endif()
392
393 if(NOT HAVE_MKDTEMP)
394         list(APPEND compat_SOURCES compat/mkdtemp.c)
395 endif()
396
397 if(NOT HAVE_PREAD)
398         list(APPEND compat_SOURCES compat/pread.c)
399 endif()
400
401 if(NOT HAVE_MEMMEM)
402         list(APPEND compat_SOURCES compat/memmem.c)
403 endif()
404
405 if(NOT WIN32)
406         if(NOT HAVE_UNSETENV)
407                 list(APPEND compat_SOURCES compat/unsetenv.c)
408         endif()
409
410         if(NOT HAVE_HSTRERROR)
411                 list(APPEND compat_SOURCES compat/hstrerror.c)
412         endif()
413 endif()
414
415 check_function_exists(getdelim HAVE_GETDELIM)
416 if(HAVE_GETDELIM)
417         add_compile_definitions(HAVE_GETDELIM)
418 endif()
419
420 check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
421 check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_CLOCK_MONOTONIC)
422 if(HAVE_CLOCK_GETTIME)
423         add_compile_definitions(HAVE_CLOCK_GETTIME)
424 endif()
425 if(HAVE_CLOCK_MONOTONIC)
426         add_compile_definitions(HAVE_CLOCK_MONOTONIC)
427 endif()
428
429 #check for st_blocks in struct stat
430 check_struct_has_member("struct stat" st_blocks "sys/stat.h" STRUCT_STAT_HAS_ST_BLOCKS)
431 if(NOT STRUCT_STAT_HAS_ST_BLOCKS)
432         add_compile_definitions(NO_ST_BLOCKS_IN_STRUCT_STAT)
433 endif()
434
435 #compile checks
436 check_c_source_runs("
437 #include<stdio.h>
438 #include<stdarg.h>
439 #include<string.h>
440 #include<stdlib.h>
441
442 int test_vsnprintf(char *str, size_t maxsize, const char *format, ...)
443 {
444         int ret;
445         va_list ap;
446
447         va_start(ap, format);
448         ret = vsnprintf(str, maxsize, format, ap);
449         va_end(ap);
450         return ret;
451 }
452
453 int main(void)
454 {
455         char buf[6];
456
457         if (test_vsnprintf(buf, 3, \"%s\", \"12345\") != 5
458                 || strcmp(buf, \"12\"))
459                         return 1;
460         if (snprintf(buf, 3, \"%s\", \"12345\") != 5
461                 || strcmp(buf, \"12\"))
462                         return 1;
463         return 0;
464 }"
465 SNPRINTF_OK)
466 if(NOT SNPRINTF_OK)
467         add_compile_definitions(SNPRINTF_RETURNS_BOGUS)
468         list(APPEND compat_SOURCES compat/snprintf.c)
469 endif()
470
471 check_c_source_runs("
472 #include<stdio.h>
473
474 int main(void)
475 {
476         FILE *f = fopen(\".\", \"r\");
477
478         return f != NULL;
479 }"
480 FREAD_READS_DIRECTORIES_NO)
481 if(NOT FREAD_READS_DIRECTORIES_NO)
482         add_compile_definitions(FREAD_READS_DIRECTORIES)
483         list(APPEND compat_SOURCES compat/fopen.c)
484 endif()
485
486 check_c_source_compiles("
487 #include <regex.h>
488 #ifndef REG_STARTEND
489 #error oops we dont have it
490 #endif
491
492 int main(void)
493 {
494         return 0;
495 }"
496 HAVE_REGEX)
497 if(NOT HAVE_REGEX)
498         include_directories(${CMAKE_SOURCE_DIR}/compat/regex)
499         list(APPEND compat_SOURCES compat/regex/regex.c )
500         add_compile_definitions(NO_REGEX NO_MBSUPPORT GAWK)
501 endif()
502
503
504 check_c_source_compiles("
505 #include <stddef.h>
506 #include <sys/types.h>
507 #include <sys/sysctl.h>
508
509 int main(void)
510 {
511         int val, mib[2];
512         size_t len;
513
514         mib[0] = CTL_HW;
515         mib[1] = 1;
516         len = sizeof(val);
517         return sysctl(mib, 2, &val, &len, NULL, 0) ? 1 : 0;
518 }"
519 HAVE_BSD_SYSCTL)
520 if(HAVE_BSD_SYSCTL)
521         add_compile_definitions(HAVE_BSD_SYSCTL)
522 endif()
523
524 set(CMAKE_REQUIRED_LIBRARIES ${Iconv_LIBRARIES})
525 set(CMAKE_REQUIRED_INCLUDES ${Iconv_INCLUDE_DIRS})
526
527 check_c_source_compiles("
528 #include <iconv.h>
529
530 extern size_t iconv(iconv_t cd,
531                 char **inbuf, size_t *inbytesleft,
532                 char **outbuf, size_t *outbytesleft);
533
534 int main(void)
535 {
536         return 0;
537 }"
538 HAVE_NEW_ICONV)
539 if(HAVE_NEW_ICONV)
540         set(HAVE_OLD_ICONV 0)
541 else()
542         set(HAVE_OLD_ICONV 1)
543 endif()
544
545 check_c_source_runs("
546 #include <iconv.h>
547 #if ${HAVE_OLD_ICONV}
548 typedef const char *iconv_ibp;
549 #else
550 typedef char *iconv_ibp;
551 #endif
552
553 int main(void)
554 {
555         int v;
556         iconv_t conv;
557         char in[] = \"a\";
558         iconv_ibp pin = in;
559         char out[20] = \"\";
560         char *pout = out;
561         size_t isz = sizeof(in);
562         size_t osz = sizeof(out);
563
564         conv = iconv_open(\"UTF-16\", \"UTF-8\");
565         iconv(conv, &pin, &isz, &pout, &osz);
566         iconv_close(conv);
567         v = (unsigned char)(out[0]) + (unsigned char)(out[1]);
568         return v != 0xfe + 0xff;
569 }"
570 ICONV_DOESNOT_OMIT_BOM)
571 if(NOT ICONV_DOESNOT_OMIT_BOM)
572         add_compile_definitions(ICONV_OMITS_BOM)
573 endif()
574
575 unset(CMAKE_REQUIRED_LIBRARIES)
576 unset(CMAKE_REQUIRED_INCLUDES)
577
578
579 #programs
580 set(PROGRAMS_BUILT
581         git git-daemon git-http-backend git-sh-i18n--envsubst
582         git-shell)
583
584 if(NOT CURL_FOUND)
585         list(APPEND excluded_progs git-http-fetch git-http-push)
586         add_compile_definitions(NO_CURL)
587         message(WARNING "git-http-push and git-http-fetch will not be built")
588 else()
589         list(APPEND PROGRAMS_BUILT git-http-fetch git-http-push git-imap-send git-remote-http)
590         if(CURL_VERSION_STRING VERSION_GREATER_EQUAL 7.34.0)
591                 add_compile_definitions(USE_CURL_FOR_IMAP_SEND)
592         endif()
593 endif()
594
595 if(NOT EXPAT_FOUND)
596         list(APPEND excluded_progs git-http-push)
597         add_compile_definitions(NO_EXPAT)
598 else()
599         list(APPEND PROGRAMS_BUILT git-http-push)
600         if(EXPAT_VERSION_STRING VERSION_LESS_EQUAL 1.2)
601                 add_compile_definitions(EXPAT_NEEDS_XMLPARSE_H)
602         endif()
603 endif()
604
605 list(REMOVE_DUPLICATES excluded_progs)
606 list(REMOVE_DUPLICATES PROGRAMS_BUILT)
607
608
609 foreach(p ${excluded_progs})
610         list(APPEND EXCLUSION_PROGS --exclude-program ${p} )
611 endforeach()
612
613 #for comparing null values
614 list(APPEND EXCLUSION_PROGS empty)
615 set(EXCLUSION_PROGS_CACHE ${EXCLUSION_PROGS} CACHE STRING "Programs not built" FORCE)
616
617 if(NOT EXISTS ${CMAKE_BINARY_DIR}/command-list.h OR NOT EXCLUSION_PROGS_CACHE STREQUAL EXCLUSION_PROGS)
618         list(REMOVE_ITEM EXCLUSION_PROGS empty)
619         message("Generating command-list.h")
620         execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-cmdlist.sh ${EXCLUSION_PROGS} command-list.txt
621                         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
622                         OUTPUT_FILE ${CMAKE_BINARY_DIR}/command-list.h)
623 endif()
624
625 if(NOT EXISTS ${CMAKE_BINARY_DIR}/config-list.h)
626         message("Generating config-list.h")
627         execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-configlist.sh
628                         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
629                         OUTPUT_FILE ${CMAKE_BINARY_DIR}/config-list.h)
630 endif()
631
632 include_directories(${CMAKE_BINARY_DIR})
633
634 #build
635 #libgit
636 parse_makefile_for_sources(libgit_SOURCES "LIB_OBJS")
637
638 list(TRANSFORM libgit_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
639 list(TRANSFORM compat_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
640 add_library(libgit ${libgit_SOURCES} ${compat_SOURCES})
641
642 #libxdiff
643 parse_makefile_for_sources(libxdiff_SOURCES "XDIFF_OBJS")
644
645 list(TRANSFORM libxdiff_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
646 add_library(xdiff STATIC ${libxdiff_SOURCES})
647
648 if(WIN32)
649         if(NOT MSVC)#use windres when compiling with gcc and clang
650                 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
651                                 COMMAND ${WINDRES_EXE} -O coff -DMAJOR=${PROJECT_VERSION_MAJOR} -DMINOR=${PROJECT_VERSION_MINOR}
652                                         -DMICRO=${PROJECT_VERSION_PATCH} -DPATCHLEVEL=0 -DGIT_VERSION="\\\"${PROJECT_VERSION}.GIT\\\""
653                                         -i ${CMAKE_SOURCE_DIR}/git.rc -o ${CMAKE_BINARY_DIR}/git.res
654                                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
655                                 VERBATIM)
656         else()#MSVC use rc
657                 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
658                                 COMMAND ${CMAKE_RC_COMPILER} /d MAJOR=${PROJECT_VERSION_MAJOR} /d MINOR=${PROJECT_VERSION_MINOR}
659                                         /d MICRO=${PROJECT_VERSION_PATCH} /d PATCHLEVEL=0 /d GIT_VERSION="${PROJECT_VERSION}.GIT"
660                                         /fo ${CMAKE_BINARY_DIR}/git.res ${CMAKE_SOURCE_DIR}/git.rc
661                                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
662                                 VERBATIM)
663         endif()
664         add_custom_target(git-rc DEPENDS ${CMAKE_BINARY_DIR}/git.res)
665 endif()
666
667 #link all required libraries to common-main
668 add_library(common-main OBJECT ${CMAKE_SOURCE_DIR}/common-main.c)
669
670 target_link_libraries(common-main libgit xdiff ${ZLIB_LIBRARIES})
671 if(Intl_FOUND)
672         target_link_libraries(common-main ${Intl_LIBRARIES})
673 endif()
674 if(Iconv_FOUND)
675         target_link_libraries(common-main ${Iconv_LIBRARIES})
676 endif()
677 if(WIN32)
678         target_link_libraries(common-main ws2_32 ntdll ${CMAKE_BINARY_DIR}/git.res)
679         add_dependencies(common-main git-rc)
680         if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
681                 target_link_options(common-main PUBLIC -municode -Wl,--nxcompat -Wl,--dynamicbase -Wl,--pic-executable,-e,mainCRTStartup)
682         elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
683                 target_link_options(common-main PUBLIC -municode -Wl,-nxcompat -Wl,-dynamicbase -Wl,-entry:wmainCRTStartup -Wl,invalidcontinue.obj)
684         elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
685                 target_link_options(common-main PUBLIC /IGNORE:4217 /IGNORE:4049 /NOLOGO /ENTRY:wmainCRTStartup /SUBSYSTEM:CONSOLE invalidcontinue.obj)
686         else()
687                 message(FATAL_ERROR "Unhandled compiler: ${CMAKE_C_COMPILER_ID}")
688         endif()
689 elseif(UNIX)
690         target_link_libraries(common-main pthread rt)
691 endif()
692
693 #git
694 parse_makefile_for_sources(git_SOURCES "BUILTIN_OBJS")
695
696 list(TRANSFORM git_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
697 add_executable(git ${CMAKE_SOURCE_DIR}/git.c ${git_SOURCES})
698 target_link_libraries(git common-main)
699
700 add_executable(git-daemon ${CMAKE_SOURCE_DIR}/daemon.c)
701 target_link_libraries(git-daemon common-main)
702
703 add_executable(git-http-backend ${CMAKE_SOURCE_DIR}/http-backend.c)
704 target_link_libraries(git-http-backend common-main)
705
706 add_executable(git-sh-i18n--envsubst ${CMAKE_SOURCE_DIR}/sh-i18n--envsubst.c)
707 target_link_libraries(git-sh-i18n--envsubst common-main)
708
709 add_executable(git-shell ${CMAKE_SOURCE_DIR}/shell.c)
710 target_link_libraries(git-shell common-main)
711
712 if(CURL_FOUND)
713         add_library(http_obj OBJECT ${CMAKE_SOURCE_DIR}/http.c)
714
715         add_executable(git-imap-send ${CMAKE_SOURCE_DIR}/imap-send.c)
716         target_link_libraries(git-imap-send http_obj common-main ${CURL_LIBRARIES})
717
718         add_executable(git-http-fetch ${CMAKE_SOURCE_DIR}/http-walker.c ${CMAKE_SOURCE_DIR}/http-fetch.c)
719         target_link_libraries(git-http-fetch http_obj common-main ${CURL_LIBRARIES})
720
721         add_executable(git-remote-http ${CMAKE_SOURCE_DIR}/http-walker.c ${CMAKE_SOURCE_DIR}/remote-curl.c)
722         target_link_libraries(git-remote-http http_obj common-main ${CURL_LIBRARIES} )
723
724         if(EXPAT_FOUND)
725                 add_executable(git-http-push ${CMAKE_SOURCE_DIR}/http-push.c)
726                 target_link_libraries(git-http-push http_obj common-main ${CURL_LIBRARIES} ${EXPAT_LIBRARIES})
727         endif()
728 endif()
729
730 parse_makefile_for_executables(git_builtin_extra "BUILT_INS")
731
732 option(SKIP_DASHED_BUILT_INS "Skip hardlinking the dashed versions of the built-ins")
733
734 #Creating hardlinks
735 if(NOT SKIP_DASHED_BUILT_INS)
736 foreach(s ${git_SOURCES} ${git_builtin_extra})
737         string(REPLACE "${CMAKE_SOURCE_DIR}/builtin/" "" s ${s})
738         string(REPLACE ".c" "" s ${s})
739         file(APPEND ${CMAKE_BINARY_DIR}/CreateLinks.cmake "file(CREATE_LINK git${EXE_EXTENSION} git-${s}${EXE_EXTENSION})\n")
740         list(APPEND git_links ${CMAKE_BINARY_DIR}/git-${s}${EXE_EXTENSION})
741 endforeach()
742 endif()
743
744 if(CURL_FOUND)
745         set(remote_exes
746                 git-remote-https git-remote-ftp git-remote-ftps)
747         foreach(s ${remote_exes})
748                 file(APPEND ${CMAKE_BINARY_DIR}/CreateLinks.cmake "file(CREATE_LINK git-remote-http${EXE_EXTENSION} ${s}${EXE_EXTENSION})\n")
749                 list(APPEND git_http_links ${CMAKE_BINARY_DIR}/${s}${EXE_EXTENSION})
750         endforeach()
751 endif()
752
753 add_custom_command(OUTPUT ${git_links} ${git_http_links}
754                 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/CreateLinks.cmake
755                 DEPENDS git git-remote-http)
756 add_custom_target(git-links ALL DEPENDS ${git_links} ${git_http_links})
757
758
759 #creating required scripts
760 set(SHELL_PATH /bin/sh)
761 set(PERL_PATH /usr/bin/perl)
762 set(LOCALEDIR ${FALLBACK_RUNTIME_PREFIX}/share/locale)
763 set(GITWEBDIR ${FALLBACK_RUNTIME_PREFIX}/share/locale)
764 set(INSTLIBDIR ${FALLBACK_RUNTIME_PREFIX}/share/perl5)
765
766 #shell scripts
767 parse_makefile_for_scripts(git_sh_scripts "SCRIPT_SH" ".sh")
768 parse_makefile_for_scripts(git_shlib_scripts "SCRIPT_LIB" "")
769 set(git_shell_scripts
770         ${git_sh_scripts} ${git_shlib_scripts} git-instaweb)
771
772 foreach(script ${git_shell_scripts})
773         file(STRINGS ${CMAKE_SOURCE_DIR}/${script}.sh content NEWLINE_CONSUME)
774         string(REPLACE "@SHELL_PATH@" "${SHELL_PATH}" content "${content}")
775         string(REPLACE "@@DIFF@@" "diff" content "${content}")
776         string(REPLACE "@LOCALEDIR@" "${LOCALEDIR}" content "${content}")
777         string(REPLACE "@GITWEBDIR@" "${GITWEBDIR}" content "${content}")
778         string(REPLACE "@@NO_CURL@@" "" content "${content}")
779         string(REPLACE "@@USE_GETTEXT_SCHEME@@" "" content "${content}")
780         string(REPLACE "# @@BROKEN_PATH_FIX@@" "" content "${content}")
781         string(REPLACE "@@PERL@@" "${PERL_PATH}" content "${content}")
782         string(REPLACE "@@SANE_TEXT_GREP@@" "-a" content "${content}")
783         string(REPLACE "@@PAGER_ENV@@" "LESS=FRX LV=-c" content "${content}")
784         file(WRITE ${CMAKE_BINARY_DIR}/${script} ${content})
785 endforeach()
786
787 #perl scripts
788 parse_makefile_for_scripts(git_perl_scripts "SCRIPT_PERL" ".perl")
789
790 #create perl header
791 file(STRINGS ${CMAKE_SOURCE_DIR}/perl/header_templates/fixed_prefix.template.pl perl_header )
792 string(REPLACE "@@PATHSEP@@" ":" perl_header "${perl_header}")
793 string(REPLACE "@@INSTLIBDIR@@" "${INSTLIBDIR}" perl_header "${perl_header}")
794
795 foreach(script ${git_perl_scripts})
796         file(STRINGS ${CMAKE_SOURCE_DIR}/${script}.perl content NEWLINE_CONSUME)
797         string(REPLACE "#!/usr/bin/perl" "#!/usr/bin/perl\n${perl_header}\n" content "${content}")
798         string(REPLACE "@@GIT_VERSION@@" "${PROJECT_VERSION}" content "${content}")
799         file(WRITE ${CMAKE_BINARY_DIR}/${script} ${content})
800 endforeach()
801
802 #python script
803 file(STRINGS ${CMAKE_SOURCE_DIR}/git-p4.py content NEWLINE_CONSUME)
804 string(REPLACE "#!/usr/bin/env python" "#!/usr/bin/python" content "${content}")
805 file(WRITE ${CMAKE_BINARY_DIR}/git-p4 ${content})
806
807 #perl modules
808 file(GLOB_RECURSE perl_modules "${CMAKE_SOURCE_DIR}/perl/*.pm")
809
810 foreach(pm ${perl_modules})
811         string(REPLACE "${CMAKE_SOURCE_DIR}/perl/" "" file_path ${pm})
812         file(STRINGS ${pm} content NEWLINE_CONSUME)
813         string(REPLACE "@@LOCALEDIR@@" "${LOCALEDIR}" content "${content}")
814         string(REPLACE "@@NO_PERL_CPAN_FALLBACKS@@" "" content "${content}")
815         file(WRITE ${CMAKE_BINARY_DIR}/perl/build/lib/${file_path} ${content})
816 #test-lib.sh requires perl/build/lib to be the build directory of perl modules
817 endforeach()
818
819
820 #templates
821 file(GLOB templates "${CMAKE_SOURCE_DIR}/templates/*")
822 list(TRANSFORM templates REPLACE "${CMAKE_SOURCE_DIR}/templates/" "")
823 list(REMOVE_ITEM templates ".gitignore")
824 list(REMOVE_ITEM templates "Makefile")
825 list(REMOVE_ITEM templates "blt")# Prevents an error when reconfiguring for in source builds
826
827 list(REMOVE_ITEM templates "branches--")
828 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/templates/blt/branches) #create branches
829
830 #templates have @.*@ replacement so use configure_file instead
831 foreach(tm ${templates})
832         string(REPLACE "--" "/" blt_tm ${tm})
833         string(REPLACE "this" "" blt_tm ${blt_tm})# for this--
834         configure_file(${CMAKE_SOURCE_DIR}/templates/${tm} ${CMAKE_BINARY_DIR}/templates/blt/${blt_tm} @ONLY)
835 endforeach()
836
837
838 #translations
839 if(MSGFMT_EXE)
840         file(GLOB po_files "${CMAKE_SOURCE_DIR}/po/*.po")
841         list(TRANSFORM po_files REPLACE "${CMAKE_SOURCE_DIR}/po/" "")
842         list(TRANSFORM po_files REPLACE ".po" "")
843         foreach(po ${po_files})
844                 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES)
845                 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo
846                                 COMMAND ${MSGFMT_EXE} --check --statistics -o ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo ${CMAKE_SOURCE_DIR}/po/${po}.po)
847                 list(APPEND po_gen ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo)
848         endforeach()
849         add_custom_target(po-gen ALL DEPENDS ${po_gen})
850 endif()
851
852
853 #to help with the install
854 list(TRANSFORM git_shell_scripts PREPEND "${CMAKE_BINARY_DIR}/")
855 list(TRANSFORM git_perl_scripts PREPEND "${CMAKE_BINARY_DIR}/")
856
857 #install
858 foreach(program ${PROGRAMS_BUILT})
859 if(program STREQUAL "git" OR program STREQUAL "git-shell")
860 install(TARGETS ${program}
861         RUNTIME DESTINATION bin)
862 else()
863 install(TARGETS ${program}
864         RUNTIME DESTINATION libexec/git-core)
865 endif()
866 endforeach()
867
868 install(PROGRAMS ${CMAKE_BINARY_DIR}/git-cvsserver
869         DESTINATION bin)
870
871 set(bin_links
872         git-receive-pack git-upload-archive git-upload-pack)
873
874 foreach(b ${bin_links})
875 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/bin/${b}${EXE_EXTENSION})")
876 endforeach()
877
878 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git${EXE_EXTENSION})")
879 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git-shell${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git-shell${EXE_EXTENSION})")
880
881 foreach(b ${git_links})
882         string(REPLACE "${CMAKE_BINARY_DIR}" "" b ${b})
883         install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/${b})")
884 endforeach()
885
886 foreach(b ${git_http_links})
887         string(REPLACE "${CMAKE_BINARY_DIR}" "" b ${b})
888         install(CODE "file(CREATE_LINK  ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git-remote-http${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/${b})")
889 endforeach()
890
891 install(PROGRAMS ${git_shell_scripts} ${git_perl_scripts} ${CMAKE_BINARY_DIR}/git-p4
892         DESTINATION libexec/git-core)
893
894 install(DIRECTORY ${CMAKE_SOURCE_DIR}/mergetools DESTINATION libexec/git-core)
895 install(DIRECTORY ${CMAKE_BINARY_DIR}/perl/build/lib/ DESTINATION share/perl5
896         FILES_MATCHING PATTERN "*.pm")
897 install(DIRECTORY ${CMAKE_BINARY_DIR}/templates/blt/ DESTINATION share/git-core/templates)
898
899 if(MSGFMT_EXE)
900         install(DIRECTORY ${CMAKE_BINARY_DIR}/po/build/locale DESTINATION share)
901 endif()
902
903
904 if(BUILD_TESTING)
905
906 #tests-helpers
907 add_executable(test-fake-ssh ${CMAKE_SOURCE_DIR}/t/helper/test-fake-ssh.c)
908 target_link_libraries(test-fake-ssh common-main)
909
910 #test-tool
911 parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")
912
913 list(TRANSFORM test-tool_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/t/helper/")
914 add_executable(test-tool ${CMAKE_SOURCE_DIR}/t/helper/test-tool.c ${test-tool_SOURCES})
915 target_link_libraries(test-tool common-main)
916
917 set_target_properties(test-fake-ssh test-tool
918                         PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/helper)
919
920 if(MSVC)
921         set_target_properties(test-fake-ssh test-tool
922                                 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/helper)
923         set_target_properties(test-fake-ssh test-tool
924                                 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/helper)
925 endif()
926
927 #wrapper scripts
928 set(wrapper_scripts
929         git git-upload-pack git-receive-pack git-upload-archive git-shell git-remote-ext)
930
931 set(wrapper_test_scripts
932         test-fake-ssh test-tool)
933
934
935 foreach(script ${wrapper_scripts})
936         file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
937         string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
938         string(REPLACE "@@PROG@@" "${script}${EXE_EXTENSION}" content "${content}")
939         file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/${script} ${content})
940 endforeach()
941
942 foreach(script ${wrapper_test_scripts})
943         file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
944         string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
945         string(REPLACE "@@PROG@@" "t/helper/${script}${EXE_EXTENSION}" content "${content}")
946         file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/${script} ${content})
947 endforeach()
948
949 file(STRINGS ${CMAKE_SOURCE_DIR}/wrap-for-bin.sh content NEWLINE_CONSUME)
950 string(REPLACE "@@BUILD_DIR@@" "${CMAKE_BINARY_DIR}" content "${content}")
951 string(REPLACE "@@PROG@@" "git-cvsserver" content "${content}")
952 file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/git-cvsserver ${content})
953
954 #options for configuring test options
955 option(PERL_TESTS "Perform tests that use perl" ON)
956 option(PYTHON_TESTS "Perform tests that use python" ON)
957
958 #GIT-BUILD-OPTIONS
959 set(TEST_SHELL_PATH ${SHELL_PATH})
960 set(DIFF diff)
961 set(PYTHON_PATH /usr/bin/python)
962 set(TAR tar)
963 set(NO_CURL )
964 set(NO_EXPAT )
965 set(USE_LIBPCRE2 )
966 set(NO_PERL )
967 set(NO_PTHREADS )
968 set(NO_PYTHON )
969 set(PAGER_ENV "LESS=FRX LV=-c")
970 set(DC_SHA1 YesPlease)
971 set(RUNTIME_PREFIX true)
972 set(NO_GETTEXT )
973
974 if(NOT CURL_FOUND)
975         set(NO_CURL 1)
976 endif()
977
978 if(NOT EXPAT_FOUND)
979         set(NO_EXPAT 1)
980 endif()
981
982 if(NOT Intl_FOUND)
983         set(NO_GETTEXT 1)
984 endif()
985
986 if(NOT PERL_TESTS)
987         set(NO_PERL 1)
988 endif()
989
990 if(NOT PYTHON_TESTS)
991         set(NO_PYTHON 1)
992 endif()
993
994 file(WRITE ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "SHELL_PATH='${SHELL_PATH}'\n")
995 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "TEST_SHELL_PATH='${TEST_SHELL_PATH}'\n")
996 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PERL_PATH='${PERL_PATH}'\n")
997 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "DIFF='${DIFF}'\n")
998 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PYTHON_PATH='${PYTHON_PATH}'\n")
999 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "TAR='${TAR}'\n")
1000 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_CURL='${NO_CURL}'\n")
1001 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_EXPAT='${NO_EXPAT}'\n")
1002 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PERL='${NO_PERL}'\n")
1003 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PTHREADS='${NO_PTHREADS}'\n")
1004 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_UNIX_SOCKETS='${NO_UNIX_SOCKETS}'\n")
1005 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PAGER_ENV='${PAGER_ENV}'\n")
1006 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "DC_SHA1='${DC_SHA1}'\n")
1007 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "X='${EXE_EXTENSION}'\n")
1008 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_GETTEXT='${NO_GETTEXT}'\n")
1009 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "RUNTIME_PREFIX='${RUNTIME_PREFIX}'\n")
1010 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PYTHON='${NO_PYTHON}'\n")
1011 file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "SUPPORTS_SIMPLE_IPC='${SUPPORTS_SIMPLE_IPC}'\n")
1012 if(USE_VCPKG)
1013         file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PATH=\"$PATH:$TEST_DIRECTORY/../compat/vcbuild/vcpkg/installed/x64-windows/bin\"\n")
1014 endif()
1015
1016 #Make the tests work when building out of the source tree
1017 get_filename_component(CACHE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../CMakeCache.txt ABSOLUTE)
1018 if(NOT ${CMAKE_BINARY_DIR}/CMakeCache.txt STREQUAL ${CACHE_PATH})
1019         file(RELATIVE_PATH BUILD_DIR_RELATIVE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}/CMakeCache.txt)
1020         string(REPLACE "/CMakeCache.txt" "" BUILD_DIR_RELATIVE ${BUILD_DIR_RELATIVE})
1021         #Setting the build directory in test-lib.sh before running tests
1022         file(WRITE ${CMAKE_BINARY_DIR}/CTestCustom.cmake
1023                 "file(STRINGS ${CMAKE_SOURCE_DIR}/t/test-lib.sh GIT_BUILD_DIR_REPL REGEX \"GIT_BUILD_DIR=(.*)\")\n"
1024                 "file(STRINGS ${CMAKE_SOURCE_DIR}/t/test-lib.sh content NEWLINE_CONSUME)\n"
1025                 "string(REPLACE \"\${GIT_BUILD_DIR_REPL}\" \"GIT_BUILD_DIR=\\\"$TEST_DIRECTORY/../${BUILD_DIR_RELATIVE}\\\"\" content \"\${content}\")\n"
1026                 "file(WRITE ${CMAKE_SOURCE_DIR}/t/test-lib.sh \${content})")
1027         #misc copies
1028         file(COPY ${CMAKE_SOURCE_DIR}/t/chainlint.sed DESTINATION ${CMAKE_BINARY_DIR}/t/)
1029         file(COPY ${CMAKE_SOURCE_DIR}/po/is.po DESTINATION ${CMAKE_BINARY_DIR}/po/)
1030         file(COPY ${CMAKE_SOURCE_DIR}/mergetools/tkdiff DESTINATION ${CMAKE_BINARY_DIR}/mergetools/)
1031         file(COPY ${CMAKE_SOURCE_DIR}/contrib/completion/git-prompt.sh DESTINATION ${CMAKE_BINARY_DIR}/contrib/completion/)
1032         file(COPY ${CMAKE_SOURCE_DIR}/contrib/completion/git-completion.bash DESTINATION ${CMAKE_BINARY_DIR}/contrib/completion/)
1033 endif()
1034
1035 file(GLOB test_scipts "${CMAKE_SOURCE_DIR}/t/t[0-9]*.sh")
1036
1037 #test
1038 foreach(tsh ${test_scipts})
1039         add_test(NAME ${tsh}
1040                 COMMAND ${SH_EXE} ${tsh}
1041                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t)
1042 endforeach()
1043
1044 endif()#BUILD_TESTING