Build: add ARM and aarch64 target architectures
[marnav.git] / cmake / GitInformation.cmake
blob7d9ca73af77ccb66acc098de8a8886cdc6236dba
1 find_package(Git)
2 if (GIT_FOUND)
4         # checks the current source directory for the presence of
5         # a git repository.
6         #
7         # usage:
8         #
9         #    git_check_repository(GIT_REPO_FOUND)
10         #
11         function(git_check_repository found)
12                 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
13                         set(${found} TRUE  PARENT_SCOPE)
14                 else()
15                         set(${found} FALSE PARENT_SCOPE)
16                 endif()
17         endfunction()
19         # get commit hash of HEAD
20         #
21         # This function assumes to have a git repository present.
22         #
23         # usage:
24         #
25         #    git_commit_hash(GIT_HASH)
26         #
27         # an optional second argument is supported, which returns
28         # the short version of the hash:
29         #
30         #    git_commit_hash(GIT_HASH GIT_HASH_SHORT)
31         #
32         function(git_commit_hash hash)
33                 if(${ARGC} GREATER 2)
34                         message(FATAL "too many arugments for `git_commit_hash`")
35                         return()
36                 endif()
38                 execute_process(
39                         COMMAND ${GIT_EXECUTABLE} log -1 --format=format:%H HEAD
40                         WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
41                         OUTPUT_VARIABLE commit_hash
42                         OUTPUT_STRIP_TRAILING_WHITESPACE
43                         )
44                 set(${hash} ${commit_hash} PARENT_SCOPE)
46                 if(${ARGC} GREATER 1)
47                         string(SUBSTRING "${commit_hash}" 0 7 commit_hash_short)
48                         set(${ARGV1} ${commit_hash_short} PARENT_SCOPE)
49                 endif()
50         endfunction()
52         # get branch name
53         #
54         # This function assumes to have a git repository present.
55         #
56         # usage:
57         #
58         #    git_branch_name(GIT_BRANCH)
59         #
60         function(git_branch_name branch_name)
61                 execute_process(
62                         COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
63                         WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
64                         OUTPUT_VARIABLE branch
65                         OUTPUT_STRIP_TRAILING_WHITESPACE
66                         )
67                 set(${branch_name} ${branch} PARENT_SCOPE)
68         endfunction()
70         # generate list of authors
71         #
72         # This function assumes to have a git repository present.
73         #
74         # usage:
75         #
76         #    git_authtors(GIT_AUTHOS)
77         #
78         # function is able to return a nice bullet point list also.
79         # pass a second argument to the function:
80         #
81         #    git_authors(AUTHORS AUTHORS_LIST)
82         #
83         function(git_authors authors)
84                 if(${ARGC} GREATER 2)
85                         message(FATAL "too many arugments for `git_authors`")
86                 endif()
88                 execute_process(
89                         COMMAND ${GIT_EXECUTABLE} log --format=%an
90                         WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
91                         OUTPUT_VARIABLE GIT_AUTHORS
92                         OUTPUT_STRIP_TRAILING_WHITESPACE
93                         )
94                 string(REPLACE "\n" ";" GIT_AUTHORS "${GIT_AUTHORS}")
95                 list(SORT GIT_AUTHORS)
96                 list(REMOVE_DUPLICATES GIT_AUTHORS)
97                 set(${authors} ${GIT_AUTHORS} PARENT_SCOPE)
99                 if(${ARGC} GREATER 1)
100                         set(GIT_AUTHORS_LIST "")
101                         foreach(AUTHOR ${GIT_AUTHORS})
102                                 string(CONCAT GIT_AUTHORS_LIST ${GIT_AUTHORS_LIST} "- ${AUTHOR}\n")
103                         endforeach(AUTHOR)
104                         set(${ARGV1} ${GIT_AUTHORS_LIST} PARENT_SCOPE)
105                 endif()
106         endfunction()
108         # returns the tag that points at HEAD, empty if none
109         #
110         # usage:
111         #
112         #    git_head_tag(TAG)
113         #
114         function(git_head_tag tag)
115                 execute_process(
116                         COMMAND ${GIT_EXECUTABLE} tag --points-at HEAD
117                         WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
118                         OUTPUT_VARIABLE head_tag
119                         OUTPUT_STRIP_TRAILING_WHITESPACE
120                         )
121                 set(${tag} ${head_tag} PARENT_SCOPE)
122         endfunction()
123 endif()