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.

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