-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
198 lines (171 loc) · 8.76 KB
/
CMakeLists.txt
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# ---------------------------------------------------------------------------- #
#
# Copyright (c) 2020 C++ Modern Framework
#
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
#
# Following variables will be set when calling GitInfo
#
# GIT_DIR: path to the project .git directory
# GIT_IS_DIRTY: whether or not the working tree is dirty
# GIT_HEAD_BRANCH : name of the branch associated te HEAD
# GIT_REVISION_HASH: current HEAD sha hash
# GIT_REVISION: shorten version of GIT_REVISION_HASH
# GIT_REVISION_NAME: name associated to GIT_REVISION_HASH
# GIT_AUTHOR_NAME : author name
# GIT_AUTHOR_EMAIL : author email
# GIT_AUTHOR_DATE : author date
# GIT_AUTHOR_DATE_ISO : author date, strict ISO 8601 format
# GIT_COMMITTER_NAME : committer name
# GIT_COMMITTER_EMAIL : committer email
# GIT_COMMITTER_DATE : committer date
# GIT_COMMITTER_DATE_ISO : committer date, strict ISO 8601 format
# GIT_REMOTE_ORIGIN_URL : origin remote url
# GIT_LATEST_TAG_LONG : most recent tag of the current branch
# GIT_LATEST_TAG : most recent tagname of the current branch
#
# ---------------------------------------------------------------------------- #
# This is the main function to call in project CMakeLists.txt
# source should point to the root project directory
function(GitInfo source)
# Check is source is a valid path
if(NOT EXISTS ${source})
message(FATAL_ERROR "'${source}' is not a valid path")
endif()
# Define the possible location of the .git directory
set(GIT_DIR "${source}/.git")
# Check if .git folder exist
if(EXISTS ${GIT_DIR})
#
set(GIT_DIR "${GIT_DIR}" CACHE PATH "Project .git directory")
# Check if git is installed
if(NOT GIT_FOUND)
find_package(Git QUIET)
endif()
if(NOT GIT_FOUND)
message(AUTHOR_WARNING "Git not found, cannot get git informations")
return()
endif()
# whether or not the working tree is dirty
execute_process(COMMAND ${GIT_EXECUTABLE} status --porcelain
WORKING_DIRECTORY ${source}
RESULT_VARIABLE exit_code
OUTPUT_VARIABLE GIT_IS_DIRTY OUTPUT_STRIP_TRAILING_WHITESPACE)
# the working tree is dirty when the error code is different from 0
# or if the output is not empty
if(NOT exit_code EQUAL 0 OR NOT ${GIT_IS_DIRTY} STREQUAL "")
unset(GIT_IS_DIRTY)
set(GIT_IS_DIRTY ON CACHE BOOL "Indicate if current branch is dirty")
else()
unset(GIT_IS_DIRTY)
set(GIT_IS_DIRTY OFF CACHE BOOL "Indicate if current branch is dirty")
endif()
# name of the brack associated te HEAD
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_HEAD_BRANCH OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_HEAD_BRANCH "${GIT_HEAD_BRANCH}" CACHE INTERNAL "name of the brack associated te HEAD")
# git revision full hash
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%H" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_REVISION_HASH OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_REVISION_HASH "${GIT_REVISION_HASH}" CACHE INTERNAL "git revision full hash")
# shorten version of git revision
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%h" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_REVISION OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_REVISION "${GIT_REVISION}" CACHE INTERNAL "shorten version of git revision")
# shorten version of git revision name
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%s" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_REVISION_NAME OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_REVISION_NAME "${GIT_REVISION_NAME}" CACHE INTERNAL "shorten version of git revision name")
# author name
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%an" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_AUTHOR_NAME OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_AUTHOR_NAME "${GIT_AUTHOR_NAME}" CACHE INTERNAL "git author name")
# author email
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%ae" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_AUTHOR_EMAIL OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_AUTHOR_EMAIL "${GIT_AUTHOR_EMAIL}" CACHE INTERNAL "git author email")
# author date
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%ad" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_AUTHOR_DATE OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_AUTHOR_DATE "${GIT_AUTHOR_DATE}" CACHE INTERNAL "git author date")
# author date, strict ISO 8601 format
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%aI" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_AUTHOR_DATE_ISO OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_AUTHOR_DATE_ISO "${GIT_AUTHOR_DATE_ISO}" CACHE INTERNAL "git author date, strict ISO 8601 format")
# committer name
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%cn" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_COMMITTER_NAME OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_COMMITTER_NAME "${GIT_COMMITTER_NAME}" CACHE INTERNAL "git committer name")
# committer email
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%ce" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_COMMITTER_EMAIL OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_COMMITTER_EMAIL "${GIT_COMMITTER_EMAIL}" CACHE INTERNAL "git committer email")
# committer date
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%cd" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_COMMITTER_DATE OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_COMMITTER_DATE "${GIT_COMMITTER_DATE}" CACHE INTERNAL "git committer date")
# committer date, strict ISO 8601 format
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%cI" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_COMMITTER_DATE_ISO OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_COMMITTER_DATE_ISO "${GIT_COMMITTER_DATE_ISO}" CACHE INTERNAL "git committer date, strict ISO 8601 format")
# origin remote url
execute_process(COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_REMOTE_ORIGIN_URL OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_REMOTE_ORIGIN_URL "${GIT_REMOTE_ORIGIN_URL}" CACHE INTERNAL "git origin remote url")
# most recent tag of the current branch
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_LATEST_TAG_LONG OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_LATEST_TAG_LONG "${GIT_LATEST_TAG_LONG}" CACHE INTERNAL "git most recent tag of the current branch")
# most recent tagname of the current branch
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_LATEST_TAG OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_LATEST_TAG "${GIT_LATEST_TAG}" CACHE INTERNAL "git most recent tagname of the current branch")
endif()
endfunction()
# Report git information
function(GitInfoReport)
message(STATUS "")
message(STATUS "----------------------------------------------------")
message(STATUS " GitInfo.cmake")
message(STATUS "")
message(STATUS "GIT_DIR : ${GIT_DIR}")
message(STATUS "")
message(STATUS "GIT_IS_DIRTY : ${GIT_IS_DIRTY}")
message(STATUS "GIT_HEAD_BRANCH : ${GIT_HEAD_BRANCH}")
message(STATUS "GIT_REVISION : ${GIT_REVISION}")
message(STATUS "GIT_REVISION_HASH : ${GIT_REVISION_HASH}")
message(STATUS "GIT_REVISION_NAME : ${GIT_REVISION_NAME}")
message(STATUS "")
message(STATUS "GIT_AUTHOR_NAME : ${GIT_AUTHOR_NAME}")
message(STATUS "GIT_AUTHOR_EMAIL : ${GIT_AUTHOR_EMAIL}")
message(STATUS "GIT_AUTHOR_DATE : ${GIT_AUTHOR_DATE}")
message(STATUS "GIT_AUTHOR_DATE_ISO : ${GIT_AUTHOR_DATE_ISO}")
message(STATUS "")
message(STATUS "GIT_COMMITTER_NAME : ${GIT_COMMITTER_NAME}")
message(STATUS "GIT_COMMITTER_EMAIL : ${GIT_COMMITTER_EMAIL}")
message(STATUS "GIT_COMMITTER_DATE : ${GIT_COMMITTER_DATE}")
message(STATUS "GIT_COMMITTER_DATE_ISO : ${GIT_COMMITTER_DATE_ISO}")
message(STATUS "")
message(STATUS "GIT_REMOTE_ORIGIN_URL : ${GIT_REMOTE_ORIGIN_URL}")
message(STATUS "GIT_LATEST_TAG_LONG : ${GIT_LATEST_TAG_LONG}")
message(STATUS "GIT_LATEST_TAG : ${GIT_LATEST_TAG}")
message(STATUS "")
message(STATUS "----------------------------------------------------")
message(STATUS "")
endfunction()