@@ -51,4 +51,54 @@ if(EXISTS ${AUTOCONF_PROJ_CONFIG_H})
5151 "before CMake's build." )
5252endif ()
5353
54+ # Get the git hash to write it in the release message.
55+ set (PROJ_GIT_HASH "" )
56+ if (DEFINED ENV{PROJ_GITHUB_SHA})
57+ string (SUBSTRING "$ENV{PROJ_GITHUB_SHA} " 0 7 PROJ_GIT_HASH)
58+ else ()
59+ find_package (Git QUIET )
60+ if (Git_FOUND AND EXISTS "${PROJ_SOURCE_DIR } /.git" )
61+ # Get the short hash
62+ execute_process (
63+ COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
64+ WORKING_DIRECTORY "${PROJ_SOURCE_DIR } "
65+ RESULT_VARIABLE GIT_REV_RESULT
66+ OUTPUT_VARIABLE PROJ_GIT_HASH
67+ OUTPUT_STRIP_TRAILING_WHITESPACE
68+ ERROR_QUIET
69+ )
70+
71+ if (GIT_REV_RESULT EQUAL 0)
72+ # Check if the working directory is dirty
73+ execute_process (
74+ COMMAND ${GIT_EXECUTABLE} status --porcelain --untracked-files=no
75+ WORKING_DIRECTORY "${PROJ_SOURCE_DIR } "
76+ RESULT_VARIABLE GIT_STATUS_RESULT
77+ OUTPUT_VARIABLE GIT_STATUS_OUTPUT
78+ OUTPUT_STRIP_TRAILING_WHITESPACE
79+ ERROR_QUIET
80+ )
81+
82+ # If the command succeeds and outputs anything, the tree is dirty
83+ if (GIT_STATUS_RESULT EQUAL 0 AND NOT GIT_STATUS_OUTPUT STREQUAL "" )
84+ string (APPEND PROJ_GIT_HASH "*" )
85+ endif ()
86+ else ()
87+ set (PROJ_GIT_HASH "" )
88+ endif ()
89+ endif ()
90+ endif ()
91+ if (PROJ_GIT_HASH)
92+ add_compile_definitions (PROJ_GIT_HASH= \"${PROJ_GIT_HASH} \" )
93+ endif()
94+ message(PROJ_GIT_HASH=\" ${PROJ_GIT_HASH} \" )
95+
96+ # Tell CMake to re-configure if the Git HEAD or index changes
97+ if(Git_FOUND AND EXISTS " ${PROJ_SOURCE_DIR } /.git ")
98+ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
99+ " ${PROJ_SOURCE_DIR } /.git/HEAD " # for the hash
100+ " ${PROJ_SOURCE_DIR } /.git/index " # for the *
101+ )
102+ endif()
103+
54104configure_file(cmake/proj_config.cmake.in src/proj_config.h)
0 commit comments