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 # Requires CMake 2.6 or newer (uses the 'function' command)
24 # 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
25 # http://academic.cleardefinition.com
26 # Iowa State University HCI Graduate Program/VRAC
28 # Copyright Iowa State University 2009-2010.
29 # Distributed under the Boost Software License, Version 1.0.
30 # (See accompanying file LICENSE_1_0.txt or copy at
31 # http://www.boost.org/LICENSE_1_0.txt)
33 if(__get_git_revision_description)
36 set(__get_git_revision_description YES)
38 # We must run the following at "include" time, not at function call time,
39 # to find the path to this module rather than the path to a calling list file
40 get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
42 function(get_git_head_revision _refspecvar _hashvar)
43 set(GIT_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
44 set(GIT_DIR "${GIT_PARENT_DIR}/.git")
45 while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
46 set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
47 get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
48 if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
49 # We have reached the root directory, we are not in git
50 set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
51 set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
54 set(GIT_DIR "${GIT_PARENT_DIR}/.git")
56 # check if this is a submodule
57 if(NOT IS_DIRECTORY ${GIT_DIR})
58 file(READ ${GIT_DIR} submodule)
59 string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
60 get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
61 get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
63 set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
64 if(NOT EXISTS "${GIT_DATA}")
65 file(MAKE_DIRECTORY "${GIT_DATA}")
68 if(NOT EXISTS "${GIT_DIR}/HEAD")
71 set(HEAD_FILE "${GIT_DATA}/HEAD")
72 configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
74 configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
75 "${GIT_DATA}/grabRef.cmake"
77 include("${GIT_DATA}/grabRef.cmake")
79 set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
80 set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
83 function(git_describe _var)
85 find_package(Git QUIET)
87 get_git_head_revision(refspec hash)
89 set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
93 set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
98 #if((${ARGN}" MATCHES "&&") OR
99 # (ARGN MATCHES "||") OR
100 # (ARGN MATCHES "\\;"))
101 # message("Please report the following error to the project!")
102 # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
105 #message(STATUS "Arguments to execute_process: ${ARGN}")
107 execute_process(COMMAND
113 "${CMAKE_CURRENT_SOURCE_DIR}"
119 OUTPUT_STRIP_TRAILING_WHITESPACE)
121 set(out "${out}-${res}-NOTFOUND")
124 set(${_var} "${out}" PARENT_SCOPE)
127 function(git_get_exact_tag _var)
128 git_describe(out --exact-match ${ARGN})
129 set(${_var} "${out}" PARENT_SCOPE)