-
Notifications
You must be signed in to change notification settings - Fork 18
/
git_info.cmake
74 lines (63 loc) · 2.08 KB
/
git_info.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
# (c) https://github.com/dev-cafe/autocmake/blob/master/AUTHORS.md
# licensed under BSD-3: https://github.com/dev-cafe/autocmake/blob/master/LICENSE
#.rst:
#
# Creates git_info.h in the build directory.
# This file can be included in sources to print
# Git repository version and status information
# to the program output.
#
# autocmake.yml configuration::
#
# url_root: https://github.com/dev-cafe/autocmake/raw/master/
# fetch:
# - "%(url_root)modules/git_info/git_info.h.in"
get_filename_component(_current_dir ${CMAKE_CURRENT_LIST_FILE} PATH)
function(generate_git_info_header _header_location _header_name)
# _header_location: where the Git info header file should be generated
# _header_name: the Git info header name, complete with extension (.h, .hpp, .hxx or whatever)
find_package(Git)
set(_git_last_commit_hash "unknown")
set(_git_last_commit_author "unknown")
set(_git_last_commit_date "unknown")
set(_git_branch "unknown")
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%h -n 1
OUTPUT_VARIABLE _git_last_commit_hash
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
execute_process(
COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%aN -n 1
OUTPUT_VARIABLE _git_last_commit_author
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
execute_process(
COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%ad -n 1
OUTPUT_VARIABLE _git_last_commit_date
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
OUTPUT_VARIABLE _git_branch
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
endif()
configure_file(
${_current_dir}/git_info.h.in
${_header_location}/${_header_name}
@ONLY
)
unset(_git_last_commit_hash)
unset(_git_last_commit_author)
unset(_git_last_commit_date)
unset(_git_branch)
add_custom_target(
git_info
ALL DEPENDS ${_header_location}/${_header_name}
)
endfunction()