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.

747 lines
26KB

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