Backported #6865: Disable websockets command line option
[platform/upstream/freerdp.git] / cmake / GetGitRevisionDescription.cmake
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 <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 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
44         set(GIT_PARENT_DIR "${CMAKE_SOURCE_DIR}")
45         set(GIT_DIR "${GIT_PARENT_DIR}/.git")
46         while(NOT EXISTS "${GIT_DIR}")  # .git dir not found, search parent directories
47                 set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
48                 get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
49                 if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
50                         # We have reached the root directory, we are not in git
51                         set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
52                         set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
53                         return()
54                 endif()
55                 set(GIT_DIR "${GIT_PARENT_DIR}/.git")
56         endwhile()
57         set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
58         if(NOT EXISTS "${GIT_DATA}")
59                 file(MAKE_DIRECTORY "${GIT_DATA}")
60         endif()
61
62         if(NOT EXISTS "${GIT_DIR}/HEAD")
63                 return()
64         endif()
65         set(HEAD_FILE "${GIT_DATA}/HEAD")
66         configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
67
68         configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
69                 "${GIT_DATA}/grabRef.cmake"
70                 @ONLY)
71         include("${GIT_DATA}/grabRef.cmake")
72
73         set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
74         set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
75 endfunction()
76
77 function(git_rev_parse _var)
78         if(NOT GIT_FOUND)
79                 find_package(Git QUIET)
80         endif()
81         if(NOT GIT_FOUND)
82                 set(${_var} "n/a" PARENT_SCOPE)
83                 return()
84         endif()
85         get_git_head_revision(refspec hash)
86         if(NOT hash)
87                 set(${_var} "n/a" PARENT_SCOPE)
88                 return()
89         endif()
90
91         execute_process(COMMAND "${GIT_EXECUTABLE}" rev-parse ${ARGN} ${hash}
92                 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
93                 RESULT_VARIABLE res
94                 OUTPUT_VARIABLE out
95                 ERROR_QUIET
96                 OUTPUT_STRIP_TRAILING_WHITESPACE)
97         if(NOT res EQUAL 0)
98                 set(out "n/a")
99         endif()
100
101         set(${_var} "${out}" PARENT_SCOPE)
102 endfunction()
103
104
105 function(git_describe _var)
106         if(NOT GIT_FOUND)
107                 find_package(Git QUIET)
108         endif()
109         if(NOT GIT_FOUND)
110                 set(${_var} "n/a" PARENT_SCOPE)
111                 return()
112         endif()
113         get_git_head_revision(refspec hash)
114         if(NOT hash)
115                 set(${_var} "n/a" PARENT_SCOPE)
116                 return()
117         endif()
118
119         execute_process(COMMAND "${GIT_EXECUTABLE}" describe ${hash} ${ARGN}
120                 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
121                 RESULT_VARIABLE res
122                 OUTPUT_VARIABLE out
123                 ERROR_QUIET
124                 OUTPUT_STRIP_TRAILING_WHITESPACE)
125         if(NOT res EQUAL 0)
126                 set(out "n/a")
127         endif()
128
129         set(${_var} "${out}" PARENT_SCOPE)
130 endfunction()
131
132 function(git_get_exact_tag _var)
133         git_describe(out --exact-match ${ARGN})
134         set(${_var} "${out}" PARENT_SCOPE)
135 endfunction()