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.

808 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. if(MSVC)
  146. message(WARNING "TODO: VST3 is not supported on MSVC yet")
  147. else()
  148. dpf__build_vst3("${NAME}" "${_dgl_library}")
  149. endif()
  150. else()
  151. message(FATAL_ERROR "Unrecognized target type for plugin: ${_target}")
  152. endif()
  153. endforeach()
  154. endfunction()
  155. # ------------------------------------------------------------------------------
  156. # DPF private functions (prefixed with `dpf__`)
  157. # ------------------------------------------------------------------------------
  158. # Note: The $<0:> trick is to prevent MSVC from appending the build type
  159. # to the output directory.
  160. #
  161. # dpf__build_jack
  162. # ------------------------------------------------------------------------------
  163. #
  164. # Add build rules for a JACK program.
  165. #
  166. function(dpf__build_jack NAME DGL_LIBRARY)
  167. dpf__create_dummy_source_list(_no_srcs)
  168. dpf__add_executable("${NAME}-jack" ${_no_srcs})
  169. dpf__add_plugin_main("${NAME}-jack" "jack")
  170. dpf__add_ui_main("${NAME}-jack" "jack" "${DGL_LIBRARY}")
  171. target_link_libraries("${NAME}-jack" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  172. set_target_properties("${NAME}-jack" PROPERTIES
  173. RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  174. OUTPUT_NAME "${NAME}")
  175. # Note: libjack will be linked at runtime
  176. if((NOT WIN32) AND (NOT APPLE) AND (NOT HAIKU))
  177. target_link_libraries("${NAME}-jack" PRIVATE "dl")
  178. endif()
  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 ${CMAKE_CROSSCOMPILING_EMULATOR}
  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.vst/Contents/Info.plist"
  309. "${PROJECT_BINARY_DIR}/bin/${NAME}.vst/Contents/Info.plist" @ONLY)
  310. file(COPY "${DPF_ROOT_DIR}/utils/plugin.vst/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:>")
  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.vst/Contents/Info.plist"
  387. "${PROJECT_BINARY_DIR}/bin/${NAME}.vst3/Contents/Info.plist" @ONLY)
  388. file(COPY "${DPF_ROOT_DIR}/utils/plugin.vst/Contents/PkgInfo"
  389. DESTINATION "${PROJECT_BINARY_DIR}/bin/${NAME}.vst3/Contents")
  390. endif()
  391. endfunction()
  392. # dpf__add_dgl_cairo
  393. # ------------------------------------------------------------------------------
  394. #
  395. # Add the Cairo variant of DGL, if not already available.
  396. #
  397. function(dpf__add_dgl_cairo)
  398. if(TARGET dgl-cairo)
  399. return()
  400. endif()
  401. find_package(PkgConfig)
  402. pkg_check_modules(CAIRO "cairo" REQUIRED)
  403. link_directories(${CAIRO_LIBRARY_DIRS})
  404. dpf__add_static_library(dgl-cairo STATIC
  405. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  406. "${DPF_ROOT_DIR}/dgl/src/ApplicationPrivateData.cpp"
  407. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  408. "${DPF_ROOT_DIR}/dgl/src/EventHandlers.cpp"
  409. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  410. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  411. "${DPF_ROOT_DIR}/dgl/src/ImageBaseWidgets.cpp"
  412. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  413. "${DPF_ROOT_DIR}/dgl/src/SubWidget.cpp"
  414. "${DPF_ROOT_DIR}/dgl/src/SubWidgetPrivateData.cpp"
  415. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidget.cpp"
  416. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidgetPrivateData.cpp"
  417. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  418. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp"
  419. "${DPF_ROOT_DIR}/dgl/src/Window.cpp"
  420. "${DPF_ROOT_DIR}/dgl/src/WindowPrivateData.cpp"
  421. "${DPF_ROOT_DIR}/dgl/src/Cairo.cpp")
  422. if(NOT APPLE)
  423. target_sources(dgl-cairo PRIVATE
  424. "${DPF_ROOT_DIR}/dgl/src/pugl.cpp")
  425. else() # Note: macOS pugl will be built as part of DistrhoUI_macOS.mm
  426. #target_sources(dgl-opengl PRIVATE
  427. # "${DPF_ROOT_DIR}/dgl/src/pugl.mm")
  428. endif()
  429. target_include_directories(dgl-cairo PUBLIC
  430. "${DPF_ROOT_DIR}/dgl")
  431. target_include_directories(dgl-cairo PUBLIC
  432. "${DPF_ROOT_DIR}/dgl/src/pugl-upstream/include")
  433. dpf__add_dgl_system_libs()
  434. target_link_libraries(dgl-cairo PRIVATE dgl-system-libs)
  435. add_library(dgl-cairo-definitions INTERFACE)
  436. target_compile_definitions(dgl-cairo-definitions INTERFACE "DGL_CAIRO" "HAVE_CAIRO" "HAVE_DGL")
  437. target_include_directories(dgl-cairo PUBLIC ${CAIRO_INCLUDE_DIRS})
  438. if(MINGW)
  439. target_link_libraries(dgl-cairo PRIVATE ${CAIRO_STATIC_LIBRARIES})
  440. else()
  441. target_link_libraries(dgl-cairo PRIVATE ${CAIRO_LIBRARIES})
  442. endif()
  443. target_link_libraries(dgl-cairo PRIVATE dgl-cairo-definitions)
  444. endfunction()
  445. # dpf__add_dgl_opengl
  446. # ------------------------------------------------------------------------------
  447. #
  448. # Add the OpenGL variant of DGL, if not already available.
  449. #
  450. function(dpf__add_dgl_opengl)
  451. if(TARGET dgl-opengl)
  452. return()
  453. endif()
  454. if(NOT OpenGL_GL_PREFERENCE)
  455. set(OpenGL_GL_PREFERENCE "LEGACY")
  456. endif()
  457. find_package(OpenGL REQUIRED)
  458. dpf__add_static_library(dgl-opengl STATIC
  459. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  460. "${DPF_ROOT_DIR}/dgl/src/ApplicationPrivateData.cpp"
  461. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  462. "${DPF_ROOT_DIR}/dgl/src/EventHandlers.cpp"
  463. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  464. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  465. "${DPF_ROOT_DIR}/dgl/src/ImageBaseWidgets.cpp"
  466. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  467. "${DPF_ROOT_DIR}/dgl/src/SubWidget.cpp"
  468. "${DPF_ROOT_DIR}/dgl/src/SubWidgetPrivateData.cpp"
  469. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidget.cpp"
  470. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidgetPrivateData.cpp"
  471. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  472. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp"
  473. "${DPF_ROOT_DIR}/dgl/src/Window.cpp"
  474. "${DPF_ROOT_DIR}/dgl/src/WindowPrivateData.cpp"
  475. "${DPF_ROOT_DIR}/dgl/src/OpenGL.cpp"
  476. "${DPF_ROOT_DIR}/dgl/src/NanoVG.cpp")
  477. if(NOT APPLE)
  478. target_sources(dgl-opengl PRIVATE
  479. "${DPF_ROOT_DIR}/dgl/src/pugl.cpp")
  480. else() # Note: macOS pugl will be built as part of DistrhoUI_macOS.mm
  481. #target_sources(dgl-opengl PRIVATE
  482. # "${DPF_ROOT_DIR}/dgl/src/pugl.mm")
  483. endif()
  484. target_include_directories(dgl-opengl PUBLIC
  485. "${DPF_ROOT_DIR}/dgl")
  486. target_include_directories(dgl-opengl PUBLIC
  487. "${DPF_ROOT_DIR}/dgl/src/pugl-upstream/include")
  488. if(APPLE)
  489. target_compile_definitions(dgl-opengl PUBLIC "GL_SILENCE_DEPRECATION")
  490. endif()
  491. dpf__add_dgl_system_libs()
  492. target_link_libraries(dgl-opengl PRIVATE dgl-system-libs)
  493. add_library(dgl-opengl-definitions INTERFACE)
  494. target_compile_definitions(dgl-opengl-definitions INTERFACE "DGL_OPENGL" "HAVE_OPENGL" "HAVE_DGL")
  495. target_include_directories(dgl-opengl PUBLIC "${OPENGL_INCLUDE_DIR}")
  496. target_link_libraries(dgl-opengl PRIVATE dgl-opengl-definitions "${OPENGL_gl_LIBRARY}")
  497. endfunction()
  498. # dpf__add_plugin_specific_ui_sources
  499. # ------------------------------------------------------------------------------
  500. #
  501. # Compile plugin-specific UI sources into the target designated by the given
  502. # name. There are some special considerations here:
  503. # - On most platforms, sources can be compiled only once, as part of DGL;
  504. # - On macOS, for any sources which define Objective-C interfaces, these must
  505. # be recompiled for each plugin under a unique namespace. In this case, the
  506. # name must be a plugin-specific identifier, and it will be used for computing
  507. # the unique ID along with the project version.
  508. function(dpf__add_plugin_specific_ui_sources NAME)
  509. if(APPLE)
  510. target_sources("${NAME}" PRIVATE
  511. "${DPF_ROOT_DIR}/distrho/DistrhoUI_macOS.mm")
  512. string(SHA256 _hash "${NAME}:${PROJECT_VERSION}")
  513. target_compile_definitions("${NAME}" PUBLIC "PUGL_NAMESPACE=${_hash}")
  514. endif()
  515. endfunction()
  516. # dpf__add_dgl_system_libs
  517. # ------------------------------------------------------------------------------
  518. #
  519. # Find system libraries required by DGL and add them as an interface target.
  520. #
  521. function(dpf__add_dgl_system_libs)
  522. if(TARGET dgl-system-libs)
  523. return()
  524. endif()
  525. add_library(dgl-system-libs INTERFACE)
  526. add_library(dgl-system-libs-definitions INTERFACE)
  527. if(HAIKU)
  528. target_link_libraries(dgl-system-libs INTERFACE "be")
  529. elseif(WIN32)
  530. target_link_libraries(dgl-system-libs INTERFACE "gdi32" "comdlg32")
  531. elseif(APPLE)
  532. find_library(APPLE_COCOA_FRAMEWORK "Cocoa")
  533. find_library(APPLE_COREVIDEO_FRAMEWORK "CoreVideo")
  534. target_link_libraries(dgl-system-libs INTERFACE "${APPLE_COCOA_FRAMEWORK}" "${APPLE_COREVIDEO_FRAMEWORK}")
  535. else()
  536. find_package(X11 REQUIRED)
  537. target_include_directories(dgl-system-libs INTERFACE "${X11_INCLUDE_DIR}")
  538. target_link_libraries(dgl-system-libs INTERFACE "${X11_X11_LIB}")
  539. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_X11")
  540. if(X11_Xext_FOUND)
  541. target_link_libraries(dgl-system-libs INTERFACE "${X11_Xext_LIB}")
  542. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XEXT")
  543. endif()
  544. if(X11_XSync_FOUND)
  545. target_link_libraries(dgl-system-libs INTERFACE "${X11_XSync_LIB}")
  546. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XSYNC")
  547. endif()
  548. if(X11_Xrandr_FOUND)
  549. target_link_libraries(dgl-system-libs INTERFACE "${X11_Xrandr_LIB}")
  550. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XRANDR")
  551. endif()
  552. #if(X11_Xcursor_FOUND)
  553. # target_link_libraries(dgl-system-libs INTERFACE "${X11_Xcursor_LIB}")
  554. # target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XCURSOR")
  555. #endif()
  556. endif()
  557. if(MSVC)
  558. file(MAKE_DIRECTORY "${DPF_ROOT_DIR}/khronos/GL")
  559. foreach(_gl_header "glext.h")
  560. if(NOT EXISTS "${DPF_ROOT_DIR}/khronos/GL/${_gl_header}")
  561. file(DOWNLOAD "https://www.khronos.org/registry/OpenGL/api/GL/${_gl_header}" "${DPF_ROOT_DIR}/khronos/GL/${_gl_header}" SHOW_PROGRESS)
  562. endif()
  563. endforeach()
  564. foreach(_khr_header "khrplatform.h")
  565. if(NOT EXISTS "${DPF_ROOT_DIR}/khronos/KHR/${_khr_header}")
  566. file(DOWNLOAD "https://www.khronos.org/registry/EGL/api/KHR/${_khr_header}" "${DPF_ROOT_DIR}/khronos/KHR/${_khr_header}" SHOW_PROGRESS)
  567. endif()
  568. endforeach()
  569. target_include_directories(dgl-system-libs-definitions INTERFACE "${DPF_ROOT_DIR}/khronos")
  570. endif()
  571. target_link_libraries(dgl-system-libs INTERFACE dgl-system-libs-definitions)
  572. endfunction()
  573. # dpf__add_executable
  574. # ------------------------------------------------------------------------------
  575. #
  576. # Adds an executable target, and set some default properties on the target.
  577. #
  578. function(dpf__add_executable NAME)
  579. add_executable("${NAME}" ${ARGN})
  580. dpf__set_target_defaults("${NAME}")
  581. if(MINGW)
  582. target_link_libraries("${NAME}" PRIVATE "-static")
  583. endif()
  584. endfunction()
  585. # dpf__add_module
  586. # ------------------------------------------------------------------------------
  587. #
  588. # Adds a module target, and set some default properties on the target.
  589. #
  590. function(dpf__add_module NAME)
  591. add_library("${NAME}" MODULE ${ARGN})
  592. dpf__set_target_defaults("${NAME}")
  593. if(MINGW)
  594. target_link_libraries("${NAME}" PRIVATE "-static")
  595. endif()
  596. endfunction()
  597. # dpf__add_static_library
  598. # ------------------------------------------------------------------------------
  599. #
  600. # Adds a static library target, and set some default properties on the target.
  601. #
  602. function(dpf__add_static_library NAME)
  603. add_library("${NAME}" STATIC ${ARGN})
  604. dpf__set_target_defaults("${NAME}")
  605. endfunction()
  606. # dpf__set_module_export_list
  607. # ------------------------------------------------------------------------------
  608. #
  609. # Applies a list of exported symbols to the module target.
  610. #
  611. function(dpf__set_module_export_list NAME EXPORTS)
  612. if(WIN32)
  613. target_sources("${NAME}" PRIVATE "${DPF_ROOT_DIR}/utils/symbols/${EXPORTS}.def")
  614. elseif(APPLE)
  615. set_property(TARGET "${NAME}" APPEND PROPERTY LINK_OPTIONS
  616. "-Xlinker" "-exported_symbols_list"
  617. "-Xlinker" "${DPF_ROOT_DIR}/utils/symbols/${EXPORTS}.exp")
  618. else()
  619. set_property(TARGET "${NAME}" APPEND PROPERTY LINK_OPTIONS
  620. "-Xlinker" "--version-script=${DPF_ROOT_DIR}/utils/symbols/${EXPORTS}.version")
  621. endif()
  622. endfunction()
  623. # dpf__set_target_defaults
  624. # ------------------------------------------------------------------------------
  625. #
  626. # Set default properties which must apply to all DPF-defined targets.
  627. #
  628. function(dpf__set_target_defaults NAME)
  629. set_target_properties("${NAME}" PROPERTIES
  630. POSITION_INDEPENDENT_CODE TRUE
  631. C_VISIBILITY_PRESET "hidden"
  632. CXX_VISIBILITY_PRESET "hidden"
  633. VISIBILITY_INLINES_HIDDEN TRUE)
  634. if(WIN32)
  635. target_compile_definitions("${NAME}" PUBLIC "NOMINMAX")
  636. endif()
  637. if (MINGW)
  638. target_compile_options("${NAME}" PUBLIC "-mstackrealign")
  639. endif()
  640. if (MSVC)
  641. target_compile_options("${NAME}" PUBLIC "/UTF-8")
  642. target_compile_definitions("${NAME}" PUBLIC "_CRT_SECURE_NO_WARNINGS")
  643. endif()
  644. endfunction()
  645. # dpf__add_plugin_main
  646. # ------------------------------------------------------------------------------
  647. #
  648. # Adds plugin code to the given target.
  649. #
  650. function(dpf__add_plugin_main NAME TARGET)
  651. target_sources("${NAME}" PRIVATE
  652. "${DPF_ROOT_DIR}/distrho/DistrhoPluginMain.cpp")
  653. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  654. endfunction()
  655. # dpf__add_ui_main
  656. # ------------------------------------------------------------------------------
  657. #
  658. # Adds UI code to the given target (only if the target has UI).
  659. #
  660. function(dpf__add_ui_main NAME TARGET HAS_UI)
  661. if(HAS_UI)
  662. target_sources("${NAME}" PRIVATE
  663. "${DPF_ROOT_DIR}/distrho/DistrhoUIMain.cpp")
  664. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  665. endif()
  666. endfunction()
  667. # dpf__add_plugin_target_definition
  668. # ------------------------------------------------------------------------------
  669. #
  670. # Adds the plugins target macro definition.
  671. # This selects which entry file is compiled according to the target type.
  672. #
  673. function(dpf__add_plugin_target_definition NAME TARGET)
  674. string(TOUPPER "${TARGET}" _upperTarget)
  675. target_compile_definitions("${NAME}" PRIVATE "DISTRHO_PLUGIN_TARGET_${_upperTarget}")
  676. endfunction()
  677. # dpf__add_lv2_ttl_generator
  678. # ------------------------------------------------------------------------------
  679. #
  680. # Build the LV2 TTL generator.
  681. #
  682. function(dpf__add_lv2_ttl_generator)
  683. if(TARGET lv2_ttl_generator)
  684. return()
  685. endif()
  686. add_executable(lv2_ttl_generator "${DPF_ROOT_DIR}/utils/lv2-ttl-generator/lv2_ttl_generator.c")
  687. if((NOT WIN32) AND (NOT APPLE) AND (NOT HAIKU))
  688. target_link_libraries(lv2_ttl_generator PRIVATE "dl")
  689. endif()
  690. endfunction()
  691. # dpf__ensure_sources_non_empty
  692. # ------------------------------------------------------------------------------
  693. #
  694. # Ensure the given source list contains at least one file.
  695. # The function appends an empty source file to the list if necessary.
  696. # This is useful when CMake does not permit to add targets without sources.
  697. #
  698. function(dpf__ensure_sources_non_empty VAR)
  699. if(NOT "" STREQUAL "${${VAR}}")
  700. return()
  701. endif()
  702. set(_file "${CMAKE_CURRENT_BINARY_DIR}/_dpf_empty.c")
  703. if(NOT EXISTS "${_file}")
  704. file(WRITE "${_file}" "")
  705. endif()
  706. set("${VAR}" "${_file}" PARENT_SCOPE)
  707. endfunction()
  708. # dpf__create_dummy_source_list
  709. # ------------------------------------------------------------------------------
  710. #
  711. # Create a dummy source list which is equivalent to compiling nothing.
  712. # This is only for compatibility with older CMake versions, which refuse to add
  713. # targets without any sources.
  714. #
  715. macro(dpf__create_dummy_source_list VAR)
  716. set("${VAR}")
  717. if(CMAKE_VERSION VERSION_LESS "3.11")
  718. dpf__ensure_sources_non_empty("${VAR}")
  719. endif()
  720. endmacro()
  721. # dpf__warn_once
  722. # ------------------------------------------------------------------------------
  723. #
  724. # Prints a warning message once only.
  725. #
  726. function(dpf__warn_once_only TOKEN MESSAGE)
  727. get_property(_warned GLOBAL PROPERTY "dpf__have_warned_${TOKEN}")
  728. if(NOT _warned)
  729. set_property(GLOBAL PROPERTY "dpf__have_warned_${TOKEN}" TRUE)
  730. message(WARNING "${MESSAGE}")
  731. endif()
  732. endfunction()