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