GetGitRevisionDescription.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. if(__get_git_revision_description)
  33. return()
  34. endif()
  35. set(__get_git_revision_description YES)
  36. # We must run the following at "include" time, not at function call time,
  37. # to find the path to this module rather than the path to a calling list file
  38. get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
  39. function(get_git_head_revision _refspecvar _hashvar)
  40. set(GIT_PARENT_DIR "${CMAKE_SOURCE_DIR}")
  41. set(GIT_DIR "${GIT_PARENT_DIR}/.git")
  42. while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
  43. set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
  44. get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
  45. if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
  46. # We have reached the root directory, we are not in git
  47. set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
  48. set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
  49. return()
  50. endif()
  51. set(GIT_DIR "${GIT_PARENT_DIR}/.git")
  52. endwhile()
  53. set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
  54. if(NOT EXISTS "${GIT_DATA}")
  55. file(MAKE_DIRECTORY "${GIT_DATA}")
  56. endif()
  57. if(NOT EXISTS "${GIT_DIR}/HEAD")
  58. return()
  59. endif()
  60. set(HEAD_FILE "${GIT_DATA}/HEAD")
  61. configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
  62. configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
  63. "${GIT_DATA}/grabRef.cmake"
  64. @ONLY)
  65. include("${GIT_DATA}/grabRef.cmake")
  66. set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
  67. set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
  68. endfunction()
  69. function(git_rev_parse _var)
  70. if(NOT GIT_FOUND)
  71. find_package(Git QUIET)
  72. endif()
  73. if(NOT GIT_FOUND)
  74. set(${_var} "n/a" PARENT_SCOPE)
  75. return()
  76. endif()
  77. get_git_head_revision(refspec hash)
  78. if(NOT hash)
  79. set(${_var} "n/a" PARENT_SCOPE)
  80. return()
  81. endif()
  82. execute_process(COMMAND "${GIT_EXECUTABLE}" rev-parse ${ARGN} ${hash}
  83. WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
  84. RESULT_VARIABLE res
  85. OUTPUT_VARIABLE out
  86. ERROR_QUIET
  87. OUTPUT_STRIP_TRAILING_WHITESPACE)
  88. if(NOT res EQUAL 0)
  89. set(out "n/a")
  90. endif()
  91. set(${_var} "${out}" PARENT_SCOPE)
  92. endfunction()
  93. function(git_describe _var)
  94. if(NOT GIT_FOUND)
  95. find_package(Git QUIET)
  96. endif()
  97. if(NOT GIT_FOUND)
  98. set(${_var} "n/a" PARENT_SCOPE)
  99. return()
  100. endif()
  101. get_git_head_revision(refspec hash)
  102. if(NOT hash)
  103. set(${_var} "n/a" PARENT_SCOPE)
  104. return()
  105. endif()
  106. execute_process(COMMAND "${GIT_EXECUTABLE}" describe ${hash} ${ARGN}
  107. WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
  108. RESULT_VARIABLE res
  109. OUTPUT_VARIABLE out
  110. ERROR_QUIET
  111. OUTPUT_STRIP_TRAILING_WHITESPACE)
  112. if(NOT res EQUAL 0)
  113. set(out "n/a")
  114. endif()
  115. set(${_var} "${out}" PARENT_SCOPE)
  116. endfunction()
  117. function(git_get_exact_tag _var)
  118. git_describe(out --exact-match ${ARGN})
  119. set(${_var} "${out}" PARENT_SCOPE)
  120. endfunction()
  121. # get git hash
  122. macro(get_git_hash _git_hash)
  123. find_package(Git QUIET)
  124. if(GIT_FOUND)
  125. execute_process(
  126. COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%h
  127. OUTPUT_VARIABLE ${_git_hash}
  128. OUTPUT_STRIP_TRAILING_WHITESPACE
  129. ERROR_QUIET
  130. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  131. )
  132. endif()
  133. endmacro()
  134. # get git branch
  135. macro(get_git_branch _git_branch)
  136. find_package(Git QUIET)
  137. if(GIT_FOUND)
  138. # git branch | grep \"*\" | awk -F \"_\" '{print $2}'
  139. message(STATUS "git found: ${GIT_EXECUTABLE} in version: ${GIT_VERSION_STRING}")
  140. set(CUR_WORKING_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  141. message(STATUS "current directory: ${CUR_WORKING_DIR}")
  142. execute_process(
  143. COMMAND ${GIT_EXECUTABLE} symbolic-ref --short -q HEAD
  144. OUTPUT_VARIABLE ${_git_branch}
  145. OUTPUT_STRIP_TRAILING_WHITESPACE
  146. ERROR_QUIET
  147. WORKING_DIRECTORY
  148. ${CUR_WORKING_DIR}
  149. )
  150. endif()
  151. endmacro()