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.

804 lines
28KB

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