Skip to content

Commit a390a84

Browse files
committed
cmake: move defines into "features.h" header
In a future commit, we will split out the build instructions for our library directory and move them into a subdirectory. One of the benefits is fixing scoping issues, where e.g. defines do not leak to build targets where they do not belong to. But unfortunately, this does also pose the problem of how to propagate some defines which are required by both the library and the test suite. One way would be to create another variable keeping track of all added defines and declare it inside of the parent scope. While this is the most obvious and simplest way of going ahead, it is kind of unfortunate. The main reason to not use this is that these defines become implicit dependencies between the build targets. By simply observing a define inside of the CMakeLists.txt file, one cannot reason whether this define is only required by the current target or whether it is required by different targets, as well. Another approach would be to use an internal header file keeping track of all defines shared between targets. While configuring the library, we will set various variables and let CMake configure the file, adding or removing defines based on what has been configured. Like this, one can easily keep track of the current environment by simply inspecting the header file. Furthermore, these dependencies are becoming clear inside the CMakeLists.txt, as instead of simply adding a define, we now call e.g. `SET(GIT_THREADSAFE 1)`. Having this header file though requires us to make sure it is always included before any "#ifdef"-preprocessor checks are executed. As we have already refactored code to always include the "common.h" header file before any statement inside of a file, this becomes easy: just make sure "common.h" includes the new "features.h" header file first.
1 parent 35087f0 commit a390a84

File tree

5 files changed

+70
-30
lines changed

5 files changed

+70
-30
lines changed

CMakeLists.txt

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ OPTION( USE_EXT_HTTP_PARSER "Use system HTTP_Parser if available" ON)
5151
OPTION( DEBUG_POOL "Enable debug pool allocator" OFF )
5252

5353
IF(DEBUG_POOL)
54-
ADD_DEFINITIONS(-DGIT_DEBUG_POOL)
54+
SET(GIT_DEBUG_POOL 1)
5555
ENDIF()
5656

5757
IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
@@ -237,12 +237,12 @@ IF (WIN32 AND EMBED_SSH_PATH)
237237
FILE(GLOB SRC_SSH "${EMBED_SSH_PATH}/src/*.c")
238238
INCLUDE_DIRECTORIES("${EMBED_SSH_PATH}/include")
239239
FILE(WRITE "${EMBED_SSH_PATH}/src/libssh2_config.h" "#define HAVE_WINCNG\n#define LIBSSH2_WINCNG\n#include \"../win32/libssh2_config.h\"")
240-
ADD_DEFINITIONS(-DGIT_SSH)
240+
SET(GIT_SSH 1)
241241
ENDIF()
242242

243243
IF (WIN32 AND WINHTTP)
244-
ADD_DEFINITIONS(-DGIT_WINHTTP)
245-
ADD_DEFINITIONS(-DGIT_HTTPS)
244+
SET(GIT_WINHTTP 1)
245+
SET(GIT_HTTPS 1)
246246

247247
# Since MinGW does not come with headers or an import library for winhttp,
248248
# we have to include a private header and generate our own import library
@@ -289,7 +289,7 @@ ELSE ()
289289
ENDIF ()
290290

291291
IF (CURL_FOUND)
292-
ADD_DEFINITIONS(-DGIT_CURL)
292+
SET(GIT_CURL 1)
293293
INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS})
294294
LINK_DIRECTORIES(${CURL_LIBRARY_DIRS})
295295
LINK_LIBRARIES(${CURL_LIBRARIES})
@@ -299,18 +299,18 @@ ENDIF()
299299

