-
Notifications
You must be signed in to change notification settings - Fork 4
/
getgitversion.cmake
40 lines (38 loc) · 1.79 KB
/
getgitversion.cmake
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
FIND_PACKAGE(Git)
IF(GIT_FOUND)
EXECUTE_PROCESS(COMMAND ${GIT_EXECUTABLE} describe --match version-* WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} RESULT_VARIABLE res_var OUTPUT_VARIABLE GIT_COM_ID OUTPUT_STRIP_TRAILING_WHITESPACE)
if( ${res_var} EQUAL 0 )
string(REGEX MATCHALL "[^-]+|-|[^-]+$" ID_PARTS ${GIT_COM_ID})
LIST(GET ID_PARTS 2 VERSION_ID )
string(REGEX MATCHALL "[^.]+|.|[^.]+$" VERSION_PARTS ${VERSION_ID})
LIST(GET VERSION_PARTS 0 VERSION_MAJOR)
LIST(GET VERSION_PARTS 2 VERSION_MINOR)
LIST(LENGTH VERSION_PARTS PART_COUNT )
IF( ${PART_COUNT} LESS 5 )
SET( VERSION_BUILD 0 )
ELSE()
LIST( GET VERSION_PARTS 4 VERSION_BUILD )
ENDIF()
STRING(REGEX REPLACE "\r|\n|\r\n" "" VERSION_MINOR ${VERSION_MINOR})
LIST(LENGTH ID_PARTS PART_COUNT )
IF( ${PART_COUNT} GREATER_EQUAL 5 )
LIST( GET ID_PARTS 4 VERSION_PATCH )
string(APPEND VERSION_BUILD "-${VERSION_PATCH}")
ENDIF()
message( STATUS "VERSION: Major: ${VERSION_MAJOR} Minor: ${VERSION_MINOR} Patch: ${VERSION_BUILD}")
set( GIT_COMMIT_ID "GT")
set( vstring "# Autogenerated file, do not change!\n"
"SET( VERSION_MAJOR \"${VERSION_MAJOR}\")\n"
"SET( VERSION_MINOR \"${VERSION_MINOR}\")\n"
"SET( VERSION_BUILD \"${VERSION_BUILD}\")\n")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/version.cmake.txt ${vstring} )
# copy the file to the final header only if the version changes
# reduces needless rebuilds
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_BINARY_DIR}/version.cmake.txt ${CMAKE_CURRENT_BINARY_DIR}/version.cmake)
else()
message( WARNING "Git failed (not a repo, or no tags). Build will not contain git revision info." )
endif()
else()
message( WARNING "Git not found. Build will not contain git revision info." )
endif()