GetGitRevisionDescription.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # * Returns a version string from Git
  2. #
  3. # These functions force a re-configure on each git commit so that you can trust the values of the
  4. # 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 the output so that it tests
  13. # 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, and adjusting the output so
  18. # that it tests false if there was no exact matching tag.
  19. #
  20. # git_local_changes(<var>)
  21. #
  22. # Returns either "CLEAN" or "DIRTY" with respect to uncommitted changes. Uses the return code of
  23. # "git diff-index --quiet HEAD --". Does not regard untracked files.
  24. #
  25. # git_count_parent_commits(<var>)
  26. #
  27. # Returns number of commits preceeding current commit -1 if git rev-list --count HEAD failed or
  28. # "GIT-NOTFOUND" if git executable was not found or "HEAD-HASH-NOTFOUND" if head hash was not found.
  29. # I don't know if get_git_head_revision() must be called internally or not, as reason of calling it
  30. # is not clear for me also in git_local_changes().
  31. #
  32. # Requires CMake 2.6 or newer (uses the 'function' command)
  33. #
  34. # Original Author: 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
  35. # http://academic.cleardefinition.com Iowa State University HCI Graduate Program/VRAC
  36. #
  37. # Copyright Iowa State University 2009-2010. Distributed under the Boost Software License, Version
  38. # 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  39. if(__get_git_revision_description)
  40. return()
  41. endif()
  42. set(__get_git_revision_description YES)
  43. # We must run the following at "include" time, not at function call time, to find the path to this
  44. # module rather than the path to a calling list file
  45. get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
  46. function(get_git_head_revision _refspecvar _hashvar)
  47. set(GIT_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
  48. set(GIT_DIR "${GIT_PARENT_DIR}/.git")
  49. while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
  50. set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
  51. get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
  52. if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
  53. # We have reached the root directory, we are not in git
  54. set(${_refspecvar}
  55. "GITDIR-NOTFOUND"
  56. PARENT_SCOPE
  57. )
  58. set(${_hashvar}
  59. "GITDIR-NOTFOUND"
  60. PARENT_SCOPE
  61. )
  62. return()
  63. endif()
  64. set(GIT_DIR "${GIT_PARENT_DIR}/.git")
  65. endwhile()
  66. # check if this is a submodule
  67. if(NOT IS_DIRECTORY ${GIT_DIR})
  68. file(READ ${GIT_DIR} submodule)
  69. string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
  70. get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
  71. get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
  72. endif()
  73. set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
  74. if(NOT EXISTS "${GIT_DATA}")
  75. file(MAKE_DIRECTORY "${GIT_DATA}")
  76. endif()
  77. if(NOT EXISTS "${GIT_DIR}/HEAD")
  78. return()
  79. endif()
  80. set(HEAD_FILE "${GIT_DATA}/HEAD")
  81. configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
  82. configure_file(
  83. "${_gitdescmoddir}/GetGitRevisionDescription.cmake.in" "${GIT_DATA}/grabRef.cmake" @ONLY
  84. )
  85. include("${GIT_DATA}/grabRef.cmake")
  86. set(${_refspecvar}
  87. "${HEAD_REF}"
  88. PARENT_SCOPE
  89. )
  90. set(${_hashvar}
  91. "${HEAD_HASH}"
  92. PARENT_SCOPE
  93. )
  94. endfunction()
  95. function(git_describe _var)
  96. if(NOT GIT_FOUND)
  97. find_package(Git QUIET)
  98. endif()
  99. get_git_head_revision(refspec hash)
  100. if(NOT GIT_FOUND)
  101. set(${_var}
  102. "GIT-NOTFOUND"
  103. PARENT_SCOPE
  104. )
  105. return()
  106. endif()
  107. if(NOT hash)
  108. set(${_var}
  109. "HEAD-HASH-NOTFOUND"
  110. PARENT_SCOPE
  111. )
  112. return()
  113. endif()
  114. # TODO sanitize if((${ARGN}" MATCHES "&&") OR (ARGN MATCHES "||") OR (ARGN MATCHES "\\;"))
  115. # message("Please report the following error to the project!") message(FATAL_ERROR "Looks like
  116. # someone's doing something nefarious with git_describe! Passed arguments ${ARGN}") endif()
  117. # message(STATUS "Arguments to execute_process: ${ARGN}")
  118. execute_process(
  119. COMMAND "${GIT_EXECUTABLE}" describe ${hash} ${ARGN}
  120. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  121. RESULT_VARIABLE res
  122. OUTPUT_VARIABLE out
  123. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE
  124. )
  125. if(NOT res EQUAL 0)
  126. set(out "${out}-${res}-NOTFOUND")
  127. endif()
  128. set(${_var}
  129. "${out}"
  130. PARENT_SCOPE
  131. )
  132. endfunction()
  133. function(git_get_exact_tag _var)
  134. git_describe(out --exact-match ${ARGN})
  135. set(${_var}
  136. "${out}"
  137. PARENT_SCOPE
  138. )
  139. endfunction()
  140. function(git_local_changes _var)
  141. if(NOT GIT_FOUND)
  142. find_package(Git QUIET)
  143. endif()
  144. get_git_head_revision(refspec hash)
  145. if(NOT GIT_FOUND)
  146. set(${_var}
  147. "GIT-NOTFOUND"
  148. PARENT_SCOPE
  149. )
  150. return()
  151. endif()
  152. if(NOT hash)
  153. set(${_var}
  154. "HEAD-HASH-NOTFOUND"
  155. PARENT_SCOPE
  156. )
  157. return()
  158. endif()
  159. execute_process(
  160. COMMAND "${GIT_EXECUTABLE}" diff-index --quiet HEAD --
  161. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  162. RESULT_VARIABLE res
  163. OUTPUT_VARIABLE out
  164. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE
  165. )
  166. if(res EQUAL 0)
  167. set(${_var}
  168. "CLEAN"
  169. PARENT_SCOPE
  170. )
  171. else()
  172. set(${_var}
  173. "DIRTY"
  174. PARENT_SCOPE
  175. )
  176. endif()
  177. endfunction()
  178. function(git_count_parent_commits _var)
  179. if(NOT GIT_FOUND)
  180. find_package(Git QUIET)
  181. endif()
  182. get_git_head_revision(refspec hash)
  183. if(NOT GIT_FOUND)
  184. set(${_var}
  185. "GIT-NOTFOUND"
  186. PARENT_SCOPE
  187. )
  188. return()
  189. endif()
  190. if(NOT hash)
  191. set(${_var}
  192. "HEAD-HASH-NOTFOUND"
  193. PARENT_SCOPE
  194. )
  195. return()
  196. endif()
  197. execute_process(
  198. COMMAND "${GIT_EXECUTABLE}" rev-list --count HEAD
  199. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  200. RESULT_VARIABLE res
  201. OUTPUT_VARIABLE out
  202. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE
  203. )
  204. if(res EQUAL 0)
  205. set(${_var}
  206. "${out}"
  207. PARENT_SCOPE
  208. )
  209. else()
  210. set(${_var}
  211. "-1"
  212. PARENT_SCOPE
  213. )
  214. endif()
  215. endfunction()