ProjectVersion.cmake 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #
  2. # This file is responsible for setting the following variables:
  3. #
  4. # ~~~
  5. # BUILD_NUMBER (1035)
  6. # PROJECT_VERSION (4.0.3)
  7. # PROJECT_VERSION_FULL (4.0.3-BETA+1035.PR111.B4)
  8. # PROJECT_VERSION_SUFFIX (-BETA+1035.PR111.B4)
  9. # PROJECT_VERSION_SUFFIX_SHORT (+1035)
  10. #
  11. # The `PROJECT_VERSION` variable is set as soon as the file is included.
  12. # To set the rest, the function `resolve_version_variables` has to be called.
  13. #
  14. # ~~~
  15. FILE(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/Firmware/Configuration.h CFG_VER_DATA REGEX "#define FW_[A-Z]+ ([0-9]+)" )
  16. LIST(GET CFG_VER_DATA 0 PROJECT_VERSION_MAJOR)
  17. LIST(GET CFG_VER_DATA 1 PROJECT_VERSION_MINOR)
  18. LIST(GET CFG_VER_DATA 2 PROJECT_VERSION_REV)
  19. STRING(REGEX MATCH "FW_MAJOR ([0-9]+)" PROJECT_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
  20. SET(PROJECT_VERSION_MAJOR "${CMAKE_MATCH_1}")
  21. STRING(REGEX MATCH "FW_MINOR ([0-9]+)" PROJECT_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
  22. SET(PROJECT_VERSION_MINOR ${CMAKE_MATCH_1})
  23. STRING(REGEX MATCH "FW_REVISION +([0-9]+)" PROJECT_VERSION_REV "${PROJECT_VERSION_REV}")
  24. SET(PROJECT_VERSION_REV ${CMAKE_MATCH_1})
  25. SET(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_REV}")
  26. function(resolve_version_variables)
  27. # BUILD_NUMBER
  28. if(NOT BUILD_NUMBER)
  29. git_count_parent_commits(BUILD_NUMBER)
  30. set(ERRORS "GIT-NOTFOUND" "HEAD-HASH-NOTFOUND")
  31. if(BUILD_NUMBER IN_LIST ERRORS)
  32. message(WARNING "Failed to resolve build number: ${BUILD_NUMBER}. Setting to zero.")
  33. set(BUILD_NUMBER "0")
  34. endif()
  35. set(BUILD_NUMBER
  36. ${BUILD_NUMBER}
  37. PARENT_SCOPE
  38. )
  39. endif()
  40. # PROJECT_VERSION_SUFFIX
  41. if(PROJECT_VERSION_SUFFIX STREQUAL "<auto>")
  42. # TODO: set to +<sha>.dirty?.debug?
  43. set(PROJECT_VERSION_SUFFIX "+${BUILD_NUMBER}.LOCAL")
  44. set(PROJECT_VERSION_SUFFIX
  45. "+${BUILD_NUMBER}.LOCAL"
  46. PARENT_SCOPE
  47. )
  48. endif()
  49. # PROJECT_VERSION_SUFFIX_SHORT
  50. if(PROJECT_VERSION_SUFFIX_SHORT STREQUAL "<auto>")
  51. set(PROJECT_VERSION_SUFFIX_SHORT
  52. "+${BUILD_NUMBER}"
  53. PARENT_SCOPE
  54. )
  55. endif()
  56. # PROJECT_VERSION_FULL
  57. set(PROJECT_VERSION_FULL
  58. "${PROJECT_VERSION}${PROJECT_VERSION_SUFFIX}"
  59. PARENT_SCOPE
  60. )
  61. endfunction()