DISTRHO Plugin Framework
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.

806 lines
28KB

  1. # DISTRHO Plugin Framework (DPF)
  2. # Copyright (C) 2021 Jean Pierre Cimalando <jp-dev@inbox.ru>
  3. #
  4. # SPDX-License-Identifier: ISC
  5. # ------------------------------------------------------------------------------
  6. # CMake support module for the DISTRHO Plugin Framework
  7. #
  8. # The purpose of this module is to help building music plugins easily, when the
  9. # project uses CMake as its build system.
  10. #
  11. # In order to use the helpers provided by this module, a plugin author should
  12. # add DPF as a subproject, making the function `dpf_add_plugin` available.
  13. # The usage of this function is documented below in greater detail.
  14. #
  15. # Example project `CMakeLists.txt`:
  16. #
  17. # ```
  18. # cmake_minimum_required(VERSION 3.7)
  19. # project(MyPlugin)
  20. #
  21. # add_subdirectory(DPF)
  22. #
  23. # dpf_add_plugin(MyPlugin
  24. # TARGETS lv2 vst2 vst3
  25. # UI_TYPE opengl
  26. # FILES_DSP
  27. # src/MyPlugin.cpp
  28. # FILES_UI
  29. # src/MyUI.cpp)
  30. #
  31. # target_include_directories(MyPlugin
  32. # PUBLIC src)
  33. # ```
  34. #
  35. # Important: note that properties, such as include directories, definitions,
  36. # and linked libraries *must* be marked with `PUBLIC` so they take effect and
  37. # propagate into all the plugin targets.
  38. include(CMakeParseArguments)
  39. # ------------------------------------------------------------------------------
  40. # DPF public functions
  41. # ------------------------------------------------------------------------------
  42. # dpf_add_plugin(name <args...>)
  43. # ------------------------------------------------------------------------------
  44. #
  45. # Add a plugin built using the DISTRHO Plugin Framework.
  46. #
  47. # ------------------------------------------------------------------------------
  48. # Created targets:
  49. #
  50. # `<name>`
  51. # static library: the common part of the plugin
  52. # The public properties set on this target apply to both DSP and UI.
  53. #
  54. # `<name>-dsp`
  55. # static library: the DSP part of the plugin
  56. # The public properties set on this target apply to the DSP only.
  57. #
  58. # `<name>-ui`
  59. # static library: the UI part of the plugin
  60. # The public properties set on this target apply to the UI only.
  61. #
  62. # `<name>-<target>` for each target specified with the `TARGETS` argument.
  63. # This is target-dependent and not intended for public use.
  64. #
  65. # ------------------------------------------------------------------------------
  66. # Arguments:
  67. #
  68. # `TARGETS` <tgt1>...<tgtN>
  69. # a list of one of more of the following target types:
  70. # `jack`, `ladspa`, `dssi`, `lv2`, `vst2`, `vst3`
  71. #
  72. # `UI_TYPE` <type>
  73. # the user interface type: `opengl` (default), `cairo`
  74. #
  75. # `MONOLITHIC`
  76. # build LV2 as a single binary for UI and DSP
  77. #
  78. # `FILES_DSP` <file1>...<fileN>
  79. # list of sources which are part of the DSP
  80. #
  81. # `FILES_UI` <file1>...<fileN>
  82. # list of sources which are part of the UI
  83. # empty indicates the plugin does not have UI
  84. #
  85. # `FILES_COMMON` <file1>...<fileN>
  86. # list of sources which are part of both DSP and UI
  87. #
  88. function(dpf_add_plugin NAME)
  89. set(options MONOLITHIC)
  90. set(oneValueArgs UI_TYPE)
  91. set(multiValueArgs TARGETS FILES_DSP FILES_UI FILES_COMMON)
  92. cmake_parse_arguments(_dpf_plugin "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  93. if("${_dpf_plugin_UI_TYPE}" STREQUAL "")
  94. set(_dpf_plugin_UI_TYPE "opengl")
  95. endif()
  96. set(_dgl_library)
  97. if(_dpf_plugin_FILES_UI)
  98. if(_dpf_plugin_UI_TYPE STREQUAL "cairo")
  99. dpf__add_dgl_cairo()
  100. set(_dgl_library dgl-cairo)
  101. elseif(_dpf_plugin_UI_TYPE STREQUAL "opengl")
  102. dpf__add_dgl_opengl()
  103. set(_dgl_library dgl-opengl)
  104. else()
  105. message(FATAL_ERROR "Unrecognized UI type for plugin: ${_dpf_plugin_UI_TYPE}")
  106. endif()
  107. endif()
  108. ###
  109. dpf__ensure_sources_non_empty(_dpf_plugin_FILES_COMMON)
  110. dpf__ensure_sources_non_empty(_dpf_plugin_FILES_DSP)
  111. dpf__ensure_sources_non_empty(_dpf_plugin_FILES_UI)
  112. ###
  113. dpf__add_static_library("${NAME}" ${_dpf_plugin_FILES_COMMON})
  114. target_include_directories("${NAME}" PUBLIC
  115. "${DPF_ROOT_DIR}/distrho")
  116. if((NOT WIN32) AND (NOT APPLE) AND (NOT HAIKU))
  117. target_link_libraries("${NAME}" PRIVATE "dl")
  118. endif()
  119. if(_dgl_library)
  120. # make sure that all code will see DGL_* definitions
  121. target_link_libraries("${NAME}" PUBLIC
  122. "${_dgl_library}-definitions"
  123. dgl-system-libs-definitions)
  124. endif()
  125. dpf__add_static_library("${NAME}-dsp" ${_dpf_plugin_FILES_DSP})
  126. target_link_libraries("${NAME}-dsp" PUBLIC "${NAME}")
  127. if(_dgl_library)
  128. dpf__add_static_library("${NAME}-ui" ${_dpf_plugin_FILES_UI})
  129. target_link_libraries("${NAME}-ui" PUBLIC "${NAME}" ${_dgl_library})
  130. if((NOT WIN32) AND (NOT APPLE) AND (NOT HAIKU))
  131. target_link_libraries("${NAME}-ui" PRIVATE "dl")
  132. endif()
  133. # add the files containing Objective-C classes, recompiled under namespace
  134. dpf__add_plugin_specific_ui_sources("${NAME}-ui")
  135. else()
  136. add_library("${NAME}-ui" INTERFACE)
  137. endif()
  138. ###
  139. foreach(_target ${_dpf_plugin_TARGETS})
  140. if(_target STREQUAL "jack")
  141. dpf__build_jack("${NAME}" "${_dgl_library}")
  142. elseif(_target STREQUAL "ladspa")
  143. dpf__build_ladspa("${NAME}")
  144. elseif(_target STREQUAL "dssi")
  145. dpf__build_dssi("${NAME}" "${_dgl_library}")
  146. elseif(_target STREQUAL "lv2")
  147. dpf__build_lv2("${NAME}" "${_dgl_library}" "${_dpf_plugin_MONOLITHIC}")
  148. elseif(_target STREQUAL "vst2")
  149. dpf__build_vst2("${NAME}" "${_dgl_library}")
  150. elseif(_target STREQUAL "vst3")
  151. dpf__build_vst3("${NAME}" "${_dgl_library}")
  152. else()
  153. message(FATAL_ERROR "Unrecognized target type for plugin: ${_target}")
  154. endif()
  155. endforeach()
  156. endfunction()
  157. # ------------------------------------------------------------------------------
  158. # DPF private functions (prefixed with `dpf__`)
  159. # ------------------------------------------------------------------------------
  160. # Note: The $<0:> trick is to prevent MSVC from appending the build type
  161. # to the output directory.
  162. #
  163. # dpf__build_jack
  164. # ------------------------------------------------------------------------------
  165. #
  166. # Add build rules for a JACK program.
  167. #
  168. function(dpf__build_jack NAME DGL_LIBRARY)
  169. dpf__create_dummy_source_list(_no_srcs)
  170. dpf__add_executable("${NAME}-jack" ${_no_srcs})
  171. dpf__add_plugin_main("${NAME}-jack" "jack")
  172. dpf__add_ui_main("${NAME}-jack" "jack" "${DGL_LIBRARY}")
  173. target_link_libraries("${NAME}-jack" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  174. set_target_properties("${NAME}-jack" PROPERTIES
  175. RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  176. OUTPUT_NAME "${NAME}")
  177. # for RtAudio native fallback
  178. if(APPLE)
  179. find_library(APPLE_COREAUDIO_FRAMEWORK "CoreAudio")
  180. find_library(APPLE_COREFOUNDATION_FRAMEWORK "CoreFoundation")
  181. target_link_libraries("${NAME}-jack" PRIVATE "${APPLE_COREAUDIO_FRAMEWORK}" "${APPLE_COREFOUNDATION_FRAMEWORK}")
  182. endif()
  183. endfunction()
  184. # dpf__build_ladspa
  185. # ------------------------------------------------------------------------------
  186. #
  187. # Add build rules for a LADSPA plugin.
  188. #
  189. function(dpf__build_ladspa NAME)
  190. dpf__create_dummy_source_list(_no_srcs)
  191. dpf__add_module("${NAME}-ladspa" ${_no_srcs})
  192. dpf__add_plugin_main("${NAME}-ladspa" "ladspa")
  193. dpf__set_module_export_list("${NAME}-ladspa" "ladspa")
  194. target_link_libraries("${NAME}-ladspa" PRIVATE "${NAME}-dsp")
  195. set_target_properties("${NAME}-ladspa" PROPERTIES
  196. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  197. ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/obj/ladspa/$<0:>"
  198. OUTPUT_NAME "${NAME}-ladspa"
  199. PREFIX "")
  200. endfunction()
  201. # dpf__build_dssi
  202. # ------------------------------------------------------------------------------
  203. #
  204. # Add build rules for a DSSI plugin.
  205. #
  206. function(dpf__build_dssi NAME DGL_LIBRARY)
  207. find_package(PkgConfig)
  208. pkg_check_modules(LIBLO "liblo")
  209. if(NOT LIBLO_FOUND)
  210. dpf__warn_once_only(missing_liblo
  211. "liblo is not found, skipping the `dssi` plugin targets")
  212. return()
  213. endif()
  214. link_directories(${LIBLO_LIBRARY_DIRS})
  215. dpf__create_dummy_source_list(_no_srcs)
  216. dpf__add_module("${NAME}-dssi" ${_no_srcs})
  217. dpf__add_plugin_main("${NAME}-dssi" "dssi")
  218. dpf__set_module_export_list("${NAME}-dssi" "dssi")
  219. target_link_libraries("${NAME}-dssi" PRIVATE "${NAME}-dsp")
  220. set_target_properties("${NAME}-dssi" PROPERTIES
  221. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  222. ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/obj/dssi/$<0:>"
  223. OUTPUT_NAME "${NAME}-dssi"
  224. PREFIX "")
  225. if(DGL_LIBRARY)
  226. dpf__add_executable("${NAME}-dssi-ui" ${_no_srcs})
  227. dpf__add_ui_main("${NAME}-dssi-ui" "dssi" "${DGL_LIBRARY}")
  228. target_link_libraries("${NAME}-dssi-ui" PRIVATE "${NAME}-ui")
  229. set_target_properties("${NAME}-dssi-ui" PROPERTIES
  230. RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}-dssi/$<0:>"
  231. OUTPUT_NAME "${NAME}_ui")
  232. target_include_directories("${NAME}-dssi-ui" PRIVATE ${LIBLO_INCLUDE_DIRS})
  233. target_link_libraries("${NAME}-dssi-ui" PRIVATE ${LIBLO_LIBRARIES})
  234. endif()
  235. endfunction()
  236. # dpf__build_lv2
  237. # ------------------------------------------------------------------------------
  238. #
  239. # Add build rules for an LV2 plugin.
  240. #
  241. function(dpf__build_lv2 NAME DGL_LIBRARY MONOLITHIC)
  242. dpf__create_dummy_source_list(_no_srcs)
  243. dpf__add_module("${NAME}-lv2" ${_no_srcs})
  244. dpf__add_plugin_main("${NAME}-lv2" "lv2")
  245. if(DGL_LIBRARY AND MONOLITHIC)
  246. dpf__set_module_export_list("${NAME}-lv2" "lv2")
  247. else()
  248. dpf__set_module_export_list("${NAME}-lv2" "lv2-dsp")
  249. endif()
  250. target_link_libraries("${NAME}-lv2" PRIVATE "${NAME}-dsp")
  251. set_target_properties("${NAME}-lv2" PROPERTIES
  252. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2/$<0:>"
  253. ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/obj/lv2/$<0:>"
  254. OUTPUT_NAME "${NAME}_dsp"
  255. PREFIX "")
  256. if(DGL_LIBRARY)
  257. if(MONOLITHIC)
  258. dpf__add_ui_main("${NAME}-lv2" "lv2" "${DGL_LIBRARY}")
  259. target_link_libraries("${NAME}-lv2" PRIVATE "${NAME}-ui")
  260. set_target_properties("${NAME}-lv2" PROPERTIES
  261. OUTPUT_NAME "${NAME}")
  262. else()
  263. dpf__add_module("${NAME}-lv2-ui" ${_no_srcs})
  264. dpf__add_ui_main("${NAME}-lv2-ui" "lv2" "${DGL_LIBRARY}")
  265. dpf__set_module_export_list("${NAME}-lv2-ui" "lv2-ui")
  266. target_link_libraries("${NAME}-lv2-ui" PRIVATE "${NAME}-ui")
  267. set_target_properties("${NAME}-lv2-ui" PROPERTIES
  268. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2/$<0:>"
  269. ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/obj/lv2/$<0:>"
  270. OUTPUT_NAME "${NAME}_ui"
  271. PREFIX "")
  272. endif()
  273. endif()
  274. dpf__add_lv2_ttl_generator()
  275. add_dependencies("${NAME}-lv2" lv2_ttl_generator)
  276. add_custom_command(TARGET "${NAME}-lv2" POST_BUILD
  277. COMMAND
  278. "$<TARGET_FILE:lv2_ttl_generator>"
  279. "$<TARGET_FILE:${NAME}-lv2>"
  280. WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2"
  281. DEPENDS lv2_ttl_generator)
  282. endfunction()
  283. # dpf__build_vst2
  284. # ------------------------------------------------------------------------------
  285. #
  286. # Add build rules for a VST2 plugin.
  287. #
  288. function(dpf__build_vst2 NAME DGL_LIBRARY)
  289. dpf__create_dummy_source_list(_no_srcs)
  290. dpf__add_module("${NAME}-vst2" ${_no_srcs})
  291. dpf__add_plugin_main("${NAME}-vst2" "vst2")
  292. dpf__add_ui_main("${NAME}-vst2" "vst2" "${DGL_LIBRARY}")
  293. dpf__set_module_export_list("${NAME}-vst2" "vst2")
  294. target_link_libraries("${NAME}-vst2" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  295. set_target_properties("${NAME}-vst2" PROPERTIES
  296. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  297. ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/obj/vst2/$<0:>"
  298. OUTPUT_NAME "${NAME}-vst2"
  299. PREFIX "")
  300. if(APPLE)
  301. set_target_properties("${NAME}-vst2" PROPERTIES
  302. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.vst/Contents/MacOS/$<0:>"
  303. OUTPUT_NAME "${NAME}"
  304. SUFFIX "")
  305. set(INFO_PLIST_PROJECT_NAME "${NAME}")
  306. configure_file("${DPF_ROOT_DIR}/utils/plugin.vst/Contents/Info.plist"
  307. "${PROJECT_BINARY_DIR}/bin/${NAME}.vst/Contents/Info.plist" @ONLY)
  308. file(COPY "${DPF_ROOT_DIR}/utils/plugin.vst/Contents/PkgInfo"
  309. DESTINATION "${PROJECT_BINARY_DIR}/bin/${NAME}.vst/Contents")
  310. endif()
  311. endfunction()
  312. # dpf__determine_vst3_package_architecture
  313. # ------------------------------------------------------------------------------
  314. #
  315. # Determines the package architecture for a VST3 plugin target.
  316. #
  317. function(dpf__determine_vst3_package_architecture OUTPUT_VARIABLE)
  318. # if set by variable, override the detection
  319. if(DPF_VST3_ARCHITECTURE)
  320. set("${OUTPUT_VARIABLE}" "${DPF_VST3_ARCHITECTURE}" PARENT_SCOPE)
  321. return()
  322. endif()
  323. # not used on Apple, which supports universal binary
  324. if(APPLE)
  325. set("${OUTPUT_VARIABLE}" "universal" PARENT_SCOPE)
  326. return()
  327. endif()
  328. # identify the target processor (special case of MSVC, problematic sometimes)
  329. if(MSVC)
  330. set(vst3_system_arch "${MSVC_CXX_ARCHITECTURE_ID}")
  331. else()
  332. set(vst3_system_arch "${CMAKE_SYSTEM_PROCESSOR}")
  333. endif()
  334. # transform the processor name to a format that VST3 recognizes
  335. if(vst3_system_arch MATCHES "^(x86_64|amd64|AMD64|x64|X64)$")
  336. set(vst3_package_arch "x86_64")
  337. elseif(vst3_system_arch MATCHES "^(i.86|x86|X86)$")
  338. if(WIN32)
  339. set(vst3_package_arch "x86")
  340. else()
  341. set(vst3_package_arch "i386")
  342. endif()
  343. elseif(vst3_system_arch MATCHES "^(armv[3-8][a-z]*)$")
  344. set(vst3_package_arch "${vst3_system_arch}")
  345. elseif(vst3_system_arch MATCHES "^(aarch64)$")
  346. set(vst3_package_arch "aarch64")
  347. else()
  348. message(FATAL_ERROR "We don't know this architecture for VST3: ${vst3_system_arch}.")
  349. endif()
  350. # TODO: the detections for Windows arm/arm64 when supported
  351. set("${OUTPUT_VARIABLE}" "${vst3_package_arch}" PARENT_SCOPE)
  352. endfunction()
  353. # dpf__build_vst3
  354. # ------------------------------------------------------------------------------
  355. #
  356. # Add build rules for a VST3 plugin.
  357. #
  358. function(dpf__build_vst3 NAME DGL_LIBRARY)
  359. dpf__determine_vst3_package_architecture(vst3_arch)
  360. dpf__create_dummy_source_list(_no_srcs)
  361. dpf__add_module("${NAME}-vst3" ${_no_srcs})
  362. dpf__add_plugin_main("${NAME}-vst3" "vst3")
  363. dpf__add_ui_main("${NAME}-vst3" "vst3" "${DGL_LIBRARY}")
  364. dpf__set_module_export_list("${NAME}-vst3" "vst3")
  365. target_link_libraries("${NAME}-vst3" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  366. set_target_properties("${NAME}-vst3" PROPERTIES
  367. ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/obj/vst3/$<0:>"
  368. OUTPUT_NAME "${NAME}"
  369. PREFIX "")
  370. if(APPLE)
  371. set_target_properties("${NAME}-vst3" PROPERTIES
  372. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.vst3/Contents/MacOS/$<0:>"
  373. SUFFIX "")
  374. elseif(WIN32)
  375. set_target_properties("${NAME}-vst3" PROPERTIES
  376. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.vst3/Contents/${vst3_arch}-win/$<0:>" SUFFIX ".vst3")
  377. else()
  378. set_target_properties("${NAME}-vst3" PROPERTIES
  379. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.vst3/Contents/${vst3_arch}-linux/$<0:>")
  380. endif()
  381. if(APPLE)
  382. # Uses the same macOS bundle template as VST2
  383. set(INFO_PLIST_PROJECT_NAME "${NAME}")
  384. configure_file("${DPF_ROOT_DIR}/utils/plugin.vst/Contents/Info.plist"
  385. "${PROJECT_BINARY_DIR}/bin/${NAME}.vst3/Contents/Info.plist" @ONLY)
  386. file(COPY "${DPF_ROOT_DIR}/utils/plugin.vst/Contents/PkgInfo"
  387. DESTINATION "${PROJECT_BINARY_DIR}/bin/${NAME}.vst3/Contents")
  388. endif()
  389. endfunction()
  390. # dpf__add_dgl_cairo
  391. # ------------------------------------------------------------------------------
  392. #
  393. # Add the Cairo variant of DGL, if not already available.
  394. #
  395. function(dpf__add_dgl_cairo)
  396. if(TARGET dgl-cairo)
  397. return()
  398. endif()
  399. find_package(PkgConfig)
  400. pkg_check_modules(CAIRO "cairo" REQUIRED)
  401. link_directories(${CAIRO_LIBRARY_DIRS})
  402. dpf__add_static_library(dgl-cairo STATIC
  403. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  404. "${DPF_ROOT_DIR}/dgl/src/ApplicationPrivateData.cpp"
  405. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  406. "${DPF_ROOT_DIR}/dgl/src/EventHandlers.cpp"
  407. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  408. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  409. "${DPF_ROOT_DIR}/dgl/src/ImageBaseWidgets.cpp"
  410. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  411. "${DPF_ROOT_DIR}/dgl/src/SubWidget.cpp"
  412. "${DPF_ROOT_DIR}/dgl/src/SubWidgetPrivateData.cpp"
  413. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidget.cpp"
  414. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidgetPrivateData.cpp"
  415. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  416. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp"
  417. "${DPF_ROOT_DIR}/dgl/src/Window.cpp"
  418. "${DPF_ROOT_DIR}/dgl/src/WindowPrivateData.cpp"
  419. "${DPF_ROOT_DIR}/dgl/src/Cairo.cpp")
  420. if(NOT APPLE)
  421. target_sources(dgl-cairo PRIVATE
  422. "${DPF_ROOT_DIR}/dgl/src/pugl.cpp")
  423. else()
  424. target_sources(dgl-opengl PRIVATE
  425. "${DPF_ROOT_DIR}/dgl/src/pugl.mm")
  426. endif()
  427. target_include_directories(dgl-cairo PUBLIC
  428. "${DPF_ROOT_DIR}/dgl")
  429. target_include_directories(dgl-cairo PUBLIC
  430. "${DPF_ROOT_DIR}/dgl/src/pugl-upstream/include")
  431. dpf__add_dgl_system_libs()
  432. target_link_libraries(dgl-cairo PRIVATE dgl-system-libs)
  433. add_library(dgl-cairo-definitions INTERFACE)
  434. target_compile_definitions(dgl-cairo-definitions INTERFACE "DGL_CAIRO" "HAVE_CAIRO" "HAVE_DGL")
  435. target_include_directories(dgl-cairo PUBLIC ${CAIRO_INCLUDE_DIRS})
  436. if(MINGW)
  437. target_link_libraries(dgl-cairo PRIVATE ${CAIRO_STATIC_LIBRARIES})
  438. else()
  439. target_link_libraries(dgl-cairo PRIVATE ${CAIRO_LIBRARIES})
  440. endif()
  441. target_link_libraries(dgl-cairo PRIVATE dgl-cairo-definitions)
  442. endfunction()
  443. # dpf__add_dgl_opengl
  444. # ------------------------------------------------------------------------------
  445. #
  446. # Add the OpenGL variant of DGL, if not already available.
  447. #
  448. function(dpf__add_dgl_opengl)
  449. if(TARGET dgl-opengl)
  450. return()
  451. endif()
  452. if(NOT OpenGL_GL_PREFERENCE)
  453. set(OpenGL_GL_PREFERENCE "LEGACY")
  454. endif()
  455. find_package(OpenGL REQUIRED)
  456. dpf__add_static_library(dgl-opengl STATIC
  457. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  458. "${DPF_ROOT_DIR}/dgl/src/ApplicationPrivateData.cpp"
  459. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  460. "${DPF_ROOT_DIR}/dgl/src/EventHandlers.cpp"
  461. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  462. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  463. "${DPF_ROOT_DIR}/dgl/src/ImageBaseWidgets.cpp"
  464. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  465. "${DPF_ROOT_DIR}/dgl/src/SubWidget.cpp"
  466. "${DPF_ROOT_DIR}/dgl/src/SubWidgetPrivateData.cpp"
  467. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidget.cpp"
  468. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidgetPrivateData.cpp"
  469. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  470. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp"
  471. "${DPF_ROOT_DIR}/dgl/src/Window.cpp"
  472. "${DPF_ROOT_DIR}/dgl/src/WindowPrivateData.cpp"
  473. "${DPF_ROOT_DIR}/dgl/src/OpenGL.cpp"
  474. "${DPF_ROOT_DIR}/dgl/src/NanoVG.cpp")
  475. if(NOT APPLE)
  476. target_sources(dgl-opengl PRIVATE
  477. "${DPF_ROOT_DIR}/dgl/src/pugl.cpp")
  478. else()
  479. target_sources(dgl-opengl PRIVATE
  480. "${DPF_ROOT_DIR}/dgl/src/pugl.mm")
  481. endif()
  482. target_include_directories(dgl-opengl PUBLIC
  483. "${DPF_ROOT_DIR}/dgl")
  484. target_include_directories(dgl-opengl PUBLIC
  485. "${DPF_ROOT_DIR}/dgl/src/pugl-upstream/include")
  486. if(APPLE)
  487. target_compile_definitions(dgl-opengl PUBLIC "GL_SILENCE_DEPRECATION")
  488. endif()
  489. dpf__add_dgl_system_libs()
  490. target_link_libraries(dgl-opengl PRIVATE dgl-system-libs)
  491. add_library(dgl-opengl-definitions INTERFACE)
  492. target_compile_definitions(dgl-opengl-definitions INTERFACE "DGL_OPENGL" "HAVE_OPENGL" "HAVE_DGL")
  493. target_include_directories(dgl-opengl PUBLIC "${OPENGL_INCLUDE_DIR}")
  494. target_link_libraries(dgl-opengl PRIVATE dgl-opengl-definitions "${OPENGL_gl_LIBRARY}")
  495. endfunction()
  496. # dpf__add_plugin_specific_ui_sources
  497. # ------------------------------------------------------------------------------
  498. #
  499. # Compile plugin-specific UI sources into the target designated by the given
  500. # name. There are some special considerations here:
  501. # - On most platforms, sources can be compiled only once, as part of DGL;
  502. # - On macOS, for any sources which define Objective-C interfaces, these must
  503. # be recompiled for each plugin under a unique namespace. In this case, the
  504. # name must be a plugin-specific identifier, and it will be used for computing
  505. # the unique ID along with the project version.
  506. function(dpf__add_plugin_specific_ui_sources NAME)
  507. if(APPLE)
  508. target_sources("${NAME}" PRIVATE
  509. "${DPF_ROOT_DIR}/distrho/DistrhoUI_macOS.mm")
  510. string(SHA256 _hash "${NAME}:${PROJECT_VERSION}")
  511. target_compile_definitions("${NAME}" PUBLIC "PUGL_NAMESPACE=${_hash}")
  512. endif()
  513. endfunction()
  514. # dpf__add_dgl_system_libs
  515. # ------------------------------------------------------------------------------
  516. #
  517. # Find system libraries required by DGL and add them as an interface target.
  518. #
  519. function(dpf__add_dgl_system_libs)
  520. if(TARGET dgl-system-libs)
  521. return()
  522. endif()
  523. add_library(dgl-system-libs INTERFACE)
  524. add_library(dgl-system-libs-definitions INTERFACE)
  525. if(HAIKU)
  526. target_link_libraries(dgl-system-libs INTERFACE "be")
  527. elseif(WIN32)
  528. target_link_libraries(dgl-system-libs INTERFACE "gdi32" "comdlg32")
  529. elseif(APPLE)
  530. find_library(APPLE_COCOA_FRAMEWORK "Cocoa")
  531. find_library(APPLE_COREVIDEO_FRAMEWORK "CoreVideo")
  532. target_link_libraries(dgl-system-libs INTERFACE "${APPLE_COCOA_FRAMEWORK}" "${APPLE_COREVIDEO_FRAMEWORK}")
  533. else()
  534. find_package(X11 REQUIRED)
  535. target_include_directories(dgl-system-libs INTERFACE "${X11_INCLUDE_DIR}")
  536. target_link_libraries(dgl-system-libs INTERFACE "${X11_X11_LIB}")
  537. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_X11")
  538. if(X11_Xext_FOUND)
  539. target_link_libraries(dgl-system-libs INTERFACE "${X11_Xext_LIB}")
  540. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XEXT")
  541. endif()
  542. if(X11_XSync_FOUND)
  543. target_link_libraries(dgl-system-libs INTERFACE "${X11_XSync_LIB}")
  544. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XSYNC")
  545. endif()
  546. if(X11_Xrandr_FOUND)
  547. target_link_libraries(dgl-system-libs INTERFACE "${X11_Xrandr_LIB}")
  548. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XRANDR")
  549. endif()
  550. #if(X11_Xcursor_FOUND)
  551. # target_link_libraries(dgl-system-libs INTERFACE "${X11_Xcursor_LIB}")
  552. # target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XCURSOR")
  553. #endif()
  554. endif()
  555. if(MSVC)
  556. file(MAKE_DIRECTORY "${DPF_ROOT_DIR}/khronos/GL")
  557. foreach(_gl_header "glext.h")
  558. if(NOT EXISTS "${DPF_ROOT_DIR}/khronos/GL/${_gl_header}")
  559. file(DOWNLOAD "https://www.khronos.org/registry/OpenGL/api/GL/${_gl_header}" "${DPF_ROOT_DIR}/khronos/GL/${_gl_header}" SHOW_PROGRESS)
  560. endif()
  561. endforeach()
  562. foreach(_khr_header "khrplatform.h")
  563. if(NOT EXISTS "${DPF_ROOT_DIR}/khronos/KHR/${_khr_header}")
  564. file(DOWNLOAD "https://www.khronos.org/registry/EGL/api/KHR/${_khr_header}" "${DPF_ROOT_DIR}/khronos/KHR/${_khr_header}" SHOW_PROGRESS)
  565. endif()
  566. endforeach()
  567. target_include_directories(dgl-system-libs-definitions INTERFACE "${DPF_ROOT_DIR}/khronos")
  568. endif()
  569. target_link_libraries(dgl-system-libs INTERFACE dgl-system-libs-definitions)
  570. endfunction()
  571. # dpf__add_executable
  572. # ------------------------------------------------------------------------------
  573. #
  574. # Adds an executable target, and set some default properties on the target.
  575. #
  576. function(dpf__add_executable NAME)
  577. add_executable("${NAME}" ${ARGN})
  578. dpf__set_target_defaults("${NAME}")
  579. if(MINGW)
  580. target_link_libraries("${NAME}" PRIVATE "-static")
  581. endif()
  582. endfunction()
  583. # dpf__add_module
  584. # ------------------------------------------------------------------------------
  585. #
  586. # Adds a module target, and set some default properties on the target.
  587. #
  588. function(dpf__add_module NAME)
  589. add_library("${NAME}" MODULE ${ARGN})
  590. dpf__set_target_defaults("${NAME}")
  591. if(MINGW)
  592. target_link_libraries("${NAME}" PRIVATE "-static")
  593. endif()
  594. endfunction()
  595. # dpf__add_static_library
  596. # ------------------------------------------------------------------------------
  597. #
  598. # Adds a static library target, and set some default properties on the target.
  599. #
  600. function(dpf__add_static_library NAME)
  601. add_library("${NAME}" STATIC ${ARGN})
  602. dpf__set_target_defaults("${NAME}")
  603. endfunction()
  604. # dpf__set_module_export_list
  605. # ------------------------------------------------------------------------------
  606. #
  607. # Applies a list of exported symbols to the module target.
  608. #
  609. function(dpf__set_module_export_list NAME EXPORTS)
  610. if(WIN32)
  611. target_sources("${NAME}" PRIVATE "${DPF_ROOT_DIR}/utils/symbols/${EXPORTS}.def")
  612. elseif(APPLE)
  613. set_property(TARGET "${NAME}" APPEND PROPERTY LINK_OPTIONS
  614. "-Xlinker" "-exported_symbols_list"
  615. "-Xlinker" "${DPF_ROOT_DIR}/utils/symbols/${EXPORTS}.exp")
  616. else()
  617. set_property(TARGET "${NAME}" APPEND PROPERTY LINK_OPTIONS
  618. "-Xlinker" "--version-script=${DPF_ROOT_DIR}/utils/symbols/${EXPORTS}.version")
  619. endif()
  620. endfunction()
  621. # dpf__set_target_defaults
  622. # ------------------------------------------------------------------------------
  623. #
  624. # Set default properties which must apply to all DPF-defined targets.
  625. #
  626. function(dpf__set_target_defaults NAME)
  627. set_target_properties("${NAME}" PROPERTIES
  628. POSITION_INDEPENDENT_CODE TRUE
  629. C_VISIBILITY_PRESET "hidden"
  630. CXX_VISIBILITY_PRESET "hidden"
  631. VISIBILITY_INLINES_HIDDEN TRUE)
  632. if(WIN32)
  633. target_compile_definitions("${NAME}" PUBLIC "NOMINMAX")
  634. endif()
  635. if (MINGW)
  636. target_compile_options("${NAME}" PUBLIC "-mstackrealign")
  637. endif()
  638. if (MSVC)
  639. target_compile_options("${NAME}" PUBLIC "/UTF-8")
  640. target_compile_definitions("${NAME}" PUBLIC "_CRT_SECURE_NO_WARNINGS")
  641. endif()
  642. endfunction()
  643. # dpf__add_plugin_main
  644. # ------------------------------------------------------------------------------
  645. #
  646. # Adds plugin code to the given target.
  647. #
  648. function(dpf__add_plugin_main NAME TARGET)
  649. target_sources("${NAME}" PRIVATE
  650. "${DPF_ROOT_DIR}/distrho/DistrhoPluginMain.cpp")
  651. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  652. endfunction()
  653. # dpf__add_ui_main
  654. # ------------------------------------------------------------------------------
  655. #
  656. # Adds UI code to the given target (only if the target has UI).
  657. #
  658. function(dpf__add_ui_main NAME TARGET HAS_UI)
  659. if(HAS_UI)
  660. target_sources("${NAME}" PRIVATE
  661. "${DPF_ROOT_DIR}/distrho/DistrhoUIMain.cpp")
  662. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  663. endif()
  664. endfunction()
  665. # dpf__add_plugin_target_definition
  666. # ------------------------------------------------------------------------------
  667. #
  668. # Adds the plugins target macro definition.
  669. # This selects which entry file is compiled according to the target type.
  670. #
  671. function(dpf__add_plugin_target_definition NAME TARGET)
  672. string(TOUPPER "${TARGET}" _upperTarget)
  673. target_compile_definitions("${NAME}" PRIVATE "DISTRHO_PLUGIN_TARGET_${_upperTarget}")
  674. endfunction()
  675. # dpf__add_lv2_ttl_generator
  676. # ------------------------------------------------------------------------------
  677. #
  678. # Build the LV2 TTL generator.
  679. #
  680. function(dpf__add_lv2_ttl_generator)
  681. if(TARGET lv2_ttl_generator)
  682. return()
  683. endif()
  684. add_executable(lv2_ttl_generator "${DPF_ROOT_DIR}/utils/lv2-ttl-generator/lv2_ttl_generator.c")
  685. if((NOT WIN32) AND (NOT APPLE) AND (NOT HAIKU))
  686. target_link_libraries(lv2_ttl_generator PRIVATE "dl")
  687. endif()
  688. endfunction()
  689. # dpf__ensure_sources_non_empty
  690. # ------------------------------------------------------------------------------
  691. #
  692. # Ensure the given source list contains at least one file.
  693. # The function appends an empty source file to the list if necessary.
  694. # This is useful when CMake does not permit to add targets without sources.
  695. #
  696. function(dpf__ensure_sources_non_empty VAR)
  697. if(NOT "" STREQUAL "${${VAR}}")
  698. return()
  699. endif()
  700. set(_file "${CMAKE_CURRENT_BINARY_DIR}/_dpf_empty.c")
  701. if(NOT EXISTS "${_file}")
  702. file(WRITE "${_file}" "")
  703. endif()
  704. set("${VAR}" "${_file}" PARENT_SCOPE)
  705. endfunction()
  706. # dpf__create_dummy_source_list
  707. # ------------------------------------------------------------------------------
  708. #
  709. # Create a dummy source list which is equivalent to compiling nothing.
  710. # This is only for compatibility with older CMake versions, which refuse to add
  711. # targets without any sources.
  712. #
  713. macro(dpf__create_dummy_source_list VAR)
  714. set("${VAR}")
  715. if(CMAKE_VERSION VERSION_LESS "3.11")
  716. dpf__ensure_sources_non_empty("${VAR}")
  717. endif()
  718. endmacro()
  719. # dpf__warn_once
  720. # ------------------------------------------------------------------------------
  721. #
  722. # Prints a warning message once only.
  723. #
  724. function(dpf__warn_once_only TOKEN MESSAGE)
  725. get_property(_warned GLOBAL PROPERTY "dpf__have_warned_${TOKEN}")
  726. if(NOT _warned)
  727. set_property(GLOBAL PROPERTY "dpf__have_warned_${TOKEN}" TRUE)
  728. message(WARNING "${MESSAGE}")
  729. endif()
  730. endfunction()