300300
# Specify sha1 implementation
301301
IF (USE_SHA1DC)
302-
ADD_DEFINITIONS(-DGIT_SHA1_COLLISIONDETECT)
302+
SET(GIT_SHA1_COLLISIONDETECT 1)
303303
ADD_DEFINITIONS(-DSHA1DC_NO_STANDARD_INCLUDES=1)
304304
ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\")
305305
ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\")
306306
FILE(GLOB SRC_SHA1 src/hash/hash_collisiondetect.c src/hash/sha1dc/*)
307307
ELSEIF (WIN32 AND NOT MINGW)
308-
ADD_DEFINITIONS(-DGIT_SHA1_WIN32)
308+
SET(GIT_SHA1_WIN32 1)
309309
FILE(GLOB SRC_SHA1 src/hash/hash_win32.c)
310310
ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
311-
ADD_DEFINITIONS(-DGIT_SHA1_COMMON_CRYPTO)
311+
SET(GIT_SHA1_COMMON_CRYPTO 1)
312312
ELSEIF (OPENSSL_FOUND)
313-
ADD_DEFINITIONS(-DGIT_SHA1_OPENSSL)
313+
SET(GIT_SHA1_OPENSSL 1)
314314
IF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
315315
LIST(APPEND LIBGIT2_PC_LIBS "-lssl")
316316
ELSE()
@@ -322,7 +322,7 @@ ENDIF()
322322

323323
# Enable tracing
324324
IF (ENABLE_TRACE STREQUAL "ON")
325-
ADD_DEFINITIONS(-DGIT_TRACE)
325+
SET(GIT_TRACE 1)
326326
ENDIF()
327327

328328
# Include POSIX regex when it is required
@@ -365,7 +365,7 @@ IF (USE_SSH)
365365
PKG_CHECK_MODULES(LIBSSH2 libssh2)
366366
ENDIF()
367367
IF (LIBSSH2_FOUND)
368-
ADD_DEFINITIONS(-DGIT_SSH)
368+
SET(GIT_SSH 1)
369369
INCLUDE_DIRECTORIES(${LIBSSH2_INCLUDE_DIRS})
370370
LINK_DIRECTORIES(${LIBSSH2_LIBRARY_DIRS})
371371
LIST(APPEND LIBGIT2_PC_LIBS ${LIBSSH2_LDFLAGS})
@@ -374,7 +374,7 @@ IF (LIBSSH2_FOUND)
374374

375375
CHECK_LIBRARY_EXISTS("${LIBSSH2_LIBRARIES}" libssh2_userauth_publickey_frommemory "${LIBSSH2_LIBRARY_DIRS}" HAVE_LIBSSH2_MEMORY_CREDENTIALS)
376376
IF (HAVE_LIBSSH2_MEMORY_CREDENTIALS)
377-
ADD_DEFINITIONS(-DGIT_SSH_MEMORY_CREDENTIALS)
377+
SET(GIT_SSH_MEMORY_CREDENTIALS 1)
378378
ENDIF()
379379
ELSE()
380380
MESSAGE(STATUS "LIBSSH2 not found. Set CMAKE_PREFIX_PATH if it is installed outside of the default search path.")
@@ -385,15 +385,15 @@ IF (USE_GSSAPI)
385385
FIND_PACKAGE(GSSAPI)
386386
ENDIF()
387387
IF (GSSAPI_FOUND)
388-
ADD_DEFINITIONS(-DGIT_GSSAPI)
388+
SET(GIT_GSSAPI 1)
389389
ENDIF()
390390

391391
# Optional external dependency: iconv
392392
IF (USE_ICONV)
393393
FIND_PACKAGE(Iconv)
394394
ENDIF()
395395
IF (ICONV_FOUND)
396-
ADD_DEFINITIONS(-DGIT_USE_ICONV)
396+
SET(GIT_USE_ICONV 1)
397397
INCLUDE_DIRECTORIES(${ICONV_INCLUDE_DIR})
398398
LIST(APPEND LIBGIT2_PC_LIBS ${ICONV_LIBRARIES})
399399
ENDIF()
@@ -421,7 +421,8 @@ IF (MSVC)
421421
ENDIF()
422422

423423
IF (MSVC_CRTDBG)
424-
SET(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG} /DGIT_MSVC_CRTDBG")
424+
SET(GIT_MSVC_CRTDBG 1)
425+
SET(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
425426
SET(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES}" "Dbghelp.lib")
426427
ENDIF()
427428

@@ -548,16 +549,16 @@ ELSE()
548549
ENDIF()
549550

550551
IF (SECURITY_FOUND)
551-
ADD_DEFINITIONS(-DGIT_SECURE_TRANSPORT)
552-
ADD_DEFINITIONS(-DGIT_HTTPS)
553-
INCLUDE_DIRECTORIES(${SECURITY_INCLUDE_DIR})
552+
SET(GIT_SECURE_TRANSPORT 1)
553+
SET(GIT_HTTPS 1)
554+
INCLUDE_DIRECTORIES(${SECURITY_INCLUDE_DIR})
554555
ENDIF ()
555556

556557
IF (OPENSSL_FOUND)
557-
ADD_DEFINITIONS(-DGIT_OPENSSL)
558-
ADD_DEFINITIONS(-DGIT_HTTPS)
559-
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
560-
SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
558+
SET(GIT_OPENSSL 1)
559+
SET(GIT_HTTPS 1)
560+
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
561+
SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
561562
ENDIF()
562563

563564

@@ -567,19 +568,19 @@ IF (THREADSAFE)
567568
FIND_PACKAGE(Threads REQUIRED)
568569
ENDIF()
569570

570-
ADD_DEFINITIONS(-DGIT_THREADS)
571+
SET(GIT_THREADS 1)
571572
ENDIF()
572573

573574
IF (USE_NSEC)
574-
ADD_DEFINITIONS(-DGIT_USE_NSEC)
575+
SET(GIT_USE_NSEC 1)
575576
ENDIF()
576577

577578
IF (HAVE_STRUCT_STAT_ST_MTIM)
578-
ADD_DEFINITIONS(-DGIT_USE_STAT_MTIM)
579+
SET(GIT_USE_STAT_MTIM 1)
579580
ELSEIF (HAVE_STRUCT_STAT_ST_MTIMESPEC)
580-
ADD_DEFINITIONS(-DGIT_USE_STAT_MTIMESPEC)
581+
SET(GIT_USE_STAT_MTIMESPEC 1)
581582
ELSEIF (HAVE_STRUCT_STAT_ST_MTIME_NSEC)
582-
ADD_DEFINITIONS(-DGIT_USE_STAT_MTIME_NSEC)
583+
SET(GIT_USE_STAT_MTIME_NSEC 1)
583584
ENDIF()
584585

585586
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
@@ -603,15 +604,18 @@ FILE(GLOB SRC_GIT2 src/*.c src/*.h src/transports/*.c src/transports/*.h src/xdi
603604

604605
# Determine architecture of the machine
605606
IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
606-
ADD_DEFINITIONS(-DGIT_ARCH_64)
607+
SET(GIT_ARCH_64 1)
607608
ELSEIF (CMAKE_SIZEOF_VOID_P EQUAL 4)
608-
ADD_DEFINITIONS(-DGIT_ARCH_32)
609+
SET(GIT_ARCH_32 1)
609610
ELSEIF (CMAKE_SIZEOF_VOID_P)
610611
MESSAGE(FATAL_ERROR "Unsupported architecture (pointer size is ${CMAKE_SIZEOF_VOID_P} bytes)")
611612
ELSE()
612613
MESSAGE(FATAL_ERROR "Unsupported architecture (CMAKE_SIZEOF_VOID_P is unset)")
613614
ENDIF()
614615

616+
CONFIGURE_FILE(src/features.h.in git2/sys/features.h)
617+
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
618+
615619
SET(GIT2INTERNAL_OBJECTS ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1})
616620

617621
IF (CMAKE_VERSION VERSION_GREATER 2.8.7)

src/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef INCLUDE_common_h__
88
#define INCLUDE_common_h__
99

10+
#include "git2/sys/features.h"
1011
#include "git2/common.h"
1112
#include "cc-compat.h"
1213

src/features.h.in

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef INCLUDE_features_h__
2+
#define INCLUDE_features_h__
3+
4+
#cmakedefine GIT_DEBUG_POOL 1
5+
#cmakedefine GIT_TRACE 1
6+
#cmakedefine GIT_THREADS 1
7+
#cmakedefine GIT_MSVC_CRTDBG 1
8+
9+
#cmakedefine GIT_ARCH_64 1
10+
#cmakedefine GIT_ARCH_32 1
11+
12+
#cmakedefine GIT_USE_ICONV 1
13+
#cmakedefine GIT_USE_NSEC 1
14+
#cmakedefine GIT_USE_STAT_MTIM 1
15+
#cmakedefine GIT_USE_STAT_MTIMESPEC 1
16+
#cmakedefine GIT_USE_STAT_MTIME_NSEC 1
17+
18+
#cmakedefine GIT_SSH 1
19+
#cmakedefine GIT_SSH_MEMORY_CREDENTIALS 1
20+
21+
#cmakedefine GIT_GSSAPI 1
22+
#cmakedefine GIT_WINHTTP 1
23+
#cmakedefine GIT_CURL 1
24+
25+
#cmakedefine GIT_HTTPS 1
26+
#cmakedefine GIT_OPENSSL 1
27+
#cmakedefine GIT_SECURE_TRANSPORT 1
28+
29+
#cmakedefine GIT_SHA1_COLLISIONDETECT 1
30+
#cmakedefine GIT_SHA1_WIN32 1
31+
#cmakedefine GIT_SHA1_COMMON_CRYPTO 1
32+
#cmakedefine GIT_SHA1_OPENSSL 1
33+
34+
#endif

src/unix/posix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef INCLUDE_posix__unix_h__
88
#define INCLUDE_posix__unix_h__
99

10+
#include "git2/sys/features.h"
1011
#include <stdio.h>
1112
#include <dirent.h>
1213
#include <sys/param.h>

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
1010
ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
1111
ADD_DEFINITIONS(-DCLAR_TMPDIR=\"libgit2_tests\")
1212

13-
INCLUDE_DIRECTORIES(${CLAR_PATH})
13+
INCLUDE_DIRECTORIES(${CLAR_PATH} ${CMAKE_BINARY_DIR}/src)
1414
FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/*/*.h)
1515
SET(SRC_CLAR "main.c" "clar_libgit2.c" "clar_libgit2_trace.c" "clar_libgit2_timer.c" "clar.c")
1616

0 commit comments

Comments
 (0)