OpusPackageVersion.cmake 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. if(__opus_version)
  2. return()
  3. endif()
  4. set(__opus_version INCLUDED)
  5. function(get_package_version PACKAGE_VERSION PROJECT_VERSION)
  6. find_package(Git)
  7. if(GIT_FOUND AND EXISTS "${CMAKE_CURRENT_LIST_DIR}/.git")
  8. execute_process(COMMAND ${GIT_EXECUTABLE}
  9. --git-dir=${CMAKE_CURRENT_LIST_DIR}/.git describe
  10. --tags --match "v*" OUTPUT_VARIABLE OPUS_PACKAGE_VERSION)
  11. if(OPUS_PACKAGE_VERSION)
  12. string(STRIP ${OPUS_PACKAGE_VERSION}, OPUS_PACKAGE_VERSION)
  13. string(REPLACE \n
  14. ""
  15. OPUS_PACKAGE_VERSION
  16. ${OPUS_PACKAGE_VERSION})
  17. string(REPLACE ,
  18. ""
  19. OPUS_PACKAGE_VERSION
  20. ${OPUS_PACKAGE_VERSION})
  21. string(SUBSTRING ${OPUS_PACKAGE_VERSION}
  22. 1
  23. -1
  24. OPUS_PACKAGE_VERSION)
  25. message(STATUS "Opus package version from git repo: ${OPUS_PACKAGE_VERSION}")
  26. endif()
  27. elseif(EXISTS "${CMAKE_CURRENT_LIST_DIR}/package_version"
  28. AND NOT OPUS_PACKAGE_VERSION)
  29. # Not a git repo, lets' try to parse it from package_version file if exists
  30. file(STRINGS package_version OPUS_PACKAGE_VERSION
  31. LIMIT_COUNT 1
  32. REGEX "PACKAGE_VERSION=")
  33. string(REPLACE "PACKAGE_VERSION="
  34. ""
  35. OPUS_PACKAGE_VERSION
  36. ${OPUS_PACKAGE_VERSION})
  37. string(REPLACE "\""
  38. ""
  39. OPUS_PACKAGE_VERSION
  40. ${OPUS_PACKAGE_VERSION})
  41. # In case we have a unknown dist here we just replace it with 0
  42. string(REPLACE "unknown"
  43. "0"
  44. OPUS_PACKAGE_VERSION
  45. ${OPUS_PACKAGE_VERSION})
  46. message(STATUS "Opus package version from package_version file: ${OPUS_PACKAGE_VERSION}")
  47. endif()
  48. if(OPUS_PACKAGE_VERSION)
  49. string(REGEX
  50. REPLACE "^([0-9]+.[0-9]+\\.?([0-9]+)?).*"
  51. "\\1"
  52. OPUS_PROJECT_VERSION
  53. ${OPUS_PACKAGE_VERSION})
  54. else()
  55. # fail to parse version from git and package version
  56. message(WARNING "Could not get package version.")
  57. set(OPUS_PACKAGE_VERSION 0)
  58. set(OPUS_PROJECT_VERSION 0)
  59. endif()
  60. message(STATUS "Opus project version: ${OPUS_PROJECT_VERSION}")
  61. set(PACKAGE_VERSION ${OPUS_PACKAGE_VERSION} PARENT_SCOPE)
  62. set(PROJECT_VERSION ${OPUS_PROJECT_VERSION} PARENT_SCOPE)
  63. endfunction()