Skip to content

Commit 53911ed

Browse files
committed
cmake: use git2internal target to populate sources
Modern CMake is usually target-driven in that a target is first defined and then the likes of `target_sources`, `target_include_directories` etc. are used to further populate the target. We still use old-style CMake, where we first set up a set of variables and then populate the target in a single call. Let's migrate to modern CMake usage by starting to populate the sources of our git2internal target piece-by-piece. While this is a small step, it allows us to convert to target-based build instructions piece-by-piece.
1 parent 19eb1e4 commit 53911ed

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/CMakeLists.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
add_library(git2internal OBJECT)
2+
set_target_properties(git2internal PROPERTIES C_STANDARD 90)
3+
14
IF(DEBUG_POOL)
25
SET(GIT_DEBUG_POOL 1)
36
ENDIF()
@@ -81,6 +84,8 @@ ADD_FEATURE_INFO(threadsafe THREADSAFE "threadsafe support")
8184
if(WIN32 AND EMBED_SSH_PATH)
8285
file(GLOB SRC_SSH "${EMBED_SSH_PATH}/src/*.c")
8386
list(SORT SRC_SSH)
87+
target_sources(git2internal PRIVATE ${SRC_SSH})
88+
8489
list(APPEND LIBGIT2_SYSTEM_INCLUDES "${EMBED_SSH_PATH}/include")
8590
file(WRITE "${EMBED_SSH_PATH}/src/libssh2_config.h" "#define HAVE_WINCNG\n#define LIBSSH2_WINCNG\n#include \"../win32/libssh2_config.h\"")
8691
set(GIT_SSH 1)
@@ -104,8 +109,9 @@ IF (WIN32 AND WINHTTP)
104109
LIST(APPEND LIBGIT2_PC_LIBS "-lrpcrt4" "-lcrypt32" "-lole32")
105110
ENDIF()
106111

107-
Include(SelectHTTPSBackend)
108-
Include(SelectHashes)
112+
include(SelectHTTPSBackend)
113+
include(SelectHashes)
114+
target_sources(git2internal PRIVATE ${SRC_SHA1})
109115

110116
# Specify regular expression implementation
111117
FIND_PACKAGE(PCRE)
@@ -275,18 +281,21 @@ file(GLOB SRC_H
275281
"${libgit2_SOURCE_DIR}/include/git2/*.h"
276282
"${libgit2_SOURCE_DIR}/include/git2/sys/*.h")
277283
list(SORT SRC_H)
284+
target_sources(git2internal PRIVATE ${SRC_H})
278285

279286
# On Windows use specific platform sources
280287
if(WIN32 AND NOT CYGWIN)
281288
SET(WIN_RC "win32/git2.rc")
282289

283290
file(GLOB SRC_OS win32/*.c win32/*.h)
284291
list(SORT SRC_OS)
292+
target_sources(git2internal PRIVATE ${SRC_OS})
285293
elseif(AMIGA)
286294
add_definitions(-DNO_ADDRINFO -DNO_READDIR_R -DNO_MMAP)
287295
else()
288296
file(GLOB SRC_OS unix/*.c unix/*.h)
289297
list(SORT SRC_OS)
298+
target_sources(git2internal PRIVATE ${SRC_OS})
290299
endif()
291300

292301
IF (USE_LEAK_CHECKER STREQUAL "valgrind")
@@ -299,6 +308,7 @@ file(GLOB SRC_GIT2 *.c *.h
299308
transports/*.c transports/*.h
300309
xdiff/*.c xdiff/*.h)
301310
list(SORT SRC_GIT2)
311+
target_sources(git2internal PRIVATE ${SRC_GIT2})
302312

303313
IF(APPLE)
304314
# The old Secure Transport API has been deprecated in macOS 10.15.
@@ -325,10 +335,6 @@ ENDIF()
325335

326336
CONFIGURE_FILE(features.h.in git2/sys/features.h)
327337

328-
SET(LIBGIT2_SOURCES ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_SSH} ${SRC_SHA1})
329-
330-
ADD_LIBRARY(git2internal OBJECT ${LIBGIT2_SOURCES})
331-
SET_TARGET_PROPERTIES(git2internal PROPERTIES C_STANDARD 90)
332338
IDE_SPLIT_SOURCES(git2internal)
333339
LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:git2internal>)
334340

0 commit comments

Comments
 (0)