| 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 |
# Requires CMake 2.6 or newer (uses the 'function' command) |
| 22 |
# |
| 23 |
# Original Author: |
| 24 |
# 2009-2010 Ryan Pavlik <[email protected]> <[email protected]> |
| 25 |
# http://academic.cleardefinition.com |
| 26 |
# Iowa State University HCI Graduate Program/VRAC |
| 27 |
# |
| 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) |
| 32 |
|
| 33 |
if(__get_git_revision_description) |
| 34 |
return() |
| 35 |
endif() |
| 36 |
set(__get_git_revision_description YES) |
| 37 |
|
| 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) |
| 41 |
|
| 42 |
function(get_git_head_revision _refspecvar _hashvar) |
| 43 |
set(GIT_DIR "${CMAKE_SOURCE_DIR}/.git") |
| 44 |
if(NOT EXISTS "${GIT_DIR}") |
| 45 |
# not in git |
| 46 |
set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE) |
| 47 |
set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE) |
| 48 |
return() |
| 49 |
endif() |
| 50 |
set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data") |
| 51 |
if(NOT EXISTS "${GIT_DATA}") |
| 52 |
file(MAKE_DIRECTORY "${GIT_DATA}") |
| 53 |
endif() |
| 54 |
set(HEAD_FILE "${GIT_DATA}/HEAD") |
| 55 |
configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY) |
| 56 |
|
| 57 |
configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in" "${GIT_DATA}/grabRef.cmake" @ONLY) |
| 58 |
include("${GIT_DATA}/grabRef.cmake") |
| 59 |
|
| 60 |
set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE) |
| 61 |
set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE) |
| 62 |
endfunction() |
| 63 |
|
| 64 |
function(git_describe _var) |
| 65 |
if(NOT GIT_FOUND) |
| 66 |
find_package(Git QUIET) |
| 67 |
endif() |
| 68 |
get_git_head_revision(refspec hash) |
| 69 |
if(NOT GIT_FOUND) |
| 70 |
set(${_var} "GIT-NOTFOUND" PARENT_SCOPE) |
| 71 |
return() |
| 72 |
endif() |
| 73 |
if(NOT hash) |
| 74 |
set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE) |
| 75 |
return() |
| 76 |
endif() |
| 77 |
|
| 78 |
# TODO sanitize |
| 79 |
#if((${ARGN}" MATCHES "&&") OR |
| 80 |
# (ARGN MATCHES "||") OR |
| 81 |
# (ARGN MATCHES "\\;")) |
| 82 |
# message("Please report the following error to the project!") |
| 83 |
# message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}") |
| 84 |
#endif() |
| 85 |
|
| 86 |
#message(STATUS "Arguments to execute_process: ${ARGN}") |
| 87 |
|
| 88 |
execute_process(COMMAND "${GIT_EXECUTABLE}" describe ${hash} ${ARGN} |
| 89 |
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" |
| 90 |
RESULT_VARIABLE res |
| 91 |
OUTPUT_VARIABLE out |
| 92 |
ERROR_QUIET |
| 93 |
OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 94 |
if(NOT res EQUAL 0) |
| 95 |
set(out "${out}-${res}-NOTFOUND") |
| 96 |
endif() |
| 97 |
|
| 98 |
set(${_var} "${out}" PARENT_SCOPE) |
| 99 |
endfunction() |
| 100 |
|
| 101 |
function(git_get_exact_tag _var) |
| 102 |
git_describe(out --exact-match ${ARGN}) |
| 103 |
set(${_var} "${out}" PARENT_SCOPE) |
| 104 |
endfunction() |