-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-version.cmake
45 lines (37 loc) · 1.01 KB
/
git-version.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
41
42
43
44
45
set(GIT_VERSION "unknown")
find_package(Git)
if(DEFINED GIT_FOUND)
# determine SHA
exec_program(${GIT_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}"
ARGS "rev-parse HEAD"
OUTPUT_VARIABLE GIT_HASH
RETURN_VALUE RET)
if(RET EQUAL 0)
string(SUBSTRING "${GIT_HASH}" 0 8 GIT_VERSION)
endif()
# determine tag, if any
exec_program(${GIT_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}"
ARGS "tag --points-at HEAD"
OUTPUT_VARIABLE GIT_TAG
RETURN_VALUE RET)
if(RET EQUAL 0)
string(LENGTH "${GIT_TAG}" LEN_TAG)
if(LEN_TAG GREATER 0)
set(GIT_VERSION "${GIT_TAG}")
endif()
endif()
# add '*' as suffix for dirty workspace
exec_program(${GIT_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}"
ARGS "status --porcelain"
OUTPUT_VARIABLE GIT_STATUS
RETURN_VALUE RET)
if(RET EQUAL 0)
string(LENGTH "${GIT_STATUS}" LEN_STATUS)
if(LEN_STATUS GREATER 0)
set(GIT_VERSION "${GIT_VERSION}*")
endif()
endif()
endif()
message(STATUS "Version: ${GIT_VERSION}")
add_definitions(-DGIT_VERSION="${GIT_VERSION}")
# vim: ts=4