GetGitRevisionDescription.cmake 5.4 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. # git_local_changes(<var>)
  22. #
  23. # Returns either "CLEAN" or "DIRTY" with respect to uncommitted changes.
  24. # Uses the return code of "git diff-index --quiet HEAD --".
  25. # Does not regard untracked files.
  26. #
  27. # Requires CMake 2.6 or newer (uses the 'function' command)
  28. #
  29. # Original Author:
  30. # 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
  31. # http://academic.cleardefinition.com
  32. # Iowa State University HCI Graduate Program/VRAC
  33. #
  34. # Copyright Iowa State University 2009-2010.
  35. # Distributed under the Boost Software License, Version 1.0.
  36. # (See accompanying file LICENSE_1_0.txt or copy at
  37. # http://www.boost.org/LICENSE_1_0.txt)
  38. if(__get_git_revision_description)
  39. return()
  40. endif()
  41. set(__get_git_revision_description YES)
  42. # We must run the following at "include" time, not at function call time,
  43. # to find the path to this module rather than the path to a calling list file
  44. get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
  45. function(get_git_head_revision _refspecvar _hashvar)
  46. set(GIT_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
  47. set(GIT_DIR "${GIT_PARENT_DIR}/.git")
  48. while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
  49. set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
  50. get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
  51. if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
  52. # We have reached the root directory, we are not in git
  53. set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
  54. set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
  55. return()
  56. endif()
  57. set(GIT_DIR "${GIT_PARENT_DIR}/.git")
  58. endwhile()
  59. # check if this is a submodule
  60. if(NOT IS_DIRECTORY ${GIT_DIR})
  61. file(READ ${GIT_DIR} submodule)
  62. string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
  63. get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
  64. get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
  65. endif()
  66. set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
  67. if(NOT EXISTS "${GIT_DATA}")
  68. file(MAKE_DIRECTORY "${GIT_DATA}")
  69. endif()
  70. if(NOT EXISTS "${GIT_DIR}/HEAD")
  71. return()
  72. endif()
  73. set(HEAD_FILE "${GIT_DATA}/HEAD")
  74. configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
  75. configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
  76. "${GIT_DATA}/grabRef.cmake"
  77. @ONLY)
  78. include("${GIT_DATA}/grabRef.cmake")
  79. set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
  80. set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
  81. endfunction()
  82. function(git_describe _var)
  83. if(NOT GIT_FOUND)
  84. find_package(Git QUIET)
  85. endif()
  86. get_git_head_revision(refspec hash)
  87. if(NOT GIT_FOUND)
  88. set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
  89. return()
  90. endif()
  91. if(NOT hash)
  92. set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
  93. return()
  94. endif()
  95. # TODO sanitize
  96. #if((${ARGN}" MATCHES "&&") OR
  97. # (ARGN MATCHES "||") OR
  98. # (ARGN MATCHES "\\;"))
  99. # message("Please report the following error to the project!")
  100. # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
  101. #endif()
  102. #message(STATUS "Arguments to execute_process: ${ARGN}")
  103. execute_process(COMMAND
  104. "${GIT_EXECUTABLE}"
  105. describe
  106. #${hash}
  107. ${ARGN}
  108. WORKING_DIRECTORY
  109. "${CMAKE_CURRENT_SOURCE_DIR}"
  110. RESULT_VARIABLE
  111. res
  112. OUTPUT_VARIABLE
  113. out
  114. ERROR_QUIET
  115. OUTPUT_STRIP_TRAILING_WHITESPACE)
  116. if(NOT res EQUAL 0)
  117. set(out "${out}-${res}-NOTFOUND")
  118. endif()
  119. set(${_var} "${out}" PARENT_SCOPE)
  120. endfunction()
  121. function(git_get_exact_tag _var)
  122. git_describe(out --exact-match ${ARGN})
  123. set(${_var} "${out}" PARENT_SCOPE)
  124. endfunction()
  125. function(git_local_changes _var)
  126. if(NOT GIT_FOUND)
  127. find_package(Git QUIET)
  128. endif()
  129. get_git_head_revision(refspec hash)
  130. if(NOT GIT_FOUND)
  131. set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
  132. return()
  133. endif()
  134. if(NOT hash)
  135. set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
  136. return()
  137. endif()
  138. execute_process(COMMAND
  139. "${GIT_EXECUTABLE}"
  140. diff-index --quiet HEAD --
  141. WORKING_DIRECTORY
  142. "${CMAKE_CURRENT_SOURCE_DIR}"
  143. RESULT_VARIABLE
  144. res
  145. OUTPUT_VARIABLE
  146. out
  147. ERROR_QUIET
  148. OUTPUT_STRIP_TRAILING_WHITESPACE)
  149. if(res EQUAL 0)
  150. set(${_var} "CLEAN" PARENT_SCOPE)
  151. else()
  152. set(${_var} "DIRTY" PARENT_SCOPE)
  153. endif()
  154. endfunction()