forked from LLNL/blt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLTGitMacros.cmake
250 lines (203 loc) · 7.65 KB
/
BLTGitMacros.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Copyright (c) 2017-2021, Lawrence Livermore National Security, LLC and
# other BLT Project Developers. See the top-level LICENSE file for details
#
# SPDX-License-Identifier: (BSD-3-Clause)
##------------------------------------------------------------------------------
## blt_git( SOURCE_DIR <dir>
## GIT_COMMAND <command>
## OUTPUT_VARIABLE <out>
## RETURN_CODE <rc>
## [QUIET] )
##
## Runs the supplied git command on the given Git repository.
##------------------------------------------------------------------------------
macro(blt_git)
set(options)
set(singleValueArgs SOURCE_DIR OUTPUT_VARIABLE RETURN_CODE)
set(multiValueArgs GIT_COMMAND )
## parse macro arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
## ensure required arguments are supplied
if ( NOT DEFINED arg_SOURCE_DIR )
message(FATAL_ERROR "SOURCE_DIR is a required argument to blt_git()")
endif()
if ( NOT DEFINED arg_GIT_COMMAND )
message(FATAL_ERROR "GIT_COMMAND is a required argument to blt_git()")
endif()
if ( NOT DEFINED arg_OUTPUT_VARIABLE )
message(FATAL_ERROR "OUTPUT_VARIABLE is a required argument to blt_git()")
endif()
if ( NOT DEFINED arg_RETURN_CODE )
message(FATAL_ERROR "RETURN_CODE is a required argument to blt_git()")
endif()
## check arguments
if (GIT_FOUND)
## assemble the Git command
set(git_cmd "${GIT_EXECUTABLE}" "${arg_GIT_COMMAND}" )
## run it
execute_process( COMMAND
${git_cmd}
WORKING_DIRECTORY
"${arg_SOURCE_DIR}"
RESULT_VARIABLE
${arg_RETURN_CODE}
OUTPUT_VARIABLE
${arg_OUTPUT_VARIABLE}
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE
)
else( )
message( FATAL_ERROR "Git is not found. Git is required for blt_git()")
endif()
endmacro(blt_git)
##------------------------------------------------------------------------------
## blt_is_git_repo( OUTPUT_STATE <state>
## [SOURCE_DIR <dir>] )
##
## Checks if we are working with a valid Git repository.
##------------------------------------------------------------------------------
macro(blt_is_git_repo)
set(options)
set(singleValueArgs OUTPUT_STATE SOURCE_DIR )
set(multiValueArgs)
## parse macro arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN})
## ensure required variables are supplied
if ( NOT DEFINED arg_OUTPUT_STATE )
message(FATAL_ERROR "OUTPUT_STATE is a required argument to blt_is_git_repo")
endif()
## check if SOURCE_DIR was supplied
if ( NOT DEFINED arg_SOURCE_DIR )
set(git_dir ${CMAKE_CURRENT_SOURCE_DIR})
else()
set(git_dir ${arg_SOURCE_DIR})
endif()
blt_git( SOURCE_DIR ${git_dir}
GIT_COMMAND rev-parse --show-toplevel
OUTPUT_VARIABLE tmp
RETURN_CODE rc
)
if ( NOT ${rc} EQUAL 0 )
## rev-parse failed, this is not a git repo
set( ${arg_OUTPUT_STATE} FALSE )
else()
set( ${arg_OUTPUT_STATE} TRUE )
endif()
endmacro(blt_is_git_repo)
##------------------------------------------------------------------------------
## blt_git_tag( OUTPUT_TAG <tag>
## RETURN_CODE <rc>
## [SOURCE_DIR <dir>]
## [ON_BRANCH <branch>] )
##
## Returns the latest tag on a corresponding Git repository.
##------------------------------------------------------------------------------
macro(blt_git_tag)
set(options)
set(singleValueArgs SOURCE_DIR ON_BRANCH OUTPUT_TAG RETURN_CODE )
set(multiValueArgs)
## parse macro arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN})
## ensure required arguments are supplied
if ( NOT DEFINED arg_OUTPUT_TAG )
message(FATAL_ERROR "OUTPUT_TAG is a required argument to blt_git_tag")
endif()
if ( NOT DEFINED arg_RETURN_CODE )
message(FATAL_ERROR "RETURN_CODE is a required argument to blt_git_tag")
endif()
## git command to execute
if ( NOT DEFINED arg_ON_BRANCH )
set(git_cmd describe --tags )
else()
set(git_cmd describe --tags ${arg_ON_BRANCH} )
endif()
## set working directory
if ( NOT DEFINED arg_SOURCE_DIR )
set(git_dir ${CMAKE_CURRENT_SOURCE_DIR})
else()
set(git_dir ${arg_SOURCE_DIR})
endif()
blt_git( SOURCE_DIR ${git_dir}
GIT_COMMAND ${git_cmd}
OUTPUT_VARIABLE ${arg_OUTPUT_TAG}
RETURN_CODE ${arg_RETURN_CODE}
)
endmacro(blt_git_tag)
##------------------------------------------------------------------------------
## blt_git_branch( BRANCH_NAME <branch>
## RETURN_CODE <rc>
## [SOURCE_DIR <dir>] )
##
## Returns the name of the active branch in the checkout space.
##------------------------------------------------------------------------------
macro(blt_git_branch)
set(options)
set(singleValueArgs SOURCE_DIR BRANCH_NAME RETURN_CODE)
set(multiValueArgs)
## parse macro arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN})
## ensure required arguments are supplied
if ( NOT DEFINED arg_BRANCH_NAME )
message(FATAL_ERROR "BRANCH_NAME is a required argument to blt_git_branch" )
endif()
if ( NOT DEFINED arg_RETURN_CODE )
message(FATAL_ERROR "RETURN_CODE is a required argument to blt_git_branch")
endif()
## set set working directory
if ( NOT DEFINED arg_SOURCE_DIR )
set(git_dir ${CMAKE_CURRENT_SOURCE_DIR})
else()
set(git_dir ${arg_SOURCE_DIR})
endif()
blt_git( SOURCE_DIR ${git_dir}
GIT_COMMAND rev-parse --abbrev-ref HEAD
OUTPUT_VARIABLE ${arg_BRANCH_NAME}
RETURN_CODE ${arg_RETURN_CODE}
)
endmacro(blt_git_branch)
##------------------------------------------------------------------------------
## blt_git_hashcode( HASHCODE <hc>
## RETURN_CODE <rc>
## [SOURCE_DIR <dir>]
## [ON_BRANCH <branch>]
## )
##
## Returns the SHA-1 hashcode at the tip of a branch.
##------------------------------------------------------------------------------
macro(blt_git_hashcode)
set(options)
set(singleValueArgs SOURCE_DIR HASHCODE ON_BRANCH RETURN_CODE)
set(multiValueArgs)
## parse macro arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
## ensure required arguments are supplied
if ( NOT DEFINED arg_HASHCODE )
message(FATAL_ERROR "HASHCODE is a required argument to blt_git_hashcode" )
endif()
if ( NOT DEFINED arg_RETURN_CODE )
message(FATAL_ERROR "RETURN_CODE is a required argument to blt_git_hashcode" )
endif()
## set working directory
if ( NOT DEFINED arg_SOURCE_DIR )
set(git_dir ${CMAKE_CURRENT_SOURCE_DIR})
else()
set(git_dir ${arg_SOURCE_DIR})
endif()
## set target ref
if ( NOT DEFINED arg_ON_BRANCH )
set(git_cmd rev-parse --short HEAD )
else()
set(git_cmd rev-parse --short ${arg_ON_BRANCH} )
endif()
blt_git( SOURCE_DIR ${git_dir}
GIT_COMMAND ${git_cmd}
OUTPUT_VARIABLE ${arg_HASHCODE}
RETURN_CODE ${arg_RETURN_CODE}
)
endmacro(blt_git_hashcode)