forked from domoticz/domoticz
-
Notifications
You must be signed in to change notification settings - Fork 1
/
getgit.cmake
75 lines (65 loc) · 3.01 KB
/
getgit.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# this macro gets called as a custom build step by running make
# please take into account, that the variable 'SOURCE_DIR' has been defined by the caller
# the git.cmake module is part of the standard distribution
find_package(Git)
if(NOT GIT_FOUND)
MESSAGE(FATAL_ERROR "Git not found!.")
endif()
MACRO(Gitversion_GET_REVISION dir variable)
EXECUTE_PROCESS(COMMAND ${GIT_EXECUTABLE} --git-dir ./.git rev-list HEAD --count
WORKING_DIRECTORY ${dir}
OUTPUT_VARIABLE ${variable}
OUTPUT_STRIP_TRAILING_WHITESPACE)
ENDMACRO(Gitversion_GET_REVISION)
MACRO(Gitversion_GET_HASH dir variable)
EXECUTE_PROCESS(COMMAND ${GIT_EXECUTABLE} --git-dir ./.git rev-parse --short HEAD
WORKING_DIRECTORY ${dir}
OUTPUT_VARIABLE ${variable}
OUTPUT_STRIP_TRAILING_WHITESPACE)
ENDMACRO(Gitversion_GET_HASH)
MACRO(Gitversion_GET_DATE dir variable)
EXECUTE_PROCESS(COMMAND ${GIT_EXECUTABLE} --git-dir ./.git show -s --format=%ct
WORKING_DIRECTORY ${dir}
OUTPUT_VARIABLE ${variable}
OUTPUT_STRIP_TRAILING_WHITESPACE)
ENDMACRO(Gitversion_GET_DATE)
MACRO(Gitversion_CHECK_DIRTY dir variable)
EXECUTE_PROCESS(COMMAND ${GIT_EXECUTABLE} --git-dir ./.git diff-index -m --name-only HEAD
WORKING_DIRECTORY ${dir}
OUTPUT_VARIABLE ${variable}
OUTPUT_STRIP_TRAILING_WHITESPACE)
ENDMACRO(Gitversion_CHECK_DIRTY)
Gitversion_GET_REVISION("${SOURCE_DIR}" ProjectRevision)
IF(NOT ProjectRevision)
MESSAGE(STATUS "Failed to get ProjectRevision from git, set it to 0")
set (ProjectRevision 0)
ELSE(NOT ProjectRevision)
MATH(EXPR ProjectRevision "${ProjectRevision}+2107")
ENDIF(NOT ProjectRevision)
Gitversion_GET_HASH("${SOURCE_DIR}" ProjectHash)
IF(NOT ProjectHash)
MESSAGE(STATUS "Failed to get ProjectHash from git, set it to 0")
set (ProjectHash 0)
ENDIF(NOT ProjectHash)
Gitversion_GET_DATE("${SOURCE_DIR}" ProjectDate)
IF(NOT ProjectDate)
MESSAGE(STATUS "Failed to get ProjectDate from git, set it to 0")
set (ProjectDate 0)
ENDIF(NOT ProjectDate)
Gitversion_CHECK_DIRTY("${SOURCE_DIR}" ProjectDirty)
IF(ProjectDirty)
MESSAGE(STATUS "domoticz has been modified locally: adding \"-modified\" to hash")
set (ProjectHash "${ProjectHash}-modified")
ENDIF(ProjectDirty)
# write a file with the APPVERSION define
file(WRITE ${SOURCE_DIR}/appversion.h.txt "#define APPVERSION ${ProjectRevision}\n#define APPHASH \"${ProjectHash}\"\n#define APPDATE ${ProjectDate}\n")
# if ProjectDate is 0, create appversion.h.txt from a copy of appversion.default
IF(NOT ProjectDate AND EXISTS ${SOURCE_DIR}/appversion.default)
MESSAGE(STATUS "ProjectDate is 0 and appversion.default exists, copy it")
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${SOURCE_DIR}/appversion.default ${SOURCE_DIR}/appversion.h.txt)
ENDIF(NOT ProjectDate AND EXISTS ${SOURCE_DIR}/appversion.default)
# copy the file to the final header only if the version changes
# reduces needless rebuilds
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${SOURCE_DIR}/appversion.h.txt ${SOURCE_DIR}/appversion.h)