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.

58 lines
1.8 KiB

1 month ago
  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.8.11+
  14. find_package(Git)
  15. if(__get_git_version)
  16. return()
  17. endif()
  18. set(__get_git_version INCLUDED)
  19. function(get_git_version var)
  20. if(GIT_EXECUTABLE)
  21. execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --match "v[0-9]*.[0-9]*.[0-9]*" --abbrev=8
  22. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  23. RESULT_VARIABLE status
  24. OUTPUT_VARIABLE GIT_DESCRIBE_VERSION
  25. ERROR_QUIET)
  26. if(status)
  27. set(GIT_DESCRIBE_VERSION "v0.0.0")
  28. endif()
  29. string(STRIP ${GIT_DESCRIBE_VERSION} GIT_DESCRIBE_VERSION)
  30. if(GIT_DESCRIBE_VERSION MATCHES v[^-]*-)
  31. string(REGEX REPLACE "v([^-]*)-([0-9]+)-.*" "\\1.\\2" GIT_VERSION ${GIT_DESCRIBE_VERSION})
  32. else()
  33. string(REGEX REPLACE "v(.*)" "\\1" GIT_VERSION ${GIT_DESCRIBE_VERSION})
  34. endif()
  35. # Work out if the repository is dirty
  36. execute_process(COMMAND ${GIT_EXECUTABLE} update-index -q --refresh
  37. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  38. OUTPUT_QUIET
  39. ERROR_QUIET)
  40. execute_process(COMMAND ${GIT_EXECUTABLE} diff-index --name-only HEAD --
  41. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  42. OUTPUT_VARIABLE GIT_DIFF_INDEX
  43. ERROR_QUIET)
  44. string(COMPARE NOTEQUAL "${GIT_DIFF_INDEX}" "" GIT_DIRTY)
  45. if (${GIT_DIRTY})
  46. set(GIT_DESCRIBE_VERSION "${GIT_DESCRIBE_VERSION}-dirty")
  47. endif()
  48. message(STATUS "git version: ${GIT_DESCRIBE_VERSION} normalized to ${GIT_VERSION}")
  49. else()
  50. set(GIT_VERSION "0.0.0")
  51. endif()
  52. set(${var} ${GIT_VERSION} PARENT_SCOPE)
  53. endfunction()