NetdataVersion.cmake 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (c) 2024 Netdata Inc.
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # Function to provide information regarding the Netdata version.
  4. #
  5. # The high-level logic is (a) use git-describe, (b) fallback to info from
  6. # packaging/version. This version field are used for cmake's project,
  7. # cpack's packaging, and the agent's functionality.
  8. function(netdata_version)
  9. find_package(Git)
  10. if(GIT_EXECUTABLE)
  11. execute_process(COMMAND ${GIT_EXECUTABLE} describe
  12. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  13. RESULT_VARIABLE GIT_DESCRIBE_RESULT
  14. OUTPUT_VARIABLE GIT_DESCRIBE_OUTPUT)
  15. if(GIT_DESCRIBE_RESULT)
  16. file(STRINGS "${CMAKE_SOURCE_DIR}/packaging/version" GIT_DESCRIBE_OUTPUT)
  17. message(WARNING "using version from packaging/version: '${GIT_DESCRIBE_OUTPUT}'")
  18. endif()
  19. else()
  20. file(STRINGS packaging/version GIT_DESCRIBE_OUTPUT)
  21. message(WARNING "using version from packaging/version: '${GIT_DESCRIBE_OUTPUT}'")
  22. endif()
  23. string(STRIP ${GIT_DESCRIBE_OUTPUT} GIT_DESCRIBE_OUTPUT)
  24. set(NETDATA_VERSION_STRING "${GIT_DESCRIBE_OUTPUT}" PARENT_SCOPE)
  25. string(REGEX MATCH "v?([0-9]+)\\.([0-9]+)\\.([0-9]+)-?([0-9]+)?-?([0-9a-zA-Z]+)?" MATCHES "${GIT_DESCRIBE_OUTPUT}")
  26. if(CMAKE_MATCH_COUNT EQUAL 3)
  27. set(NETDATA_VERSION_MAJOR ${CMAKE_MATCH_1} PARENT_SCOPE)
  28. set(NETDATA_VERSION_MINOR ${CMAKE_MATCH_2} PARENT_SCOPE)
  29. set(NETDATA_VERSION_PATCH ${CMAKE_MATCH_3} PARENT_SCOPE)
  30. set(NETDATA_VERSION_TWEAK 0 PARENT_SCOPE)
  31. set(NETDATA_VERSION_DESCR "N/A" PARENT_SCOPE)
  32. elseif(CMAKE_MATCH_COUNT EQUAL 4)
  33. set(NETDATA_VERSION_MAJOR ${CMAKE_MATCH_1} PARENT_SCOPE)
  34. set(NETDATA_VERSION_MINOR ${CMAKE_MATCH_2} PARENT_SCOPE)
  35. set(NETDATA_VERSION_PATCH ${CMAKE_MATCH_3} PARENT_SCOPE)
  36. set(NETDATA_VERSION_TWEAK ${CMAKE_MATCH_4} PARENT_SCOPE)
  37. set(NETDATA_VERSION_DESCR "N/A" PARENT_SCOPE)
  38. elseif(CMAKE_MATCH_COUNT EQUAL 5)
  39. set(NETDATA_VERSION_MAJOR ${CMAKE_MATCH_1} PARENT_SCOPE)
  40. set(NETDATA_VERSION_MINOR ${CMAKE_MATCH_2} PARENT_SCOPE)
  41. set(NETDATA_VERSION_PATCH ${CMAKE_MATCH_3} PARENT_SCOPE)
  42. set(NETDATA_VERSION_TWEAK ${CMAKE_MATCH_4} PARENT_SCOPE)
  43. set(NETDATA_VERSION_DESCR ${CMAKE_MATCH_5} PARENT_SCOPE)
  44. else()
  45. message(FATAL_ERROR "Wrong version regex match count ${CMAKE_MATCH_COUNT} (should be in 3, 4 or 5)")
  46. endif()
  47. endfunction()