3 #! \brief Returns a version string from Git
5 #! These functions force a re-configure on each git commit so that you can
6 #! trust the values of the variables in your build system.
8 #! Requires %CMake 2.6 or newer (uses the 'function' command)
11 # 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
12 # http://academic.cleardefinition.com
13 # Iowa State University HCI Graduate Program/VRAC
15 # Copyright Iowa State University 2009-2010.
16 # Distributed under the Boost Software License, Version 1.0.
17 # (See accompanying file LICENSE.txt or copy at
18 # http://www.boost.org/LICENSE_1_0.txt)
20 if(__get_git_revision_description)
23 set(__get_git_revision_description YES)
25 # We must run the following at "include" time, not at function call time,
26 # to find the path to this module rather than the path to a calling list file
27 get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
29 #! \brief Returns the refspec and sha hash of the current head revision
31 #! \param _refspecvar The refspec is returned in this variable.
32 #! \param _hashvar The sha hash is returned in this variable.
34 #! Additional arguments are passed through to `git describe`.
36 #! If the variable \ref CMake::GIT_SHA_OVERRIDE "GIT_SHA_OVERRIDE" is defined, both `_refspecvar` and
37 #! `_hashvar` are set to this value. This is used to set something meaningful
38 #! in automated Debian builds, where the git repository is not available.
39 function(get_git_head_revision _refspecvar _hashvar)
41 set(${_refspecvar} "${GIT_SHA_OVERRIDE}" PARENT_SCOPE)
42 set(${_hashvar} "${GIT_SHA_OVERRIDE}" PARENT_SCOPE)
46 set(GIT_PARENT_DIR "${CMAKE_CURRENT_LIST_DIR}")
47 set(GIT_DIR "${GIT_PARENT_DIR}/.git")
48 while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
49 set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
50 get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
51 if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
52 # We have reached the root directory, we are not in git
53 set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
54 set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
57 set(GIT_DIR "${GIT_PARENT_DIR}/.git")
59 # check if this is a submodule
60 if(NOT IS_DIRECTORY ${GIT_DIR})
61 file(READ ${GIT_DIR} submodule)
62 string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
63 get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
64 get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
66 set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
67 if(NOT EXISTS "${GIT_DATA}")
68 file(MAKE_DIRECTORY "${GIT_DATA}")
71 if(NOT EXISTS "${GIT_DIR}/HEAD")
74 set(HEAD_FILE "${GIT_DATA}/HEAD")
75 configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
77 configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
78 "${GIT_DATA}/grabRef.cmake"
80 include("${GIT_DATA}/grabRef.cmake")
82 set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
83 set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
86 #! \brief Returns the results of `git describe` on the source tree.
88 #! \param _var The result variable.
90 #! The output is adjusted so that it tests false if an error occurs.
91 function(git_describe _var)
93 find_package(Git QUIET)
95 get_git_head_revision(refspec hash)
97 set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
101 set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
105 #message(STATUS "Arguments to execute_process: ${ARGN}")
107 execute_process(COMMAND
113 "${CMAKE_SOURCE_DIR}"
119 OUTPUT_STRIP_TRAILING_WHITESPACE)
121 set(out "${out}-${res}-NOTFOUND")
124 set(${_var} "${out}" PARENT_SCOPE)
127 #! \brief Returns the results of `git describe --exact-match` on the source tree.
129 #! \param _var The result variable.
131 #! The output is adjusted so that it tests false if there was no exact
133 function(git_get_exact_tag _var)
134 git_describe(out --exact-match ${ARGN})
135 set(${_var} "${out}" PARENT_SCOPE)