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
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)
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)
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)
60 set(GIT_DIR "${GIT_PARENT_DIR}/.git")
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)
69 if(NOT IS_DIRECTORY "${GIT_DIR}")
70 file(READ ${GIT_DIR} worktree)
71 string(REGEX REPLACE "gitdir: (.*)worktrees(.*)\n$" "\\1" GIT_DIR ${worktree})
73 set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
74 if(NOT EXISTS "${GIT_DATA}")
75 file(MAKE_DIRECTORY "${GIT_DATA}")
78 if(NOT EXISTS "${GIT_DIR}/HEAD")
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"
87 include("${GIT_DATA}/grabRef.cmake")
89 set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
90 set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
93 function(git_describe _var)
95 find_package(Git QUIET)
97 get_git_head_revision(refspec hash)
99 set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
103 set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
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}")
115 #message(STATUS "Arguments to execute_process: ${ARGN}")
117 execute_process(COMMAND
123 "${CMAKE_CURRENT_SOURCE_DIR}"
129 OUTPUT_STRIP_TRAILING_WHITESPACE)
131 set(out "${out}-${res}-NOTFOUND")
134 set(${_var} "${out}" PARENT_SCOPE)
137 function(git_get_exact_tag _var)
138 git_describe(out --exact-match ${ARGN})
139 set(${_var} "${out}" PARENT_SCOPE)
142 function(git_local_changes _var)
144 find_package(Git QUIET)
146 get_git_head_revision(refspec hash)
148 set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
152 set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
156 execute_process(COMMAND
158 diff-index --quiet HEAD --
160 "${CMAKE_CURRENT_SOURCE_DIR}"
166 OUTPUT_STRIP_TRAILING_WHITESPACE)
168 set(${_var} "CLEAN" PARENT_SCOPE)
170 set(${_var} "DIRTY" PARENT_SCOPE)