cmake: use PROJECT_SOURCE_DIR of CMAKE_SOURCE_DIR Also applies to *_BINARY_DIR. This effectively reverts 84083dcc8bd41332ccac9d7b537f3e254d79011c, which broke all users of libgit2 that use it as a CMake subdirectory (via `add_subdirectory()`). This is because CMAKE_SOURCE_DIR refers to the root-most CMake directory, which in the case of `add_subdirectory()` is a parent project to libgit2 and thus the paths don't make any sense to the configuration files. Corollary, CMAKE_SOURCE_DIR only makes sense if the CMake project is always the root project - which can rarely be guaranteed. In all honesty, CMake should deprecate and eventually remove CMAKE_SOURCE_DIR and CMAKE_BINARY_DIR. It's been the source of headaches and confusion for years, they're rarely useful over CMAKE_CURRENT_(SOURCE|BINARY)_DIR or PROJECT_(SOURCE|BINARY)_DIR, and they cause a lot of confusing configuration and source code layouts to boot. Any time they are used, they break `add_subdirectory()` almost 100% of the time, cause confusing error messages, and hide subtle bugs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3a01068..32bb450 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.5.1)
project(libgit2 VERSION "1.3.0" LANGUAGES C)
# Add find modules to the path
-set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
#
# Build options
diff --git a/cmake/IdeSplitSources.cmake b/cmake/IdeSplitSources.cmake
index 9e16cbc..4f928ac 100644
--- a/cmake/IdeSplitSources.cmake
+++ b/cmake/IdeSplitSources.cmake
@@ -8,7 +8,7 @@ function(IDE_SPLIT_SOURCES target)
get_target_property(sources ${target} SOURCES)
foreach(source ${sources})
if(source MATCHES ".*/")
- string(REPLACE ${CMAKE_SOURCE_DIR}/ "" rel ${source})
+ string(REPLACE ${PROJECT_SOURCE_DIR}/ "" rel ${source})
if(rel)
string(REGEX REPLACE "/([^/]*)$" "" rel ${rel})
if(rel)
diff --git a/cmake/SelectHTTPParser.cmake b/cmake/SelectHTTPParser.cmake
index ffc897f..bb6ef55 100644
--- a/cmake/SelectHTTPParser.cmake
+++ b/cmake/SelectHTTPParser.cmake
@@ -12,8 +12,8 @@ if(USE_HTTP_PARSER STREQUAL "system")
endif()
else()
message(STATUS "http-parser version 2 was not found or disabled; using bundled 3rd-party sources.")
- add_subdirectory("${CMAKE_SOURCE_DIR}/deps/http-parser" "${CMAKE_BINARY_DIR}/deps/http-parser")
- list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${CMAKE_SOURCE_DIR}/deps/http-parser")
+ add_subdirectory("${PROJECT_SOURCE_DIR}/deps/http-parser" "${PROJECT_BINARY_DIR}/deps/http-parser")
+ list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/http-parser")
list(APPEND LIBGIT2_DEPENDENCY_OBJECTS "$<TARGET_OBJECTS:http-parser>")
add_feature_info(http-parser ON "http-parser support (bundled)")
endif()
diff --git a/cmake/SelectRegex.cmake b/cmake/SelectRegex.cmake
index 88aa156..2a3a91b 100644
--- a/cmake/SelectRegex.cmake
+++ b/cmake/SelectRegex.cmake
@@ -43,8 +43,8 @@ elseif(REGEX_BACKEND STREQUAL "builtin")
add_feature_info(regex ON "using bundled PCRE")
set(GIT_REGEX_BUILTIN 1)
- add_subdirectory("${CMAKE_SOURCE_DIR}/deps/pcre" "${CMAKE_BINARY_DIR}/deps/pcre")
- list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${CMAKE_SOURCE_DIR}/deps/pcre")
+ add_subdirectory("${PROJECT_SOURCE_DIR}/deps/pcre" "${PROJECT_BINARY_DIR}/deps/pcre")
+ list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/pcre")
list(APPEND LIBGIT2_DEPENDENCY_OBJECTS $<TARGET_OBJECTS:pcre>)
else()
message(FATAL_ERROR "The REGEX_BACKEND option provided is not supported")
diff --git a/cmake/SelectWinHTTP.cmake b/cmake/SelectWinHTTP.cmake
index 38642e1..96e0bdb 100644
--- a/cmake/SelectWinHTTP.cmake
+++ b/cmake/SelectWinHTTP.cmake
@@ -4,9 +4,9 @@ if(WIN32 AND USE_WINHTTP)
# Since MinGW does not come with headers or an import library for winhttp,
# we have to include a private header and generate our own import library
if(MINGW)
- add_subdirectory("${CMAKE_SOURCE_DIR}/deps/winhttp" "${CMAKE_BINARY_DIR}/deps/winhttp")
+ add_subdirectory("${PROJECT_SOURCE_DIR}/deps/winhttp" "${PROJECT_BINARY_DIR}/deps/winhttp")
list(APPEND LIBGIT2_SYSTEM_LIBS winhttp)
- list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${CMAKE_SOURCE_DIR}/deps/winhttp")
+ list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/winhttp")
else()
list(APPEND LIBGIT2_SYSTEM_LIBS "winhttp")
list(APPEND LIBGIT2_PC_LIBS "-lwinhttp")
diff --git a/cmake/SelectZlib.cmake b/cmake/SelectZlib.cmake
index 0de1d4a..fb4361a 100644
--- a/cmake/SelectZlib.cmake
+++ b/cmake/SelectZlib.cmake
@@ -22,13 +22,13 @@ if(USE_BUNDLED_ZLIB STREQUAL "OFF")
endif()
endif()
if(USE_BUNDLED_ZLIB STREQUAL "Chromium")
- add_subdirectory("${CMAKE_SOURCE_DIR}/deps/chromium-zlib" "${CMAKE_BINARY_DIR}/deps/chromium-zlib")
- list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${CMAKE_SOURCE_DIR}/deps/chromium-zlib")
+ add_subdirectory("${PROJECT_SOURCE_DIR}/deps/chromium-zlib" "${PROJECT_BINARY_DIR}/deps/chromium-zlib")
+ list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/chromium-zlib")
list(APPEND LIBGIT2_DEPENDENCY_OBJECTS $<TARGET_OBJECTS:chromium_zlib>)
add_feature_info(zlib ON "using (Chromium) bundled zlib")
elseif(USE_BUNDLED_ZLIB OR NOT ZLIB_FOUND)
- add_subdirectory("${CMAKE_SOURCE_DIR}/deps/zlib" "${CMAKE_BINARY_DIR}/deps/zlib")
- list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${CMAKE_SOURCE_DIR}/deps/zlib")
+ add_subdirectory("${PROJECT_SOURCE_DIR}/deps/zlib" "${PROJECT_BINARY_DIR}/deps/zlib")
+ list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/zlib")
list(APPEND LIBGIT2_DEPENDENCY_OBJECTS $<TARGET_OBJECTS:zlib>)
add_feature_info(zlib ON "using bundled zlib")
endif()
diff --git a/deps/winhttp/CMakeLists.txt b/deps/winhttp/CMakeLists.txt
index c11a867..1a87989 100644
--- a/deps/winhttp/CMakeLists.txt
+++ b/deps/winhttp/CMakeLists.txt
@@ -3,7 +3,7 @@ if(NOT DLLTOOL)
message(FATAL_ERROR "Could not find dlltool command")
endif()
-set(LIBWINHTTP_PATH "${CMAKE_BINARY_DIR}/deps/winhttp")
+set(LIBWINHTTP_PATH "${PROJECT_BINARY_DIR}/deps/winhttp")
set(LIBWINHTTP_PATH ${LIBWINHTTP_PATH} PARENT_SCOPE)
file(MAKE_DIRECTORY ${LIBWINHTTP_PATH})
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 03609b6..98881b5 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -36,8 +36,8 @@ set(LIBGIT2_PC_LIBS "")
set(LIBGIT2_INCLUDES
"${CMAKE_CURRENT_BINARY_DIR}"
- "${CMAKE_SOURCE_DIR}/src"
- "${CMAKE_SOURCE_DIR}/include")
+ "${PROJECT_SOURCE_DIR}/src"
+ "${PROJECT_SOURCE_DIR}/include")
if(HAVE_FUTIMENS)
set(GIT_USE_FUTIMENS 1)
@@ -117,8 +117,8 @@ target_sources(git2internal PRIVATE ${SRC_SHA1})
# Optional external dependency: ntlmclient
if(USE_NTLMCLIENT)
set(GIT_NTLM 1)
- add_subdirectory("${CMAKE_SOURCE_DIR}/deps/ntlmclient" "${CMAKE_BINARY_DIR}/deps/ntlmclient")
- list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${CMAKE_SOURCE_DIR}/deps/ntlmclient")
+ add_subdirectory("${PROJECT_SOURCE_DIR}/deps/ntlmclient" "${PROJECT_BINARY_DIR}/deps/ntlmclient")
+ list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/ntlmclient")
list(APPEND LIBGIT2_DEPENDENCY_OBJECTS "$<TARGET_OBJECTS:ntlmclient>")
endif()
add_feature_info(ntlmclient GIT_NTLM "NTLM authentication support for Unix")
@@ -164,9 +164,9 @@ target_compile_definitions(git2internal PRIVATE _FILE_OFFSET_BITS=64)
# Collect sourcefiles
file(GLOB SRC_H
- "${CMAKE_SOURCE_DIR}/include/git2.h"
- "${CMAKE_SOURCE_DIR}/include/git2/*.h"
- "${CMAKE_SOURCE_DIR}/include/git2/sys/*.h")
+ "${PROJECT_SOURCE_DIR}/include/git2.h"
+ "${PROJECT_SOURCE_DIR}/include/git2/*.h"
+ "${PROJECT_SOURCE_DIR}/include/git2/sys/*.h")
list(SORT SRC_H)
target_sources(git2internal PRIVATE ${SRC_H})
@@ -225,7 +225,7 @@ configure_file(features.h.in git2/sys/features.h)
ide_split_sources(git2internal)
list(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:git2internal> ${LIBGIT2_DEPENDENCY_OBJECTS})
-target_include_directories(git2internal PRIVATE ${LIBGIT2_INCLUDES} ${LIBGIT2_DEPENDENCY_INCLUDES} PUBLIC ${CMAKE_SOURCE_DIR}/include)
+target_include_directories(git2internal PRIVATE ${LIBGIT2_INCLUDES} ${LIBGIT2_DEPENDENCY_INCLUDES} PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_include_directories(git2internal SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES})
set(LIBGIT2_INCLUDES ${LIBGIT2_INCLUDES} PARENT_SCOPE)
@@ -247,9 +247,9 @@ add_library(git2 ${WIN_RC} ${LIBGIT2_OBJECTS})
target_link_libraries(git2 ${LIBGIT2_SYSTEM_LIBS})
set_target_properties(git2 PROPERTIES C_STANDARD 90)
-set_target_properties(git2 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
-set_target_properties(git2 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
-set_target_properties(git2 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
+set_target_properties(git2 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
+set_target_properties(git2 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
+set_target_properties(git2 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
# Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
# Win64+MSVC+static libs = linker error
@@ -290,5 +290,5 @@ install(TARGETS git2
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
-install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/git2 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
-install(FILES ${CMAKE_SOURCE_DIR}/include/git2.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
+install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/git2 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
+install(FILES ${PROJECT_SOURCE_DIR}/include/git2.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 2272dec..fdf194d 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -38,7 +38,7 @@ set_source_files_properties(
add_executable(libgit2_tests ${SRC_CLAR} ${SRC_TEST} ${LIBGIT2_OBJECTS})
set_target_properties(libgit2_tests PROPERTIES C_STANDARD 90)
-set_target_properties(libgit2_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
+set_target_properties(libgit2_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
target_include_directories(libgit2_tests PRIVATE ${TEST_INCLUDES} ${LIBGIT2_INCLUDES} ${LIBGIT2_DEPENDENCY_INCLUDES})
target_include_directories(libgit2_tests SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES})
@@ -62,9 +62,9 @@ endif()
function(ADD_CLAR_TEST name)
if(NOT USE_LEAK_CHECKER STREQUAL "OFF")
- add_test(${name} "${CMAKE_SOURCE_DIR}/script/${USE_LEAK_CHECKER}.sh" "${CMAKE_BINARY_DIR}/libgit2_tests" ${ARGN})
+ add_test(${name} "${PROJECT_SOURCE_DIR}/script/${USE_LEAK_CHECKER}.sh" "${PROJECT_BINARY_DIR}/libgit2_tests" ${ARGN})
else()
- add_test(${name} "${CMAKE_BINARY_DIR}/libgit2_tests" ${ARGN})
+ add_test(${name} "${PROJECT_BINARY_DIR}/libgit2_tests" ${ARGN})
endif()
endfunction(ADD_CLAR_TEST)