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.

843 lines
29KB

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