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.

896 lines
32KB

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