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.

835 lines
29KB

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