This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpile_git.cmake
180 lines (151 loc) · 6.55 KB
/
pile_git.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
# ============================================================================
#
# Git Helpers
# ===========
#
# This file contains support CMake script for working with git.
#
# Usage
# -----
#
# The file is included by pile_support.cmake, so all the
# user has to do to use it is to:
#
# include(pile_support)
#
# ============================================================================
find_package(Git)
# ============================================================================
# Get git commit and branch.
#
# Arguments
# - path: full path to the file to generate
# - commit (output variable): resulted commit
# - branch (output variable): resulted branch
#
# An error is generated if the path is not part of a git repository.
#
macro (pileGitStamp
pile_git_stamp__path
pile_git_stamp__commit
pile_git_stamp__branch)
set(pile_git_stamp__describe_build )
set(pile_git_stamp__err )
if(GIT_FOUND)
unset(pile_git_stamp__err)
unset(pile_git_stamp__temp)
execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --pretty=%H
WORKING_DIRECTORY ${pile_git_stamp__path}
OUTPUT_VARIABLE pile_git_stamp__temp
ERROR_VARIABLE pile_git_stamp__err
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (pile_git_stamp__err)
message(WARNING "Error extracting git commit from ${pile_git_stamp__path}: ${pile_git_stamp__err}")
set(${pile_git_stamp__commit} "error")
else()
STRING(REGEX REPLACE "\n" ";" pile_git_stamp__temp "${pile_git_stamp__temp}")
STRING(REGEX REPLACE "\r" ";" pile_git_stamp__temp "${pile_git_stamp__temp}")
list(GET pile_git_stamp__temp -1 pile_git_stamp__temp)
string(STRIP "${pile_git_stamp__temp}" ${pile_git_stamp__commit})
endif()
unset(pile_git_stamp__temp)
execute_process(
COMMAND ${GIT_EXECUTABLE} symbolic-ref --short HEAD
WORKING_DIRECTORY ${pile_git_stamp__path}
OUTPUT_VARIABLE pile_git_stamp__temp
ERROR_VARIABLE pile_git_stamp__err
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (pile_git_stamp__err)
message(WARNING "Error extracting git branch: ${pile_git_stamp__err}")
set(${pile_git_stamp__branch} "error")
else()
STRING(REGEX REPLACE "\n" ";" pile_git_stamp__temp "${pile_git_stamp__temp}")
STRING(REGEX REPLACE "\r" ";" pile_git_stamp__temp "${pile_git_stamp__temp}")
list(GET pile_git_stamp__temp -1 pile_git_stamp__temp)
string(STRIP "${pile_git_stamp__temp}" ${pile_git_stamp__branch})
endif()
else()
set(${pile_git_stamp__commit} "git not found")
set(${pile_git_stamp__branch} "git not found")
endif()
endmacro ()
# ============================================================================
# Generates a C header file containing git commit and branch as defines.
#
# Arguments
# - path: full path to the git repository
# - out: full path to the file to generate
# - header (optional): text to add before the definition
# - footer (optional): text to add after the definition
#
macro (pileGitStampHeader
pile_git_stamp_header__path
pile_git_stamp_header__out
pile_git_stamp_header__header
pile_git_stamp_header__footer)
# get the info from git
unset (pile_git_stamp_header__branch )
unset (pile_git_stamp_header__commit )
pileGitStamp(${pile_git_stamp_header__path}
pile_git_stamp_header__commit
pile_git_stamp_header__branch)
file(WRITE "${pile_git_stamp_header__out}"
"${pile_git_stamp_header__header}
#ifndef SOURCE_CODE_GIT_HASH
# define SOURCE_CODE_GIT_HASH \"${pile_git_stamp_header__commit}\"
#endif // SOURCE_CODE_GIT_HASH
#ifndef SOURCE_CODE_GIT_BRANCH
# define SOURCE_CODE_GIT_BRANCH ${pile_git_stamp_header__branch}
#endif // SOURCE_CODE_GIT_BRANCH
${pile_git_stamp_header__footer}
")
endmacro ()
# ============================================================================
# Generates a C header file containing git commit and branch as defines.
#
# Arguments
# - path: full path to the git repository
# - out: full path to the file to generate
# - header (optional): text to add before the definition
# - footer (optional): text to add after the definition
#
# If the file already exists, its content is inspected to see if an update is
# required.
macro (pileGitStampHeaderCheck
pile_git_stamp_header_check__path
pile_git_stamp_header_check__out
pile_git_stamp_header_check__header
pile_git_stamp_header_check__footer
pile_git_stamp_header_check__prefix)
unset (pile_git_stamp_header_check__branch )
unset (pile_git_stamp_header_check__commit )
pileGitStamp(${pile_git_stamp_header_check__path}
pile_git_stamp_header_check__commit
pile_git_stamp_header_check__branch)
set (pile_git_stamp_header_check__intermed
"${CMAKE_CURRENT_BINARY_DIR}/git_info.h.in")
if(NOT EXISTS ${pile_git_stamp_header_check__intermed})
file(WRITE "${pile_git_stamp_header_check__intermed}"
"${pile_git_stamp_header_check__header}
#ifndef ${pile_git_stamp_header_check__prefix}_SOURCE_CODE_GIT_HASH
# define ${pile_git_stamp_header_check__prefix}_SOURCE_CODE_GIT_HASH \"@${pile_git_stamp_header_check__prefix}_SOURCE_CODE_GIT_HASH@\"
#endif // ${pile_git_stamp_header_check__prefix}_SOURCE_CODE_GIT_HASH
#ifndef ${pile_git_stamp_header_check__prefix}_SOURCE_CODE_GIT_BRANCH
# define ${pile_git_stamp_header_check__prefix}_SOURCE_CODE_GIT_BRANCH @${pile_git_stamp_header_check__prefix}_SOURCE_CODE_GIT_BRANCH@
#endif // ${pile_git_stamp_header_check__prefix}_SOURCE_CODE_GIT_BRANCH
${pile_git_stamp_header_check__footer}
")
endif()
set (${pile_git_stamp_header_check__prefix}_SOURCE_CODE_GIT_HASH "${pile_git_stamp_header_check__commit}"
CACHE STRING "Git hash of source code for ${pile_git_stamp_header_check__prefix}" FORCE)
set (${pile_git_stamp_header_check__prefix}_SOURCE_CODE_GIT_BRANCH "${pile_git_stamp_header_check__branch}"
CACHE STRING "Git branch of source code for ${pile_git_stamp_header_check__prefix}" FORCE)
unset(pile_git_stamp_header_check__commit)
unset(pile_git_stamp_header_check__branch)
configure_file(
"${pile_git_stamp_header_check__intermed}"
"${pile_git_stamp_header_check__out}"
@ONLY)
endmacro ()
# ============================================================================