SetGitBasedVersion.cmake 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. get_filename_component(VERSION_SRC_DIR ${CMAKE_CURRENT_LIST_DIR} DIRECTORY)
  2. set(VERSION_SRC_DIR "${VERSION_SRC_DIR}/..")
  3. find_package(Git)
  4. function(set_open62541_version)
  5. # Generate a git-describe version string from Git repository tags
  6. if(GIT_EXECUTABLE AND NOT DEFINED OPEN62541_VERSION)
  7. execute_process(
  8. COMMAND ${GIT_EXECUTABLE} describe --tags --dirty --match "v*"
  9. WORKING_DIRECTORY "${VERSION_SRC_DIR}"
  10. OUTPUT_VARIABLE GIT_DESCRIBE_VERSION
  11. RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE
  12. OUTPUT_STRIP_TRAILING_WHITESPACE
  13. )
  14. if(NOT GIT_DESCRIBE_ERROR_CODE)
  15. set(OPEN62541_VERSION ${GIT_DESCRIBE_VERSION})
  16. # Example values can be:
  17. # v1.2
  18. # v1.2.3
  19. # v1.2.3-rc1
  20. # v1.2.3-rc1-dirty
  21. # v1.2.3-5-g4538abcd
  22. # v1.2.3-5-g4538abcd-dirty
  23. STRING(REGEX REPLACE "^(v[0-9\\.]+)(.*)$"
  24. "\\1"
  25. GIT_VERSION_NUMBERS
  26. "${GIT_DESCRIBE_VERSION}" )
  27. STRING(REGEX REPLACE "^v([0-9\\.]+)(.*)$"
  28. "\\2"
  29. GIT_VERSION_LABEL
  30. "${GIT_DESCRIBE_VERSION}" )
  31. if("${GIT_VERSION_NUMBERS}" MATCHES "^v([0-9]+)(.*)$")
  32. STRING(REGEX REPLACE "^v([0-9]+)\\.?(.*)$"
  33. "\\1"
  34. GIT_VER_MAJOR
  35. "${GIT_VERSION_NUMBERS}" )
  36. if("${GIT_VERSION_NUMBERS}" MATCHES "^v([0-9]+)\\.([0-9]+)(.*)$")
  37. STRING(REGEX REPLACE "^v([0-9]+)\\.([0-9]+)(.*)$"
  38. "\\2"
  39. GIT_VER_MINOR
  40. "${GIT_VERSION_NUMBERS}" )
  41. if("${GIT_VERSION_NUMBERS}" MATCHES "^v([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)$")
  42. STRING(REGEX REPLACE "^v([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)$"
  43. "\\3"
  44. GIT_VER_PATCH
  45. "${GIT_VERSION_NUMBERS}" )
  46. else()
  47. set(GIT_VER_PATCH 0)
  48. endif()
  49. else()
  50. set(GIT_VER_MINOR 0)
  51. set(GIT_VER_PATCH 0)
  52. endif()
  53. else()
  54. set(GIT_VER_MAJOR 0)
  55. set(GIT_VER_MINOR 0)
  56. set(GIT_VER_PATCH 0)
  57. endif()
  58. set(OPEN62541_VER_MAJOR ${GIT_VER_MAJOR} PARENT_SCOPE)
  59. set(OPEN62541_VER_MINOR ${GIT_VER_MINOR} PARENT_SCOPE)
  60. set(OPEN62541_VER_PATCH ${GIT_VER_PATCH} PARENT_SCOPE)
  61. set(OPEN62541_VER_LABEL "${GIT_VERSION_LABEL}" PARENT_SCOPE)
  62. endif()
  63. set(OPEN62541_VER_COMMIT ${GIT_DESCRIBE_VERSION} PARENT_SCOPE)
  64. endif()
  65. # Final fallback: Just use a bogus version string that is semantically older
  66. # than anything else and spit out a warning to the developer.
  67. if(NOT DEFINED OPEN62541_VERSION)
  68. set(OPEN62541_VERSION "v0.0.0-unknown")
  69. set(OPEN62541_VER_MAJOR 0 PARENT_SCOPE)
  70. set(OPEN62541_VER_MINOR 0 PARENT_SCOPE)
  71. set(OPEN62541_VER_PATCH 0 PARENT_SCOPE)
  72. set(OPEN62541_VER_LABEL "-unknown" PARENT_SCOPE)
  73. set(OPEN62541_VER_COMMIT "undefined" PARENT_SCOPE)
  74. message(WARNING "Failed to determine OPEN62541_VERSION from repository tags. Using default version \"${OPEN62541_VERSION}\".")
  75. endif()
  76. endfunction()