You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Utilities.cmake 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function(debug_message)
  2. if(${Flag_VerboseOutput})
  3. message(STATUS "${ARGV}")
  4. endif()
  5. endfunction()
  6. # Set version based on git tag.
  7. # If current commit is tagged, use the tag as it is, and add build number based on content of .build file, written by Jenkins
  8. # Else use last tag major and minor number and set patch number to 99
  9. #
  10. # This function should remain generic to be usable in every projects.
  11. function(set_version)
  12. find_package(Git)
  13. if(EXISTS ${CMAKE_SOURCE_DIR}/.version)
  14. # These versions are used by the subprojects by default.
  15. # If you wish to maintain specific version numbers for a subproject, please do so in the projects CMakeLists.txt
  16. file(READ ${CMAKE_SOURCE_DIR}/.version PROJECT_VERSION)
  17. string(STRIP ${PROJECT_VERSION} PROJECT_VERSION)
  18. string(REPLACE "." ";" VERSION_LIST ${PROJECT_VERSION})
  19. list(GET VERSION_LIST 0 PROJECT_VERSION_MAJOR)
  20. list(GET VERSION_LIST 1 PROJECT_VERSION_MINOR)
  21. list(GET VERSION_LIST 2 PROJECT_VERSION_PATCH)
  22. elseif(NOT GIT_FOUND)
  23. message(WARNING "Git not found, set version to 0.0.0")
  24. set(PROJECT_VERSION "0.0.0")
  25. set(PROJECT_BRANCH_STRING "unknown")
  26. set(PROJECT_COMMITHASH_STRING "0")
  27. else()
  28. debug_message("Found Git: ${GIT_EXECUTABLE}")
  29. execute_process(COMMAND ${GIT_EXECUTABLE} describe
  30. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  31. OUTPUT_VARIABLE PROJECT_VERSION
  32. ERROR_VARIABLE ERROR)
  33. if(ERROR)
  34. set(PROJECT_VERSION "0.0.0")
  35. message(WARNING "No tags found, set version to 0.0.0")
  36. else()
  37. # codename = the name of the current branch
  38. execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
  39. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  40. OUTPUT_VARIABLE PROJECT_BRANCH_STRING)
  41. # command output may contain carriage return
  42. string(STRIP ${PROJECT_BRANCH_STRING} PROJECT_BRANCH_STRING)
  43. # commithash = short hash of latest revision
  44. execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
  45. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  46. OUTPUT_VARIABLE PROJECT_COMMITHASH_STRING)
  47. # command output may contain carriage return
  48. string(STRIP ${PROJECT_COMMITHASH_STRING} PROJECT_COMMITHASH_STRING)
  49. endif()
  50. endif()
  51. # if current commit is not tagged result is formed as: "major.minor.patch-number of commits since last tag-hash"
  52. string(STRIP ${PROJECT_VERSION} PROJECT_VERSION)
  53. string(REPLACE "-" ";" version_list ${PROJECT_VERSION})
  54. list(LENGTH version_list version_list_length)
  55. if(${version_list_length} EQUAL 3) # if result is formed as "major.minor.patch-number of commits since last tag-hash" set patch as 99
  56. list(GET version_list 0 PROJECT_VERSION)
  57. set(PROJECT_VERSION_PATCH 99)
  58. endif()
  59. string(REPLACE "." ";" version_list ${PROJECT_VERSION})
  60. list(GET version_list 0 PROJECT_VERSION_MAJOR)
  61. list(GET version_list 1 PROJECT_VERSION_MINOR)
  62. if(NOT PROJECT_VERSION_PATCH)
  63. list(GET version_list 2 PROJECT_VERSION_PATCH)
  64. endif()
  65. # These versions are used by the subprojects by default.
  66. # If you wish to maintain specific version numbers for a subproject, please do so in the projects CMakeLists.txt
  67. if(EXISTS ${CMAKE_SOURCE_DIR}/.build)
  68. file(READ ${CMAKE_SOURCE_DIR}/.build PROJECT_VERSION_BUILD)
  69. string(STRIP ${PROJECT_VERSION_BUILD} PROJECT_VERSION_BUILD)
  70. else()
  71. set(PROJECT_VERSION_BUILD 0)
  72. endif()
  73. set(PROJECT_BRANCH ${PROJECT_BRANCH_STRING} PARENT_SCOPE)
  74. set(PROJECT_COMMITHASH ${PROJECT_COMMITHASH_STRING} PARENT_SCOPE)
  75. set(PROJECT_VERSION_MAJOR ${PROJECT_VERSION_MAJOR} PARENT_SCOPE)
  76. set(PROJECT_VERSION_MINOR ${PROJECT_VERSION_MINOR} PARENT_SCOPE)
  77. set(PROJECT_VERSION_PATCH ${PROJECT_VERSION_PATCH} PARENT_SCOPE)
  78. set(PROJECT_VERSION_BUILD ${PROJECT_VERSION_BUILD} PARENT_SCOPE)
  79. set(PROJECT_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} PARENT_SCOPE)
  80. endfunction()