GetGitRevisionDescription.cmake 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 ${CMAKE_CURRENT_LIST_FILE} PATH)
  39. function(get_git_head_revision _refspecvar _hashvar)
  40. set(GIT_DIR "${PROJECT_SOURCE_DIR}/.git")
  41. if (NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
  42. set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
  43. set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
  44. return()
  45. endif()
  46. set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
  47. if(NOT EXISTS "${GIT_DATA}")
  48. file(MAKE_DIRECTORY "${GIT_DATA}")
  49. endif()
  50. if(NOT EXISTS "${GIT_DIR}/HEAD")
  51. return()
  52. endif()
  53. set(HEAD_FILE "${GIT_DATA}/HEAD")
  54. configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
  55. configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
  56. "${GIT_DATA}/grabRef.cmake"
  57. @ONLY)
  58. include("${GIT_DATA}/grabRef.cmake")
  59. set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
  60. set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
  61. endfunction()
  62. function(git_describe _var)
  63. if(NOT GIT_FOUND)
  64. find_package(Git QUIET)
  65. endif()
  66. get_git_head_revision(refspec hash)
  67. if(NOT GIT_FOUND)
  68. set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
  69. return()
  70. endif()
  71. if(NOT hash)
  72. set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
  73. return()
  74. endif()
  75. # TODO sanitize
  76. #if((${ARGN}" MATCHES "&&") OR
  77. # (ARGN MATCHES "||") OR
  78. # (ARGN MATCHES "\\;"))
  79. # message("Please report the following error to the project!")
  80. # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
  81. #endif()
  82. #message(STATUS "Arguments to execute_process: ${ARGN}")
  83. execute_process(COMMAND
  84. "${GIT_EXECUTABLE}"
  85. describe
  86. ${hash}
  87. ${ARGN}
  88. WORKING_DIRECTORY
  89. "${CMAKE_SOURCE_DIR}"
  90. RESULT_VARIABLE
  91. res
  92. OUTPUT_VARIABLE
  93. out
  94. ERROR_QUIET
  95. OUTPUT_STRIP_TRAILING_WHITESPACE)
  96. if(NOT res EQUAL 0)
  97. set(out "${out}-${res}-NOTFOUND")
  98. endif()
  99. set(${_var} "${out}" PARENT_SCOPE)
  100. endfunction()
  101. function(git_get_exact_tag _var)
  102. git_describe(out --exact-match ${ARGN})
  103. set(${_var} "${out}" PARENT_SCOPE)
  104. endfunction()