VersionInfo.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. FIND_PACKAGE(Git)
  2. IF(GIT_FOUND AND NOT FORCE_VERSION)
  3. SET(MAJOR_VERSION 0)
  4. SET(MINOR_VERSION 0)
  5. SET(PATCH_VERSION 0)
  6. # Look for git tag information (e.g. Tagged: "v1.0.0", Untagged: "v1.0.0-123-a1b2c3d")
  7. # Untagged format: [latest tag]-[number of commits]-[latest commit hash]
  8. EXECUTE_PROCESS(
  9. COMMAND "${GIT_EXECUTABLE}" describe --tags --match v[0-9].[0-9].[0-9]*
  10. OUTPUT_VARIABLE GIT_TAG
  11. WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
  12. TIMEOUT 10
  13. OUTPUT_STRIP_TRAILING_WHITESPACE)
  14. # Read: TAG_LIST = GIT_TAG.split("-")
  15. STRING(REPLACE "-" ";" TAG_LIST "${GIT_TAG}")
  16. # Read: TAG_LIST_LENGTH = TAG_LIST.length()
  17. LIST(LENGTH TAG_LIST TAG_LIST_LENGTH)
  18. # Untagged versions contain at least 2 dashes, giving 3 strings on split.
  19. # Hence, for untagged versions TAG_LIST_LENGTH = [dashes in latest tag] + 3.
  20. # Corollary: if TAG_LIST_LENGTH <= 2, the version must be tagged.
  21. IF(TAG_LIST_LENGTH GREATER 0)
  22. # Set FORCE_VERSION to TAG_LIST[0], strip any 'v's to get MAJ.MIN.PAT
  23. LIST(GET TAG_LIST 0 FORCE_VERSION)
  24. STRING(REPLACE "v" "" FORCE_VERSION "${FORCE_VERSION}")
  25. # Split FORCE_VERSION on '.' and populate MAJOR/MINOR/PATCH_VERSION
  26. STRING(REPLACE "." ";" MAJ_MIN_PAT "${FORCE_VERSION}")
  27. LIST(GET MAJ_MIN_PAT 0 MAJOR_VERSION)
  28. LIST(GET MAJ_MIN_PAT 1 MINOR_VERSION)
  29. LIST(GET MAJ_MIN_PAT 2 PATCH_VERSION)
  30. ENDIF()
  31. # 1 dash total: Dash in latest tag, no additional commits => pre-release
  32. IF(TAG_LIST_LENGTH EQUAL 2)
  33. LIST(GET TAG_LIST 1 VERSION_STAGE)
  34. SET(FORCE_VERSION "${FORCE_VERSION}-${VERSION_STAGE}")
  35. # 2 dashes: Assume untagged with no dashes in latest tag name => stable + commits
  36. ELSEIF(TAG_LIST_LENGTH EQUAL 3)
  37. # Get the number of commits and latest commit hash
  38. LIST(GET TAG_LIST 1 EXTRA_COMMITS)
  39. LIST(GET TAG_LIST 2 COMMIT_HASH)
  40. # Bump the patch version
  41. MATH(EXPR PATCH_VERSION "${PATCH_VERSION}+1")
  42. # Set the version to MAJOR.MINOR.PATCH-EXTRA_COMMITS+COMMIT_HASH
  43. SET(FORCE_VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}")
  44. SET(FORCE_VERSION "${FORCE_VERSION}-${EXTRA_COMMITS}+${COMMIT_HASH}")
  45. # 3 dashes: Assume untagged with 1 dash in latest tag name => pre-release + commits
  46. ELSEIF(TAG_LIST_LENGTH EQUAL 4)
  47. # Get pre-release stage, number of commits, and latest commit hash
  48. LIST(GET TAG_LIST 1 VERSION_STAGE)
  49. LIST(GET TAG_LIST 2 EXTRA_COMMITS)
  50. LIST(GET TAG_LIST 3 COMMIT_HASH)
  51. # Set the version to MAJOR.MINOR.PATCH-VERSION_STAGE.EXTRA_COMMITS+COMMIT_HASH
  52. SET(FORCE_VERSION "${FORCE_VERSION}-${VERSION_STAGE}")
  53. SET(FORCE_VERSION "${FORCE_VERSION}.${EXTRA_COMMITS}+${COMMIT_HASH}")
  54. ENDIF()
  55. ENDIF()
  56. IF(FORCE_VERSION STREQUAL "internal")
  57. # Use release info from /CMakeLists.txt
  58. ELSEIF(FORCE_VERSION)
  59. STRING(REPLACE "." ";" VERSION_LIST "${FORCE_VERSION}")
  60. LIST(LENGTH VERSION_LIST VERSION_LENGTH)
  61. LIST(GET VERSION_LIST 0 VERSION_MAJOR)
  62. LIST(GET VERSION_LIST 1 VERSION_MINOR)
  63. LIST(GET VERSION_LIST 2 VERSION_RELEASE)
  64. SET(VERSION_STAGE "")
  65. SET(VERSION_BUILD 0)
  66. IF(VERSION_LENGTH GREATER 3)
  67. LIST(GET VERSION_LIST 3 VERSION_BUILD)
  68. ENDIF()
  69. STRING(REPLACE "-" ";" VERSION_LIST "${VERSION_RELEASE}")
  70. LIST(LENGTH VERSION_LIST VERSION_LENGTH)
  71. IF(VERSION_LENGTH GREATER 1)
  72. LIST(GET VERSION_LIST 0 VERSION_RELEASE)
  73. LIST(GET VERSION_LIST 1 VERSION_STAGE)
  74. ENDIF()
  75. SET(VERSION "${FORCE_VERSION}")
  76. ELSEIF(GIT_FOUND)
  77. MESSAGE(
  78. "Could not get project version. Using release info from /CMakeLists.txt"
  79. )
  80. ELSE()
  81. MESSAGE("Git not found. Using release info from /CMakeLists.txt")
  82. ENDIF()
  83. MESSAGE("\n"
  84. "Configuring ${PROJECT_NAME_UCASE}\n"
  85. "--------------------------\n"
  86. "* Project version : ${VERSION}\n"
  87. "* Major version : ${VERSION_MAJOR}\n"
  88. "* Minor version : ${VERSION_MINOR}\n"
  89. "* Release version : ${VERSION_RELEASE}\n"
  90. "* Stage version : ${VERSION_STAGE}\n"
  91. "* Build version : ${VERSION_BUILD}\n"
  92. "*\n\n"
  93. "Optional Version Usage:\n"
  94. "--------------------------\n"
  95. "* Override version: -DFORCE_VERSION=x.x.x-x\n"
  96. "* Ignore Git information: -DFORCE_VERSION=internal\n"
  97. )