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> [ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR])
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_describe_working_tree(<var> [<additional arguments to git describe> ...])
17 # Returns the results of git describe on the working tree (--dirty option),
18 # and adjusting the output so that it tests false if an error occurs.
20 # git_get_exact_tag(<var> [<additional arguments to git describe> ...])
22 # Returns the results of git describe --exact-match on the source tree,
23 # and adjusting the output so that it tests false if there was no exact
26 # git_local_changes(<var>)
28 # Returns either "CLEAN" or "DIRTY" with respect to uncommitted changes.
29 # Uses the return code of "git diff-index --quiet HEAD --".
30 # Does not regard untracked files.
32 # Requires CMake 2.6 or newer (uses the 'function' command)
35 # 2009-2020 Ryan Pavlik <ryan.pavlik@gmail.com> <abiryan@ryand.net>
36 # http://academic.cleardefinition.com
38 # Copyright 2009-2013, Iowa State University.
39 # Copyright 2013-2020, Ryan Pavlik
40 # Copyright 2013-2020, Contributors
41 # SPDX-License-Identifier: BSL-1.0
42 # Distributed under the Boost Software License, Version 1.0.
43 # (See accompanying file LICENSE_1_0.txt or copy at
44 # http://www.boost.org/LICENSE_1_0.txt)
46 if(__get_git_revision_description)
49 set(__get_git_revision_description YES)
51 # We must run the following at "include" time, not at function call time,
52 # to find the path to this module rather than the path to a calling list file
53 get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
55 # Function _git_find_closest_git_dir finds the next closest .git directory
56 # that is part of any directory in the path defined by _start_dir.
57 # The result is returned in the parent scope variable whose name is passed
58 # as variable _git_dir_var. If no .git directory can be found, the
59 # function returns an empty string via _git_dir_var.
61 # Example: Given a path C:/bla/foo/bar and assuming C:/bla/.git exists and
62 # neither foo nor bar contain a file/directory .git. This wil return
65 function(_git_find_closest_git_dir _start_dir _git_dir_var)
66 set(cur_dir "${_start_dir}")
67 set(git_dir "${_start_dir}/.git")
68 while(NOT EXISTS "${git_dir}")
69 # .git dir not found, search parent directories
70 set(git_previous_parent "${cur_dir}")
71 get_filename_component(cur_dir "${cur_dir}" DIRECTORY)
72 if(cur_dir STREQUAL git_previous_parent)
73 # We have reached the root directory, we are not in git
79 set(git_dir "${cur_dir}/.git")
86 function(get_git_head_revision _refspecvar _hashvar)
87 _git_find_closest_git_dir("${CMAKE_CURRENT_SOURCE_DIR}" GIT_DIR)
89 if("${ARGN}" STREQUAL "ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR")
90 set(ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR TRUE)
92 set(ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR FALSE)
94 if(NOT "${GIT_DIR}" STREQUAL "")
95 file(RELATIVE_PATH _relative_to_source_dir "${CMAKE_SOURCE_DIR}"
97 if("${_relative_to_source_dir}" MATCHES "[.][.]" AND NOT ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR)
98 # We've gone above the CMake root dir.
102 if("${GIT_DIR}" STREQUAL "")
112 # Check if the current source dir is a git submodule or a worktree.
113 # In both cases .git is a file instead of a directory.
115 if(NOT IS_DIRECTORY ${GIT_DIR})
116 # The following git command will return a non empty string that
117 # points to the super project working tree if the current
118 # source dir is inside a git submodule.
119 # Otherwise the command will return an empty string.
122 COMMAND "${GIT_EXECUTABLE}" rev-parse
123 --show-superproject-working-tree
124 WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
126 ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
127 if(NOT "${out}" STREQUAL "")
128 # If out is empty, GIT_DIR/CMAKE_CURRENT_SOURCE_DIR is in a submodule
129 file(READ ${GIT_DIR} submodule)
130 string(REGEX REPLACE "gitdir: (.*)$" "\\1" GIT_DIR_RELATIVE
132 string(STRIP ${GIT_DIR_RELATIVE} GIT_DIR_RELATIVE)
133 get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
134 get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE}
136 set(HEAD_SOURCE_FILE "${GIT_DIR}/HEAD")
138 # GIT_DIR/CMAKE_CURRENT_SOURCE_DIR is in a worktree
139 file(READ ${GIT_DIR} worktree_ref)
140 # The .git directory contains a path to the worktree information directory
141 # inside the parent git repo of the worktree.
143 string(REGEX REPLACE "gitdir: (.*)$" "\\1" git_worktree_dir
145 string(STRIP ${git_worktree_dir} git_worktree_dir)
146 _git_find_closest_git_dir("${git_worktree_dir}" GIT_DIR)
147 set(HEAD_SOURCE_FILE "${git_worktree_dir}/HEAD")
150 set(HEAD_SOURCE_FILE "${GIT_DIR}/HEAD")
152 set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
153 if(NOT EXISTS "${GIT_DATA}")
154 file(MAKE_DIRECTORY "${GIT_DATA}")
157 if(NOT EXISTS "${HEAD_SOURCE_FILE}")
160 set(HEAD_FILE "${GIT_DATA}/HEAD")
161 configure_file("${HEAD_SOURCE_FILE}" "${HEAD_FILE}" COPYONLY)
163 configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
164 "${GIT_DATA}/grabRef.cmake" @ONLY)
165 include("${GIT_DATA}/grabRef.cmake")
175 function(git_describe _var)
177 find_package(Git QUIET)
179 get_git_head_revision(refspec hash)
194 #if((${ARGN}" MATCHES "&&") OR
195 # (ARGN MATCHES "||") OR
196 # (ARGN MATCHES "\\;"))
197 # message("Please report the following error to the project!")
198 # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
201 #message(STATUS "Arguments to execute_process: ${ARGN}")
204 COMMAND "${GIT_EXECUTABLE}" describe --tags --always ${hash} ${ARGN}
205 WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
208 ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
210 set(out "${out}-${res}-NOTFOUND")
218 function(git_describe_working_tree _var)
220 find_package(Git QUIET)
230 COMMAND "${GIT_EXECUTABLE}" describe --dirty ${ARGN}
231 WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
234 ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
236 set(out "${out}-${res}-NOTFOUND")
244 function(git_get_exact_tag _var)
245 git_describe(out --exact-match ${ARGN})
251 function(git_local_changes _var)
253 find_package(Git QUIET)
255 get_git_head_revision(refspec hash)
270 COMMAND "${GIT_EXECUTABLE}" diff-index --quiet HEAD --
271 WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
274 ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)