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