Cleanup
[inav.git] / cmake / GetGitRevisionDescription.cmake
blob7b2c705ed540b9f1e806eb9d0cb8265d17df6e73
1 # - Returns a version string from Git
3 # These functions force a re-configure on each git commit so that you can
4 # trust the values of the variables in your build system.
6 #  get_git_head_revision(<refspecvar> <hashvar> [<additional arguments to git describe> ...])
8 # Returns the refspec and sha hash of the current head revision
10 #  git_describe(<var> [<additional arguments to git describe> ...])
12 # Returns the results of git describe on the source tree, and adjusting
13 # the output so that it tests false if an error occurs.
15 #  git_get_exact_tag(<var> [<additional arguments to git describe> ...])
17 # Returns the results of git describe --exact-match on the source tree,
18 # and adjusting the output so that it tests false if there was no exact
19 # matching tag.
21 #  git_local_changes(<var>)
23 # Returns either "CLEAN" or "DIRTY" with respect to uncommitted changes.
24 # Uses the return code of "git diff-index --quiet HEAD --".
25 # Does not regard untracked files.
27 # Requires CMake 2.6 or newer (uses the 'function' command)
29 # Original Author:
30 # 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
31 # http://academic.cleardefinition.com
32 # Iowa State University HCI Graduate Program/VRAC
34 # Copyright Iowa State University 2009-2010.
35 # Distributed under the Boost Software License, Version 1.0.
36 # (See accompanying file LICENSE_1_0.txt or copy at
37 # http://www.boost.org/LICENSE_1_0.txt)
39 if(__get_git_revision_description)
40         return()
41 endif()
42 set(__get_git_revision_description YES)
44 # We must run the following at "include" time, not at function call time,
45 # to find the path to this module rather than the path to a calling list file
46 get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
48 function(get_git_head_revision _refspecvar _hashvar)
49         set(GIT_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
50         set(GIT_DIR "${GIT_PARENT_DIR}/.git")
51         while(NOT EXISTS "${GIT_DIR}")  # .git dir not found, search parent directories
52                 set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
53                 get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
54                 if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
55                         # We have reached the root directory, we are not in git
56                         set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
57                         set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
58                         return()
59                 endif()
60                 set(GIT_DIR "${GIT_PARENT_DIR}/.git")
61         endwhile()
62         # check if this is a submodule
63         if(NOT IS_DIRECTORY ${GIT_DIR})
64                 file(READ ${GIT_DIR} submodule)
65                 string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
66                 get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
67                 get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
68         endif()
69         if(NOT IS_DIRECTORY "${GIT_DIR}")
70                 file(READ ${GIT_DIR} worktree)
71                 string(REGEX REPLACE "gitdir: (.*)worktrees(.*)\n$" "\\1" GIT_DIR ${worktree})
72         endif()
73         set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
74         if(NOT EXISTS "${GIT_DATA}")
75                 file(MAKE_DIRECTORY "${GIT_DATA}")
76         endif()
78         if(NOT EXISTS "${GIT_DIR}/HEAD")
79                 return()
80         endif()
81         set(HEAD_FILE "${GIT_DATA}/HEAD")
82         configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
84         configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
85                 "${GIT_DATA}/grabRef.cmake"
86                 @ONLY)
87         include("${GIT_DATA}/grabRef.cmake")
89         set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
90         set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
91 endfunction()
93 function(git_describe _var)
94         if(NOT GIT_FOUND)
95                 find_package(Git QUIET)
96         endif()
97         get_git_head_revision(refspec hash)
98         if(NOT GIT_FOUND)
99                 set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
100                 return()
101         endif()
102         if(NOT hash)
103                 set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
104                 return()
105         endif()
107         # TODO sanitize
108         #if((${ARGN}" MATCHES "&&") OR
109         #       (ARGN MATCHES "||") OR
110         #       (ARGN MATCHES "\\;"))
111         #       message("Please report the following error to the project!")
112         #       message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
113         #endif()
115         #message(STATUS "Arguments to execute_process: ${ARGN}")
117         execute_process(COMMAND
118                 "${GIT_EXECUTABLE}"
119                 describe
120                 ${hash}
121                 ${ARGN}
122                 WORKING_DIRECTORY
123                 "${CMAKE_CURRENT_SOURCE_DIR}"
124                 RESULT_VARIABLE
125                 res
126                 OUTPUT_VARIABLE
127                 out
128                 ERROR_QUIET
129                 OUTPUT_STRIP_TRAILING_WHITESPACE)
130         if(NOT res EQUAL 0)
131                 set(out "${out}-${res}-NOTFOUND")
132         endif()
134         set(${_var} "${out}" PARENT_SCOPE)
135 endfunction()
137 function(git_get_exact_tag _var)
138         git_describe(out --exact-match ${ARGN})
139         set(${_var} "${out}" PARENT_SCOPE)
140 endfunction()
142 function(git_local_changes _var)
143         if(NOT GIT_FOUND)
144                 find_package(Git QUIET)
145         endif()
146         get_git_head_revision(refspec hash)
147         if(NOT GIT_FOUND)
148                 set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
149                 return()
150         endif()
151         if(NOT hash)
152                 set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
153                 return()
154         endif()
156         execute_process(COMMAND
157                 "${GIT_EXECUTABLE}"
158                 diff-index --quiet HEAD --
159                 WORKING_DIRECTORY
160                 "${CMAKE_CURRENT_SOURCE_DIR}"
161                 RESULT_VARIABLE
162                 res
163                 OUTPUT_VARIABLE
164                 out
165                 ERROR_QUIET
166                 OUTPUT_STRIP_TRAILING_WHITESPACE)
167         if(res EQUAL 0)
168                 set(${_var} "CLEAN" PARENT_SCOPE)
169         else()
170                 set(${_var} "DIRTY" PARENT_SCOPE)
171         endif()
172 endfunction()