GetGitRevisionDescription.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # SPDX-FileCopyrightText: 2009 Iowa State University
  2. # SPDX-FileContributor: Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
  3. # SPDX-License-Identifier: BSL-1.0
  4. # - Returns a version string from Git
  5. #
  6. # These functions force a re-configure on each git commit so that you can
  7. # trust the values of the variables in your build system.
  8. #
  9. # get_git_head_revision(<refspecvar> <hashvar> [<additional arguments to git describe> ...])
  10. #
  11. # Returns the refspec and sha hash of the current head revision
  12. #
  13. # git_describe(<var> [<additional arguments to git describe> ...])
  14. #
  15. # Returns the results of git describe on the source tree, and adjusting
  16. # the output so that it tests false if an error occurs.
  17. #
  18. # git_get_exact_tag(<var> [<additional arguments to git describe> ...])
  19. #
  20. # Returns the results of git describe --exact-match on the source tree,
  21. # and adjusting the output so that it tests false if there was no exact
  22. # matching tag.
  23. #
  24. # Requires CMake 2.6 or newer (uses the 'function' command)
  25. #
  26. # Original Author:
  27. # 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
  28. # http://academic.cleardefinition.com
  29. # Iowa State University HCI Graduate Program/VRAC
  30. #
  31. # Copyright Iowa State University 2009-2010.
  32. # Distributed under the Boost Software License, Version 1.0.
  33. # (See accompanying file LICENSE_1_0.txt or copy at
  34. # http://www.boost.org/LICENSE_1_0.txt)
  35. if(__get_git_revision_description)
  36. return()
  37. endif()
  38. set(__get_git_revision_description YES)
  39. # We must run the following at "include" time, not at function call time,
  40. # to find the path to this module rather than the path to a calling list file
  41. get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
  42. function(get_git_head_revision _refspecvar _hashvar)
  43. set(GIT_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
  44. set(GIT_DIR "${GIT_PARENT_DIR}/.git")
  45. while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
  46. set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
  47. get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
  48. if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
  49. # We have reached the root directory, we are not in git
  50. set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
  51. set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
  52. return()
  53. endif()
  54. set(GIT_DIR "${GIT_PARENT_DIR}/.git")
  55. endwhile()
  56. # check if this is a submodule
  57. if(NOT IS_DIRECTORY ${GIT_DIR})
  58. file(READ ${GIT_DIR} submodule)
  59. string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
  60. get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
  61. get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
  62. endif()
  63. set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
  64. if(NOT EXISTS "${GIT_DATA}")
  65. file(MAKE_DIRECTORY "${GIT_DATA}")
  66. endif()
  67. if(NOT EXISTS "${GIT_DIR}/HEAD")
  68. return()
  69. endif()
  70. set(HEAD_FILE "${GIT_DATA}/HEAD")
  71. configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
  72. configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
  73. "${GIT_DATA}/grabRef.cmake"
  74. @ONLY)
  75. include("${GIT_DATA}/grabRef.cmake")
  76. set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
  77. set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
  78. endfunction()
  79. function(git_branch_name _var)
  80. if(NOT GIT_FOUND)
  81. find_package(Git QUIET)
  82. endif()
  83. if(NOT GIT_FOUND)
  84. set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
  85. return()
  86. endif()
  87. execute_process(COMMAND
  88. "${GIT_EXECUTABLE}"
  89. rev-parse --abbrev-ref HEAD
  90. WORKING_DIRECTORY
  91. "${CMAKE_SOURCE_DIR}"
  92. RESULT_VARIABLE
  93. res
  94. OUTPUT_VARIABLE
  95. out
  96. ERROR_QUIET
  97. OUTPUT_STRIP_TRAILING_WHITESPACE)
  98. if(NOT res EQUAL 0)
  99. set(out "${out}-${res}-NOTFOUND")
  100. endif()
  101. set(${_var} "${out}" PARENT_SCOPE)
  102. endfunction()
  103. function(git_describe _var)
  104. if(NOT GIT_FOUND)
  105. find_package(Git QUIET)
  106. endif()
  107. #get_git_head_revision(refspec hash)
  108. if(NOT GIT_FOUND)
  109. set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
  110. return()
  111. endif()
  112. #if(NOT hash)
  113. # set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
  114. # return()
  115. #endif()
  116. # TODO sanitize
  117. #if((${ARGN}" MATCHES "&&") OR
  118. # (ARGN MATCHES "||") OR
  119. # (ARGN MATCHES "\\;"))
  120. # message("Please report the following error to the project!")
  121. # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
  122. #endif()
  123. #message(STATUS "Arguments to execute_process: ${ARGN}")
  124. execute_process(COMMAND
  125. "${GIT_EXECUTABLE}"
  126. describe
  127. ${hash}
  128. ${ARGN}
  129. WORKING_DIRECTORY
  130. "${CMAKE_SOURCE_DIR}"
  131. RESULT_VARIABLE
  132. res
  133. OUTPUT_VARIABLE
  134. out
  135. ERROR_QUIET
  136. OUTPUT_STRIP_TRAILING_WHITESPACE)
  137. if(NOT res EQUAL 0)
  138. set(out "${out}-${res}-NOTFOUND")
  139. endif()
  140. set(${_var} "${out}" PARENT_SCOPE)
  141. endfunction()
  142. function(git_get_exact_tag _var)
  143. git_describe(out --exact-match ${ARGN})
  144. set(${_var} "${out}" PARENT_SCOPE)
  145. endfunction()