cmake: Skip using git when building from source tarball or as subproject
What changed, and why it matters
This commit changes Bitcoin Core's build system so it does not try to use Git when compiling from a source tarball or when Bitcoin Core is included as a subproject inside another project. It uses Git's 'export-subst' feature to detect tarball builds. There is no security vulnerability here; it is a build-system robustness improvement.
No security action required. Treat as a normal build-system maintenance change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds src/CMakeLists.txt to .gitattributes with the export-subst attribute, and inserts a generated line into src/CMakeLists.txt that Git expands to set(IS_SOURCE_TARBALL TRUE) in exported archives. The CMake logic then only calls find_package(Git) when the build is neither from a source tarball nor a subproject. This prevents unnecessary Git dependency failures in those contexts.
Changed components
src/CMakeLists.txt.gitattributesCMake build configurationInspect captured patch +7 / −1
diff --git a/.gitattributes b/.gitattributes
index c9cf4a7d..25303e74 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1 +1,2 @@
src/clientversion.cpp export-subst
+src/CMakeLists.txt export-subst
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ddbf2d6d..43923996 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -37,7 +37,12 @@ if(NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
endif()
-find_package(Git QUIET)
+set(IS_SOURCE_TARBALL FALSE)
+# git will expand the next line to "set(IS_SOURCE_TARBALL TRUE)" inside archives:
+#$Format:%nset(IS_SOURCE_TARBALL TRUE)$
+if(NOT IS_SOURCE_TARBALL AND PROJECT_IS_TOP_LEVEL)
+ find_package(Git QUIET)
+endif()
add_custom_target(generate_build_info
BYPRODUCTS ${PROJECT_BINARY_DIR}/src/bitcoin-build-info.h
COMMAND ${CMAKE_COMMAND} -DGIT_EXECUTABLE=${GIT_EXECUTABLE} -DBUILD_INFO_HEADER_PATH=${PROJECT_BINARY_DIR}/src/bitcoin-build-info.h -DSOURCE_DIR=${PROJECT_SOURCE_DIR} -P ${PROJECT_SOURCE_DIR}/cmake/script/GenerateBuildInfo.cmake
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.