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.

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