cmake: Retrieve the git revision on platforms without bash
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
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 78aa3ab..01e2c54 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2378,11 +2378,34 @@ listtostr(EXTRA_CFLAGS _EXTRA_CFLAGS)
set(EXTRA_CFLAGS ${_EXTRA_CFLAGS})
# Compat helpers for the configuration files
-if(NOT CMAKE_HOST_WIN32)
- # TODO: we need a Windows script, too
- execute_process(COMMAND sh ${SDL2_SOURCE_DIR}/build-scripts/updaterev.sh
- WORKING_DIRECTORY ${SDL2_BINARY_DIR})
+find_package(Git)
+if(Git_FOUND)
+ execute_process(COMMAND
+ "${GIT_EXECUTABLE}" remote get-url origin
+ WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
+ RESULT_VARIABLE GIT_URL_STATUS
+ OUTPUT_VARIABLE GIT_URL
+ ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+ execute_process(COMMAND
+ "${GIT_EXECUTABLE}" rev-list --max-count=1 HEAD~..
+ WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
+ RESULT_VARIABLE GIT_REVISION_STATUS
+ OUTPUT_VARIABLE GIT_REVISION
+ ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+ if(GIT_URL_STATUS EQUAL 0 OR GIT_REVISION_STATUS EQUAL 0)
+ set(SDL_REVISION "${GIT_URL}@${GIT_REVISION}")
+ else()
+ set(SDL_REVISION "")
+ endif()
+else()
+ set(SDL_REVISION "")
endif()
+
+configure_file("${SDL2_SOURCE_DIR}/include/SDL_revision.h.cmake"
+ "${SDL2_BINARY_DIR}/include/SDL_revision.h")
+
if(NOT WINDOWS OR CYGWIN OR MINGW)
set(prefix ${CMAKE_INSTALL_PREFIX})
@@ -2513,6 +2536,7 @@ message(STATUS "")
message(STATUS "Platform: ${CMAKE_SYSTEM}")
message(STATUS "64-bit: ${ARCH_64}")
message(STATUS "Compiler: ${CMAKE_C_COMPILER}")
+message(STATUS "Revision: ${SDL_REVISION}")
message(STATUS "")
message(STATUS "Subsystems:")
foreach(_SUB ${SDL_SUBSYSTEMS})
diff --git a/include/SDL_revision.h.cmake b/include/SDL_revision.h.cmake
new file mode 100644
index 0000000..b27ec11
--- /dev/null
+++ b/include/SDL_revision.h.cmake
@@ -0,0 +1,6 @@
+#cmakedefine SDL_REVISION "@SDL_REVISION@"
+#define SDL_REVISION_NUMBER 0
+
+#ifndef SDL_REVISION
+#define SDL_REVISION ""
+#endif