helpers_common.cmake 18 KB

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