GetGitRevisionDescription.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # https://github.com/rpavlik/cmake-modules
  2. #
  3. # - Returns a version string from Git
  4. #
  5. # These functions force a re-configure on each git commit so that you can
  6. # trust the values of the variables in your build system.
  7. #
  8. # get_git_head_revision(<refspecvar> <hashvar> [<additional arguments to git describe> ...])
  9. #
  10. # Returns the refspec and sha hash of the current head revision
  11. #
  12. # git_describe(<var> [<additional arguments to git describe> ...])
  13. #
  14. # Returns the results of git describe on the source tree, and adjusting
  15. # the output so that it tests false if an error occurs.
  16. #
  17. # git_get_exact_tag(<var> [<additional arguments to git describe> ...])
  18. #
  19. # Returns the results of git describe --exact-match on the source tree,
  20. # and adjusting the output so that it tests false if there was no exact
  21. # matching tag.
  22. #
  23. # Requires CMake 2.6 or newer (uses the 'function' command)
  24. #
  25. # Original Author:
  26. # 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
  27. # http://academic.cleardefinition.com
  28. # Iowa State University HCI Graduate Program/VRAC
  29. #
  30. # Copyright Iowa State University 2009-2010.
  31. # Distributed under the Boost Software License, Version 1.0.
  32. # (See accompanying file LICENSE_1_0.txt or copy at
  33. # http://www.boost.org/LICENSE_1_0.txt)
  34. if(__get_git_revision_description)
  35. return()
  36. endif()
  37. set(__get_git_revision_description YES)
  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. function(get_git_dir _gitdir)
  42. # check FORCED_GIT_DIR first
  43. if(FORCED_GIT_DIR)
  44. set(${_gitdir} ${FORCED_GIT_DIR} PARENT_SCOPE)
  45. return()
  46. endif()
  47. # check GIT_DIR in environment
  48. set(GIT_DIR $ENV{GIT_DIR})
  49. if(NOT GIT_DIR)
  50. set(GIT_PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  51. set(GIT_DIR ${GIT_PARENT_DIR}/.git)
  52. endif()
  53. # .git dir not found, search parent directories
  54. while(NOT EXISTS ${GIT_DIR})
  55. set(GIT_PREVIOUS_PARENT ${GIT_PARENT_DIR})
  56. get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
  57. if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
  58. return()
  59. endif()
  60. set(GIT_DIR ${GIT_PARENT_DIR}/.git)
  61. endwhile()
  62. # check if this is a submodule
  63. if(NOT IS_DIRECTORY ${GIT_DIR})
  64. file(READ ${GIT_DIR} submodule)
  65. string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
  66. get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
  67. get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
  68. endif()
  69. set(${_gitdir} ${GIT_DIR} PARENT_SCOPE)
  70. endfunction()
  71. function(get_git_head_revision _refspecvar _hashvar)
  72. get_git_dir(GIT_DIR)
  73. if(NOT GIT_DIR)
  74. return()
  75. endif()
  76. set(GIT_DATA ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data)
  77. if(NOT EXISTS ${GIT_DATA})
  78. file(MAKE_DIRECTORY ${GIT_DATA})
  79. endif()
  80. if(NOT EXISTS ${GIT_DIR}/HEAD)
  81. return()
  82. endif()
  83. set(HEAD_FILE ${GIT_DATA}/HEAD)
  84. configure_file(${GIT_DIR}/HEAD ${HEAD_FILE} COPYONLY)
  85. configure_file(${_gitdescmoddir}/GetGitRevisionDescription.cmake.in
  86. ${GIT_DATA}/grabRef.cmake
  87. @ONLY)
  88. include(${GIT_DATA}/grabRef.cmake)
  89. set(${_refspecvar} ${HEAD_REF} PARENT_SCOPE)
  90. set(${_hashvar} ${HEAD_HASH} PARENT_SCOPE)
  91. endfunction()
  92. function(git_describe _var)
  93. get_git_dir(GIT_DIR)
  94. if(NOT GIT_DIR)
  95. return()
  96. endif()
  97. if(NOT GIT_FOUND)
  98. find_package(Git QUIET)
  99. endif()
  100. if(NOT GIT_FOUND)
  101. set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
  102. return()
  103. endif()
  104. get_git_head_revision(refspec hash)
  105. if(NOT hash)
  106. set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
  107. return()
  108. endif()
  109. execute_process(COMMAND
  110. ${GIT_EXECUTABLE}
  111. describe
  112. ${hash}
  113. ${ARGN}
  114. WORKING_DIRECTORY
  115. ${GIT_DIR}
  116. RESULT_VARIABLE
  117. res
  118. OUTPUT_VARIABLE
  119. out
  120. ERROR_QUIET
  121. OUTPUT_STRIP_TRAILING_WHITESPACE)
  122. if(NOT res EQUAL 0)
  123. set(out "${out}-${res}-NOTFOUND")
  124. endif()
  125. set(${_var} ${out} PARENT_SCOPE)
  126. endfunction()
  127. function(git_timestamp _var)
  128. get_git_dir(GIT_DIR)
  129. if(NOT GIT_DIR)
  130. return()
  131. endif()
  132. if(NOT GIT_FOUND)
  133. find_package(Git QUIET)
  134. endif()
  135. if(NOT GIT_FOUND)
  136. return()
  137. endif()
  138. get_git_head_revision(refspec hash)
  139. if(NOT hash)
  140. set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
  141. return()
  142. endif()
  143. execute_process(COMMAND ${GIT_EXECUTABLE} log -1 --format="%ci" ${hash} ${ARGN}
  144. WORKING_DIRECTORY ${GIT_DIR}
  145. RESULT_VARIABLE res
  146. OUTPUT_VARIABLE out
  147. ERROR_QUIET
  148. OUTPUT_STRIP_TRAILING_WHITESPACE)
  149. if(res EQUAL 0)
  150. string(REGEX REPLACE "[-\" :]" "" out ${out})
  151. string(SUBSTRING ${out} 0 12 out)
  152. else()
  153. set(out "${out}-${res}-NOTFOUND")
  154. endif()
  155. set(${_var} ${out} PARENT_SCOPE)
  156. endfunction()
  157. function(git_get_exact_tag _var)
  158. git_describe(out --exact-match ${ARGN})
  159. set(${_var} ${out} PARENT_SCOPE)
  160. endfunction()