Commit b85eefb4604d3ca6983bf80e7dc9d1cedde072fd

Patrick Steinhardt 2020-05-15T19:52:40

cmake: Sort source files for reproducible builds We currently use `FILE(GLOB ...)` in most places to find source and header files. This is problematic in that the order of files returned depends on the operating system's directory iteration order and may thus not be deterministic. As a result, we link object files in unspecified order, which may cause the linker to emit different code across runs. Fix this issue by sorting all code used as input to the libgit2 library to improve the reliability of reproducible builds.

diff --git a/cmake/Modules/SelectHashes.cmake b/cmake/Modules/SelectHashes.cmake
index 27059db..06672ab 100644
--- a/cmake/Modules/SelectHashes.cmake
+++ b/cmake/Modules/SelectHashes.cmake
@@ -56,4 +56,6 @@ ELSE()
 	MESSAGE(FATAL_ERROR "Asked for unknown SHA1 backend: ${USE_SHA1}")
 ENDIF()
 
+list(SORT SRC_SHA1)
+
 ADD_FEATURE_INFO(SHA ON "using ${USE_SHA1}")
diff --git a/deps/http-parser/CMakeLists.txt b/deps/http-parser/CMakeLists.txt
index 4a8bafd..b9da249 100644
--- a/deps/http-parser/CMakeLists.txt
+++ b/deps/http-parser/CMakeLists.txt
@@ -1,5 +1,6 @@
-FILE(GLOB SRC_HTTP "*.c" "*.h")
+file(GLOB SRC_HTTP "*.c" "*.h")
+list(SORT SRC_HTTP)
 
-ADD_LIBRARY(http-parser OBJECT ${SRC_HTTP})
+add_library(http-parser OBJECT ${SRC_HTTP})
 
-ENABLE_WARNINGS(implicit-fallthrough=1)
+enable_warnings(implicit-fallthrough=1)
diff --git a/deps/ntlmclient/CMakeLists.txt b/deps/ntlmclient/CMakeLists.txt
index 43d3218..5fbf0d0 100644
--- a/deps/ntlmclient/CMakeLists.txt
+++ b/deps/ntlmclient/CMakeLists.txt
@@ -1,4 +1,5 @@
 FILE(GLOB SRC_NTLMCLIENT "ntlm.c" "unicode_builtin.c" "util.c")
+LIST(SORT SRC_NTLMCLIENT)
 
 ADD_DEFINITIONS(-DNTLM_STATIC=1)
 
diff --git a/deps/zlib/CMakeLists.txt b/deps/zlib/CMakeLists.txt
index afa5a19..435877d 100644
--- a/deps/zlib/CMakeLists.txt
+++ b/deps/zlib/CMakeLists.txt
@@ -1,5 +1,6 @@
-DISABLE_WARNINGS(implicit-fallthrough)
-ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
-FILE(GLOB SRC_ZLIB "*.c" "*.h")
-INCLUDE_DIRECTORIES(".")
-ADD_LIBRARY(zlib OBJECT ${SRC_ZLIB})
+disable_warnings(implicit-fallthrough)
+add_definitions(-DNO_VIZ -DSTDC -DNO_GZIP)
+file(GLOB SRC_ZLIB "*.c" "*.h")
+list(SORT SRC_ZLIB)
+include_directories(".")
+add_library(zlib OBJECT ${SRC_ZLIB})
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index dff1d94..1fb1c53 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -76,12 +76,13 @@ ENDIF()
 ADD_FEATURE_INFO(threadsafe THREADSAFE "threadsafe support")
 
 
-IF (WIN32 AND EMBED_SSH_PATH)
-	FILE(GLOB SRC_SSH "${EMBED_SSH_PATH}/src/*.c")
-	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${EMBED_SSH_PATH}/include")
-	FILE(WRITE "${EMBED_SSH_PATH}/src/libssh2_config.h" "#define HAVE_WINCNG\n#define LIBSSH2_WINCNG\n#include \"../win32/libssh2_config.h\"")
-	SET(GIT_SSH 1)
-ENDIF()
+if(WIN32 AND EMBED_SSH_PATH)
+	file(GLOB SRC_SSH "${EMBED_SSH_PATH}/src/*.c")
+	list(SORT SRC_SSH)
+	list(APPEND LIBGIT2_SYSTEM_INCLUDES "${EMBED_SSH_PATH}/include")
+	file(WRITE "${EMBED_SSH_PATH}/src/libssh2_config.h" "#define HAVE_WINCNG\n#define LIBSSH2_WINCNG\n#include \"../win32/libssh2_config.h\"")
+	set(GIT_SSH 1)
+endif()
 
 IF (WIN32 AND WINHTTP)
 	SET(GIT_WINHTTP 1)
@@ -267,33 +268,38 @@ ENDIF()
 ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
 
 # Collect sourcefiles
-FILE(GLOB SRC_H
+file(GLOB SRC_H
 	"${libgit2_SOURCE_DIR}/include/git2.h"
 	"${libgit2_SOURCE_DIR}/include/git2/*.h"
 	"${libgit2_SOURCE_DIR}/include/git2/sys/*.h")
+list(SORT SRC_H)
 
 # On Windows use specific platform sources
-IF (WIN32 AND NOT CYGWIN)
-	IF(MSVC)
+if(WIN32 AND NOT CYGWIN)
+	if(MSVC)
 		SET(WIN_RC "win32/git2.rc")
-	ENDIF()
+	endif()
 
-	FILE(GLOB SRC_OS win32/*.c win32/*.h)
-ELSEIF (AMIGA)
-	ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R -DNO_MMAP)
-ELSE()
-	FILE(GLOB SRC_OS unix/*.c unix/*.h)
-ENDIF()
+	file(GLOB SRC_OS win32/*.c win32/*.h)
+	list(SORT SRC_OS)
+elseif(AMIGA)
+	add_definitions(-DNO_ADDRINFO -DNO_READDIR_R -DNO_MMAP)
+else()
+	file(GLOB SRC_OS unix/*.c unix/*.h)
+	list(SORT SRC_OS)
+endif()
 
 IF (USE_LEAK_CHECKER STREQUAL "valgrind")
 	ADD_DEFINITIONS(-DVALGRIND)
 ENDIF()
 
-FILE(GLOB SRC_GIT2 *.c *.h
+file(GLOB SRC_GIT2 *.c *.h
 	allocators/*.c allocators/*.h
 	streams/*.c streams/*.h
 	transports/*.c transports/*.h
 	xdiff/*.c xdiff/*.h)
+list(SORT SRC_GIT2)
+
 IF(APPLE)
 	# The old Secure Transport API has been deprecated in macOS 10.15.
 	SET_SOURCE_FILES_PROPERTIES(streams/stransport.c PROPERTIES COMPILE_FLAGS -Wno-deprecated)