GetGitRevisionDescription.cmake 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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> [ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR])
  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_describe_working_tree(<var> [<additional arguments to git describe> ...])
  16. #
  17. # Returns the results of git describe on the working tree (--dirty option),
  18. # and adjusting the output so that it tests false if an error occurs.
  19. #
  20. # git_get_exact_tag(<var> [<additional arguments to git describe> ...])
  21. #
  22. # Returns the results of git describe --exact-match on the source tree,
  23. # and adjusting the output so that it tests false if there was no exact
  24. # matching tag.
  25. #
  26. # git_local_changes(<var>)
  27. #
  28. # Returns either "CLEAN" or "DIRTY" with respect to uncommitted changes.
  29. # Uses the return code of "git diff-index --quiet HEAD --".
  30. # Does not regard untracked files.
  31. #
  32. # Requires CMake 2.6 or newer (uses the 'function' command)
  33. #
  34. # Original Author:
  35. # 2009-2020 Ryan Pavlik <ryan.pavlik@gmail.com> <abiryan@ryand.net>
  36. # http://academic.cleardefinition.com
  37. #
  38. # Copyright 2009-2013, Iowa State University.
  39. # Copyright 2013-2020, Ryan Pavlik
  40. # Copyright 2013-2020, Contributors
  41. # SPDX-License-Identifier: BSL-1.0
  42. # Distributed under the Boost Software License, Version 1.0.
  43. # (See accompanying file LICENSE_1_0.txt or copy at
  44. # http://www.boost.org/LICENSE_1_0.txt)
  45. if(__get_git_revision_description)
  46. return()
  47. endif()
  48. set(__get_git_revision_description YES)
  49. # We must run the following at "include" time, not at function call time,
  50. # to find the path to this module rather than the path to a calling list file
  51. get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
  52. # Function _git_find_closest_git_dir finds the next closest .git directory
  53. # that is part of any directory in the path defined by _start_dir.
  54. # The result is returned in the parent scope variable whose name is passed
  55. # as variable _git_dir_var. If no .git directory can be found, the
  56. # function returns an empty string via _git_dir_var.
  57. #
  58. # Example: Given a path C:/bla/foo/bar and assuming C:/bla/.git exists and
  59. # neither foo nor bar contain a file/directory .git. This will return
  60. # C:/bla/.git
  61. #
  62. function(_git_find_closest_git_dir _start_dir _git_dir_var)
  63. set(cur_dir "${_start_dir}")
  64. set(git_dir "${_start_dir}/.git")
  65. while(NOT EXISTS "${git_dir}")
  66. # .git dir not found, search parent directories
  67. set(git_previous_parent "${cur_dir}")
  68. get_filename_component(cur_dir "${cur_dir}" DIRECTORY)
  69. if(cur_dir STREQUAL git_previous_parent)
  70. # We have reached the root directory, we are not in git
  71. set(${_git_dir_var}
  72. ""
  73. PARENT_SCOPE)
  74. return()
  75. endif()
  76. set(git_dir "${cur_dir}/.git")
  77. endwhile()
  78. set(${_git_dir_var}
  79. "${git_dir}"
  80. PARENT_SCOPE)
  81. endfunction()
  82. function(get_git_head_revision _refspecvar _hashvar)
  83. _git_find_closest_git_dir("${CMAKE_CURRENT_SOURCE_DIR}" GIT_DIR)
  84. if("${ARGN}" STREQUAL "ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR")
  85. set(ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR TRUE)
  86. else()
  87. set(ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR FALSE)
  88. endif()
  89. if(NOT "${GIT_DIR}" STREQUAL "")
  90. file(RELATIVE_PATH _relative_to_source_dir "${CMAKE_SOURCE_DIR}"
  91. "${GIT_DIR}")
  92. if("${_relative_to_source_dir}" MATCHES "[.][.]" AND NOT ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR)
  93. # We've gone above the CMake root dir.
  94. set(GIT_DIR "")
  95. endif()
  96. endif()
  97. if("${GIT_DIR}" STREQUAL "")
  98. set(${_refspecvar}
  99. "GITDIR-NOTFOUND"
  100. PARENT_SCOPE)
  101. set(${_hashvar}
  102. "GITDIR-NOTFOUND"
  103. PARENT_SCOPE)
  104. return()
  105. endif()
  106. # Check if the current source dir is a git submodule or a worktree.
  107. # In both cases .git is a file instead of a directory.
  108. #
  109. if(NOT IS_DIRECTORY ${GIT_DIR})
  110. # The following git command will return a non empty string that
  111. # points to the super project working tree if the current
  112. # source dir is inside a git submodule.
  113. # Otherwise the command will return an empty string.
  114. #
  115. execute_process(
  116. COMMAND "${GIT_EXECUTABLE}" rev-parse
  117. --show-superproject-working-tree
  118. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  119. OUTPUT_VARIABLE out
  120. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  121. if(NOT "${out}" STREQUAL "")
  122. # If out is empty, GIT_DIR/CMAKE_CURRENT_SOURCE_DIR is in a submodule
  123. file(READ ${GIT_DIR} submodule)
  124. string(REGEX REPLACE "gitdir: (.*)$" "\\1" GIT_DIR_RELATIVE
  125. ${submodule})
  126. string(STRIP ${GIT_DIR_RELATIVE} GIT_DIR_RELATIVE)
  127. get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
  128. get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE}
  129. ABSOLUTE)
  130. set(HEAD_SOURCE_FILE "${GIT_DIR}/HEAD")
  131. else()
  132. # GIT_DIR/CMAKE_CURRENT_SOURCE_DIR is in a worktree
  133. file(READ ${GIT_DIR} worktree_ref)
  134. # The .git directory contains a path to the worktree information directory
  135. # inside the parent git repo of the worktree.
  136. #
  137. string(REGEX REPLACE "gitdir: (.*)$" "\\1" git_worktree_dir
  138. ${worktree_ref})
  139. string(STRIP ${git_worktree_dir} git_worktree_dir)
  140. _git_find_closest_git_dir("${git_worktree_dir}" GIT_DIR)
  141. set(HEAD_SOURCE_FILE "${git_worktree_dir}/HEAD")
  142. endif()
  143. else()
  144. set(HEAD_SOURCE_FILE "${GIT_DIR}/HEAD")
  145. endif()
  146. set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
  147. if(NOT EXISTS "${GIT_DATA}")
  148. file(MAKE_DIRECTORY "${GIT_DATA}")
  149. endif()
  150. if(NOT EXISTS "${HEAD_SOURCE_FILE}")
  151. return()
  152. endif()
  153. set(HEAD_FILE "${GIT_DATA}/HEAD")
  154. configure_file("${HEAD_SOURCE_FILE}" "${HEAD_FILE}" COPYONLY)
  155. configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
  156. "${GIT_DATA}/grabRef.cmake" @ONLY)
  157. include("${GIT_DATA}/grabRef.cmake")
  158. set(${_refspecvar}
  159. "${HEAD_REF}"
  160. PARENT_SCOPE)
  161. set(${_hashvar}
  162. "${HEAD_HASH}"
  163. PARENT_SCOPE)
  164. endfunction()
  165. function(git_describe _var)
  166. if(NOT GIT_FOUND)
  167. find_package(Git QUIET)
  168. endif()
  169. get_git_head_revision(refspec hash)
  170. if(NOT GIT_FOUND)
  171. set(${_var}
  172. "GIT-NOTFOUND"
  173. PARENT_SCOPE)
  174. return()
  175. endif()
  176. if(NOT hash)
  177. set(${_var}
  178. "HEAD-HASH-NOTFOUND"
  179. PARENT_SCOPE)
  180. return()
  181. endif()
  182. # TODO sanitize
  183. #if((${ARGN}" MATCHES "&&") OR
  184. # (ARGN MATCHES "||") OR
  185. # (ARGN MATCHES "\\;"))
  186. # message("Please report the following error to the project!")
  187. # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
  188. #endif()
  189. #message(STATUS "Arguments to execute_process: ${ARGN}")
  190. execute_process(
  191. COMMAND "${GIT_EXECUTABLE}" describe --tags --always ${hash} ${ARGN}
  192. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  193. RESULT_VARIABLE res
  194. OUTPUT_VARIABLE out
  195. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  196. if(NOT res EQUAL 0)
  197. set(out "${out}-${res}-NOTFOUND")
  198. endif()
  199. set(${_var}
  200. "${out}"
  201. PARENT_SCOPE)
  202. endfunction()
  203. function(git_describe_working_tree _var)
  204. if(NOT GIT_FOUND)
  205. find_package(Git QUIET)
  206. endif()
  207. if(NOT GIT_FOUND)
  208. set(${_var}
  209. "GIT-NOTFOUND"
  210. PARENT_SCOPE)
  211. return()
  212. endif()
  213. execute_process(
  214. COMMAND "${GIT_EXECUTABLE}" describe --dirty ${ARGN}
  215. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  216. RESULT_VARIABLE res
  217. OUTPUT_VARIABLE out
  218. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  219. if(NOT res EQUAL 0)
  220. set(out "${out}-${res}-NOTFOUND")
  221. endif()
  222. set(${_var}
  223. "${out}"
  224. PARENT_SCOPE)
  225. endfunction()
  226. function(git_get_exact_tag _var)
  227. git_describe(out --exact-match ${ARGN})
  228. set(${_var}
  229. "${out}"
  230. PARENT_SCOPE)
  231. endfunction()
  232. function(git_local_changes _var)
  233. if(NOT GIT_FOUND)
  234. find_package(Git QUIET)
  235. endif()
  236. get_git_head_revision(refspec hash)
  237. if(NOT GIT_FOUND)
  238. set(${_var}
  239. "GIT-NOTFOUND"
  240. PARENT_SCOPE)
  241. return()
  242. endif()
  243. if(NOT hash)
  244. set(${_var}
  245. "HEAD-HASH-NOTFOUND"
  246. PARENT_SCOPE)
  247. return()
  248. endif()
  249. execute_process(
  250. COMMAND "${GIT_EXECUTABLE}" diff-index --quiet HEAD --
  251. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  252. RESULT_VARIABLE res
  253. OUTPUT_VARIABLE out
  254. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  255. if(res EQUAL 0)
  256. set(${_var}
  257. "CLEAN"
  258. PARENT_SCOPE)
  259. else()
  260. set(${_var}
  261. "DIRTY"
  262. PARENT_SCOPE)
  263. endif()
  264. endfunction()