xref: /phasta/phSolver/cmake/GetGitRevisionDescription.cmake (revision 43c56ca5a35722d85cb4464ff0683d63f8eb4a00)
1*43c56ca5SCameron Smith# - Returns a version string from Git
2*43c56ca5SCameron Smith#
3*43c56ca5SCameron Smith# These functions force a re-configure on each git commit so that you can
4*43c56ca5SCameron Smith# trust the values of the variables in your build system.
5*43c56ca5SCameron Smith#
6*43c56ca5SCameron Smith#  get_git_head_revision(<refspecvar> <hashvar> [<additional arguments to git describe> ...])
7*43c56ca5SCameron Smith#
8*43c56ca5SCameron Smith# Returns the refspec and sha hash of the current head revision
9*43c56ca5SCameron Smith#
10*43c56ca5SCameron Smith#  git_describe(<var> [<additional arguments to git describe> ...])
11*43c56ca5SCameron Smith#
12*43c56ca5SCameron Smith# Returns the results of git describe on the source tree, and adjusting
13*43c56ca5SCameron Smith# the output so that it tests false if an error occurs.
14*43c56ca5SCameron Smith#
15*43c56ca5SCameron Smith#  git_get_exact_tag(<var> [<additional arguments to git describe> ...])
16*43c56ca5SCameron Smith#
17*43c56ca5SCameron Smith# Returns the results of git describe --exact-match on the source tree,
18*43c56ca5SCameron Smith# and adjusting the output so that it tests false if there was no exact
19*43c56ca5SCameron Smith# matching tag.
20*43c56ca5SCameron Smith#
21*43c56ca5SCameron Smith# Requires CMake 2.6 or newer (uses the 'function' command)
22*43c56ca5SCameron Smith#
23*43c56ca5SCameron Smith# Original Author:
24*43c56ca5SCameron Smith# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
25*43c56ca5SCameron Smith# http://academic.cleardefinition.com
26*43c56ca5SCameron Smith# Iowa State University HCI Graduate Program/VRAC
27*43c56ca5SCameron Smith#
28*43c56ca5SCameron Smith# Copyright Iowa State University 2009-2010.
29*43c56ca5SCameron Smith# Distributed under the Boost Software License, Version 1.0.
30*43c56ca5SCameron Smith# (See accompanying file LICENSE_1_0.txt or copy at
31*43c56ca5SCameron Smith# http://www.boost.org/LICENSE_1_0.txt)
32*43c56ca5SCameron Smith
33*43c56ca5SCameron Smithif(__get_git_revision_description)
34*43c56ca5SCameron Smith    return()
35*43c56ca5SCameron Smithendif()
36*43c56ca5SCameron Smithset(__get_git_revision_description YES)
37*43c56ca5SCameron Smith
38*43c56ca5SCameron Smith# We must run the following at "include" time, not at function call time,
39*43c56ca5SCameron Smith# to find the path to this module rather than the path to a calling list file
40*43c56ca5SCameron Smithget_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
41*43c56ca5SCameron Smith
42*43c56ca5SCameron Smithfunction(get_git_head_revision _refspecvar _hashvar)
43*43c56ca5SCameron Smith    set(GIT_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
44*43c56ca5SCameron Smith    set(GIT_DIR "${GIT_PARENT_DIR}/.git")
45*43c56ca5SCameron Smith    while(NOT EXISTS "${GIT_DIR}")    # .git dir not found, search parent directories
46*43c56ca5SCameron Smith        set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
47*43c56ca5SCameron Smith        get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
48*43c56ca5SCameron Smith        if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
49*43c56ca5SCameron Smith            # We have reached the root directory, we are not in git
50*43c56ca5SCameron Smith            set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
51*43c56ca5SCameron Smith            set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
52*43c56ca5SCameron Smith            return()
53*43c56ca5SCameron Smith        endif()
54*43c56ca5SCameron Smith        set(GIT_DIR "${GIT_PARENT_DIR}/.git")
55*43c56ca5SCameron Smith    endwhile()
56*43c56ca5SCameron Smith    # check if this is a submodule
57*43c56ca5SCameron Smith    if(NOT IS_DIRECTORY ${GIT_DIR})
58*43c56ca5SCameron Smith        file(READ ${GIT_DIR} submodule)
59*43c56ca5SCameron Smith        string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
60*43c56ca5SCameron Smith        get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
61*43c56ca5SCameron Smith        get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
62*43c56ca5SCameron Smith    endif()
63*43c56ca5SCameron Smith    set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
64*43c56ca5SCameron Smith    if(NOT EXISTS "${GIT_DATA}")
65*43c56ca5SCameron Smith        file(MAKE_DIRECTORY "${GIT_DATA}")
66*43c56ca5SCameron Smith    endif()
67*43c56ca5SCameron Smith
68*43c56ca5SCameron Smith    if(NOT EXISTS "${GIT_DIR}/HEAD")
69*43c56ca5SCameron Smith        return()
70*43c56ca5SCameron Smith    endif()
71*43c56ca5SCameron Smith    set(HEAD_FILE "${GIT_DATA}/HEAD")
72*43c56ca5SCameron Smith    configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
73*43c56ca5SCameron Smith
74*43c56ca5SCameron Smith    configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
75*43c56ca5SCameron Smith        "${GIT_DATA}/grabRef.cmake"
76*43c56ca5SCameron Smith        @ONLY)
77*43c56ca5SCameron Smith    include("${GIT_DATA}/grabRef.cmake")
78*43c56ca5SCameron Smith
79*43c56ca5SCameron Smith    set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
80*43c56ca5SCameron Smith    set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
81*43c56ca5SCameron Smithendfunction()
82*43c56ca5SCameron Smith
83*43c56ca5SCameron Smithfunction(git_describe _var)
84*43c56ca5SCameron Smith    if(NOT GIT_FOUND)
85*43c56ca5SCameron Smith        find_package(Git QUIET)
86*43c56ca5SCameron Smith    endif()
87*43c56ca5SCameron Smith    get_git_head_revision(refspec hash)
88*43c56ca5SCameron Smith    if(NOT GIT_FOUND)
89*43c56ca5SCameron Smith        set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
90*43c56ca5SCameron Smith        return()
91*43c56ca5SCameron Smith    endif()
92*43c56ca5SCameron Smith    if(NOT hash)
93*43c56ca5SCameron Smith        set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
94*43c56ca5SCameron Smith        return()
95*43c56ca5SCameron Smith    endif()
96*43c56ca5SCameron Smith
97*43c56ca5SCameron Smith    # TODO sanitize
98*43c56ca5SCameron Smith    #if((${ARGN}" MATCHES "&&") OR
99*43c56ca5SCameron Smith    #    (ARGN MATCHES "||") OR
100*43c56ca5SCameron Smith    #    (ARGN MATCHES "\\;"))
101*43c56ca5SCameron Smith    #    message("Please report the following error to the project!")
102*43c56ca5SCameron Smith    #    message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
103*43c56ca5SCameron Smith    #endif()
104*43c56ca5SCameron Smith
105*43c56ca5SCameron Smith    #message(STATUS "Arguments to execute_process: ${ARGN}")
106*43c56ca5SCameron Smith
107*43c56ca5SCameron Smith    execute_process(COMMAND
108*43c56ca5SCameron Smith        "${GIT_EXECUTABLE}"
109*43c56ca5SCameron Smith        describe
110*43c56ca5SCameron Smith        ${hash}
111*43c56ca5SCameron Smith        ${ARGN}
112*43c56ca5SCameron Smith        WORKING_DIRECTORY
113*43c56ca5SCameron Smith        "${CMAKE_CURRENT_SOURCE_DIR}"
114*43c56ca5SCameron Smith        RESULT_VARIABLE
115*43c56ca5SCameron Smith        res
116*43c56ca5SCameron Smith        OUTPUT_VARIABLE
117*43c56ca5SCameron Smith        out
118*43c56ca5SCameron Smith        ERROR_QUIET
119*43c56ca5SCameron Smith        OUTPUT_STRIP_TRAILING_WHITESPACE)
120*43c56ca5SCameron Smith    if(NOT res EQUAL 0)
121*43c56ca5SCameron Smith        set(out "${out}-${res}-NOTFOUND")
122*43c56ca5SCameron Smith    endif()
123*43c56ca5SCameron Smith
124*43c56ca5SCameron Smith    set(${_var} "${out}" PARENT_SCOPE)
125*43c56ca5SCameron Smithendfunction()
126*43c56ca5SCameron Smith
127*43c56ca5SCameron Smithfunction(git_get_exact_tag _var)
128*43c56ca5SCameron Smith    git_describe(out --exact-match ${ARGN})
129*43c56ca5SCameron Smith    set(${_var} "${out}" PARENT_SCOPE)
130*43c56ca5SCameron Smithendfunction()
131