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.

879 lines
31KB

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