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> <repo dir> [<additional arguments to git describe> ...])
7#
8# Returns the refspec and sha hash of the current head revision
9#
10#  git_describe(<var> <repo dir> [<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> <repo dir> [<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 <rpavlik@iastate.edu> <abiryan@ryand.net>
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# Updated 2018 Espressif Systems to add _repo_dir argument
34# to get revision of other repositories
35
36if(__get_git_revision_description)
37    return()
38endif()
39set(__get_git_revision_description YES)
40
41# We must run the following at "include" time, not at function call time,
42# to find the path to this module rather than the path to a calling list file
43get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
44
45function(get_git_head_revision _refspecvar _hashvar _repo_dir)
46    execute_process(COMMAND
47        "${GIT_EXECUTABLE}"
48        rev-parse
49        --git-dir
50        WORKING_DIRECTORY
51        ${_repo_dir}
52        RESULT_VARIABLE
53        res
54        OUTPUT_VARIABLE
55        GIT_DIR
56        ERROR_QUIET
57        OUTPUT_STRIP_TRAILING_WHITESPACE)
58
59    if(NOT res EQUAL 0)
60        return()
61    endif()
62
63    get_filename_component(GIT_DIR "${GIT_DIR}" ABSOLUTE BASE_DIR "${_repo_dir}")
64
65    set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
66    if(NOT EXISTS "${GIT_DATA}")
67        file(MAKE_DIRECTORY "${GIT_DATA}")
68    endif()
69
70    if(NOT EXISTS "${GIT_DIR}/HEAD")
71        return()
72    endif()
73
74    set(HEAD_FILE "${GIT_DATA}/HEAD")
75    configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
76
77    configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
78        "${GIT_DATA}/grabRef.cmake"
79        @ONLY)
80    include("${GIT_DATA}/grabRef.cmake")
81
82    set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
83    set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
84endfunction()
85
86function(git_describe _var _repo_dir)
87    if(NOT GIT_FOUND)
88        find_package(Git QUIET)
89    endif()
90    get_git_head_revision(refspec hash "${_repo_dir}")
91    if(NOT GIT_FOUND)
92        set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
93        return()
94    endif()
95    if(NOT hash)
96        set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
97        return()
98
99    endif()
100
101    # TODO sanitize
102    #if((${ARGN}" MATCHES "&&") OR
103    #	(ARGN MATCHES "||") OR
104    #	(ARGN MATCHES "\\;"))
105    #	message("Please report the following error to the project!")
106    #	message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
107    #endif()
108
109    #message(STATUS "Arguments to execute_process: ${ARGN}")
110
111    execute_process(COMMAND
112        "${GIT_EXECUTABLE}"
113        "-C"
114        ${_repo_dir}
115        describe
116        "--always"
117        "--tags"
118        "--dirty"
119        ${ARGN}
120        WORKING_DIRECTORY
121        "${CMAKE_CURRENT_SOURCE_DIR}"
122        RESULT_VARIABLE
123        res
124        OUTPUT_VARIABLE
125        out
126        ERROR_QUIET
127        OUTPUT_STRIP_TRAILING_WHITESPACE)
128    if(NOT res EQUAL 0)
129        set(out "${out}-${res}-NOTFOUND")
130    endif()
131
132    set(${_var} "${out}" PARENT_SCOPE)
133endfunction()
134
135function(git_get_exact_tag _var _repo_dir)
136    git_describe(out "${_repo_dir}" --exact-match ${ARGN})
137    set(${_var} "${out}" PARENT_SCOPE)
138endfunction()
139