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.

927 lines
33KB

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