helpers_common.cmake 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # CMake common helper functions module
  2. # cmake-format: off
  3. # cmake-lint: disable=C0103
  4. # cmake-format: on
  5. include_guard(GLOBAL)
  6. # find_qt: Macro to find best possible Qt version for use with the project:
  7. macro(find_qt)
  8. set(multiValueArgs COMPONENTS COMPONENTS_WIN COMPONENTS_MAC COMPONENTS_LINUX)
  9. cmake_parse_arguments(find_qt "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  10. # Do not use versionless targets in the first step to avoid Qt::Core being clobbered by later opportunistic
  11. # find_package runs
  12. set(QT_NO_CREATE_VERSIONLESS_TARGETS TRUE)
  13. message(DEBUG "Attempting to find Qt 6")
  14. find_package(
  15. Qt6
  16. COMPONENTS Core
  17. REQUIRED)
  18. # Enable versionless targets for the remaining Qt components
  19. set(QT_NO_CREATE_VERSIONLESS_TARGETS FALSE)
  20. set(qt_components ${find_qt_COMPONENTS})
  21. if(OS_WINDOWS)
  22. list(APPEND qt_components ${find_qt_COMPONENTS_WIN})
  23. elseif(OS_MACOS)
  24. list(APPEND qt_components ${find_qt_COMPONENTS_MAC})
  25. else()
  26. list(APPEND qt_components ${find_qt_COMPONENTS_LINUX})
  27. endif()
  28. message(DEBUG "Trying to find Qt components ${qt_components}...")
  29. find_package(Qt6 REQUIRED ${qt_components})
  30. list(APPEND qt_components Core)
  31. if("Gui" IN_LIST find_qt_COMPONENTS_LINUX)
  32. list(APPEND qt_components "GuiPrivate")
  33. endif()
  34. # Check for versionless targets of each requested component and create if necessary
  35. foreach(component IN LISTS qt_components)
  36. message(DEBUG "Checking for target Qt::${component}")
  37. if(NOT TARGET Qt::${component} AND TARGET Qt6::${component})
  38. add_library(Qt::${component} INTERFACE IMPORTED)
  39. set_target_properties(Qt::${component} PROPERTIES INTERFACE_LINK_LIBRARIES Qt6::${component})
  40. endif()
  41. set_property(TARGET Qt::${component} PROPERTY INTERFACE_COMPILE_FEATURES "")
  42. endforeach()
  43. endmacro()
  44. # check_uuid: Helper function to check for valid UUID
  45. function(check_uuid uuid_string return_value)
  46. set(valid_uuid TRUE)
  47. set(uuid_token_lengths 8 4 4 4 12)
  48. set(token_num 0)
  49. string(REPLACE "-" ";" uuid_tokens ${uuid_string})
  50. list(LENGTH uuid_tokens uuid_num_tokens)
  51. if(uuid_num_tokens EQUAL 5)
  52. message(DEBUG "UUID ${uuid_string} is valid with 5 tokens.")
  53. foreach(uuid_token IN LISTS uuid_tokens)
  54. list(GET uuid_token_lengths ${token_num} uuid_target_length)
  55. string(LENGTH "${uuid_token}" uuid_actual_length)
  56. if(uuid_actual_length EQUAL uuid_target_length)
  57. string(REGEX MATCH "[0-9a-fA-F]+" uuid_hex_match ${uuid_token})
  58. if(NOT uuid_hex_match STREQUAL uuid_token)
  59. set(valid_uuid FALSE)
  60. break()
  61. endif()
  62. else()
  63. set(valid_uuid FALSE)
  64. break()
  65. endif()
  66. math(EXPR token_num "${token_num}+1")
  67. endforeach()
  68. else()
  69. set(valid_uuid FALSE)
  70. endif()
  71. message(DEBUG "UUID ${uuid_string} valid: ${valid_uuid}")
  72. set(${return_value}
  73. ${valid_uuid}
  74. PARENT_SCOPE)
  75. endfunction()
  76. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/plugin-support.c.in")
  77. configure_file(src/plugin-support.c.in plugin-support.c @ONLY)
  78. add_library(plugin-support STATIC)
  79. target_sources(
  80. plugin-support
  81. PRIVATE plugin-support.c
  82. PUBLIC src/plugin-support.h)
  83. target_include_directories(plugin-support PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
  84. if(OS_LINUX
  85. OR OS_FREEBSD
  86. OR OS_OPENBSD)
  87. # add fPIC on Linux to prevent shared object errors
  88. set_property(TARGET plugin-support PROPERTY POSITION_INDEPENDENT_CODE ON)
  89. endif()
  90. endif()