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.

894 lines
31KB

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