GetGitRevisionDescription.cmake 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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> <repo dir> [<additional arguments to git describe> ...])
  7. #
  8. # Returns the refspec and sha hash of the current head revision
  9. #
  10. # git_describe(<var> <repo dir> [<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> <repo dir> [<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. #
  33. # Updated 2018 Espressif Systems to add _repo_dir argument
  34. # to get revision of other repositories
  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 _repo_dir)
  43. execute_process(COMMAND
  44. "${GIT_EXECUTABLE}"
  45. rev-parse
  46. --git-dir
  47. WORKING_DIRECTORY
  48. ${_repo_dir}
  49. RESULT_VARIABLE
  50. res
  51. OUTPUT_VARIABLE
  52. GIT_DIR
  53. ERROR_VARIABLE
  54. error
  55. OUTPUT_STRIP_TRAILING_WHITESPACE)
  56. if(NOT res EQUAL 0)
  57. string(STRIP "${error}" error)
  58. message(STATUS "git rev-parse returned '${error}'")
  59. return()
  60. endif()
  61. get_filename_component(GIT_DIR "${GIT_DIR}" ABSOLUTE BASE_DIR "${_repo_dir}")
  62. set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
  63. if(NOT EXISTS "${GIT_DATA}")
  64. file(MAKE_DIRECTORY "${GIT_DATA}")
  65. endif()
  66. if(NOT EXISTS "${GIT_DIR}/HEAD")
  67. return()
  68. endif()
  69. set(HEAD_FILE "${GIT_DATA}/HEAD")
  70. configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
  71. configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
  72. "${GIT_DATA}/grabRef.cmake"
  73. @ONLY)
  74. include("${GIT_DATA}/grabRef.cmake")
  75. set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
  76. set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
  77. endfunction()
  78. function(git_describe _var _repo_dir)
  79. if(NOT GIT_FOUND)
  80. find_package(Git QUIET)
  81. endif()
  82. get_git_head_revision(refspec hash "${_repo_dir}")
  83. if(NOT GIT_FOUND)
  84. set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
  85. return()
  86. endif()
  87. if(NOT hash)
  88. set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
  89. return()
  90. endif()
  91. # TODO sanitize
  92. #if((${ARGN}" MATCHES "&&") OR
  93. # (ARGN MATCHES "||") OR
  94. # (ARGN MATCHES "\\;"))
  95. # message("Please report the following error to the project!")
  96. # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
  97. #endif()
  98. #message(STATUS "Arguments to execute_process: ${ARGN}")
  99. execute_process(COMMAND
  100. "${GIT_EXECUTABLE}"
  101. "-C"
  102. ${_repo_dir}
  103. describe
  104. "--always"
  105. "--tags"
  106. "--dirty"
  107. ${ARGN}
  108. WORKING_DIRECTORY
  109. "${CMAKE_CURRENT_SOURCE_DIR}"
  110. RESULT_VARIABLE
  111. res
  112. OUTPUT_VARIABLE
  113. out
  114. ERROR_VARIABLE
  115. error
  116. OUTPUT_STRIP_TRAILING_WHITESPACE)
  117. if(NOT res EQUAL 0)
  118. string(STRIP "${error}" error)
  119. message(STATUS "git describe returned '${error}'")
  120. set(out "${out}-${res}-NOTFOUND")
  121. endif()
  122. set(${_var} "${out}" PARENT_SCOPE)
  123. endfunction()
  124. function(git_get_exact_tag _var _repo_dir)
  125. git_describe(out "${_repo_dir}" --exact-match ${ARGN})
  126. set(${_var} "${out}" PARENT_SCOPE)
  127. endfunction()