GetGitRevisionDescription.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_QUIET
  54. OUTPUT_STRIP_TRAILING_WHITESPACE)
  55. if(NOT res EQUAL 0)
  56. return()
  57. endif()
  58. get_filename_component(GIT_DIR "${GIT_DIR}" ABSOLUTE BASE_DIR "${_repo_dir}")
  59. set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
  60. if(NOT EXISTS "${GIT_DATA}")
  61. file(MAKE_DIRECTORY "${GIT_DATA}")
  62. endif()
  63. if(NOT EXISTS "${GIT_DIR}/HEAD")
  64. return()
  65. endif()
  66. set(HEAD_FILE "${GIT_DATA}/HEAD")
  67. configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
  68. configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
  69. "${GIT_DATA}/grabRef.cmake"
  70. @ONLY)
  71. include("${GIT_DATA}/grabRef.cmake")
  72. set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
  73. set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
  74. endfunction()
  75. function(git_describe _var _repo_dir)
  76. if(NOT GIT_FOUND)
  77. find_package(Git QUIET)
  78. endif()
  79. get_git_head_revision(refspec hash "${_repo_dir}")
  80. if(NOT GIT_FOUND)
  81. set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
  82. return()
  83. endif()
  84. if(NOT hash)
  85. set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
  86. return()
  87. endif()
  88. # TODO sanitize
  89. #if((${ARGN}" MATCHES "&&") OR
  90. # (ARGN MATCHES "||") OR
  91. # (ARGN MATCHES "\\;"))
  92. # message("Please report the following error to the project!")
  93. # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
  94. #endif()
  95. #message(STATUS "Arguments to execute_process: ${ARGN}")
  96. execute_process(COMMAND
  97. "${GIT_EXECUTABLE}"
  98. "-C"
  99. ${_repo_dir}
  100. describe
  101. "--always"
  102. "--tags"
  103. "--dirty"
  104. ${ARGN}
  105. WORKING_DIRECTORY
  106. "${CMAKE_CURRENT_SOURCE_DIR}"
  107. RESULT_VARIABLE
  108. res
  109. OUTPUT_VARIABLE
  110. out
  111. ERROR_QUIET
  112. OUTPUT_STRIP_TRAILING_WHITESPACE)
  113. if(NOT res EQUAL 0)
  114. set(out "${out}-${res}-NOTFOUND")
  115. endif()
  116. set(${_var} "${out}" PARENT_SCOPE)
  117. endfunction()
  118. function(git_get_exact_tag _var _repo_dir)
  119. git_describe(out "${_repo_dir}" --exact-match ${ARGN})
  120. set(${_var} "${out}" PARENT_SCOPE)
  121. endfunction()