1
0

helpers_common.cmake 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. # OBS CMake common helper functions module
  2. # cmake-format: off
  3. # cmake-lint: disable=C0103
  4. # cmake-lint: disable=C0301
  5. # cmake-lint: disable=C0307
  6. # cmake-lint: disable=R0912
  7. # cmake-lint: disable=R0915
  8. # cmake-format: on
  9. include_guard(GLOBAL)
  10. # message_configuration: Function to print configuration outcome
  11. function(message_configuration)
  12. include(FeatureSummary)
  13. feature_summary(WHAT ALL VAR _feature_summary)
  14. message(DEBUG "${_feature_summary}")
  15. message(
  16. NOTICE
  17. " _ _ _ _ \n"
  18. " ___ | |__ ___ ___| |_ _ _ __| (_) ___ \n"
  19. " / _ \\| '_ \\/ __|_____/ __| __| | | |/ _` | |/ _ \\ \n"
  20. " | (_) | |_) \\__ \\_____\\__ \\ |_| |_| | (_| | | (_) |\n"
  21. " \\___/|_.__/|___/ |___/\\__|\\__,_|\\__,_|_|\\___/ \n"
  22. "\nOBS: Application Version: ${OBS_VERSION} - Build Number: ${OBS_BUILD_NUMBER}\n"
  23. "==================================================================================\n\n")
  24. get_property(OBS_FEATURES_ENABLED GLOBAL PROPERTY OBS_FEATURES_ENABLED)
  25. list(
  26. SORT OBS_FEATURES_ENABLED
  27. COMPARE NATURAL
  28. CASE SENSITIVE
  29. ORDER ASCENDING)
  30. if(OBS_FEATURES_ENABLED)
  31. message(NOTICE "------------------------ Enabled Features ------------------------")
  32. foreach(feature IN LISTS OBS_FEATURES_ENABLED)
  33. message(NOTICE " - ${feature}")
  34. endforeach()
  35. endif()
  36. get_property(OBS_FEATURES_DISABLED GLOBAL PROPERTY OBS_FEATURES_DISABLED)
  37. list(
  38. SORT OBS_FEATURES_DISABLED
  39. COMPARE NATURAL
  40. CASE SENSITIVE
  41. ORDER ASCENDING)
  42. if(OBS_FEATURES_DISABLED)
  43. message(NOTICE "------------------------ Disabled Features ------------------------")
  44. foreach(feature IN LISTS OBS_FEATURES_DISABLED)
  45. message(NOTICE " - ${feature}")
  46. endforeach()
  47. endif()
  48. if(ENABLE_PLUGINS)
  49. get_property(OBS_MODULES_ENABLED GLOBAL PROPERTY OBS_MODULES_ENABLED)
  50. list(
  51. SORT OBS_MODULES_ENABLED
  52. COMPARE NATURAL
  53. CASE SENSITIVE
  54. ORDER ASCENDING)
  55. if(OBS_MODULES_ENABLED)
  56. message(NOTICE "------------------------ Enabled Modules ------------------------")
  57. foreach(feature IN LISTS OBS_MODULES_ENABLED)
  58. message(NOTICE " - ${feature}")
  59. endforeach()
  60. endif()
  61. get_property(OBS_MODULES_DISABLED GLOBAL PROPERTY OBS_MODULES_DISABLED)
  62. list(
  63. SORT OBS_MODULES_DISABLED
  64. COMPARE NATURAL
  65. CASE SENSITIVE
  66. ORDER ASCENDING)
  67. if(OBS_MODULES_DISABLED)
  68. message(NOTICE "------------------------ Disabled Modules ------------------------")
  69. foreach(feature IN LISTS OBS_MODULES_DISABLED)
  70. message(NOTICE " - ${feature}")
  71. endforeach()
  72. endif()
  73. endif()
  74. message(NOTICE "----------------------------------------------------------------------------------")
  75. endfunction()
  76. # target_enable_feature: Adds feature to list of enabled application features and sets optional compile definitions
  77. function(target_enable_feature target feature_description)
  78. set_property(GLOBAL APPEND PROPERTY OBS_FEATURES_ENABLED "${feature_description}")
  79. if(ARGN)
  80. target_compile_definitions(${target} PRIVATE ${ARGN})
  81. endif()
  82. endfunction()
  83. # target_disable_feature: Adds feature to list of disabled application features and sets optional compile definitions
  84. function(target_disable_feature target feature_description)
  85. set_property(GLOBAL APPEND PROPERTY OBS_FEATURES_DISABLED "${feature_description}")
  86. if(ARGN)
  87. target_compile_definitions(${target} PRIVATE ${ARGN})
  88. endif()
  89. endfunction()
  90. # target_disable: Adds target to list of disabled modules
  91. function(target_disable target)
  92. set_property(GLOBAL APPEND PROPERTY OBS_MODULES_DISABLED ${target})
  93. endfunction()
  94. # * Use QT_VERSION value as a hint for desired Qt version
  95. # * If "AUTO" was specified, prefer Qt6 over Qt5
  96. # * Creates versionless targets of desired component if none had been created by Qt itself (Qt versions < 5.15)
  97. if(NOT QT_VERSION)
  98. set(QT_VERSION
  99. AUTO
  100. CACHE STRING "OBS Qt version [AUTO, 5, 6]" FORCE)
  101. set_property(CACHE QT_VERSION PROPERTY STRINGS AUTO 5 6)
  102. endif()
  103. # find_qt: Macro to find best possible Qt version for use with the project:
  104. macro(find_qt)
  105. set(multiValueArgs COMPONENTS COMPONENTS_WIN COMPONENTS_MAC COMPONENTS_LINUX)
  106. cmake_parse_arguments(find_qt "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  107. # Do not use versionless targets in the first step to avoid Qt::Core being clobbered by later opportunistic
  108. # find_package runs
  109. set(QT_NO_CREATE_VERSIONLESS_TARGETS TRUE)
  110. message(DEBUG "Start Qt version discovery...")
  111. # Loop until _QT_VERSION is set or FATAL_ERROR aborts script execution early
  112. while(NOT _QT_VERSION)
  113. message(DEBUG "QT_VERSION set to ${QT_VERSION}")
  114. if(QT_VERSION STREQUAL AUTO AND NOT qt_test_version)
  115. set(qt_test_version 6)
  116. elseif(NOT QT_VERSION STREQUAL AUTO)
  117. set(qt_test_version ${QT_VERSION})
  118. endif()
  119. message(DEBUG "Attempting to find Qt${qt_test_version}")
  120. find_package(
  121. Qt${qt_test_version}
  122. COMPONENTS Core
  123. QUIET)
  124. if(TARGET Qt${qt_test_version}::Core)
  125. set(_QT_VERSION
  126. ${qt_test_version}
  127. CACHE INTERNAL "")
  128. message(STATUS "Qt version found: ${_QT_VERSION}")
  129. unset(qt_test_version)
  130. break()
  131. elseif(QT_VERSION STREQUAL AUTO)
  132. if(qt_test_version EQUAL 6)
  133. message(WARNING "Qt6 was not found, falling back to Qt5")
  134. set(qt_test_version 5)
  135. continue()
  136. endif()
  137. endif()
  138. message(FATAL_ERROR "Neither Qt6 nor Qt5 found.")
  139. endwhile()
  140. # Enable versionless targets for the remaining Qt components
  141. set(QT_NO_CREATE_VERSIONLESS_TARGETS FALSE)
  142. set(qt_components ${find_qt_COMPONENTS})
  143. if(OS_WINDOWS)
  144. list(APPEND qt_components ${find_qt_COMPONENTS_WIN})
  145. elseif(OS_MACOS)
  146. list(APPEND qt_components ${find_qt_COMPONENTS_MAC})
  147. else()
  148. list(APPEND qt_components ${find_qt_COMPONENTS_LINUX})
  149. endif()
  150. message(DEBUG "Trying to find Qt components ${qt_components}...")
  151. find_package(Qt${_QT_VERSION} REQUIRED ${qt_components})
  152. list(APPEND qt_components Core)
  153. if("Gui" IN_LIST find_qt_COMPONENTS_LINUX)
  154. list(APPEND qt_components "GuiPrivate")
  155. endif()
  156. # Check for versionless targets of each requested component and create if necessary
  157. foreach(component IN LISTS qt_components)
  158. message(DEBUG "Checking for target Qt::${component}")
  159. if(NOT TARGET Qt::${component} AND TARGET Qt${_QT_VERSION}::${component})
  160. add_library(Qt::${component} INTERFACE IMPORTED)
  161. set_target_properties(Qt::${component} PROPERTIES INTERFACE_LINK_LIBRARIES Qt${_QT_VERSION}::${component})
  162. endif()
  163. endforeach()
  164. endmacro()
  165. # find_dependencies: Check linked interface and direct dependencies of target
  166. function(find_dependencies)
  167. set(oneValueArgs TARGET FOUND_VAR)
  168. set(multiValueArgs)
  169. cmake_parse_arguments(var "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  170. if(NOT DEFINED is_root)
  171. # Root of recursive dependency resolution
  172. set(is_root TRUE)
  173. set(nested_depth 0)
  174. else()
  175. # Branch of recursive dependency resolution
  176. set(is_root FALSE)
  177. math(EXPR nested_depth "${nested_depth}+1")
  178. endif()
  179. # * LINK_LIBRARIES are direct dependencies
  180. # * INTERFACE_LINK_LIBRARIES are transitive dependencies
  181. get_target_property(linked_libraries ${var_TARGET} LINK_LIBRARIES)
  182. get_target_property(interface_libraries ${var_TARGET} INTERFACE_LINK_LIBRARIES)
  183. message(DEBUG "[${nested_depth}] Linked libraries in target ${var_TARGET}: ${linked_libraries}")
  184. message(DEBUG "[${nested_depth}] Linked interface libraries in target ${var_TARGET}: ${interface_libraries}")
  185. # Consider CMake targets only
  186. list(FILTER linked_libraries INCLUDE REGEX ".+::.+")
  187. list(FILTER interface_libraries INCLUDE REGEX ".+::.+")
  188. foreach(library IN LISTS linked_libraries interface_libraries)
  189. if(NOT library)
  190. continue()
  191. elseif(library MATCHES "\\$<.*:[^>]+>")
  192. # Generator expression found
  193. if(library MATCHES "\\$<\\$<PLATFORM_ID:[^>]+>:.+>")
  194. # Platform-dependent generator expression found - platforms are a comma-separated list of CMake host OS
  195. # identifiers. Convert to CMake list and check if current host os is contained in list.
  196. string(REGEX REPLACE "\\$<\\$<PLATFORM_ID:([^>]+)>:([^>]+)>" "\\1;\\2" gen_expression "${library}")
  197. list(GET gen_expression 0 gen_platform)
  198. list(GET gen_expression 1 gen_library)
  199. string(REPLACE "," ";" gen_platform "${gen_platform}")
  200. if(CMAKE_SYSTEM_NAME IN_LIST platform)
  201. set(library "${gen_library}")
  202. else()
  203. continue()
  204. endif()
  205. elseif(library MATCHES "\\$<\\$<BOOL:[^>]+>:.+>")
  206. # Boolean generator expression found - consider parameter a CMake variable that resolves into a CMake-like
  207. # boolean value for a simple conditional check.
  208. string(REGEX REPLACE "\\$<\\$<BOOL:([^>]+)>:([^>]+)>" "\\1;\\2" gen_expression "${library}")
  209. list(GET gen_expression 0 gen_boolean)
  210. list(GET gen_expression 1 gen_library)
  211. if(${gen_boolean})
  212. set(library "${gen_library}")
  213. else()
  214. continue()
  215. endif()
  216. elseif(library MATCHES "\\$<TARGET_NAME_IF_EXISTS:[^>]+>")
  217. # Target-dependent generator expression found - consider parameter to be a CMake target identifier and check for
  218. # target existence.
  219. string(REGEX REPLACE "\\$<TARGET_NAME_IF_EXISTS:([^>]+)>" "\\1" gen_target "${library}")
  220. if(TARGET ${gen_target})
  221. set(library "${gen_target}")
  222. else()
  223. continue()
  224. endif()
  225. elseif(library MATCHES "\\$<.*Qt6::EntryPointPrivate>" OR library MATCHES "\\$<.*Qt6::QDarwin.+PermissionPlugin>")
  226. # Known Qt6-specific generator expression, ignored.
  227. continue()
  228. else()
  229. # Unknown or unimplemented generator expression found - abort script run to either add to ignore list or
  230. # implement detection.
  231. message(FATAL_ERROR "${library} is an unsupported generator expression for linked libraries.")
  232. endif()
  233. endif()
  234. message(DEBUG "[${nested_depth}] Found ${library}...")
  235. if(NOT library IN_LIST ${var_FOUND_VAR})
  236. list(APPEND found_libraries ${library})
  237. # Enter recursive branch
  238. find_dependencies(TARGET ${library} FOUND_VAR ${var_FOUND_VAR})
  239. endif()
  240. endforeach()
  241. if(NOT is_root)
  242. set(found_libraries
  243. ${found_libraries}
  244. PARENT_SCOPE)
  245. # Exit recursive branch
  246. return()
  247. endif()
  248. list(REMOVE_DUPLICATES found_libraries)
  249. list(APPEND ${var_FOUND_VAR} ${found_libraries})
  250. set(${var_FOUND_VAR}
  251. ${${var_FOUND_VAR}}
  252. PARENT_SCOPE)
  253. endfunction()
  254. # find_qt_plugins: Find and add Qt plugin libraries associated with Qt component to target
  255. function(find_qt_plugins)
  256. set(oneValueArgs COMPONENT TARGET FOUND_VAR)
  257. cmake_parse_arguments(var "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  258. string(REPLACE "::" ";" library_tuple "${var_COMPONENT}")
  259. list(GET library_tuple 0 library_namespace)
  260. list(GET library_tuple 1 library_name)
  261. if(NOT ${library_namespace} MATCHES "Qt[56]?")
  262. message(FATAL_ERROR "'find_qt_plugins' has to be called with a valid target from the Qt, Qt5, or Qt6 namespace.")
  263. endif()
  264. list(
  265. APPEND
  266. qt_plugins_Core
  267. platforms
  268. printsupport
  269. styles
  270. imageformats
  271. iconengines)
  272. list(APPEND qt_plugins_Gui platforminputcontexts virtualkeyboard)
  273. list(APPEND qt_plugins_Network bearer)
  274. list(APPEND qt_plugins_Sql sqldrivers)
  275. list(APPEND qt_plugins_Multimedia mediaservice audio)
  276. list(APPEND qt_plugins_3dRender sceneparsers geometryloaders)
  277. list(APPEND qt_plugins_3dQuickRender renderplugins)
  278. list(APPEND qt_plugins_Positioning position)
  279. list(APPEND qt_plugins_Location geoservices)
  280. list(APPEND qt_plugins_TextToSpeech texttospeech)
  281. list(APPEND qt_plugins_WebView webview)
  282. if(qt_plugins_${library_name})
  283. get_target_property(library_location ${var_COMPONENT} IMPORTED_LOCATION)
  284. get_target_property(is_framework ${var_COMPONENT} FRAMEWORK)
  285. if(is_framework)
  286. # Resolve Qt plugin location relative to framework binary location on macOS
  287. set(plugins_location "../../../../../plugins")
  288. cmake_path(ABSOLUTE_PATH plugins_location BASE_DIRECTORY "${library_location}" NORMALIZE)
  289. else()
  290. # Resolve Qt plugin location relative to dynamic library location
  291. set(plugins_location "../../plugins")
  292. cmake_path(ABSOLUTE_PATH plugins_location BASE_DIRECTORY "${library_location}" NORMALIZE)
  293. endif()
  294. foreach(plugin IN ITEMS ${qt_plugins_${library_name}})
  295. if(NOT plugin IN_LIST plugins_list)
  296. if(EXISTS "${plugins_location}/${plugin}")
  297. # Gather all .dll or .dylib files in given plugin subdirectory
  298. file(
  299. GLOB plugin_libraries
  300. RELATIVE "${plugins_location}/${plugin}"
  301. "${plugins_location}/${plugin}/*.dylib" "${plugins_location}/${plugin}/*.dll")
  302. message(DEBUG "Found Qt plugin ${plugin} libraries: ${plugin_libraries}")
  303. foreach(plugin_library IN ITEMS ${plugin_libraries})
  304. set(plugin_full_path "${plugins_location}/${plugin}/${plugin_library}")
  305. list(APPEND plugins_list ${plugin_full_path})
  306. endforeach()
  307. endif()
  308. endif()
  309. endforeach()
  310. endif()
  311. set(${var_FOUND_VAR}
  312. ${plugins_list}
  313. PARENT_SCOPE)
  314. endfunction()
  315. # target_export: Helper function to export target as CMake package
  316. function(target_export target)
  317. if(NOT DEFINED exclude_variant)
  318. set(exclude_variant EXCLUDE_FROM_ALL)
  319. endif()
  320. get_target_property(is_framework ${target} FRAMEWORK)
  321. if(is_framework)
  322. set(package_destination "Frameworks/${target}.framework/Resources/cmake")
  323. set(include_destination "Frameworks/${target}.framework/Headers")
  324. else()
  325. set(package_destination "${OBS_CMAKE_DESTINATION}/${target}")
  326. set(include_destination "${OBS_INCLUDE_DESTINATION}")
  327. endif()
  328. install(
  329. TARGETS ${target}
  330. EXPORT ${target}Targets
  331. RUNTIME DESTINATION "${OBS_EXECUTABLE_DESTINATION}"
  332. COMPONENT Development
  333. ${exclude_variant}
  334. LIBRARY DESTINATION "${OBS_LIBRARY_DESTINATION}"
  335. COMPONENT Development
  336. ${exclude_variant}
  337. ARCHIVE DESTINATION "${OBS_LIBRARY_DESTINATION}"
  338. COMPONENT Development
  339. ${exclude_variant}
  340. FRAMEWORK DESTINATION Frameworks
  341. COMPONENT Development
  342. ${exclude_variant}
  343. INCLUDES
  344. DESTINATION "${include_destination}"
  345. PUBLIC_HEADER
  346. DESTINATION "${include_destination}"
  347. COMPONENT Development
  348. ${exclude_variant})
  349. get_target_property(obs_public_headers ${target} OBS_PUBLIC_HEADERS)
  350. if(obs_public_headers)
  351. foreach(header IN LISTS obs_public_headers)
  352. cmake_path(GET header PARENT_PATH header_dir)
  353. if(header_dir)
  354. if(NOT ${header_dir} IN_LIST header_dirs)
  355. list(APPEND header_dirs ${header_dir})
  356. endif()
  357. list(APPEND headers_${header_dir} ${header})
  358. else()
  359. list(APPEND headers ${header})
  360. endif()
  361. endforeach()
  362. foreach(header_dir IN LISTS header_dirs)
  363. install(
  364. FILES ${headers_${header_dir}}
  365. DESTINATION "${include_destination}/${header_dir}"
  366. COMPONENT Development
  367. ${exclude_variant})
  368. endforeach()
  369. if(headers)
  370. install(
  371. FILES ${headers}
  372. DESTINATION "${include_destination}"
  373. COMPONENT Development
  374. ${exclude_variant})
  375. endif()
  376. endif()
  377. if(target STREQUAL libobs AND NOT EXISTS "${include_destination}/obsconfig.h")
  378. install(
  379. FILES "${CMAKE_BINARY_DIR}/config/obsconfig.h"
  380. DESTINATION "${include_destination}"
  381. COMPONENT Development
  382. ${exclude_variant})
  383. endif()
  384. message(DEBUG "Generating export header for target ${target} as ${target}_EXPORT.h...")
  385. include(GenerateExportHeader)
  386. generate_export_header(${target} EXPORT_FILE_NAME "${target}_EXPORT.h")
  387. target_sources(${target} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/${target}_EXPORT.h>)
  388. set_property(
  389. TARGET ${target}
  390. APPEND
  391. PROPERTY PUBLIC_HEADER "${target}_EXPORT.h")
  392. set(TARGETS_EXPORT_NAME ${target}Targets)
  393. message(
  394. DEBUG
  395. "Generating CMake package configuration file ${target}Config.cmake with targets file ${TARGETS_EXPORT_NAME}...")
  396. include(CMakePackageConfigHelpers)
  397. configure_package_config_file(cmake/${target}Config.cmake.in ${target}Config.cmake
  398. INSTALL_DESTINATION "${package_destination}")
  399. message(DEBUG "Generating CMake package version configuration file ${target}ConfigVersion.cmake...")
  400. write_basic_package_version_file(
  401. "${target}ConfigVersion.cmake"
  402. VERSION ${OBS_VERSION_CANONICAL}
  403. COMPATIBILITY SameMajorVersion)
  404. export(
  405. EXPORT ${target}Targets
  406. FILE "${TARGETS_EXPORT_NAME}.cmake"
  407. NAMESPACE OBS::)
  408. export(PACKAGE ${target})
  409. install(
  410. EXPORT ${TARGETS_EXPORT_NAME}
  411. FILE ${TARGETS_EXPORT_NAME}.cmake
  412. NAMESPACE OBS::
  413. DESTINATION "${package_destination}"
  414. COMPONENT Development
  415. ${exclude_variant})
  416. install(
  417. FILES "${CMAKE_CURRENT_BINARY_DIR}/${target}Config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/${target}ConfigVersion.cmake"
  418. DESTINATION "${package_destination}"
  419. COMPONENT Development
  420. ${exclude_variant})
  421. endfunction()
  422. # check_uuid: Helper function to check for valid UUID
  423. function(check_uuid uuid_string return_value)
  424. set(valid_uuid TRUE)
  425. set(uuid_token_lengths 8 4 4 4 12)
  426. set(token_num 0)
  427. string(REPLACE "-" ";" uuid_tokens ${uuid_string})
  428. list(LENGTH uuid_tokens uuid_num_tokens)
  429. if(uuid_num_tokens EQUAL 5)
  430. message(DEBUG "UUID ${uuid_string} is valid with 5 tokens.")
  431. foreach(uuid_token IN LISTS uuid_tokens)
  432. list(GET uuid_token_lengths ${token_num} uuid_target_length)
  433. string(LENGTH "${uuid_token}" uuid_actual_length)
  434. if(uuid_actual_length EQUAL uuid_target_length)
  435. string(REGEX MATCH "[0-9a-fA-F]+" uuid_hex_match ${uuid_token})
  436. if(NOT uuid_hex_match STREQUAL uuid_token)
  437. set(valid_uuid FALSE)
  438. break()
  439. endif()
  440. else()
  441. set(valid_uuid FALSE)
  442. break()
  443. endif()
  444. math(EXPR token_num "${token_num}+1")
  445. endforeach()
  446. else()
  447. set(valid_uuid FALSE)
  448. endif()
  449. message(DEBUG "UUID ${uuid_string} valid: ${valid_uuid}")
  450. set(${return_value}
  451. ${valid_uuid}
  452. PARENT_SCOPE)
  453. endfunction()
  454. # legacy_check: Checks if new CMake framework was not enabled and load legacy rules instead
  455. macro(legacy_check)
  456. if(OBS_CMAKE_VERSION VERSION_LESS 3.0.0)
  457. message(FATAL_ERROR "CMake version changed between CMakeLists.txt.")
  458. endif()
  459. endmacro()