Raw
1 #
2 # Copyright (c) 2020 Sibi Siddharthan
3 #
4
5 #[[
6
7 Instructions how to use this in Visual Studio:
8
9 Open the worktree as a folder. Visual Studio 2019 and later will detect
10 the CMake configuration automatically and set everything up for you,
11 ready to build. You can then run the tests in `t/` via a regular Git Bash.
12
13 Note: Visual Studio also has the option of opening `CMakeLists.txt`
14 directly; Using this option, Visual Studio will not find the source code,
15 though, therefore the `File>Open>Folder...` option is preferred.
16
17 Instructions to run CMake manually:
18
19 mkdir -p contrib/buildsystems/out
20 cd contrib/buildsystems/out
21 cmake ../ -DCMAKE_BUILD_TYPE=Release
22
23 This will build the git binaries in contrib/buildsystems/out
24 directory (our top-level .gitignore file knows to ignore contents of
25 this directory).
26
27 Possible build configurations(-DCMAKE_BUILD_TYPE) with corresponding
28 compiler flags
29 Debug : -g
30 Release: -O3
31 RelWithDebInfo : -O2 -g
32 MinSizeRel : -Os
33 empty(default) :
34
35 NOTE: -DCMAKE_BUILD_TYPE is optional. For multi-config generators like Visual Studio
36 this option is ignored
37
38 This process generates a Makefile(Linux/*BSD/MacOS) , Visual Studio solution(Windows) by default.
39 Run `make` to build Git on Linux/*BSD/MacOS.
40 Open git.sln on Windows and build Git.
41
42 NOTE: By default CMake uses Makefile as the build tool on Linux and Visual Studio in Windows,
43 to use another tool say `ninja` add this to the command line when configuring.
44 `-G Ninja`
45
46 NOTE: By default CMake will install vcpkg locally to your source tree on configuration,
47 to avoid this, add `-DNO_VCPKG=TRUE` to the command line when configuring.
48
49 ]]
50 cmake_minimum_required(VERSION 3.14)
51
52 #set the source directory to root of git
53 set(CMAKE_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
54
55 option(USE_VCPKG "Whether or not to use vcpkg for obtaining dependencies. Only applicable to Windows platforms" ON)
56 if(NOT WIN32)
57 set(USE_VCPKG OFF CACHE BOOL "" FORCE)
58 endif()
59
60 if(NOT DEFINED CMAKE_EXPORT_COMPILE_COMMANDS)
61 set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
62 endif()
63
64 if(USE_VCPKG)
65 set(VCPKG_DIR "${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg")
66 if(NOT EXISTS ${VCPKG_DIR})
67 message("Initializing vcpkg and building the Git's dependencies (this will take a while...)")
68 execute_process(COMMAND ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg_install.bat)
69 endif()
70 list(APPEND CMAKE_PREFIX_PATH "${VCPKG_DIR}/installed/x64-windows")
71
72 # In the vcpkg edition, we need this to be able to link to libcurl
73 set(CURL_NO_CURL_CMAKE ON)
74
75 # Copy the necessary vcpkg DLLs (like iconv) to the install dir
76 set(X_VCPKG_APPLOCAL_DEPS_INSTALL ON)
77 set(CMAKE_TOOLCHAIN_FILE ${VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file")
78 endif()
79
80 find_program(SH_EXE sh PATHS "C:/Program Files/Git/bin" "$ENV{LOCALAPPDATA}/Programs/Git/bin")
81 if(NOT SH_EXE)
82 message(FATAL_ERROR "sh: shell interpreter was not found in your path, please install one."
83 "On Windows, you can get it as part of 'Git for Windows' install at https://gitforwindows.org/")
84 endif()
85
86 message("Generating Git version")
87 execute_process(COMMAND ${SH_EXE} "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
88 "${CMAKE_SOURCE_DIR}"
89 "${CMAKE_SOURCE_DIR}/contrib/buildsystems/git-version.in"
90 "${CMAKE_BINARY_DIR}/git-version")
91 file(STRINGS "${CMAKE_BINARY_DIR}/git-version" git_version)
92
93 project(git
94 VERSION ${git_version}
95 LANGUAGES C)
96
97
98 #TODO gitk git-gui gitweb
99 #TODO Enable NLS on windows natively
100
101 #macros for parsing the Makefile for sources and scripts
102 macro(parse_makefile_for_sources list_var makefile regex)
103 file(STRINGS ${makefile} ${list_var} REGEX "^${regex} \\+=(.*)")
104 string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
105 string(REPLACE "$(COMPAT_OBJS)" "" ${list_var} ${${list_var}}) #remove "$(COMPAT_OBJS)" This is only for libgit.
106 string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
107 string(REPLACE ".o" ".c;" ${list_var} ${${list_var}}) #change .o to .c, ; is for converting the string into a list
108 list(TRANSFORM ${list_var} STRIP) #remove trailing/leading whitespaces for each element in list
109 list(REMOVE_ITEM ${list_var} "") #remove empty list elements
110 endmacro()
111
112 macro(parse_makefile_for_scripts list_var regex lang)
113 file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+=(.*)")
114 string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
115 string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
116 string(REPLACE " " ";" ${list_var} ${${list_var}}) #convert string to a list
117 if(NOT ${lang}) #exclude for SCRIPT_LIB
118 list(TRANSFORM ${list_var} REPLACE "${lang}" "") #do the replacement
119 endif()
120 endmacro()
121
122 macro(parse_makefile_for_executables list_var regex)
123 file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+= git-(.*)")
124 string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
125 string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
126 string(REPLACE "git-" "" ${list_var} ${${list_var}}) #strip `git-` prefix
127 string(REPLACE "\$X" ";" ${list_var} ${${list_var}}) #strip $X, ; is for converting the string into a list
128 list(TRANSFORM ${list_var} STRIP) #remove trailing/leading whitespaces for each element in list
129 list(REMOVE_ITEM ${list_var} "") #remove empty list elements
130 endmacro()
131
132 include(CheckTypeSize)
133 include(CheckCSourceRuns)
134 include(CheckCSourceCompiles)
135 include(CheckIncludeFile)
136 include(CheckFunctionExists)
137 include(CheckSymbolExists)
138 include(CheckStructHasMember)
139 include(CTest)
140
141 find_package(ZLIB REQUIRED)
142 find_package(CURL)
143 find_package(EXPAT)
144 find_package(Iconv)
145
146 #Don't use libintl on Windows Visual Studio and Clang builds
147 if(NOT (WIN32 AND (CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")))
148 find_package(Intl)
149 endif()
150
151 find_package(PkgConfig)
152 if(PkgConfig_FOUND)
153 pkg_check_modules(PCRE2 libpcre2-8)
154 if(PCRE2_FOUND)
155 add_compile_definitions(USE_LIBPCRE2)
156 endif()
157 endif()
158
159 if(NOT Intl_FOUND)
160 add_compile_definitions(NO_GETTEXT)
161 if(NOT Iconv_FOUND)
162 add_compile_definitions(NO_ICONV)
163 endif()
164 endif()
165
166 include_directories(SYSTEM ${ZLIB_INCLUDE_DIRS})
167 if(CURL_FOUND)
168 include_directories(SYSTEM ${CURL_INCLUDE_DIRS})
169 endif()
170 if(EXPAT_FOUND)
171 include_directories(SYSTEM ${EXPAT_INCLUDE_DIRS})
172 endif()
173 if(Iconv_FOUND)
174 include_directories(SYSTEM ${Iconv_INCLUDE_DIRS})
175 endif()
176 if(Intl_FOUND)
177 include_directories(SYSTEM ${Intl_INCLUDE_DIRS})
178 endif()
179 if(PCRE2_FOUND)
180 include_directories(SYSTEM ${PCRE2_INCLUDE_DIRS})
181 endif()
182
183
184 if(WIN32 AND NOT MSVC)#not required for visual studio builds
185 find_program(WINDRES_EXE windres)
186 if(NOT WINDRES_EXE)
187 message(FATAL_ERROR "Install windres on Windows for resource files")
188 endif()
189 endif()
190
191 if(NO_GETTEXT)
192 message(STATUS "msgfmt not used under NO_GETTEXT")
193 else()
194 find_program(MSGFMT_EXE msgfmt)
195 if(NOT MSGFMT_EXE)
196 if(USE_VCPKG)
197 set(MSGFMT_EXE ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg/downloads/tools/msys2/msys64/usr/bin/msgfmt.exe)
198 endif()
199 if(NOT EXISTS ${MSGFMT_EXE})
200 message(WARNING "Text Translations won't be built")
201 unset(MSGFMT_EXE)
202 endif()
203 endif()
204 endif()
205
206 #Force all visual studio outputs to CMAKE_BINARY_DIR
207 if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
208 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR})
209 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR})
210 add_compile_options(/MP /std:c11)
211 endif()
212
213 #default behaviour
214 include_directories(${CMAKE_SOURCE_DIR})
215 add_compile_definitions(GIT_HOST_CPU="${CMAKE_SYSTEM_PROCESSOR}")
216 add_compile_definitions(SHA256_BLK INTERNAL_QSORT RUNTIME_PREFIX)
217 add_compile_definitions(NO_OPENSSL SHA1_DC SHA1DC_NO_STANDARD_INCLUDES
218 SHA1DC_INIT_SAFE_HASH_DEFAULT=0
219 SHA1DC_CUSTOM_INCLUDE_SHA1_C="git-compat-util.h"
220 SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C="git-compat-util.h" )
221 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)
222
223
224 add_compile_definitions(PAGER_ENV="LESS=FRX LV=-c"
225 GIT_EXEC_PATH="libexec/git-core"
226 GIT_LOCALE_PATH="share/locale"
227 GIT_MAN_PATH="share/man"
228 GIT_INFO_PATH="share/info"
229 GIT_HTML_PATH="share/doc/git-doc"
230 DEFAULT_HELP_FORMAT="html"
231 DEFAULT_GIT_TEMPLATE_DIR="share/git-core/templates"
232 BINDIR="bin")
233
234 if(WIN32)
235 set(FALLBACK_RUNTIME_PREFIX /mingw64)
236 # Move system config into top-level /etc/
237 add_compile_definitions(FALLBACK_RUNTIME_PREFIX="${FALLBACK_RUNTIME_PREFIX}"
238 ETC_GITATTRIBUTES="../etc/gitattributes"
239 ETC_GITCONFIG="../etc/gitconfig")
240 else()
241 set(FALLBACK_RUNTIME_PREFIX /home/$ENV{USER})
242 add_compile_definitions(FALLBACK_RUNTIME_PREFIX="${FALLBACK_RUNTIME_PREFIX}"
243 ETC_GITATTRIBUTES="etc/gitattributes"
244 ETC_GITCONFIG="etc/gitconfig")
245 endif()
246
247
248 #Platform Specific
249 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
250 if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
251 include_directories(${CMAKE_SOURCE_DIR}/compat/vcbuild/include)
252 add_compile_definitions(_CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
253 endif()
254 include_directories(${CMAKE_SOURCE_DIR}/compat/win32)
255 add_compile_definitions(HAVE_ALLOCA_H NO_POSIX_GOODIES NATIVE_CRLF NO_UNIX_SOCKETS WIN32
256 _CONSOLE DETECT_MSYS_TTY STRIP_EXTENSION=".exe" NO_SYMLINK_HEAD UNRELIABLE_FSTAT
257 NOGDI OBJECT_CREATION_MODE=1 __USE_MINGW_ANSI_STDIO=0
258 OVERRIDE_STRDUP MMAP_PREVENTS_DELETE USE_WIN32_MMAP
259 HAVE_WPGMPTR ENSURE_MSYSTEM_IS_SET HAVE_RTLGENRANDOM)
260 list(APPEND compat_SOURCES
261 compat/mingw.c
262 compat/winansi.c
263 compat/win32/flush.c
264 compat/win32/path-utils.c
265 compat/win32/pthread.c
266 compat/win32mmap.c
267 compat/win32/syslog.c
268 compat/win32/trace2_win32_process_info.c
269 compat/win32/dirent.c
270 compat/strdup.c)
271 set(NO_UNIX_SOCKETS 1)
272
273 elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
274 add_compile_definitions(PROCFS_EXECUTABLE_PATH="/proc/self/exe" HAVE_DEV_TTY )
275 list(APPEND compat_SOURCES unix-socket.c unix-stream-server.c compat/linux/procinfo.c)
276 elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
277 list(APPEND compat_SOURCES compat/darwin/procinfo.c)
278 endif()
279
280 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
281 list(APPEND compat_SOURCES compat/simple-ipc/ipc-shared.c compat/simple-ipc/ipc-win32.c)
282 add_compile_definitions(SUPPORTS_SIMPLE_IPC)
283 set(SUPPORTS_SIMPLE_IPC 1)
284 else()
285 # Simple IPC requires both Unix sockets and pthreads on Unix-based systems.
286 if(NOT NO_UNIX_SOCKETS AND NOT NO_PTHREADS)
287 list(APPEND compat_SOURCES compat/simple-ipc/ipc-shared.c compat/simple-ipc/ipc-unix-socket.c)
288 add_compile_definitions(SUPPORTS_SIMPLE_IPC)
289 set(SUPPORTS_SIMPLE_IPC 1)
290 endif()
291 endif()
292
293 if(SUPPORTS_SIMPLE_IPC)
294 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
295 set(FSMONITOR_DAEMON_BACKEND "win32")
296 set(FSMONITOR_OS_SETTINGS "win32")
297 elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
298 set(FSMONITOR_DAEMON_BACKEND "darwin")
299 set(FSMONITOR_OS_SETTINGS "unix")
300 elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
301 set(FSMONITOR_DAEMON_BACKEND "linux")
302 set(FSMONITOR_OS_SETTINGS "unix")
303 add_compile_definitions(HAVE_LINUX_MAGIC_H)
304 endif()
305
306 if(FSMONITOR_DAEMON_BACKEND)
307 add_compile_definitions(HAVE_FSMONITOR_DAEMON_BACKEND)
308 list(APPEND compat_SOURCES compat/fsmonitor/fsm-listen-${FSMONITOR_DAEMON_BACKEND}.c)
309 list(APPEND compat_SOURCES compat/fsmonitor/fsm-health-${FSMONITOR_DAEMON_BACKEND}.c)
310 list(APPEND compat_SOURCES compat/fsmonitor/fsm-ipc-${FSMONITOR_OS_SETTINGS}.c)
311 list(APPEND compat_SOURCES compat/fsmonitor/fsm-path-utils-${FSMONITOR_DAEMON_BACKEND}.c)
312
313 add_compile_definitions(HAVE_FSMONITOR_OS_SETTINGS)
314 list(APPEND compat_SOURCES compat/fsmonitor/fsm-settings-${FSMONITOR_OS_SETTINGS}.c)
315 endif()
316 endif()
317
318 set(EXE_EXTENSION ${CMAKE_EXECUTABLE_SUFFIX})
319
320 #header checks
321 check_include_file(libgen.h HAVE_LIBGEN_H)
322 if(NOT HAVE_LIBGEN_H)
323 add_compile_definitions(NO_LIBGEN_H)
324 list(APPEND compat_SOURCES compat/basename.c)
325 endif()
326
327 check_include_file(sys/sysinfo.h HAVE_SYSINFO)
328 if(HAVE_SYSINFO)
329 add_compile_definitions(HAVE_SYSINFO)
330 endif()
331
332 check_c_source_compiles("
333 #include <alloca.h>
334
335 int main(void)
336 {
337 char *p = (char *) alloca(2 * sizeof(int));
338
339 if (p)
340 return 0;
341 return 0;
342 }"
343 HAVE_ALLOCA_H)
344 if(HAVE_ALLOCA_H)
345 add_compile_definitions(HAVE_ALLOCA_H)
346 endif()
347
348 check_include_file(strings.h HAVE_STRINGS_H)
349 if(HAVE_STRINGS_H)
350 add_compile_definitions(HAVE_STRINGS_H)
351 endif()
352
353 check_include_file(sys/select.h HAVE_SYS_SELECT_H)
354 if(NOT HAVE_SYS_SELECT_H)
355 add_compile_definitions(NO_SYS_SELECT_H)
356 endif()
357
358 check_include_file(sys/poll.h HAVE_SYS_POLL_H)
359 if(NOT HAVE_SYS_POLL_H)
360 add_compile_definitions(NO_SYS_POLL_H)
361 endif()
362
363 check_include_file(poll.h HAVE_POLL_H)
364 if(NOT HAVE_POLL_H)
365 add_compile_definitions(NO_POLL_H)
366 endif()
367
368 check_include_file(inttypes.h HAVE_INTTYPES_H)
369 if(NOT HAVE_INTTYPES_H)
370 add_compile_definitions(NO_INTTYPES_H)
371 endif()
372
373 check_include_file(paths.h HAVE_PATHS_H)
374 if(HAVE_PATHS_H)
375 add_compile_definitions(HAVE_PATHS_H)
376 endif()
377
378 #function checks
379 set(function_checks
380 strcasestr memmem strlcpy strtoimax strtoumax strtoull
381 setenv mkdtemp poll pread memmem)
382
383 #unsetenv,hstrerror are incompatible with windows build
384 if(NOT WIN32)
385 list(APPEND function_checks unsetenv hstrerror)
386 endif()
387
388 foreach(f ${function_checks})
389 string(TOUPPER ${f} uf)
390 check_function_exists(${f} HAVE_${uf})
391 if(NOT HAVE_${uf})
392 add_compile_definitions(NO_${uf})
393 endif()
394 endforeach()
395
396 if(NOT HAVE_POLL_H OR NOT HAVE_SYS_POLL_H OR NOT HAVE_POLL)
397 include_directories(${CMAKE_SOURCE_DIR}/compat/poll)
398 add_compile_definitions(NO_POLL)
399 list(APPEND compat_SOURCES compat/poll/poll.c)
400 endif()
401
402 if(NOT HAVE_STRCASESTR)
403 list(APPEND compat_SOURCES compat/strcasestr.c)
404 endif()
405
406 if(NOT HAVE_STRLCPY)
407 list(APPEND compat_SOURCES compat/strlcpy.c)
408 endif()
409
410 if(NOT HAVE_STRTOUMAX)
411 list(APPEND compat_SOURCES compat/strtoumax.c compat/strtoimax.c)
412 endif()
413
414 if(NOT HAVE_SETENV)
415 list(APPEND compat_SOURCES compat/setenv.c)
416 endif()
417
418 if(NOT HAVE_PREAD)
419 list(APPEND compat_SOURCES compat/pread.c)
420 endif()
421
422 if(NOT HAVE_MEMMEM)
423 list(APPEND compat_SOURCES compat/memmem.c)
424 endif()
425
426 if(NOT WIN32)
427 if(NOT HAVE_UNSETENV)
428 list(APPEND compat_SOURCES compat/unsetenv.c)
429 endif()
430
431 if(NOT HAVE_HSTRERROR)
432 list(APPEND compat_SOURCES compat/hstrerror.c)
433 endif()
434 endif()
435
436 check_function_exists(getdelim HAVE_GETDELIM)
437 if(HAVE_GETDELIM)
438 add_compile_definitions(HAVE_GETDELIM)
439 endif()
440
441 check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
442 check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_CLOCK_MONOTONIC)
443 if(HAVE_CLOCK_GETTIME)
444 add_compile_definitions(HAVE_CLOCK_GETTIME)
445 endif()
446 if(HAVE_CLOCK_MONOTONIC)
447 add_compile_definitions(HAVE_CLOCK_MONOTONIC)
448 endif()
449
450 #check for st_blocks in struct stat
451 check_struct_has_member("struct stat" st_blocks "sys/stat.h" STRUCT_STAT_HAS_ST_BLOCKS)
452 if(NOT STRUCT_STAT_HAS_ST_BLOCKS)
453 add_compile_definitions(NO_ST_BLOCKS_IN_STRUCT_STAT)
454 endif()
455
456 #compile checks
457 check_c_source_runs("
458 #include<stdio.h>
459 #include<stdarg.h>
460 #include<string.h>
461 #include<stdlib.h>
462
463 int test_vsnprintf(char *str, size_t maxsize, const char *format, ...)
464 {
465 int ret;
466 va_list ap;
467
468 va_start(ap, format);
469 ret = vsnprintf(str, maxsize, format, ap);
470 va_end(ap);
471 return ret;
472 }
473
474 int main(void)
475 {
476 char buf[6];
477
478 if (test_vsnprintf(buf, 3, \"%s\", \"12345\") != 5
479 || strcmp(buf, \"12\"))
480 return 1;
481 if (snprintf(buf, 3, \"%s\", \"12345\") != 5
482 || strcmp(buf, \"12\"))
483 return 1;
484 return 0;
485 }"
486 SNPRINTF_OK)
487 if(NOT SNPRINTF_OK)
488 add_compile_definitions(SNPRINTF_RETURNS_BOGUS)
489 list(APPEND compat_SOURCES compat/snprintf.c)
490 endif()
491
492 check_c_source_runs("
493 #include<stdio.h>
494
495 int main(void)
496 {
497 FILE *f = fopen(\".\", \"r\");
498
499 return f != NULL;
500 }"
501 FREAD_READS_DIRECTORIES_NO)
502 if(NOT FREAD_READS_DIRECTORIES_NO)
503 add_compile_definitions(FREAD_READS_DIRECTORIES)
504 list(APPEND compat_SOURCES compat/fopen.c)
505 endif()
506
507 check_c_source_compiles("
508 #include <regex.h>
509 #ifndef REG_STARTEND
510 #error oops we dont have it
511 #endif
512
513 int main(void)
514 {
515 return 0;
516 }"
517 HAVE_REGEX)
518 if(NOT HAVE_REGEX)
519 include_directories(${CMAKE_SOURCE_DIR}/compat/regex)
520 list(APPEND compat_SOURCES compat/regex/regex.c )
521 add_compile_definitions(NO_REGEX NO_MBSUPPORT GAWK)
522 elseif(APPLE)
523 list(APPEND compat_SOURCES compat/darwin/regexec.c)
524 add_compile_definitions(DARWIN_REGEXEC)
525 endif()
526
527
528 check_c_source_compiles("
529 #include <stddef.h>
530 #include <sys/types.h>
531 #include <sys/sysctl.h>
532
533 int main(void)
534 {
535 int val, mib[2];
536 size_t len;
537
538 mib[0] = CTL_HW;
539 mib[1] = 1;
540 len = sizeof(val);
541 return sysctl(mib, 2, &val, &len, NULL, 0) ? 1 : 0;
542 }"
543 HAVE_BSD_SYSCTL)
544 if(HAVE_BSD_SYSCTL)
545 add_compile_definitions(HAVE_BSD_SYSCTL)
546 endif()
547
548 set(CMAKE_REQUIRED_LIBRARIES ${Iconv_LIBRARIES})
549 set(CMAKE_REQUIRED_INCLUDES ${Iconv_INCLUDE_DIRS})
550
551 check_c_source_compiles("
552 #include <iconv.h>
553
554 extern size_t iconv(iconv_t cd,
555 char **inbuf, size_t *inbytesleft,
556 char **outbuf, size_t *outbytesleft);
557
558 int main(void)
559 {
560 return 0;
561 }"
562 HAVE_NEW_ICONV)
563 if(HAVE_NEW_ICONV)
564 set(HAVE_OLD_ICONV 0)
565 else()
566 set(HAVE_OLD_ICONV 1)
567 endif()
568
569 check_c_source_runs("
570 #include <iconv.h>
571 #if ${HAVE_OLD_ICONV}
572 typedef const char *iconv_ibp;
573 #else
574 typedef char *iconv_ibp;
575 #endif
576
577 int main(void)
578 {
579 int v;
580 iconv_t conv;
581 char in[] = \"a\";
582 iconv_ibp pin = in;
583 char out[20] = \"\";
584 char *pout = out;
585 size_t isz = sizeof(in);
586 size_t osz = sizeof(out);
587
588 conv = iconv_open(\"UTF-16\", \"UTF-8\");
589 iconv(conv, &pin, &isz, &pout, &osz);
590 iconv_close(conv);
591 v = (unsigned char)(out[0]) + (unsigned char)(out[1]);
592 return v != 0xfe + 0xff;
593 }"
594 ICONV_DOESNOT_OMIT_BOM)
595 if(NOT ICONV_DOESNOT_OMIT_BOM)
596 add_compile_definitions(ICONV_OMITS_BOM)
597 endif()
598
599 unset(CMAKE_REQUIRED_LIBRARIES)
600 unset(CMAKE_REQUIRED_INCLUDES)
601
602
603 #programs
604 set(PROGRAMS_BUILT
605 git git-daemon git-http-backend git-sh-i18n--envsubst
606 git-shell scalar)
607
608 if(NOT CURL_FOUND)
609 list(APPEND excluded_progs git-http-fetch git-http-push)
610 add_compile_definitions(NO_CURL)
611 message(WARNING "git-http-push and git-http-fetch will not be built")
612 else()
613 list(APPEND PROGRAMS_BUILT git-http-fetch git-http-push git-imap-send git-remote-http)
614 if(CURL_VERSION_STRING VERSION_GREATER_EQUAL 7.34.0)
615 add_compile_definitions(USE_CURL_FOR_IMAP_SEND)
616 endif()
617 endif()
618
619 if(NOT EXPAT_FOUND)
620 list(APPEND excluded_progs git-http-push)
621 add_compile_definitions(NO_EXPAT)
622 else()
623 list(APPEND PROGRAMS_BUILT git-http-push)
624 if(EXPAT_VERSION_STRING VERSION_LESS_EQUAL 1.2)
625 add_compile_definitions(EXPAT_NEEDS_XMLPARSE_H)
626 endif()
627 endif()
628
629 list(REMOVE_DUPLICATES excluded_progs)
630 list(REMOVE_DUPLICATES PROGRAMS_BUILT)
631
632
633 foreach(p ${excluded_progs})
634 list(APPEND EXCLUSION_PROGS --exclude-program ${p} )
635 endforeach()
636
637 #for comparing null values
638 list(APPEND EXCLUSION_PROGS empty)
639 set(EXCLUSION_PROGS_CACHE ${EXCLUSION_PROGS} CACHE STRING "Programs not built" FORCE)
640
641 if(NOT EXISTS ${CMAKE_BINARY_DIR}/command-list.h OR NOT EXCLUSION_PROGS_CACHE STREQUAL EXCLUSION_PROGS)
642 list(REMOVE_ITEM EXCLUSION_PROGS empty)
643 message("Generating command-list.h")
644 execute_process(COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/tools/generate-cmdlist.sh"
645 ${EXCLUSION_PROGS}
646 "${CMAKE_SOURCE_DIR}"
647 "${CMAKE_BINARY_DIR}/command-list.h")
648 endif()
649
650 if(NOT EXISTS ${CMAKE_BINARY_DIR}/config-list.h)
651 message("Generating config-list.h")
652 execute_process(COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/tools/generate-configlist.sh"
653 "${CMAKE_SOURCE_DIR}"
654 "${CMAKE_BINARY_DIR}/config-list.h")
655 endif()
656
657 if(NOT EXISTS ${CMAKE_BINARY_DIR}/hook-list.h)
658 message("Generating hook-list.h")
659 execute_process(COMMAND "${SH_EXE}" ${CMAKE_SOURCE_DIR}/tools/generate-hooklist.sh
660 "${CMAKE_SOURCE_DIR}"
661 "${CMAKE_BINARY_DIR}/hook-list.h")
662 endif()
663
664 include_directories(${CMAKE_BINARY_DIR})
665
666 #build
667 #libgit
668 parse_makefile_for_sources(libgit_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "LIB_OBJS")
669
670 list(TRANSFORM libgit_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
671 list(TRANSFORM compat_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
672
673 add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/version-def.h"
674 COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
675 "${CMAKE_SOURCE_DIR}"
676 "${CMAKE_SOURCE_DIR}/version-def.h.in"
677 "${CMAKE_BINARY_DIR}/version-def.h"
678 DEPENDS "${SH_EXE}" "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
679 "${CMAKE_SOURCE_DIR}/version-def.h.in"
680 VERBATIM)
681 list(APPEND libgit_SOURCES "${CMAKE_BINARY_DIR}/version-def.h")
682
683 add_library(libgit ${libgit_SOURCES} ${compat_SOURCES})
684
685 if(WIN32)
686 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.rc
687 COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
688 "${CMAKE_SOURCE_DIR}"
689 "${CMAKE_SOURCE_DIR}/git.rc.in"
690 "${CMAKE_BINARY_DIR}/git.rc"
691 DEPENDS "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
692 "${CMAKE_SOURCE_DIR}/git.rc.in"
693 VERBATIM)
694
695 if(NOT MSVC)#use windres when compiling with gcc and clang
696 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
697 COMMAND ${WINDRES_EXE} -O coff -i ${CMAKE_BINARY_DIR}/git.rc -o ${CMAKE_BINARY_DIR}/git.res
698 DEPENDS "${CMAKE_BINARY_DIR}/git.rc"
699 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
700 VERBATIM)
701 else()#MSVC use rc
702 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
703 COMMAND ${CMAKE_RC_COMPILER} /fo ${CMAKE_BINARY_DIR}/git.res ${CMAKE_BINARY_DIR}/git.rc
704 DEPENDS "${CMAKE_BINARY_DIR}/git.rc"
705 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
706 VERBATIM)
707 endif()
708 add_custom_target(git-rc DEPENDS ${CMAKE_BINARY_DIR}/git.res)
709 endif()
710
711 #link all required libraries to common-main
712 add_library(common-main OBJECT ${CMAKE_SOURCE_DIR}/common-main.c)
713
714 target_link_libraries(common-main libgit ${ZLIB_LIBRARIES})
715 if(Intl_FOUND)
716 target_link_libraries(common-main ${Intl_LIBRARIES})
717 endif()
718 if(Iconv_FOUND)
719 target_link_libraries(common-main ${Iconv_LIBRARIES})
720 endif()
721 if(PCRE2_FOUND)
722 target_link_libraries(common-main ${PCRE2_LIBRARIES})
723 target_link_directories(common-main PUBLIC ${PCRE2_LIBRARY_DIRS})
724 endif()
725 if(WIN32)
726 target_link_libraries(common-main ws2_32 ntdll ${CMAKE_BINARY_DIR}/git.res)
727 add_dependencies(common-main git-rc)
728 if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
729 target_link_options(common-main PUBLIC -municode -Wl,--nxcompat -Wl,--dynamicbase -Wl,--pic-executable,-e,mainCRTStartup)
730 elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
731 target_link_options(common-main PUBLIC -municode -Wl,-nxcompat -Wl,-dynamicbase -Wl,-entry:wmainCRTStartup -Wl,invalidcontinue.obj)
732 elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
733 target_link_options(common-main PUBLIC /IGNORE:4217 /IGNORE:4049 /NOLOGO /ENTRY:wmainCRTStartup /SUBSYSTEM:CONSOLE invalidcontinue.obj)
734 else()
735 message(FATAL_ERROR "Unhandled compiler: ${CMAKE_C_COMPILER_ID}")
736 endif()
737
738 add_executable(headless-git ${CMAKE_SOURCE_DIR}/compat/win32/headless.c)
739 if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
740 target_link_options(headless-git PUBLIC -municode -Wl,-subsystem,windows)
741 elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
742 target_link_options(headless-git PUBLIC /NOLOGO /ENTRY:wWinMainCRTStartup /SUBSYSTEM:WINDOWS)
743 else()
744 message(FATAL_ERROR "Unhandled compiler: ${CMAKE_C_COMPILER_ID}")
745 endif()
746 elseif(UNIX)
747 target_link_libraries(common-main pthread rt)
748 endif()
749
750 #git
751 parse_makefile_for_sources(git_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "BUILTIN_OBJS")
752
753 list(TRANSFORM git_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
754 add_executable(git ${CMAKE_SOURCE_DIR}/git.c ${git_SOURCES})
755 target_link_libraries(git common-main)
756
757 add_executable(git-daemon ${CMAKE_SOURCE_DIR}/daemon.c)
758 target_link_libraries(git-daemon common-main)
759
760 add_executable(git-http-backend ${CMAKE_SOURCE_DIR}/http-backend.c)
761 target_link_libraries(git-http-backend common-main)
762
763 add_executable(git-sh-i18n--envsubst ${CMAKE_SOURCE_DIR}/sh-i18n--envsubst.c)
764 target_link_libraries(git-sh-i18n--envsubst common-main)
765
766 add_executable(git-shell ${CMAKE_SOURCE_DIR}/shell.c)
767 target_link_libraries(git-shell common-main)
768
769 add_executable(scalar ${CMAKE_SOURCE_DIR}/scalar.c)
770 target_link_libraries(scalar common-main)
771
772 if(CURL_FOUND)
773 add_library(http_obj OBJECT ${CMAKE_SOURCE_DIR}/http.c)
774
775 add_executable(git-imap-send ${CMAKE_SOURCE_DIR}/imap-send.c)
776 target_link_libraries(git-imap-send http_obj common-main ${CURL_LIBRARIES})
777
778 add_executable(git-http-fetch ${CMAKE_SOURCE_DIR}/http-walker.c ${CMAKE_SOURCE_DIR}/http-fetch.c)
779 target_link_libraries(git-http-fetch http_obj common-main ${CURL_LIBRARIES})
780
781 add_executable(git-remote-http ${CMAKE_SOURCE_DIR}/http-walker.c ${CMAKE_SOURCE_DIR}/remote-curl.c)
782 target_link_libraries(git-remote-http http_obj common-main ${CURL_LIBRARIES} )
783
784 if(EXPAT_FOUND)
785 add_executable(git-http-push ${CMAKE_SOURCE_DIR}/http-push.c)
786 target_link_libraries(git-http-push http_obj common-main ${CURL_LIBRARIES} ${EXPAT_LIBRARIES})
787 endif()
788 endif()
789
790 parse_makefile_for_executables(git_builtin_extra "BUILT_INS")
791
792 option(SKIP_DASHED_BUILT_INS "Skip hardlinking the dashed versions of the built-ins")
793
794 #Creating hardlinks
795 if(NOT SKIP_DASHED_BUILT_INS)
796 foreach(s ${git_SOURCES} ${git_builtin_extra})
797 string(REPLACE "${CMAKE_SOURCE_DIR}/builtin/" "" s ${s})
798 string(REPLACE ".c" "" s ${s})
799 file(APPEND ${CMAKE_BINARY_DIR}/CreateLinks.cmake "file(CREATE_LINK git${EXE_EXTENSION} git-${s}${EXE_EXTENSION})\n")
800 list(APPEND git_links ${CMAKE_BINARY_DIR}/git-${s}${EXE_EXTENSION})
801 endforeach()
802 endif()
803
804 if(CURL_FOUND)
805 set(remote_exes
806 git-remote-https git-remote-ftp git-remote-ftps)
807 foreach(s ${remote_exes})
808 file(APPEND ${CMAKE_BINARY_DIR}/CreateLinks.cmake "file(CREATE_LINK git-remote-http${EXE_EXTENSION} ${s}${EXE_EXTENSION})\n")
809 list(APPEND git_http_links ${CMAKE_BINARY_DIR}/${s}${EXE_EXTENSION})
810 endforeach()
811 endif()
812
813 add_custom_command(OUTPUT ${git_links} ${git_http_links}
814 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/CreateLinks.cmake
815 DEPENDS git git-remote-http)
816 add_custom_target(git-links ALL DEPENDS ${git_links} ${git_http_links})
817
818
819 #creating required scripts
820 set(SHELL_PATH /bin/sh)
821 set(PERL_PATH /usr/bin/perl)
822 set(LOCALEDIR ${FALLBACK_RUNTIME_PREFIX}/share/locale)
823 set(GITWEBDIR ${FALLBACK_RUNTIME_PREFIX}/share/locale)
824 set(INSTLIBDIR ${FALLBACK_RUNTIME_PREFIX}/share/perl5)
825
826 #shell scripts
827 parse_makefile_for_scripts(git_sh_scripts "SCRIPT_SH" ".sh")
828 parse_makefile_for_scripts(git_shlib_scripts "SCRIPT_LIB" "")
829 set(git_shell_scripts
830 ${git_sh_scripts} ${git_shlib_scripts} git-instaweb)
831
832 foreach(script ${git_shell_scripts})
833 if ("${script}" IN_LIST git_sh_scripts)
834 string(REPLACE ".sh" "" shell_gen_path "${script}")
835 else()
836 set(shell_gen_path "${script}")
837 endif()
838
839 add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/${shell_gen_path}"
840 COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/tools/generate-script.sh"
841 "${CMAKE_SOURCE_DIR}/${script}.sh"
842 "${CMAKE_BINARY_DIR}/${shell_gen_path}"
843 "${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS"
844 DEPENDS "${CMAKE_SOURCE_DIR}/tools/generate-script.sh"
845 "${CMAKE_SOURCE_DIR}/${script}.sh"
846 VERBATIM)
847 list(APPEND shell_gen ${CMAKE_BINARY_DIR}/${shell_gen_path})
848 endforeach()
849 add_custom_target(shell-gen ALL DEPENDS ${shell_gen})
850
851 #perl scripts
852 parse_makefile_for_scripts(git_perl_scripts "SCRIPT_PERL" "")
853 #perl modules
854 file(GLOB_RECURSE perl_modules "${CMAKE_SOURCE_DIR}/perl/*.pm")
855 list(TRANSFORM perl_modules REPLACE "${CMAKE_SOURCE_DIR}/" "")
856
857 #create perl header
858 file(STRINGS ${CMAKE_SOURCE_DIR}/perl/header_templates/fixed_prefix.template.pl perl_header )
859 string(REPLACE "@PATHSEP@" ":" perl_header "${perl_header}")
860 string(REPLACE "@INSTLIBDIR@" "${INSTLIBDIR}" perl_header "${perl_header}")
861 file(WRITE ${CMAKE_BINARY_DIR}/GIT-PERL-HEADER ${perl_header})
862
863 add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/GIT-VERSION-FILE"
864 COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
865 "${CMAKE_SOURCE_DIR}"
866 "${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE.in"
867 "${CMAKE_BINARY_DIR}/GIT-VERSION-FILE"
868 DEPENDS ${SH_EXE} "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
869 "${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE.in"
870 VERBATIM)
871
872 foreach(script ${git_perl_scripts} ${perl_modules})
873 string(REPLACE ".perl" "" perl_gen_path "${script}")
874
875 get_filename_component(perl_gen_dir "${perl_gen_path}" DIRECTORY)
876 if(script MATCHES "\.pm$")
877 string(REGEX REPLACE "^perl" "perl/build/lib" perl_gen_dir "${perl_gen_dir}")
878 string(REGEX REPLACE "^perl" "perl/build/lib" perl_gen_path "${perl_gen_path}")
879 endif()
880 file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/${perl_gen_dir}")
881
882 add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/${perl_gen_path}"
883 COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/tools/generate-perl.sh"
884 "${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS"
885 "${CMAKE_BINARY_DIR}/GIT-VERSION-FILE"
886 "${CMAKE_BINARY_DIR}/GIT-PERL-HEADER"
887 "${CMAKE_SOURCE_DIR}/${script}"
888 "${CMAKE_BINARY_DIR}/${perl_gen_path}"
889 DEPENDS "${CMAKE_SOURCE_DIR}/tools/generate-perl.sh"
890 "${CMAKE_SOURCE_DIR}/${script}"
891 "${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS"
892 "${CMAKE_BINARY_DIR}/GIT-VERSION-FILE"
893 VERBATIM)
894 list(APPEND perl_gen ${CMAKE_BINARY_DIR}/${perl_gen_path})
895 endforeach()
896 add_custom_target(perl-gen ALL DEPENDS ${perl_gen})
897
898 # Python script
899 add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/git-p4"
900 COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/tools/generate-python.sh"
901 "${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS"
902 "${CMAKE_SOURCE_DIR}/git-p4.py"
903 "${CMAKE_BINARY_DIR}/git-p4"
904 DEPENDS "${CMAKE_SOURCE_DIR}/tools/generate-python.sh"
905 "${CMAKE_SOURCE_DIR}/git-p4.py"
906 "${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS"
907 VERBATIM)
908 add_custom_target(python-gen ALL DEPENDS "${CMAKE_BINARY_DIR}/git-p4")
909
910 #${CMAKE_SOURCE_DIR}/Makefile templates
911 parse_makefile_for_sources(templates ${CMAKE_SOURCE_DIR}/templates/Makefile "TEMPLATES")
912 string(REPLACE " " ";" templates ${templates})
913 #templates have @.*@ replacement so use configure_file instead
914 foreach(tm ${templates})
915 configure_file(${CMAKE_SOURCE_DIR}/templates/${tm} ${CMAKE_BINARY_DIR}/templates/blt/${tm} @ONLY)
916 endforeach()
917
918 #translations
919 if(MSGFMT_EXE)
920 file(GLOB po_files "${CMAKE_SOURCE_DIR}/po/*.po")
921 list(TRANSFORM po_files REPLACE "${CMAKE_SOURCE_DIR}/po/" "")
922 list(TRANSFORM po_files REPLACE ".po" "")
923 foreach(po ${po_files})
924 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES)
925 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo
926 COMMAND ${MSGFMT_EXE} --check --statistics -o ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo ${CMAKE_SOURCE_DIR}/po/${po}.po)
927 list(APPEND po_gen ${CMAKE_BINARY_DIR}/po/build/locale/${po}/LC_MESSAGES/git.mo)
928 endforeach()
929 add_custom_target(po-gen ALL DEPENDS ${po_gen})
930 endif()
931
932
933 #to help with the install
934 list(TRANSFORM git_shell_scripts PREPEND "${CMAKE_BINARY_DIR}/")
935 list(TRANSFORM git_perl_scripts PREPEND "${CMAKE_BINARY_DIR}/")
936
937 #install
938 foreach(program ${PROGRAMS_BUILT})
939 if(program MATCHES "^(git|git-shell|scalar)$")
940 install(TARGETS ${program}
941 RUNTIME DESTINATION bin)
942 else()
943 install(TARGETS ${program}
944 RUNTIME DESTINATION libexec/git-core)
945 endif()
946 endforeach()
947
948 install(PROGRAMS ${CMAKE_BINARY_DIR}/git-cvsserver
949 DESTINATION bin)
950
951 set(bin_links
952 git-receive-pack git-upload-archive git-upload-pack)
953
954 foreach(b ${bin_links})
955 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/bin/${b}${EXE_EXTENSION})")
956 endforeach()
957
958 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git${EXE_EXTENSION})")
959 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git-shell${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git-shell${EXE_EXTENSION})")
960
961 foreach(b ${git_links})
962 string(REPLACE "${CMAKE_BINARY_DIR}" "" b ${b})
963 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/bin/git${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/${b})")
964 endforeach()
965
966 foreach(b ${git_http_links})
967 string(REPLACE "${CMAKE_BINARY_DIR}" "" b ${b})
968 install(CODE "file(CREATE_LINK ${CMAKE_INSTALL_PREFIX}/libexec/git-core/git-remote-http${EXE_EXTENSION} ${CMAKE_INSTALL_PREFIX}/libexec/git-core/${b})")
969 endforeach()
970
971 install(PROGRAMS ${git_shell_scripts} ${git_perl_scripts} ${CMAKE_BINARY_DIR}/git-p4
972 DESTINATION libexec/git-core)
973
974 install(DIRECTORY ${CMAKE_SOURCE_DIR}/mergetools DESTINATION libexec/git-core)
975 install(DIRECTORY ${CMAKE_BINARY_DIR}/perl/build/lib/ DESTINATION share/perl5
976 FILES_MATCHING PATTERN "*.pm")
977 install(DIRECTORY ${CMAKE_BINARY_DIR}/templates/blt/ DESTINATION share/git-core/templates)
978
979 if(MSGFMT_EXE)
980 install(DIRECTORY ${CMAKE_BINARY_DIR}/po/build/locale DESTINATION share)
981 endif()
982
983
984 if(BUILD_TESTING)
985
986 #tests-helpers
987 add_executable(test-fake-ssh ${CMAKE_SOURCE_DIR}/t/helper/test-fake-ssh.c)
988 target_link_libraries(test-fake-ssh common-main)
989
990 #unit-tests
991 parse_makefile_for_sources(unit-test_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "UNIT_TEST_OBJS")
992 list(TRANSFORM unit-test_SOURCES REPLACE "\\$\\(UNIT_TEST_DIR\\)/" "${CMAKE_SOURCE_DIR}/t/unit-tests/")
993 add_library(unit-test-lib STATIC ${unit-test_SOURCES})
994
995 parse_makefile_for_sources(clar-test_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "CLAR_TEST_OBJS")
996 list(TRANSFORM clar-test_SOURCES REPLACE "\\$\\(UNIT_TEST_DIR\\)/" "${CMAKE_SOURCE_DIR}/t/unit-tests/")
997 add_library(clar-test-lib STATIC ${clar-test_SOURCES})
998
999 file(GLOB unit_test_PROGRAMS "${CMAKE_SOURCE_DIR}/t/unit-tests/t-*.c")
1000 list(TRANSFORM unit_test_PROGRAMS REPLACE "${CMAKE_SOURCE_DIR}/" "")
1001 list(TRANSFORM unit_test_PROGRAMS REPLACE ".c" "")
1002 foreach(unit_test ${unit_test_PROGRAMS})
1003 add_executable("${unit_test}" "${CMAKE_SOURCE_DIR}/t/unit-tests/${unit_test}.c")
1004 target_link_libraries("${unit_test}" unit-test-lib clar-test-lib common-main)
1005 set_target_properties("${unit_test}"
1006 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
1007 if(MSVC)
1008 set_target_properties("${unit_test}"
1009 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
1010 set_target_properties("${unit_test}"
1011 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
1012 endif()
1013 list(APPEND PROGRAMS_BUILT "${unit_test}")
1014
1015 # t-basic intentionally fails tests, to validate the unit-test infrastructure.
1016 # Therefore, it should only be run as part of t0080, which verifies that it
1017 # fails only in the expected ways.
1018 #
1019 # All other unit tests should be run.
1020 if(NOT ${unit_test} STREQUAL "t-basic")
1021 add_test(NAME "t.unit-tests.${unit_test}"
1022 COMMAND "./${unit_test}"
1023 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t/unit-tests/bin)
1024 endif()
1025 endforeach()
1026
1027 parse_makefile_for_scripts(clar_test_SUITES "CLAR_TEST_SUITES" "")
1028 list(TRANSFORM clar_test_SUITES PREPEND "${CMAKE_SOURCE_DIR}/t/unit-tests/")
1029 list(TRANSFORM clar_test_SUITES APPEND ".c")
1030 add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h"
1031 COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-decls.sh
1032 "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h"
1033 ${clar_test_SUITES}
1034 DEPENDS ${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-decls.sh
1035 ${clar_test_SUITES}
1036 VERBATIM)
1037 add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/t/unit-tests/clar.suite"
1038 COMMAND ${SH_EXE} "${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-suites.sh"
1039 "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h"
1040 "${CMAKE_BINARY_DIR}/t/unit-tests/clar.suite"
1041 DEPENDS "${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-suites.sh"
1042 "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h"
1043 VERBATIM)
1044
1045 add_library(unit-tests-lib ${clar_test_SUITES}
1046 "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h"
1047 "${CMAKE_BINARY_DIR}/t/unit-tests/clar.suite"
1048 )
1049 target_include_directories(clar-test-lib PUBLIC "${CMAKE_BINARY_DIR}/t/unit-tests")
1050 target_include_directories(unit-tests-lib PUBLIC "${CMAKE_BINARY_DIR}/t/unit-tests")
1051 add_executable(unit-tests)
1052 target_link_libraries(unit-tests unit-tests-lib clar-test-lib common-main)
1053 set_target_properties(unit-tests
1054 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
1055 if(MSVC)
1056 set_target_properties(unit-tests
1057 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
1058 set_target_properties(unit-tests
1059 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
1060 endif()
1061
1062 #test-tool
1063 parse_makefile_for_sources(test-tool_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "TEST_BUILTINS_OBJS")
1064 add_library(test-lib OBJECT ${CMAKE_SOURCE_DIR}/t/unit-tests/test-lib.c)
1065
1066 list(TRANSFORM test-tool_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/t/helper/")
1067 add_executable(test-tool ${CMAKE_SOURCE_DIR}/t/helper/test-tool.c ${test-tool_SOURCES})
1068 target_link_libraries(test-tool test-lib common-main)
1069
1070 set_target_properties(test-fake-ssh test-tool
1071 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/helper)
1072
1073 if(MSVC)
1074 set_target_properties(test-fake-ssh test-tool
1075 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/helper)
1076 set_target_properties(test-fake-ssh test-tool
1077 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/helper)
1078 endif()
1079
1080 #wrapper scripts
1081 set(wrapper_scripts
1082 git git-upload-pack git-receive-pack git-upload-archive git-shell scalar)
1083
1084 set(wrapper_test_scripts
1085 test-fake-ssh test-tool)
1086
1087
1088 foreach(script ${wrapper_scripts})
1089 file(STRINGS ${CMAKE_SOURCE_DIR}/bin-wrappers/wrap-for-bin.sh content NEWLINE_CONSUME)
1090 string(REPLACE "@BUILD_DIR@" "${CMAKE_BINARY_DIR}" content "${content}")
1091 string(REPLACE "@TEMPLATE_DIR@" "'${CMAKE_BINARY_DIR}/templates/blt'" content "${content}")
1092 string(REPLACE "@PROG@" "${CMAKE_BINARY_DIR}/${script}${EXE_EXTENSION}" content "${content}")
1093 file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/${script} ${content})
1094 endforeach()
1095
1096 foreach(script ${wrapper_test_scripts})
1097 file(STRINGS ${CMAKE_SOURCE_DIR}/bin-wrappers/wrap-for-bin.sh content NEWLINE_CONSUME)
1098 string(REPLACE "@BUILD_DIR@" "${CMAKE_BINARY_DIR}" content "${content}")
1099 string(REPLACE "@TEMPLATE_DIR@" "'${CMAKE_BINARY_DIR}/templates/blt'" content "${content}")
1100 string(REPLACE "@PROG@" "${CMAKE_BINARY_DIR}/t/helper/${script}${EXE_EXTENSION}" content "${content}")
1101 file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/${script} ${content})
1102 endforeach()
1103
1104 file(STRINGS ${CMAKE_SOURCE_DIR}/bin-wrappers/wrap-for-bin.sh content NEWLINE_CONSUME)
1105 string(REPLACE "@BUILD_DIR@" "${CMAKE_BINARY_DIR}" content "${content}")
1106 string(REPLACE "@TEMPLATE_DIR@" "'${CMAKE_BINARY_DIR}/templates/blt'" content "${content}")
1107 string(REPLACE "@GIT_TEXTDOMAINDIR@" "${CMAKE_BINARY_DIR}/po/build/locale" content "${content}")
1108 string(REPLACE "@GITPERLLIB@" "${CMAKE_BINARY_DIR}/perl/build/lib" content "${content}")
1109 string(REPLACE "@MERGE_TOOLS_DIR@" "${CMAKE_SOURCE_DIR}/mergetools" content "${content}")
1110 string(REPLACE "@PROG@" "${CMAKE_BINARY_DIR}/git-cvsserver" content "${content}")
1111 file(WRITE ${CMAKE_BINARY_DIR}/bin-wrappers/git-cvsserver ${content})
1112
1113 #options for configuring test options
1114 option(PERL_TESTS "Perform tests that use perl" ON)
1115 option(PYTHON_TESTS "Perform tests that use python" ON)
1116
1117 #GIT-BUILD-OPTIONS
1118 set(TEST_SHELL_PATH ${SHELL_PATH})
1119 set(DIFF diff)
1120 set(PYTHON_PATH /usr/bin/python)
1121 set(TAR tar)
1122 set(NO_CURL )
1123 set(NO_ICONV )
1124 set(NO_EXPAT )
1125 set(USE_LIBPCRE2 )
1126 set(NO_PERL )
1127 set(NO_PTHREADS )
1128 set(NO_PYTHON )
1129 set(PAGER_ENV "LESS=FRX LV=-c")
1130 set(RUNTIME_PREFIX true)
1131 set(NO_GETTEXT )
1132
1133 if(NOT CURL_FOUND)
1134 set(NO_CURL 1)
1135 endif()
1136
1137 if(NOT Iconv_FOUND)
1138 SET(NO_ICONV 1)
1139 endif()
1140
1141 if(NOT EXPAT_FOUND)
1142 set(NO_EXPAT 1)
1143 endif()
1144
1145 if(NOT Intl_FOUND)
1146 set(NO_GETTEXT 1)
1147 endif()
1148
1149 if(NOT PERL_TESTS)
1150 set(NO_PERL 1)
1151 endif()
1152
1153 if(NOT PYTHON_TESTS)
1154 set(NO_PYTHON 1)
1155 endif()
1156
1157 file(STRINGS ${CMAKE_SOURCE_DIR}/GIT-BUILD-OPTIONS.in git_build_options NEWLINE_CONSUME)
1158 string(REPLACE "@BROKEN_PATH_FIX@" "" git_build_options "${git_build_options}")
1159 string(REPLACE "@DIFF@" "'${DIFF}'" git_build_options "${git_build_options}")
1160 string(REPLACE "@FSMONITOR_DAEMON_BACKEND@" "${FSMONITOR_DAEMON_BACKEND}" git_build_options "${git_build_options}")
1161 string(REPLACE "@FSMONITOR_OS_SETTINGS@" "${FSMONITOR_OS_SETTINGS}" git_build_options "${git_build_options}")
1162 string(REPLACE "@GITWEBDIR@" "'${GITWEBDIR}'" git_build_options "${git_build_options}")
1163 string(REPLACE "@GIT_INTEROP_MAKE_OPTS@" "" git_build_options "${git_build_options}")
1164 string(REPLACE "@GIT_PERF_LARGE_REPO@" "" git_build_options "${git_build_options}")
1165 string(REPLACE "@GIT_PERF_MAKE_COMMAND@" "" git_build_options "${git_build_options}")
1166 string(REPLACE "@GIT_PERF_MAKE_OPTS@" "" git_build_options "${git_build_options}")
1167 string(REPLACE "@GIT_PERF_REPEAT_COUNT@" "" git_build_options "${git_build_options}")
1168 string(REPLACE "@GIT_PERF_REPO@" "" git_build_options "${git_build_options}")
1169 string(REPLACE "@GIT_SOURCE_DIR@" "${CMAKE_SOURCE_DIR}" git_build_options "${git_build_options}")
1170 string(REPLACE "@GIT_TEST_CMP@" "" git_build_options "${git_build_options}")
1171 string(REPLACE "@GIT_TEST_CMP_USE_COPIED_CONTEXT@" "" git_build_options "${git_build_options}")
1172 string(REPLACE "@GIT_TEST_GITPERLLIB@" "'${CMAKE_BINARY_DIR}/perl/build/lib'" git_build_options "${git_build_options}")
1173 string(REPLACE "@GIT_TEST_INDEX_VERSION@" "" git_build_options "${git_build_options}")
1174 string(REPLACE "@GIT_TEST_OPTS@" "" git_build_options "${git_build_options}")
1175 string(REPLACE "@GIT_TEST_PERL_FATAL_WARNINGS@" "" git_build_options "${git_build_options}")
1176 string(REPLACE "@GIT_TEST_TEMPLATE_DIR@" "'${CMAKE_BINARY_DIR}/templates/blt'" git_build_options "${git_build_options}")
1177 string(REPLACE "@GIT_TEST_TEXTDOMAINDIR@" "'${CMAKE_BINARY_DIR}/po/build/locale'" git_build_options "${git_build_options}")
1178 string(REPLACE "@GIT_TEST_UTF8_LOCALE@" "" git_build_options "${git_build_options}")
1179 string(REPLACE "@LOCALEDIR@" "'${LOCALEDIR}'" git_build_options "${git_build_options}")
1180 string(REPLACE "@NO_CURL@" "${NO_CURL}" git_build_options "${git_build_options}")
1181 string(REPLACE "@NO_EXPAT@" "${NO_EXPAT}" git_build_options "${git_build_options}")
1182 string(REPLACE "@NO_GETTEXT@" "${NO_GETTEXT}" git_build_options "${git_build_options}")
1183 string(REPLACE "@NO_GITWEB@" "1" git_build_options "${git_build_options}")
1184 string(REPLACE "@NO_ICONV@" "${NO_ICONV}" git_build_options "${git_build_options}")
1185 string(REPLACE "@NO_PERL@" "${NO_PERL}" git_build_options "${git_build_options}")
1186 string(REPLACE "@NO_PERL_CPAN_FALLBACKS@" "" git_build_options "${git_build_options}")
1187 string(REPLACE "@NO_PTHREADS@" "${NO_PTHREADS}" git_build_options "${git_build_options}")
1188 string(REPLACE "@NO_PYTHON@" "${NO_PYTHON}" git_build_options "${git_build_options}")
1189 string(REPLACE "@NO_REGEX@" "" git_build_options "${git_build_options}")
1190 string(REPLACE "@NO_UNIX_SOCKETS@" "${NO_UNIX_SOCKETS}" git_build_options "${git_build_options}")
1191 string(REPLACE "@PAGER_ENV@" "'${PAGER_ENV}'" git_build_options "${git_build_options}")
1192 string(REPLACE "@PERL_LOCALEDIR@" "'${LOCALEDIR}'" git_build_options "${git_build_options}")
1193 string(REPLACE "@PERL_PATH@" "'${PERL_PATH}'" git_build_options "${git_build_options}")
1194 string(REPLACE "@PYTHON_PATH@" "'${PYTHON_PATH}'" git_build_options "${git_build_options}")
1195 string(REPLACE "@RUNTIME_PREFIX@" "'${RUNTIME_PREFIX}'" git_build_options "${git_build_options}")
1196 string(REPLACE "@SANITIZE_ADDRESS@" "" git_build_options "${git_build_options}")
1197 string(REPLACE "@SANITIZE_LEAK@" "" git_build_options "${git_build_options}")
1198 string(REPLACE "@SHELL_PATH@" "'${SHELL_PATH}'" git_build_options "${git_build_options}")
1199 string(REPLACE "@TAR@" "'${TAR}'" git_build_options "${git_build_options}")
1200 string(REPLACE "@TEST_OUTPUT_DIRECTORY@" "" git_build_options "${git_build_options}")
1201 string(REPLACE "@TEST_SHELL_PATH@" "'${TEST_SHELL_PATH}'" git_build_options "${git_build_options}")
1202 string(REPLACE "@USE_GETTEXT_SCHEME@" "" git_build_options "${git_build_options}")
1203 string(REPLACE "@USE_LIBPCRE2@" "" git_build_options "${git_build_options}")
1204 string(REPLACE "@WITH_BREAKING_CHANGES@" "" git_build_options "${git_build_options}")
1205 string(REPLACE "@X@" "${EXE_EXTENSION}" git_build_options "${git_build_options}")
1206 if(USE_VCPKG)
1207 string(APPEND git_build_options "PATH=\"$PATH:$TEST_DIRECTORY/../compat/vcbuild/vcpkg/installed/x64-windows/bin\"\n")
1208 endif()
1209 file(WRITE ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS ${git_build_options})
1210
1211 #Make the tests work when building out of the source tree
1212 get_filename_component(CACHE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../CMakeCache.txt ABSOLUTE)
1213 if(NOT ${CMAKE_BINARY_DIR}/CMakeCache.txt STREQUAL ${CACHE_PATH})
1214 #Setting the build directory in test-lib.sh before running tests
1215 file(WRITE ${CMAKE_BINARY_DIR}/CTestCustom.cmake
1216 "file(WRITE ${CMAKE_SOURCE_DIR}/GIT-BUILD-DIR \"${CMAKE_BINARY_DIR}\")")
1217 #misc copies
1218 file(COPY ${CMAKE_SOURCE_DIR}/t/chainlint.pl DESTINATION ${CMAKE_BINARY_DIR}/t/)
1219 file(COPY ${CMAKE_SOURCE_DIR}/po/is.po DESTINATION ${CMAKE_BINARY_DIR}/po/)
1220 file(GLOB mergetools "${CMAKE_SOURCE_DIR}/mergetools/*")
1221 file(COPY ${mergetools} DESTINATION ${CMAKE_BINARY_DIR}/mergetools/)
1222 file(COPY ${CMAKE_SOURCE_DIR}/contrib/completion/git-prompt.sh DESTINATION ${CMAKE_BINARY_DIR}/contrib/completion/)
1223 file(COPY ${CMAKE_SOURCE_DIR}/contrib/completion/git-completion.bash DESTINATION ${CMAKE_BINARY_DIR}/contrib/completion/)
1224 endif()
1225
1226 file(GLOB test_scripts "${CMAKE_SOURCE_DIR}/t/t[0-9]*.sh")
1227
1228 #test
1229 foreach(tsh ${test_scripts})
1230 string(REGEX REPLACE ".*/(.*)\\.sh" "\\1" test_name ${tsh})
1231 add_test(NAME "t.suite.${test_name}"
1232 COMMAND ${SH_EXE} ${tsh} --no-bin-wrappers --no-chain-lint -vx
1233 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t)
1234 endforeach()
1235
1236 # This test script takes an extremely long time and is known to time out even
1237 # on fast machines because it requires in excess of one hour to run
1238 set_tests_properties("t.suite.t7112-reset-submodule" PROPERTIES TIMEOUT 4000)
1239
1240 endif()#BUILD_TESTING