GetGitVersion.cmake 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # - Returns a version string from Git tags
  2. #
  3. # This function inspects the annotated git tags for the project and returns a string
  4. # into a CMake variable
  5. #
  6. # get_git_version(<var>)
  7. #
  8. # - Example
  9. #
  10. # include(GetGitVersion)
  11. # get_git_version(GIT_VERSION)
  12. #
  13. # Requires CMake 2.6+
  14. if(__get_git_version)
  15. return()
  16. endif()
  17. set(__get_git_version INCLUDED)
  18. function(get_git_version var)
  19. execute_process(COMMAND git describe --match "v[0-9]*.[0-9]*.[0-9]*" --abbrev=8
  20. RESULT_VARIABLE status
  21. OUTPUT_VARIABLE GIT_VERSION
  22. ERROR_QUIET)
  23. if(${status})
  24. set(GIT_VERSION "v0.0.0")
  25. else()
  26. string(STRIP ${GIT_VERSION} GIT_VERSION)
  27. string(REGEX REPLACE "-[0-9]+-g" "-" GIT_VERSION ${GIT_VERSION})
  28. endif()
  29. # Work out if the repository is dirty
  30. execute_process(COMMAND git update-index -q --refresh
  31. OUTPUT_QUIET
  32. ERROR_QUIET)
  33. execute_process(COMMAND git diff-index --name-only HEAD --
  34. OUTPUT_VARIABLE GIT_DIFF_INDEX
  35. ERROR_QUIET)
  36. string(COMPARE NOTEQUAL "${GIT_DIFF_INDEX}" "" GIT_DIRTY)
  37. if (${GIT_DIRTY})
  38. set(GIT_VERSION "${GIT_VERSION}-dirty")
  39. endif()
  40. message("-- git Version: ${GIT_VERSION}")
  41. set(${var} ${GIT_VERSION} PARENT_SCOPE)
  42. endfunction()