Skip to content

Commit 4da74c8

Browse files
committed
cmake: use project-relative binary and source directories
Due to our split of CMake files into multiple modules, we had to replace some uses of the `${CMAKE_CURRENT_SOURCE_DIR}` and `${CMAKE_CURRENT_BINARY_DIR}` variables and replace them with `${CMAKE_SOURCE_DIR}` and `${CMAKE_BINARY_DIR}`. This enabled us to still be able to refer to top-level files when defining build instructions inside of a subdirectory. When replacing all variables, it was assumed that the absolute set of variables is always relative to the current project. But in fact, this is not the case, as these variables always point to the source and binary directory as given by the top-levl project. So the change actually broke the ability to include libgit2 directly as a subproject, as source files cannot be found anymore. Fix this by instead using project-specific source and binary directories with `${libgit2_SOURCE_DIR}` and `${libgit2_BINARY_DIR}`.
1 parent 661cf4d commit 4da74c8

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ IF (NOT CMAKE_VERSION VERSION_LESS 3.1)
1919
ENDIF()
2020

2121
# Add find modules to the path
22-
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
22+
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${libgit2_SOURCE_DIR}/cmake/Modules/")
2323

2424
INCLUDE(CheckLibraryExists)
2525
INCLUDE(CheckFunctionExists)
@@ -125,7 +125,7 @@ FUNCTION(IDE_SPLIT_SOURCES target)
125125
GET_TARGET_PROPERTY(sources ${target} SOURCES)
126126
FOREACH(source ${sources})
127127
IF(source MATCHES ".*/")
128-
STRING(REPLACE ${CMAKE_SOURCE_DIR}/ "" rel ${source})
128+
STRING(REPLACE ${libgit2_SOURCE_DIR}/ "" rel ${source})
129129
IF(rel)
130130
STRING(REGEX REPLACE "/([^/]*)$" "" rel ${rel})
131131
IF(rel)
@@ -138,14 +138,14 @@ FUNCTION(IDE_SPLIT_SOURCES target)
138138
ENDIF()
139139
ENDFUNCTION()
140140

141-
FILE(STRINGS "${CMAKE_SOURCE_DIR}/include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
141+
FILE(STRINGS "${libgit2_SOURCE_DIR}/include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
142142

143143
STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}")
144144
STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR "${GIT2_HEADER}")
145145
STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}")
146146
SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}")
147147

148-
FILE(STRINGS "${CMAKE_SOURCE_DIR}/include/git2/version.h" GIT2_HEADER_SOVERSION REGEX "^#define LIBGIT2_SOVERSION [0-9]+$")
148+
FILE(STRINGS "${libgit2_SOURCE_DIR}/include/git2/version.h" GIT2_HEADER_SOVERSION REGEX "^#define LIBGIT2_SOVERSION [0-9]+$")
149149
STRING(REGEX REPLACE "^.*LIBGIT2_SOVERSION ([0-9]+)$" "\\1" LIBGIT2_SOVERSION "${GIT2_HEADER_SOVERSION}")
150150

151151
# Platform specific compilation flags

deps/winhttp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ IF (NOT DLLTOOL)
33
MESSAGE(FATAL_ERROR "Could not find dlltool command")
44
ENDIF ()
55

6-
SET(LIBWINHTTP_PATH "${CMAKE_BINARY_DIR}/deps/winhttp")
6+
SET(LIBWINHTTP_PATH "${libgit2_BINARY_DIR}/deps/winhttp")
77
SET(LIBWINHTTP_PATH ${LIBWINHTTP_PATH} PARENT_SCOPE)
88
FILE(MAKE_DIRECTORY ${LIBWINHTTP_PATH})
99

src/CMakeLists.txt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ SET(LIBGIT2_PC_LIBS "")
1818

1919
SET(LIBGIT2_INCLUDES
2020
"${CMAKE_CURRENT_BINARY_DIR}"
21-
"${CMAKE_SOURCE_DIR}/src"
22-
"${CMAKE_SOURCE_DIR}/include")
21+
"${libgit2_SOURCE_DIR}/src"
22+
"${libgit2_SOURCE_DIR}/include")
2323
SET(LIBGIT2_LIBS "")
2424
SET(LIBGIT2_LIBDIRS "")
2525

