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