|
| 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> [<additional arguments to git describe> ...]) |
| 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_get_exact_tag(<var> [<additional arguments to git describe> ...]) |
| 16 | +# |
| 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 |
| 19 | +# matching tag. |
| 20 | +# |
| 21 | +# git_local_changes(<var>) |
| 22 | +# |
| 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. |
| 26 | +# |
| 27 | +# Requires CMake 2.6 or newer (uses the 'function' command) |
| 28 | +# |
| 29 | +# Original Author: |
| 30 | + |
| 31 | +# http://academic.cleardefinition.com |
| 32 | +# Iowa State University HCI Graduate Program/VRAC |
| 33 | +# |
| 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) |
| 38 | + |
| 39 | +if(__get_git_revision_description) |
| 40 | + return() |
| 41 | +endif() |
| 42 | +set(__get_git_revision_description YES) |
| 43 | + |
| 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) |
| 47 | + |
| 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) |
| 58 | + return() |
| 59 | + endif() |
| 60 | + set(GIT_DIR "${GIT_PARENT_DIR}/.git") |
| 61 | + endwhile() |
| 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) |
| 68 | + endif() |
| 69 | + set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data") |
| 70 | + if(NOT EXISTS "${GIT_DATA}") |
| 71 | + file(MAKE_DIRECTORY "${GIT_DATA}") |
| 72 | + endif() |
| 73 | + |
| 74 | + if(NOT EXISTS "${GIT_DIR}/HEAD") |
| 75 | + return() |
| 76 | + endif() |
| 77 | + set(HEAD_FILE "${GIT_DATA}/HEAD") |
| 78 | + configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY) |
| 79 | + |
| 80 | + configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in" |
| 81 | + "${GIT_DATA}/grabRef.cmake" |
| 82 | + @ONLY) |
| 83 | + include("${GIT_DATA}/grabRef.cmake") |
| 84 | + |
| 85 | + set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE) |
| 86 | + set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE) |
| 87 | +endfunction() |
| 88 | + |
| 89 | +function(git_describe _var) |
| 90 | + if(NOT GIT_FOUND) |
| 91 | + find_package(Git QUIET) |
| 92 | + endif() |
| 93 | + get_git_head_revision(refspec hash) |
| 94 | + if(NOT GIT_FOUND) |
| 95 | + set(${_var} "GIT-NOTFOUND" PARENT_SCOPE) |
| 96 | + return() |
| 97 | + endif() |
| 98 | + if(NOT hash) |
| 99 | + set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE) |
| 100 | + return() |
| 101 | + endif() |
| 102 | + |
| 103 | + # TODO sanitize |
| 104 | + #if((${ARGN}" MATCHES "&&") OR |
| 105 | + # (ARGN MATCHES "||") OR |
| 106 | + # (ARGN MATCHES "\\;")) |
| 107 | + # message("Please report the following error to the project!") |
| 108 | + # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}") |
| 109 | + #endif() |
| 110 | + |
| 111 | + #message(STATUS "Arguments to execute_process: ${ARGN}") |
| 112 | + |
| 113 | + execute_process(COMMAND |
| 114 | + "${GIT_EXECUTABLE}" |
| 115 | + describe |
| 116 | + ${hash} |
| 117 | + ${ARGN} |
| 118 | + WORKING_DIRECTORY |
| 119 | + "${CMAKE_CURRENT_SOURCE_DIR}" |
| 120 | + RESULT_VARIABLE |
| 121 | + res |
| 122 | + OUTPUT_VARIABLE |
| 123 | + out |
| 124 | + ERROR_QUIET |
| 125 | + OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 126 | + if(NOT res EQUAL 0) |
| 127 | + set(out "${out}-${res}-NOTFOUND") |
| 128 | + endif() |
| 129 | + |
| 130 | + set(${_var} "${out}" PARENT_SCOPE) |
| 131 | +endfunction() |
| 132 | + |
| 133 | +function(git_get_exact_tag _var) |
| 134 | + git_describe(out --exact-match ${ARGN}) |
| 135 | + set(${_var} "${out}" PARENT_SCOPE) |
| 136 | +endfunction() |
| 137 | + |
| 138 | +function(git_local_changes _var) |
| 139 | + if(NOT GIT_FOUND) |
| 140 | + find_package(Git QUIET) |
| 141 | + endif() |
| 142 | + get_git_head_revision(refspec hash) |
| 143 | + if(NOT GIT_FOUND) |
| 144 | + set(${_var} "GIT-NOTFOUND" PARENT_SCOPE) |
| 145 | + return() |
| 146 | + endif() |
| 147 | + if(NOT hash) |
| 148 | + set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE) |
| 149 | + return() |
| 150 | + endif() |
| 151 | + |
| 152 | + execute_process(COMMAND |
| 153 | + "${GIT_EXECUTABLE}" |
| 154 | + diff-index --quiet HEAD -- |
| 155 | + WORKING_DIRECTORY |
| 156 | + "${CMAKE_CURRENT_SOURCE_DIR}" |
| 157 | + RESULT_VARIABLE |
| 158 | + res |
| 159 | + OUTPUT_VARIABLE |
| 160 | + out |
| 161 | + ERROR_QUIET |
| 162 | + OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 163 | + if(res EQUAL 0) |
| 164 | + set(${_var} "CLEAN" PARENT_SCOPE) |
| 165 | + else() |
| 166 | + set(${_var} "DIRTY" PARENT_SCOPE) |
| 167 | + endif() |
| 168 | +endfunction() |
0 commit comments