@@ -129,9 +129,9 @@ IF (WIN32 AND WINHTTP)
129129
# Since MinGW does not come with headers or an import library for winhttp,
130130
# we have to include a private header and generate our own import library
131131
IF (MINGW)
132-
ADD_SUBDIRECTORY("${CMAKE_SOURCE_DIR}/deps/winhttp" "${CMAKE_BINARY_DIR}/deps/winhttp")
132+
ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/winhttp" "${libgit2_BINARY_DIR}/deps/winhttp")
133133
LIST(APPEND LIBGIT2_LIBS winhttp)
134-
LIST(APPEND LIBGIT2_INCLUDES "${CMAKE_SOURCE_DIR}/deps/winhttp")
134+
LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/winhttp")
135135
LIST(APPEND LIBGIT2_LIBDIRS ${LIBWINHTTP_PATH})
136136
ELSE()
137137
LIST(APPEND LIBGIT2_LIBS "winhttp")
@@ -184,8 +184,8 @@ ENDIF()
184184

185185
# Include POSIX regex when it is required
186186
IF(WIN32 OR AMIGA OR CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
187-
ADD_SUBDIRECTORY("${CMAKE_SOURCE_DIR}/deps/regex" "${CMAKE_BINARY_DIR}/deps/regex")
188-
LIST(APPEND LIBGIT2_INCLUDES "${CMAKE_SOURCE_DIR}/deps/regex")
187+
ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/regex" "${libgit2_BINARY_DIR}/deps/regex")
188+
LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/regex")
189189
LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:regex>)
190190
ENDIF()
191191

@@ -197,8 +197,8 @@ IF (USE_EXT_HTTP_PARSER AND HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUA
197197
LIST(APPEND LIBGIT2_PC_LIBS "-lhttp_parser")
198198
ELSE()
199199
MESSAGE(STATUS "http-parser version 2 was not found or disabled; using bundled 3rd-party sources.")
200-
ADD_SUBDIRECTORY("${CMAKE_SOURCE_DIR}/deps/http-parser" "${CMAKE_BINARY_DIR}/deps/http-parser")
201-
LIST(APPEND LIBGIT2_INCLUDES "${CMAKE_SOURCE_DIR}/deps/http-parser")
200+
ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/http-parser" "${libgit2_BINARY_DIR}/deps/http-parser")
201+
LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/http-parser")
202202
LIST(APPEND LIBGIT2_OBJECTS "$<TARGET_OBJECTS:http-parser>")
203203
ENDIF()
204204

@@ -215,8 +215,8 @@ IF (ZLIB_FOUND)
215215
ENDIF()
216216
ELSE()
217217
MESSAGE(STATUS "zlib was not found; using bundled 3rd-party sources." )
218-
ADD_SUBDIRECTORY("${CMAKE_SOURCE_DIR}/deps/zlib" "${CMAKE_BINARY_DIR}/deps/zlib")
219-
LIST(APPEND LIBGIT2_INCLUDES "${CMAKE_SOURCE_DIR}/deps/zlib")
218+
ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/zlib" "${libgit2_BINARY_DIR}/deps/zlib")
219+
LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/zlib")
220220
LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:zlib>)
221221
ENDIF()
222222

@@ -299,9 +299,9 @@ ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
299299

300300
# Collect sourcefiles
301301
FILE(GLOB SRC_H
302-
"${CMAKE_SOURCE_DIR}/include/git2.h"
303-
"${CMAKE_SOURCE_DIR}/include/git2/*.h"
304-
"${CMAKE_SOURCE_DIR}/include/git2/sys/*.h")
302+
"${libgit2_SOURCE_DIR}/include/git2.h"
303+
"${libgit2_SOURCE_DIR}/include/git2/*.h"
304+
"${libgit2_SOURCE_DIR}/include/git2/sys/*.h")
305305

306306
# On Windows use specific platform sources
307307
IF (WIN32 AND NOT CYGWIN)
@@ -346,7 +346,7 @@ IF (${CMAKE_VERSION} VERSION_LESS 2.8.12)
346346
ELSE()
347347
TARGET_INCLUDE_DIRECTORIES(git2internal
348348
PRIVATE ${LIBGIT2_INCLUDES}
349-
PUBLIC ${CMAKE_SOURCE_DIR}/include)
349+
PUBLIC ${libgit2_SOURCE_DIR}/include)
350350
ENDIF()
351351

352352
SET(LIBGIT2_OBJECTS ${LIBGIT2_OBJECTS} PARENT_SCOPE)
@@ -359,9 +359,9 @@ LINK_DIRECTORIES(${LIBGIT2_LIBDIRS})
359359
ADD_LIBRARY(git2 ${WIN_RC} ${LIBGIT2_OBJECTS})
360360
TARGET_LINK_LIBRARIES(git2 ${LIBGIT2_LIBS})
361361

