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