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.

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