362-
SET_TARGET_PROPERTIES(git2 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
363-
SET_TARGET_PROPERTIES(git2 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
364-
SET_TARGET_PROPERTIES(git2 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
362+
SET_TARGET_PROPERTIES(git2 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR})
363+
SET_TARGET_PROPERTIES(git2 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR})
364+
SET_TARGET_PROPERTIES(git2 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR})
365365

366366
# Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
367367
# Win64+MSVC+static libs = linker error
@@ -382,7 +382,7 @@ IF (SONAME)
382382
ENDIF()
383383
ENDIF()
384384
STRING(REPLACE ";" " " LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS}")
385-
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/libgit2.pc.in ${CMAKE_BINARY_DIR}/libgit2.pc @ONLY)
385+
CONFIGURE_FILE(${libgit2_SOURCE_DIR}/libgit2.pc.in ${libgit2_BINARY_DIR}/libgit2.pc @ONLY)
386386

387387
IF (MSVC_IDE)
388388
# Precompiled headers
@@ -396,6 +396,6 @@ INSTALL(TARGETS git2
396396
LIBRARY DESTINATION ${LIB_INSTALL_DIR}
397397
ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
398398
)
399-
INSTALL(FILES ${CMAKE_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig )
400-
INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} )
401-
INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} )
399+
INSTALL(FILES ${libgit2_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig )
400+
INSTALL(DIRECTORY ${libgit2_SOURCE_DIR}/include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} )
401+
INSTALL(FILES ${libgit2_SOURCE_DIR}/include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} )

tests/CMakeLists.txt

Lines changed: 7 additions & 7 deletions
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} ${CMAKE_BINARY_DIR}/src)
13+
INCLUDE_DIRECTORIES(${CLAR_PATH} ${libgit2_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

@@ -35,7 +35,7 @@ INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
3535

3636
ADD_EXECUTABLE(libgit2_clar ${SRC_CLAR} ${SRC_TEST} ${LIBGIT2_OBJECTS})
3737

38-
SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
38+
SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR})
3939

4040
IF (${CMAKE_VERSION} VERSION_LESS 2.8.12)
4141
# Already handled by a global INCLUDE_DIRECTORY()
@@ -53,13 +53,13 @@ IF (MSVC_IDE)
5353
ENDIF ()
5454

5555
IF (WINHTTP OR OPENSSL_FOUND OR SECURITY_FOUND)
56-
ADD_TEST(libgit2_clar "${CMAKE_BINARY_DIR}/libgit2_clar" -ionline -xclone::local::git_style_unc_paths -xclone::local::standard_unc_paths_are_written_git_style)
56+
ADD_TEST(libgit2_clar "${libgit2_BINARY_DIR}/libgit2_clar" -ionline -xclone::local::git_style_unc_paths -xclone::local::standard_unc_paths_are_written_git_style)
5757
ELSE ()
58-
ADD_TEST(libgit2_clar "${CMAKE_BINARY_DIR}/libgit2_clar" -v -xclone::local::git_style_unc_paths -xclone::local::standard_unc_paths_are_written_git_style)
58+
ADD_TEST(libgit2_clar "${libgit2_BINARY_DIR}/libgit2_clar" -v -xclone::local::git_style_unc_paths -xclone::local::standard_unc_paths_are_written_git_style)
5959
ENDIF ()
6060

6161
# Add a test target which runs the cred callback tests, to be
6262
# called after setting the url and user
63-
ADD_TEST(libgit2_clar-cred_callback "${CMAKE_BINARY_DIR}/libgit2_clar" -v -sonline::clone::cred_callback)
64-
ADD_TEST(libgit2_clar-proxy_credentials_in_url "${CMAKE_BINARY_DIR}/libgit2_clar" -v -sonline::clone::proxy_credentials_in_url)
65-
ADD_TEST(libgit2_clar-proxy_credentials_request "${CMAKE_BINARY_DIR}/libgit2_clar" -v -sonline::clone::proxy_credentials_request)
63+
ADD_TEST(libgit2_clar-cred_callback "${libgit2_BINARY_DIR}/libgit2_clar" -v -sonline::clone::cred_callback)
64+
ADD_TEST(libgit2_clar-proxy_credentials_in_url "${libgit2_BINARY_DIR}/libgit2_clar" -v -sonline::clone::proxy_credentials_in_url)
65+
ADD_TEST(libgit2_clar-proxy_credentials_request "${libgit2_BINARY_DIR}/libgit2_clar" -v -sonline::clone::proxy_credentials_request)

0 commit comments

Comments
 (0)