| 1 |
# - Returns a version string from Git |
| 2 |
# |
| 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. |
| 5 |
# |
| 6 |
# get_git_head_revision(<refspecvar> <hashvar> [ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR]) |
| 7 |
# |
| 8 |
# Returns the refspec and sha hash of the current head revision |
| 9 |
# |
| 10 |
# git_describe(<var> [<additional arguments to git describe> ...]) |
| 11 |
# |
| 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. |
| 14 |
# |
| 15 |
# git_describe_working_tree(<var> [<additional arguments to git describe> ...]) |
| 16 |
# |
| 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. |
| 19 |
# |
| 20 |
# git_get_exact_tag(<var> [<additional arguments to git describe> ...]) |
| 21 |
# |
| 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 |
| 24 |
# matching tag. |
| 25 |
# |
| 26 |
# git_local_changes(<var>) |
| 27 |
# |
| 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. |
| 31 |
# |
| 32 |
# Requires CMake 2.6 or newer (uses the 'function' command) |
| 33 |
# |
| 34 |
# Original Author: |
| 35 |
# 2009-2020 Ryan Pavlik <[email protected]> <[email protected]> |
| 36 |
# http://academic.cleardefinition.com |
| 37 |
# |
| 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) |
| 45 |
|
| 46 |
if(__get_git_revision_description) |
| 47 |
return() |
| 48 |
endif() |
| 49 |
set(__get_git_revision_description YES) |
| 50 |
|
| 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) |
| 54 |
|
| 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. |
| 60 |
# |
| 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 |
| 63 |
# C:/bla/.git |
| 64 |
# |
| 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 |
| 74 |
set(${_git_dir_var} |
| 75 |
"" |
| 76 |
PARENT_SCOPE) |
| 77 |
return() |
| 78 |
endif() |
| 79 |
set(git_dir "${cur_dir}/.git") |
| 80 |
endwhile() |
| 81 |
set(${_git_dir_var} |
| 82 |
"${git_dir}" |
| 83 |
PARENT_SCOPE) |
| 84 |
endfunction() |
| 85 |
|
| 86 |
function(get_git_head_revision _refspecvar _hashvar) |
| 87 |
_git_find_closest_git_dir("${CMAKE_CURRENT_SOURCE_DIR}" GIT_DIR) |
| 88 |
|
| 89 |
if("${ARGN}" STREQUAL "ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR") |
| 90 |
set(ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR TRUE) |
| 91 |
else() |
| 92 |
set(ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR FALSE) |
| 93 |
endif() |
| 94 |
if(NOT "${GIT_DIR}" STREQUAL "") |
| 95 |
file(RELATIVE_PATH _relative_to_source_dir "${CMAKE_SOURCE_DIR}" |
| 96 |
"${GIT_DIR}") |
| 97 |
if("${_relative_to_source_dir}" MATCHES "[.][.]" |
| 98 |
AND NOT ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR) |
| 99 |
# We've gone above the CMake root dir. |
| 100 |
set(GIT_DIR "") |
| 101 |
endif() |
| 102 |
endif() |
| 103 |
if("${GIT_DIR}" STREQUAL "") |
| 104 |
set(${_refspecvar} |
| 105 |
"GITDIR-NOTFOUND" |
| 106 |
PARENT_SCOPE) |
| 107 |
set(${_hashvar} |
| 108 |
"GITDIR-NOTFOUND" |
| 109 |
PARENT_SCOPE) |
| 110 |
return() |
| 111 |
endif() |
| 112 |
|
| 113 |
# Check if the current source dir is a git submodule or a worktree. |
| 114 |
# In both cases .git is a file instead of a directory. |
| 115 |
# |
| 116 |
if(NOT IS_DIRECTORY ${GIT_DIR}) |
| 117 |
# The following git command will return a non empty string that |
| 118 |
# points to the super project working tree if the current |
| 119 |
# source dir is inside a git submodule. |
| 120 |
# Otherwise the command will return an empty string. |
| 121 |
# |
| 122 |
execute_process( |
| 123 |
COMMAND "${GIT_EXECUTABLE}" rev-parse |
| 124 |
--show-superproject-working-tree |
| 125 |
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" |
| 126 |
OUTPUT_VARIABLE out |
| 127 |
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 128 |
if(NOT "${out}" STREQUAL "") |
| 129 |
# If out is empty, GIT_DIR/CMAKE_CURRENT_SOURCE_DIR is in a submodule |
| 130 |
file(READ ${GIT_DIR} submodule) |
| 131 |
string(REGEX REPLACE "gitdir: (.*)$" "\\1" GIT_DIR_RELATIVE |
| 132 |
${submodule}) |
| 133 |
string(STRIP ${GIT_DIR_RELATIVE} GIT_DIR_RELATIVE) |
| 134 |
get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH) |
| 135 |
get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} |
| 136 |
ABSOLUTE) |
| 137 |
set(HEAD_SOURCE_FILE "${GIT_DIR}/HEAD") |
| 138 |
else() |
| 139 |
# GIT_DIR/CMAKE_CURRENT_SOURCE_DIR is in a worktree |
| 140 |
file(READ ${GIT_DIR} worktree_ref) |
| 141 |
# The .git directory contains a path to the worktree information directory |
| 142 |
# inside the parent git repo of the worktree. |
| 143 |
# |
| 144 |
string(REGEX REPLACE "gitdir: (.*)$" "\\1" git_worktree_dir |
| 145 |
${worktree_ref}) |
| 146 |
string(STRIP ${git_worktree_dir} git_worktree_dir) |
| 147 |
_git_find_closest_git_dir("${git_worktree_dir}" GIT_DIR) |
| 148 |
set(HEAD_SOURCE_FILE "${git_worktree_dir}/HEAD") |
| 149 |
endif() |
| 150 |
else() |
| 151 |
set(HEAD_SOURCE_FILE "${GIT_DIR}/HEAD") |
| 152 |
endif() |
| 153 |
set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data") |
| 154 |
if(NOT EXISTS "${GIT_DATA}") |
| 155 |
file(MAKE_DIRECTORY "${GIT_DATA}") |
| 156 |
endif() |
| 157 |
|
| 158 |
if(NOT EXISTS "${HEAD_SOURCE_FILE}") |
| 159 |
return() |
| 160 |
endif() |
| 161 |
set(HEAD_FILE "${GIT_DATA}/HEAD") |
| 162 |
configure_file("${HEAD_SOURCE_FILE}" "${HEAD_FILE}" COPYONLY) |
| 163 |
|
| 164 |
configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in" |
| 165 |
"${GIT_DATA}/grabRef.cmake" @ONLY) |
| 166 |
include("${GIT_DATA}/grabRef.cmake") |
| 167 |
|
| 168 |
set(${_refspecvar} |
| 169 |
"${HEAD_REF}" |
| 170 |
PARENT_SCOPE) |
| 171 |
set(${_hashvar} |
| 172 |
"${HEAD_HASH}" |
| 173 |
PARENT_SCOPE) |
| 174 |
endfunction() |
| 175 |
|
| 176 |
function(git_describe _var) |
| 177 |
if(NOT GIT_FOUND) |
| 178 |
find_package(Git QUIET) |
| 179 |
endif() |
| 180 |
get_git_head_revision(refspec hash) |
| 181 |
if(NOT GIT_FOUND) |
| 182 |
set(${_var} |
| 183 |
"GIT-NOTFOUND" |
| 184 |
PARENT_SCOPE) |
| 185 |
return() |
| 186 |
endif() |
| 187 |
if(NOT hash) |
| 188 |
set(${_var} |
| 189 |
"HEAD-HASH-NOTFOUND" |
| 190 |
PARENT_SCOPE) |
| 191 |
return() |
| 192 |
endif() |
| 193 |
|
| 194 |
# TODO sanitize |
| 195 |
#if((${ARGN}" MATCHES "&&") OR |
| 196 |
# (ARGN MATCHES "||") OR |
| 197 |
# (ARGN MATCHES "\\;")) |
| 198 |
# message("Please report the following error to the project!") |
| 199 |
# message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}") |
| 200 |
#endif() |
| 201 |
|
| 202 |
#message(STATUS "Arguments to execute_process: ${ARGN}") |
| 203 |
|
| 204 |
execute_process( |
| 205 |
COMMAND "${GIT_EXECUTABLE}" describe --tags --always ${hash} ${ARGN} |
| 206 |
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" |
| 207 |
RESULT_VARIABLE res |
| 208 |
OUTPUT_VARIABLE out |
| 209 |
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 210 |
if(NOT res EQUAL 0) |
| 211 |
set(out "${out}-${res}-NOTFOUND") |
| 212 |
endif() |
| 213 |
|
| 214 |
set(${_var} |
| 215 |
"${out}" |
| 216 |
PARENT_SCOPE) |
| 217 |
endfunction() |
| 218 |
|
| 219 |
function(git_describe_working_tree _var) |
| 220 |
if(NOT GIT_FOUND) |
| 221 |
find_package(Git QUIET) |
| 222 |
endif() |
| 223 |
if(NOT GIT_FOUND) |
| 224 |
set(${_var} |
| 225 |
"GIT-NOTFOUND" |
| 226 |
PARENT_SCOPE) |
| 227 |
return() |
| 228 |
endif() |
| 229 |
|
| 230 |
execute_process( |
| 231 |
COMMAND "${GIT_EXECUTABLE}" describe --dirty ${ARGN} |
| 232 |
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" |
| 233 |
RESULT_VARIABLE res |
| 234 |
OUTPUT_VARIABLE out |
| 235 |
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 236 |
if(NOT res EQUAL 0) |
| 237 |
set(out "${out}-${res}-NOTFOUND") |
| 238 |
endif() |
| 239 |
|
| 240 |
set(${_var} |
| 241 |
"${out}" |
| 242 |
PARENT_SCOPE) |
| 243 |
endfunction() |
| 244 |
|
| 245 |
function(git_get_exact_tag _var) |
| 246 |
git_describe(out --exact-match ${ARGN}) |
| 247 |
set(${_var} |
| 248 |
"${out}" |
| 249 |
PARENT_SCOPE) |
| 250 |
endfunction() |
| 251 |
|
| 252 |
function(git_local_changes _var) |
| 253 |
if(NOT GIT_FOUND) |
| 254 |
find_package(Git QUIET) |
| 255 |
endif() |
| 256 |
get_git_head_revision(refspec hash) |
| 257 |
if(NOT GIT_FOUND) |
| 258 |
set(${_var} |
| 259 |
"GIT-NOTFOUND" |
| 260 |
PARENT_SCOPE) |
| 261 |
return() |
| 262 |
endif() |
| 263 |
if(NOT hash) |
| 264 |
set(${_var} |
| 265 |
"HEAD-HASH-NOTFOUND" |
| 266 |
PARENT_SCOPE) |
| 267 |
return() |
| 268 |
endif() |
| 269 |
|
| 270 |
execute_process( |
| 271 |
COMMAND "${GIT_EXECUTABLE}" diff-index --quiet HEAD -- |
| 272 |
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" |
| 273 |
RESULT_VARIABLE res |
| 274 |
OUTPUT_VARIABLE out |
| 275 |
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 276 |
if(res EQUAL 0) |
| 277 |
set(${_var} |
| 278 |
"CLEAN" |
| 279 |
PARENT_SCOPE) |
| 280 |
else() |
| 281 |
set(${_var} |
| 282 |
"DIRTY" |
| 283 |
PARENT_SCOPE) |
| 284 |
endif() |
| 285 |
endfunction() |