VersionInfo.cmake 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. FIND_PACKAGE(Git)
  2. IF(GIT_FOUND AND NOT FORCE_VERSION)
  3. # Look for git tag information (e.g. Tagged: "v1.0.0", Non-tagged: "v1.0.0-123-a1b2c3d")
  4. EXECUTE_PROCESS(
  5. COMMAND "${GIT_EXECUTABLE}" describe --tags --match v[0-9].[0-9].[0-9]*
  6. OUTPUT_VARIABLE GIT_TAG
  7. WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
  8. TIMEOUT 10
  9. OUTPUT_STRIP_TRAILING_WHITESPACE)
  10. STRING(REPLACE "-" ";" TAG_LIST "${GIT_TAG}")
  11. LIST(LENGTH TAG_LIST TAG_LIST_LENGTH)
  12. IF(TAG_LIST_LENGTH GREATER 0)
  13. LIST(GET TAG_LIST 0 FORCE_VERSION)
  14. STRING(REPLACE "v" "" FORCE_VERSION "${FORCE_VERSION}")
  15. ENDIF()
  16. IF(TAG_LIST_LENGTH EQUAL 2)
  17. LIST(GET TAG_LIST 1 VERSION_STAGE)
  18. SET(FORCE_VERSION "${FORCE_VERSION}-${VERSION_STAGE}")
  19. ELSEIF(TAG_LIST_LENGTH EQUAL 3)
  20. LIST(GET TAG_LIST 1 EXTRA_COMMITS)
  21. SET(FORCE_VERSION "${FORCE_VERSION}.${EXTRA_COMMITS}")
  22. ELSEIF(TAG_LIST_LENGTH EQUAL 4)
  23. LIST(GET TAG_LIST 1 VERSION_STAGE)
  24. LIST(GET TAG_LIST 2 EXTRA_COMMITS)
  25. SET(FORCE_VERSION
  26. "${FORCE_VERSION}-${VERSION_STAGE}.${EXTRA_COMMITS}")
  27. ENDIF()
  28. ENDIF()
  29. IF(FORCE_VERSION STREQUAL "internal")
  30. # Use release info from /CMakeLists.txt
  31. ELSEIF(FORCE_VERSION)
  32. STRING(REPLACE "." ";" VERSION_LIST "${FORCE_VERSION}")
  33. LIST(LENGTH VERSION_LIST VERSION_LENGTH)
  34. LIST(GET VERSION_LIST 0 VERSION_MAJOR)
  35. LIST(GET VERSION_LIST 1 VERSION_MINOR)
  36. LIST(GET VERSION_LIST 2 VERSION_RELEASE)
  37. SET(VERSION_STAGE "")
  38. SET(VERSION_BUILD 0)
  39. IF(VERSION_LENGTH GREATER 3)
  40. LIST(GET VERSION_LIST 3 VERSION_BUILD)
  41. ENDIF()
  42. STRING(REPLACE "-" ";" VERSION_LIST "${VERSION_RELEASE}")
  43. LIST(LENGTH VERSION_LIST VERSION_LENGTH)
  44. IF(VERSION_LENGTH GREATER 1)
  45. LIST(GET VERSION_LIST 0 VERSION_RELEASE)
  46. LIST(GET VERSION_LIST 1 VERSION_STAGE)
  47. ENDIF()
  48. SET(VERSION "${FORCE_VERSION}")
  49. ELSEIF(GIT_FOUND)
  50. MESSAGE(
  51. "Could not get project version. Using release info from /CMakeLists.txt"
  52. )
  53. ELSE()
  54. MESSAGE("Git not found. Using release info from /CMakeLists.txt")
  55. ENDIF()
  56. MESSAGE("\n"
  57. "Configuring ${PROJECT_NAME_UCASE}\n"
  58. "--------------------------\n"
  59. "* Project version : ${VERSION}\n"
  60. "* Major version : ${VERSION_MAJOR}\n"
  61. "* Minor version : ${VERSION_MINOR}\n"
  62. "* Release version : ${VERSION_RELEASE}\n"
  63. "* Stage version : ${VERSION_STAGE}\n"
  64. "* Build version : ${VERSION_BUILD}\n"
  65. "*\n\n"
  66. "Optional Version Usage:\n"
  67. "--------------------------\n"
  68. "* Override version: -DFORCE_VERSION=x.x.x-x\n"
  69. "* Ignore Git information: -DFORCE_VERSION=internal\n"
  70. )