SetGitBasedVersion.cmake 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. # Example values can be:
  16. # v1.2
  17. # v1.2.3
  18. # v1.2.3-rc1
  19. # v1.2.3-rc1-dirty
  20. # v1.2.3-5-g4538abcd
  21. # v1.2.3-5-g4538abcd-dirty
  22. set(OPEN62541_VERSION ${GIT_DESCRIBE_VERSION})
  23. endif()
  24. endif()
  25. if(OPEN62541_VERSION)
  26. STRING(REGEX REPLACE "^(v[0-9\\.]+)(.*)$"
  27. "\\1"
  28. GIT_VERSION_NUMBERS
  29. "${OPEN62541_VERSION}" )
  30. STRING(REGEX REPLACE "^v([0-9\\.]+)(.*)$"
  31. "\\2"
  32. GIT_VERSION_LABEL
  33. "${OPEN62541_VERSION}" )
  34. if("${GIT_VERSION_NUMBERS}" MATCHES "^v([0-9]+)(.*)$")
  35. STRING(REGEX REPLACE "^v([0-9]+)\\.?(.*)$"
  36. "\\1"
  37. GIT_VER_MAJOR
  38. "${GIT_VERSION_NUMBERS}" )
  39. if("${GIT_VERSION_NUMBERS}" MATCHES "^v([0-9]+)\\.([0-9]+)(.*)$")
  40. STRING(REGEX REPLACE "^v([0-9]+)\\.([0-9]+)(.*)$"
  41. "\\2"
  42. GIT_VER_MINOR
  43. "${GIT_VERSION_NUMBERS}" )
  44. if("${GIT_VERSION_NUMBERS}" MATCHES "^v([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)$")
  45. STRING(REGEX REPLACE "^v([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)$"
  46. "\\3"
  47. GIT_VER_PATCH
  48. "${GIT_VERSION_NUMBERS}" )
  49. else()
  50. set(GIT_VER_PATCH 0)
  51. endif()
  52. else()
  53. set(GIT_VER_MINOR 0)
  54. set(GIT_VER_PATCH 0)
  55. endif()
  56. else()
  57. set(GIT_VER_MAJOR 0)
  58. set(GIT_VER_MINOR 0)
  59. set(GIT_VER_PATCH 0)
  60. endif()
  61. set(OPEN62541_VER_MAJOR ${GIT_VER_MAJOR} PARENT_SCOPE)
  62. set(OPEN62541_VER_MINOR ${GIT_VER_MINOR} PARENT_SCOPE)
  63. set(OPEN62541_VER_PATCH ${GIT_VER_PATCH} PARENT_SCOPE)
  64. set(OPEN62541_VER_LABEL "${GIT_VERSION_LABEL}" PARENT_SCOPE)
  65. set(OPEN62541_VER_COMMIT ${OPEN62541_VERSION} PARENT_SCOPE)
  66. else()
  67. # Final fallback: Just use a bogus version string that is semantically older
  68. # than anything else and spit out a warning to the developer.
  69. set(OPEN62541_VERSION "v0.0.0-unknown" PARENT_SCOPE)
  70. set(OPEN62541_VER_MAJOR 0 PARENT_SCOPE)
  71. set(OPEN62541_VER_MINOR 0 PARENT_SCOPE)
  72. set(OPEN62541_VER_PATCH 0 PARENT_SCOPE)
  73. set(OPEN62541_VER_LABEL "-unknown" PARENT_SCOPE)
  74. set(OPEN62541_VER_COMMIT "undefined" PARENT_SCOPE)
  75. message(WARNING "Failed to determine OPEN62541_VERSION from repository tags. Using default version \"${OPEN62541_VERSION}\".")
  76. endif()
  77. endfunction()