You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.0 KiB

  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 GetGitRevisionDescription.cmake PATH)
  39. function(get_git_head_revision _refspecvar _hashvar)
  40. set(GIT_PARENT_DIR ${PROJECT_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. # check if this is a submodule
  54. if(NOT IS_DIRECTORY ${GIT_DIR})
  55. file(READ ${GIT_DIR} submodule)
  56. string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
  57. get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
  58. get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
  59. endif()
  60. set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
  61. if(NOT EXISTS "${GIT_DATA}")
  62. file(MAKE_DIRECTORY "${GIT_DATA}")
  63. endif()
  64. if(NOT EXISTS "${GIT_DIR}/HEAD")
  65. return()
  66. endif()
  67. set(HEAD_FILE "${GIT_DATA}/HEAD")
  68. configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
  69. configure_file("${PROJECT_SOURCE_DIR}/resources/cmake/macros/GetGitRevisionDescription.cmake.in"
  70. "${GIT_DATA}/grabRef.cmake"
  71. @ONLY)
  72. include("${GIT_DATA}/grabRef.cmake")
  73. set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
  74. set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
  75. endfunction()
  76. function(git_describe _var)
  77. if(NOT GIT_FOUND)
  78. find_package(Git QUIET)
  79. endif()
  80. get_git_head_revision(refspec hash)
  81. if(NOT GIT_FOUND)
  82. set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
  83. return()
  84. endif()
  85. if(NOT hash)
  86. set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
  87. return()
  88. endif()
  89. # TODO sanitize
  90. #if((${ARGN}" MATCHES "&&") OR
  91. # (ARGN MATCHES "||") OR
  92. # (ARGN MATCHES "\\;"))
  93. # message("Please report the following error to the project!")
  94. # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
  95. #endif()
  96. #message(STATUS "Arguments to execute_process: ${ARGN}")
  97. execute_process(COMMAND
  98. "${GIT_EXECUTABLE}"
  99. describe
  100. ${hash}
  101. ${ARGN}
  102. WORKING_DIRECTORY
  103. "${CMAKE_SOURCE_DIR}"
  104. RESULT_VARIABLE
  105. res
  106. OUTPUT_VARIABLE
  107. out
  108. ERROR_QUIET
  109. OUTPUT_STRIP_TRAILING_WHITESPACE)
  110. if(NOT res EQUAL 0)
  111. set(out "${out}-${res}-NOTFOUND")
  112. endif()
  113. set(${_var} "${out}" PARENT_SCOPE)
  114. endfunction()
  115. function(git_describe_checkout _var)
  116. if(NOT GIT_FOUND)
  117. find_package(Git QUIET)
  118. endif()
  119. get_git_head_revision(refspec hash)
  120. if(NOT GIT_FOUND)
  121. set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
  122. return()
  123. endif()
  124. if(NOT hash)
  125. set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
  126. return()
  127. endif()
  128. # TODO sanitize
  129. #if((${ARGN}" MATCHES "&&") OR
  130. # (ARGN MATCHES "||") OR
  131. # (ARGN MATCHES "\\;"))
  132. # message("Please report the following error to the project!")
  133. # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
  134. #endif()
  135. #message(STATUS "Arguments to execute_process: ${ARGN}")
  136. execute_process(COMMAND
  137. "${GIT_EXECUTABLE}"
  138. describe
  139. --tags
  140. --dirty=-dirty
  141. --long
  142. ${ARGN}
  143. WORKING_DIRECTORY
  144. "${CMAKE_SOURCE_DIR}"
  145. RESULT_VARIABLE
  146. res
  147. OUTPUT_VARIABLE
  148. out
  149. ERROR_QUIET
  150. OUTPUT_STRIP_TRAILING_WHITESPACE)
  151. if(NOT res EQUAL 0)
  152. set(out "${out}-${res}-NOTFOUND")
  153. endif()
  154. set(${_var} "${out}" PARENT_SCOPE)
  155. endfunction()
  156. function(git_get_exact_tag _var)
  157. git_describe(out --exact-match ${ARGN})
  158. set(${_var} "${out}" PARENT_SCOPE)
  159. endfunction()