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 4.6KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. function(debug_message)
  2. if(${Flag_VerboseOutput})
  3. message(STATUS "${ARGV}")
  4. endif()
  5. endfunction()
  6. # SET(OV_COMPILE_TESTS "true")
  7. FUNCTION(SET_BUILD_PLATFORM)
  8. IF("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
  9. ADD_DEFINITIONS(-DTARGET_ARCHITECTURE_x64)
  10. ELSEIF("${CMAKE_SIZEOF_VOID_P}" EQUAL "4")
  11. ADD_DEFINITIONS(-DTARGET_ARCHITECTURE_i386)
  12. ELSE()
  13. ADD_DEFINITIONS(-DTARGET_ARCHITECTURE_Unknown)
  14. ENDIF()
  15. IF(WIN32)
  16. ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE)
  17. ADD_DEFINITIONS(-DTARGET_OS_Windows)
  18. ADD_DEFINITIONS(-DTARGET_COMPILER_VisualStudio)
  19. ELSEIF(APPLE)
  20. ADD_DEFINITIONS(-fnon-call-exceptions)
  21. ADD_DEFINITIONS(-DTARGET_OS_MacOS)
  22. # ADD_DEFINITIONS(-DTARGET_ARCHITECTURE_x64)
  23. ADD_DEFINITIONS(-DTARGET_COMPILER_LLVM)
  24. ELSEIF(UNIX)
  25. # ADD_DEFINITIONS(-fvisibility=hidden) # This flag should be present... man gcc
  26. ADD_DEFINITIONS(-fnon-call-exceptions)
  27. ADD_DEFINITIONS(-DTARGET_OS_Linux)
  28. ADD_DEFINITIONS(-DTARGET_COMPILER_GCC)
  29. ENDIF()
  30. ENDFUNCTION()
  31. # Set version based on git tag.
  32. # If current commit is tagged, use the tag as it is, and add build number based on content of .build file, written by Jenkins
  33. # Else use last tag major and minor number and set patch number to 99
  34. #
  35. # This function should remain generic to be usable in every projects.
  36. function(set_version)
  37. find_package(Git)
  38. if(EXISTS ${CMAKE_SOURCE_DIR}/.version)
  39. # These versions are used by the subprojects by default.
  40. # If you wish to maintain specific version numbers for a subproject, please do so in the projects CMakeLists.txt
  41. file(READ ${CMAKE_SOURCE_DIR}/.version PROJECT_VERSION)
  42. string(STRIP ${PROJECT_VERSION} PROJECT_VERSION)
  43. string(REPLACE "." ";" VERSION_LIST ${PROJECT_VERSION})
  44. list(GET VERSION_LIST 0 PROJECT_VERSION_MAJOR)
  45. list(GET VERSION_LIST 1 PROJECT_VERSION_MINOR)
  46. list(GET VERSION_LIST 2 PROJECT_VERSION_PATCH)
  47. elseif(NOT GIT_FOUND)
  48. message(WARNING "Git not found, set version to 0.0.0")
  49. set(PROJECT_VERSION "0.0.0")
  50. set(PROJECT_BRANCH_STRING "unknown")
  51. set(PROJECT_COMMITHASH_STRING "0")
  52. else()
  53. debug_message("Found Git: ${GIT_EXECUTABLE}")
  54. execute_process(COMMAND ${GIT_EXECUTABLE} describe
  55. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  56. OUTPUT_VARIABLE PROJECT_VERSION
  57. ERROR_VARIABLE ERROR)
  58. if(ERROR)
  59. message(WARNING "No tags found, set version to 0.0.0")
  60. set(PROJECT_VERSION "0.0.0")
  61. set(PROJECT_BRANCH_STRING "unknown")
  62. set(PROJECT_COMMITHASH_STRING "0")
  63. else()
  64. # codename = the name of the current branch
  65. execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
  66. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  67. OUTPUT_VARIABLE PROJECT_BRANCH_STRING)
  68. # command output may contain carriage return
  69. string(STRIP ${PROJECT_BRANCH_STRING} PROJECT_BRANCH_STRING)
  70. # commithash = short hash of latest revision
  71. execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
  72. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  73. OUTPUT_VARIABLE PROJECT_COMMITHASH_STRING)
  74. # command output may contain carriage return
  75. string(STRIP ${PROJECT_COMMITHASH_STRING} PROJECT_COMMITHASH_STRING)
  76. endif()
  77. endif()
  78. # if current commit is not tagged result is formed as: "major.minor.patch-number of commits since last tag-hash"
  79. string(STRIP ${PROJECT_VERSION} PROJECT_VERSION)
  80. string(REPLACE "-" ";" version_list ${PROJECT_VERSION})
  81. list(LENGTH version_list version_list_length)
  82. 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
  83. list(GET version_list 0 PROJECT_VERSION)
  84. set(PROJECT_VERSION_PATCH 99)
  85. endif()
  86. string(REPLACE "." ";" version_list ${PROJECT_VERSION})
  87. list(GET version_list 0 PROJECT_VERSION_MAJOR)
  88. list(GET version_list 1 PROJECT_VERSION_MINOR)
  89. if(NOT PROJECT_VERSION_PATCH)
  90. list(GET version_list 2 PROJECT_VERSION_PATCH)
  91. endif()
  92. # These versions are used by the subprojects by default.
  93. # If you wish to maintain specific version numbers for a subproject, please do so in the projects CMakeLists.txt
  94. if(EXISTS ${CMAKE_SOURCE_DIR}/.build)
  95. file(READ ${CMAKE_SOURCE_DIR}/.build PROJECT_VERSION_BUILD)
  96. string(STRIP ${PROJECT_VERSION_BUILD} PROJECT_VERSION_BUILD)
  97. else()
  98. set(PROJECT_VERSION_BUILD 0)
  99. endif()
  100. set(PROJECT_BRANCH ${PROJECT_BRANCH_STRING} PARENT_SCOPE)
  101. set(PROJECT_COMMITHASH ${PROJECT_COMMITHASH_STRING} PARENT_SCOPE)
  102. set(PROJECT_VERSION_MAJOR ${PROJECT_VERSION_MAJOR} PARENT_SCOPE)
  103. set(PROJECT_VERSION_MINOR ${PROJECT_VERSION_MINOR} PARENT_SCOPE)
  104. set(PROJECT_VERSION_PATCH ${PROJECT_VERSION_PATCH} PARENT_SCOPE)
  105. set(PROJECT_VERSION_BUILD ${PROJECT_VERSION_BUILD} PARENT_SCOPE)
  106. set(PROJECT_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} PARENT_SCOPE)
  107. endfunction()