GetGitRevisionDescription.cmake 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <[email protected]> <[email protected]>
  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. # Fallback for reftable or other storage formats
  159. if(NOT HEAD_HASH OR HEAD_HASH STREQUAL "")
  160. find_package(Git QUIET)
  161. if(GIT_FOUND)
  162. execute_process(
  163. COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD
  164. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  165. RESULT_VARIABLE res
  166. OUTPUT_VARIABLE HEAD_HASH
  167. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  168. if(NOT res EQUAL 0)
  169. set(HEAD_HASH "")
  170. endif()
  171. endif()
  172. endif()
  173. set(${_refspecvar}
  174. "${HEAD_REF}"
  175. PARENT_SCOPE)
  176. set(${_hashvar}
  177. "${HEAD_HASH}"
  178. PARENT_SCOPE)
  179. endfunction()
  180. function(git_describe _var)
  181. if(NOT GIT_FOUND)
  182. find_package(Git QUIET)
  183. endif()
  184. get_git_head_revision(refspec hash)
  185. if(NOT GIT_FOUND)
  186. set(${_var}
  187. "GIT-NOTFOUND"
  188. PARENT_SCOPE)
  189. return()
  190. endif()
  191. if(NOT hash)
  192. set(${_var}
  193. "HEAD-HASH-NOTFOUND"
  194. PARENT_SCOPE)
  195. return()
  196. endif()
  197. # TODO sanitize
  198. #if((${ARGN}" MATCHES "&&") OR
  199. # (ARGN MATCHES "||") OR
  200. # (ARGN MATCHES "\\;"))
  201. # message("Please report the following error to the project!")
  202. # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
  203. #endif()
  204. #message(STATUS "Arguments to execute_process: ${ARGN}")
  205. execute_process(
  206. COMMAND "${GIT_EXECUTABLE}" describe --tags --always ${hash} ${ARGN}
  207. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  208. RESULT_VARIABLE res
  209. OUTPUT_VARIABLE out
  210. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  211. if(NOT res EQUAL 0)
  212. set(out "${out}-${res}-NOTFOUND")
  213. endif()
  214. set(${_var}
  215. "${out}"
  216. PARENT_SCOPE)
  217. endfunction()
  218. function(git_describe_working_tree _var)
  219. if(NOT GIT_FOUND)
  220. find_package(Git QUIET)
  221. endif()
  222. if(NOT GIT_FOUND)
  223. set(${_var}
  224. "GIT-NOTFOUND"
  225. PARENT_SCOPE)
  226. return()
  227. endif()
  228. execute_process(
  229. COMMAND "${GIT_EXECUTABLE}" describe --dirty ${ARGN}
  230. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  231. RESULT_VARIABLE res
  232. OUTPUT_VARIABLE out
  233. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  234. if(NOT res EQUAL 0)
  235. set(out "${out}-${res}-NOTFOUND")
  236. endif()
  237. set(${_var}
  238. "${out}"
  239. PARENT_SCOPE)
  240. endfunction()
  241. function(git_get_exact_tag _var)
  242. git_describe(out --exact-match ${ARGN})
  243. set(${_var}
  244. "${out}"
  245. PARENT_SCOPE)
  246. endfunction()
  247. function(git_local_changes _var)
  248. if(NOT GIT_FOUND)
  249. find_package(Git QUIET)
  250. endif()
  251. get_git_head_revision(refspec hash)
  252. if(NOT GIT_FOUND)
  253. set(${_var}
  254. "GIT-NOTFOUND"
  255. PARENT_SCOPE)
  256. return()
  257. endif()
  258. if(NOT hash)
  259. set(${_var}
  260. "HEAD-HASH-NOTFOUND"
  261. PARENT_SCOPE)
  262. return()
  263. endif()
  264. execute_process(
  265. COMMAND "${GIT_EXECUTABLE}" diff-index --quiet HEAD --
  266. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  267. RESULT_VARIABLE res
  268. OUTPUT_VARIABLE out
  269. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  270. if(res EQUAL 0)
  271. set(${_var}
  272. "CLEAN"
  273. PARENT_SCOPE)
  274. else()
  275. set(${_var}
  276. "DIRTY"
  277. PARENT_SCOPE)
  278. endif()
  279. endfunction()