Fork from inav at github. Modified for some special functions needs.
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.

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