helpers_common.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # CMake common helper functions module
  2. # cmake-format: off
  3. # cmake-lint: disable=C0103
  4. # cmake-format: on
  5. include_guard(GLOBAL)
  6. # * Use QT_VERSION value as a hint for desired Qt version
  7. # * If "AUTO" was specified, prefer Qt6 over Qt5
  8. # * Creates versionless targets of desired component if none had been created by Qt itself (Qt versions < 5.15)
  9. if(NOT QT_VERSION)
  10. set(QT_VERSION
  11. AUTO
  12. CACHE STRING "OBS Qt version [AUTO, 5, 6]" FORCE)
  13. set_property(CACHE QT_VERSION PROPERTY STRINGS AUTO 5 6)
  14. endif()
  15. # find_qt: Macro to find best possible Qt version for use with the project:
  16. macro(find_qt)
  17. set(multiValueArgs COMPONENTS COMPONENTS_WIN COMPONENTS_MAC COMPONENTS_LINUX)
  18. cmake_parse_arguments(find_qt "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  19. # Do not use versionless targets in the first step to avoid Qt::Core being clobbered by later opportunistic
  20. # find_package runs
  21. set(QT_NO_CREATE_VERSIONLESS_TARGETS TRUE)
  22. message(DEBUG "Start Qt version discovery...")
  23. # Loop until _QT_VERSION is set or FATAL_ERROR aborts script execution early
  24. while(NOT _QT_VERSION)
  25. message(DEBUG "QT_VERSION set to ${QT_VERSION}")
  26. if(QT_VERSION STREQUAL AUTO AND NOT qt_test_version)
  27. set(qt_test_version 6)
  28. elseif(NOT QT_VERSION STREQUAL AUTO)
  29. set(qt_test_version ${QT_VERSION})
  30. endif()
  31. message(DEBUG "Attempting to find Qt${qt_test_version}")
  32. find_package(
  33. Qt${qt_test_version}
  34. COMPONENTS Core
  35. QUIET)
  36. if(TARGET Qt${qt_test_version}::Core)
  37. set(_QT_VERSION
  38. ${qt_test_version}
  39. CACHE INTERNAL "")
  40. message(STATUS "Qt version found: ${_QT_VERSION}")
  41. unset(qt_test_version)
  42. break()
  43. elseif(QT_VERSION STREQUAL AUTO)
  44. if(qt_test_version EQUAL 6)
  45. message(WARNING "Qt6 was not found, falling back to Qt5")
  46. set(qt_test_version 5)
  47. continue()
  48. endif()
  49. endif()
  50. message(FATAL_ERROR "Neither Qt6 nor Qt5 found.")
  51. endwhile()
  52. # Enable versionless targets for the remaining Qt components
  53. set(QT_NO_CREATE_VERSIONLESS_TARGETS FALSE)
  54. set(qt_components ${find_qt_COMPONENTS})
  55. if(OS_WINDOWS)
  56. list(APPEND qt_components ${find_qt_COMPONENTS_WIN})
  57. elseif(OS_MACOS)
  58. list(APPEND qt_components ${find_qt_COMPONENTS_MAC})
  59. else()
  60. list(APPEND qt_components ${find_qt_COMPONENTS_LINUX})
  61. endif()
  62. message(DEBUG "Trying to find Qt components ${qt_components}...")
  63. find_package(Qt${_QT_VERSION} REQUIRED ${qt_components})
  64. list(APPEND qt_components Core)
  65. if("Gui" IN_LIST find_qt_COMPONENTS_LINUX)
  66. list(APPEND qt_components "GuiPrivate")
  67. endif()
  68. # Check for versionless targets of each requested component and create if necessary
  69. foreach(component IN LISTS qt_components)
  70. message(DEBUG "Checking for target Qt::${component}")
  71. if(NOT TARGET Qt::${component} AND TARGET Qt${_QT_VERSION}::${component})
  72. add_library(Qt::${component} INTERFACE IMPORTED)
  73. set_target_properties(Qt::${component} PROPERTIES INTERFACE_LINK_LIBRARIES Qt${_QT_VERSION}::${component})
  74. endif()
  75. set_property(TARGET Qt::${component} PROPERTY INTERFACE_COMPILE_FEATURES "")
  76. endforeach()
  77. endmacro()
  78. # check_uuid: Helper function to check for valid UUID
  79. function(check_uuid uuid_string return_value)
  80. set(valid_uuid TRUE)
  81. set(uuid_token_lengths 8 4 4 4 12)
  82. set(token_num 0)
  83. string(REPLACE "-" ";" uuid_tokens ${uuid_string})
  84. list(LENGTH uuid_tokens uuid_num_tokens)
  85. if(uuid_num_tokens EQUAL 5)
  86. message(DEBUG "UUID ${uuid_string} is valid with 5 tokens.")
  87. foreach(uuid_token IN LISTS uuid_tokens)
  88. list(GET uuid_token_lengths ${token_num} uuid_target_length)
  89. string(LENGTH "${uuid_token}" uuid_actual_length)
  90. if(uuid_actual_length EQUAL uuid_target_length)
  91. string(REGEX MATCH "[0-9a-fA-F]+" uuid_hex_match ${uuid_token})
  92. if(NOT uuid_hex_match STREQUAL uuid_token)
  93. set(valid_uuid FALSE)
  94. break()
  95. endif()
  96. else()
  97. set(valid_uuid FALSE)
  98. break()
  99. endif()
  100. math(EXPR token_num "${token_num}+1")
  101. endforeach()
  102. else()
  103. set(valid_uuid FALSE)
  104. endif()
  105. message(DEBUG "UUID ${uuid_string} valid: ${valid_uuid}")
  106. set(${return_value}
  107. ${valid_uuid}
  108. PARENT_SCOPE)
  109. endfunction()
  110. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/plugin-support.c.in")
  111. configure_file(src/plugin-support.c.in plugin-support.c @ONLY)
  112. add_library(plugin-support STATIC)
  113. target_sources(
  114. plugin-support
  115. PRIVATE plugin-support.c
  116. PUBLIC src/plugin-support.h)
  117. target_include_directories(plugin-support PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
  118. if(OS_LINUX
  119. OR OS_FREEBSD
  120. OR OS_OPENBSD)
  121. # add fPIC on Linux to prevent shared object errors
  122. set_property(TARGET plugin-support PROPERTY POSITION_INDEPENDENT_CODE ON)
  123. endif()
  124. endif()