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.

668 lines
23KB

  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
  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`
  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. else()
  145. message(FATAL_ERROR "Unrecognized target type for plugin: ${_target}")
  146. endif()
  147. endforeach()
  148. endfunction()
  149. # ------------------------------------------------------------------------------
  150. # DPF private functions (prefixed with `dpf__`)
  151. # ------------------------------------------------------------------------------
  152. # Note: The $<0:> trick is to prevent MSVC from appending the build type
  153. # to the output directory.
  154. #
  155. # dpf__build_jack
  156. # ------------------------------------------------------------------------------
  157. #
  158. # Add build rules for a JACK program.
  159. #
  160. function(dpf__build_jack NAME DGL_LIBRARY)
  161. dpf__create_dummy_source_list(_no_srcs)
  162. dpf__add_executable("${NAME}-jack" ${_no_srcs})
  163. dpf__add_plugin_main("${NAME}-jack" "jack")
  164. dpf__add_ui_main("${NAME}-jack" "jack" "${DGL_LIBRARY}")
  165. target_link_libraries("${NAME}-jack" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  166. set_target_properties("${NAME}-jack" PROPERTIES
  167. RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  168. OUTPUT_NAME "${NAME}")
  169. # Note: libjack will be linked at runtime
  170. if((NOT WIN32) AND (NOT APPLE) AND (NOT HAIKU))
  171. target_link_libraries("${NAME}-jack" PRIVATE "dl")
  172. endif()
  173. endfunction()
  174. # dpf__build_ladspa
  175. # ------------------------------------------------------------------------------
  176. #
  177. # Add build rules for a DSSI plugin.
  178. #
  179. function(dpf__build_ladspa NAME)
  180. dpf__create_dummy_source_list(_no_srcs)
  181. dpf__add_module("${NAME}-ladspa" ${_no_srcs})
  182. dpf__add_plugin_main("${NAME}-ladspa" "ladspa")
  183. target_link_libraries("${NAME}-ladspa" PRIVATE "${NAME}-dsp")
  184. set_target_properties("${NAME}-ladspa" PROPERTIES
  185. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  186. OUTPUT_NAME "${NAME}-ladspa"
  187. PREFIX "")
  188. endfunction()
  189. # dpf__build_dssi
  190. # ------------------------------------------------------------------------------
  191. #
  192. # Add build rules for a DSSI plugin.
  193. #
  194. function(dpf__build_dssi NAME DGL_LIBRARY)
  195. find_package(PkgConfig)
  196. pkg_check_modules(LIBLO "liblo")
  197. if(NOT LIBLO_FOUND)
  198. dpf__warn_once_only(missing_liblo
  199. "liblo is not found, skipping the `dssi` plugin targets")
  200. return()
  201. endif()
  202. link_directories(${LIBLO_LIBRARY_DIRS})
  203. dpf__create_dummy_source_list(_no_srcs)
  204. dpf__add_module("${NAME}-dssi" ${_no_srcs})
  205. dpf__add_plugin_main("${NAME}-dssi" "dssi")
  206. target_link_libraries("${NAME}-dssi" PRIVATE "${NAME}-dsp")
  207. set_target_properties("${NAME}-dssi" PROPERTIES
  208. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  209. OUTPUT_NAME "${NAME}-dssi"
  210. PREFIX "")
  211. if(DGL_LIBRARY)
  212. dpf__add_executable("${NAME}-dssi-ui" ${_no_srcs})
  213. dpf__add_ui_main("${NAME}-dssi-ui" "dssi" "${DGL_LIBRARY}")
  214. target_link_libraries("${NAME}-dssi-ui" PRIVATE "${NAME}-ui")
  215. set_target_properties("${NAME}-dssi-ui" PROPERTIES
  216. RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}-dssi/$<0:>"
  217. OUTPUT_NAME "${NAME}_ui")
  218. target_include_directories("${NAME}-dssi-ui" PRIVATE ${LIBLO_INCLUDE_DIRS})
  219. target_link_libraries("${NAME}-dssi-ui" PRIVATE ${LIBLO_LIBRARIES})
  220. endif()
  221. endfunction()
  222. # dpf__build_lv2
  223. # ------------------------------------------------------------------------------
  224. #
  225. # Add build rules for a LV2 plugin.
  226. #
  227. function(dpf__build_lv2 NAME DGL_LIBRARY MONOLITHIC)
  228. dpf__create_dummy_source_list(_no_srcs)
  229. dpf__add_module("${NAME}-lv2" ${_no_srcs})
  230. dpf__add_plugin_main("${NAME}-lv2" "lv2")
  231. target_link_libraries("${NAME}-lv2" PRIVATE "${NAME}-dsp")
  232. set_target_properties("${NAME}-lv2" PROPERTIES
  233. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2/$<0:>"
  234. OUTPUT_NAME "${NAME}_dsp"
  235. PREFIX "")
  236. if(DGL_LIBRARY)
  237. if(MONOLITHIC)
  238. dpf__add_ui_main("${NAME}-lv2" "lv2" "${DGL_LIBRARY}")
  239. target_link_libraries("${NAME}-lv2" PRIVATE "${NAME}-ui")
  240. set_target_properties("${NAME}-lv2" PROPERTIES
  241. OUTPUT_NAME "${NAME}")
  242. else()
  243. dpf__add_module("${NAME}-lv2-ui" ${_no_srcs})
  244. dpf__add_ui_main("${NAME}-lv2-ui" "lv2" "${DGL_LIBRARY}")
  245. target_link_libraries("${NAME}-lv2-ui" PRIVATE "${NAME}-ui")
  246. set_target_properties("${NAME}-lv2-ui" PROPERTIES
  247. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2/$<0:>"
  248. OUTPUT_NAME "${NAME}_ui"
  249. PREFIX "")
  250. endif()
  251. endif()
  252. dpf__add_lv2_ttl_generator()
  253. add_dependencies("${NAME}-lv2" lv2_ttl_generator)
  254. add_custom_command(TARGET "${NAME}-lv2" POST_BUILD
  255. COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR}
  256. "$<TARGET_FILE:lv2_ttl_generator>"
  257. "$<TARGET_FILE:${NAME}-lv2>"
  258. WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2"
  259. DEPENDS lv2_ttl_generator)
  260. endfunction()
  261. # dpf__build_vst2
  262. # ------------------------------------------------------------------------------
  263. #
  264. # Add build rules for a VST2 plugin.
  265. #
  266. function(dpf__build_vst2 NAME DGL_LIBRARY)
  267. dpf__create_dummy_source_list(_no_srcs)
  268. dpf__add_module("${NAME}-vst2" ${_no_srcs})
  269. dpf__add_plugin_main("${NAME}-vst2" "vst2")
  270. dpf__add_ui_main("${NAME}-vst2" "vst2" "${DGL_LIBRARY}")
  271. target_link_libraries("${NAME}-vst2" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  272. set_target_properties("${NAME}-vst2" PROPERTIES
  273. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  274. OUTPUT_NAME "${NAME}-vst2"
  275. PREFIX "")
  276. endfunction()
  277. # dpf__add_dgl_cairo
  278. # ------------------------------------------------------------------------------
  279. #
  280. # Add the Cairo variant of DGL, if not already available.
  281. #
  282. function(dpf__add_dgl_cairo)
  283. if(TARGET dgl-cairo)
  284. return()
  285. endif()
  286. find_package(PkgConfig)
  287. pkg_check_modules(CAIRO "cairo" REQUIRED)
  288. link_directories(${CAIRO_LIBRARY_DIRS})
  289. dpf__add_static_library(dgl-cairo STATIC
  290. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  291. "${DPF_ROOT_DIR}/dgl/src/ApplicationPrivateData.cpp"
  292. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  293. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  294. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  295. "${DPF_ROOT_DIR}/dgl/src/ImageBaseWidgets.cpp"
  296. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  297. "${DPF_ROOT_DIR}/dgl/src/SubWidget.cpp"
  298. "${DPF_ROOT_DIR}/dgl/src/SubWidgetPrivateData.cpp"
  299. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidget.cpp"
  300. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidgetPrivateData.cpp"
  301. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  302. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp"
  303. "${DPF_ROOT_DIR}/dgl/src/Window.cpp"
  304. "${DPF_ROOT_DIR}/dgl/src/WindowPrivateData.cpp"
  305. "${DPF_ROOT_DIR}/dgl/src/Cairo.cpp")
  306. if(NOT APPLE)
  307. target_sources(dgl-cairo PRIVATE
  308. "${DPF_ROOT_DIR}/dgl/src/pugl.cpp")
  309. else() # Note: macOS pugl will be built as part of DistrhoUI_macOS.mm
  310. #target_sources(dgl-opengl PRIVATE
  311. # "${DPF_ROOT_DIR}/dgl/src/pugl.mm")
  312. endif()
  313. target_include_directories(dgl-cairo PUBLIC
  314. "${DPF_ROOT_DIR}/dgl")
  315. target_include_directories(dgl-cairo PUBLIC
  316. "${DPF_ROOT_DIR}/dgl/src/pugl-upstream/include")
  317. dpf__add_dgl_system_libs()
  318. target_link_libraries(dgl-cairo PRIVATE dgl-system-libs)
  319. add_library(dgl-cairo-definitions INTERFACE)
  320. target_compile_definitions(dgl-cairo-definitions INTERFACE "DGL_CAIRO" "HAVE_CAIRO")
  321. target_include_directories(dgl-cairo PUBLIC ${CAIRO_INCLUDE_DIRS})
  322. if(MINGW)
  323. target_link_libraries(dgl-cairo PRIVATE ${CAIRO_STATIC_LIBRARIES})
  324. else()
  325. target_link_libraries(dgl-cairo PRIVATE ${CAIRO_LIBRARIES})
  326. endif()
  327. target_link_libraries(dgl-cairo PRIVATE dgl-cairo-definitions)
  328. endfunction()
  329. # dpf__add_dgl_opengl
  330. # ------------------------------------------------------------------------------
  331. #
  332. # Add the OpenGL variant of DGL, if not already available.
  333. #
  334. function(dpf__add_dgl_opengl)
  335. if(TARGET dgl-opengl)
  336. return()
  337. endif()
  338. if(NOT OpenGL_GL_PREFERENCE)
  339. set(OpenGL_GL_PREFERENCE "LEGACY")
  340. endif()
  341. find_package(OpenGL REQUIRED)
  342. dpf__add_static_library(dgl-opengl STATIC
  343. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  344. "${DPF_ROOT_DIR}/dgl/src/ApplicationPrivateData.cpp"
  345. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  346. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  347. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  348. "${DPF_ROOT_DIR}/dgl/src/ImageBaseWidgets.cpp"
  349. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  350. "${DPF_ROOT_DIR}/dgl/src/SubWidget.cpp"
  351. "${DPF_ROOT_DIR}/dgl/src/SubWidgetPrivateData.cpp"
  352. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidget.cpp"
  353. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidgetPrivateData.cpp"
  354. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  355. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp"
  356. "${DPF_ROOT_DIR}/dgl/src/Window.cpp"
  357. "${DPF_ROOT_DIR}/dgl/src/WindowPrivateData.cpp"
  358. "${DPF_ROOT_DIR}/dgl/src/OpenGL.cpp"
  359. "${DPF_ROOT_DIR}/dgl/src/NanoVG.cpp")
  360. if(NOT APPLE)
  361. target_sources(dgl-opengl PRIVATE
  362. "${DPF_ROOT_DIR}/dgl/src/pugl.cpp")
  363. else() # Note: macOS pugl will be built as part of DistrhoUI_macOS.mm
  364. #target_sources(dgl-opengl PRIVATE
  365. # "${DPF_ROOT_DIR}/dgl/src/pugl.mm")
  366. endif()
  367. target_include_directories(dgl-opengl PUBLIC
  368. "${DPF_ROOT_DIR}/dgl")
  369. target_include_directories(dgl-opengl PUBLIC
  370. "${DPF_ROOT_DIR}/dgl/src/pugl-upstream/include")
  371. if(APPLE)
  372. target_compile_definitions(dgl-opengl PUBLIC "GL_SILENCE_DEPRECATION")
  373. endif()
  374. dpf__add_dgl_system_libs()
  375. target_link_libraries(dgl-opengl PRIVATE dgl-system-libs)
  376. add_library(dgl-opengl-definitions INTERFACE)
  377. target_compile_definitions(dgl-opengl-definitions INTERFACE "DGL_OPENGL" "HAVE_OPENGL")
  378. target_include_directories(dgl-opengl PUBLIC "${OPENGL_INCLUDE_DIR}")
  379. target_link_libraries(dgl-opengl PRIVATE dgl-opengl-definitions "${OPENGL_gl_LIBRARY}")
  380. endfunction()
  381. # dpf__add_plugin_specific_ui_sources
  382. # ------------------------------------------------------------------------------
  383. #
  384. # Compile plugin-specific UI sources into the target designated by the given
  385. # name. There are some special considerations here:
  386. # - On most platforms, sources can be compiled only once, as part of DGL;
  387. # - On macOS, for any sources which define Objective-C interfaces, these must
  388. # be recompiled for each plugin under a unique namespace. In this case, the
  389. # name must be a plugin-specific identifier, and it will be used for computing
  390. # the unique ID along with the project version.
  391. function(dpf__add_plugin_specific_ui_sources NAME)
  392. if(APPLE)
  393. target_sources("${NAME}" PRIVATE
  394. "${DPF_ROOT_DIR}/distrho/DistrhoUI_macOS.mm")
  395. string(SHA256 _hash "${NAME}:${PROJECT_VERSION}")
  396. target_compile_definitions("${NAME}" PUBLIC "PUGL_NAMESPACE=${_hash}")
  397. endif()
  398. endfunction()
  399. # dpf__add_dgl_system_libs
  400. # ------------------------------------------------------------------------------
  401. #
  402. # Find system libraries required by DGL and add them as an interface target.
  403. #
  404. function(dpf__add_dgl_system_libs)
  405. if(TARGET dgl-system-libs)
  406. return()
  407. endif()
  408. add_library(dgl-system-libs INTERFACE)
  409. add_library(dgl-system-libs-definitions INTERFACE)
  410. if(HAIKU)
  411. target_link_libraries(dgl-system-libs INTERFACE "be")
  412. elseif(WIN32)
  413. target_link_libraries(dgl-system-libs INTERFACE "gdi32" "comdlg32")
  414. elseif(APPLE)
  415. find_library(APPLE_COCOA_FRAMEWORK "Cocoa")
  416. find_library(APPLE_COREVIDEO_FRAMEWORK "CoreVideo")
  417. target_link_libraries(dgl-system-libs INTERFACE "${APPLE_COCOA_FRAMEWORK}" "${APPLE_COREVIDEO_FRAMEWORK}")
  418. else()
  419. find_package(X11 REQUIRED)
  420. target_include_directories(dgl-system-libs INTERFACE "${X11_INCLUDE_DIR}")
  421. target_link_libraries(dgl-system-libs INTERFACE "${X11_X11_LIB}")
  422. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_X11")
  423. if(X11_Xext_FOUND)
  424. target_link_libraries(dgl-system-libs INTERFACE "${X11_Xext_LIB}")
  425. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XEXT")
  426. endif()
  427. if(X11_XSync_FOUND)
  428. target_link_libraries(dgl-system-libs INTERFACE "${X11_XSync_LIB}")
  429. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XSYNC")
  430. endif()
  431. if(X11_Xrandr_FOUND)
  432. target_link_libraries(dgl-system-libs INTERFACE "${X11_Xrandr_LIB}")
  433. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XRANDR")
  434. endif()
  435. #if(X11_Xcursor_FOUND)
  436. # target_link_libraries(dgl-system-libs INTERFACE "${X11_Xcursor_LIB}")
  437. # target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_XCURSOR")
  438. #endif()
  439. endif()
  440. if(MSVC)
  441. file(MAKE_DIRECTORY "${DPF_ROOT_DIR}/khronos/GL")
  442. foreach(_gl_header "glext.h")
  443. if(NOT EXISTS "${DPF_ROOT_DIR}/khronos/GL/${_gl_header}")
  444. file(DOWNLOAD "https://www.khronos.org/registry/OpenGL/api/GL/${_gl_header}" "${DPF_ROOT_DIR}/khronos/GL/${_gl_header}" SHOW_PROGRESS)
  445. endif()
  446. endforeach()
  447. foreach(_khr_header "khrplatform.h")
  448. if(NOT EXISTS "${DPF_ROOT_DIR}/khronos/KHR/${_khr_header}")
  449. file(DOWNLOAD "https://www.khronos.org/registry/EGL/api/KHR/${_khr_header}" "${DPF_ROOT_DIR}/khronos/KHR/${_khr_header}" SHOW_PROGRESS)
  450. endif()
  451. endforeach()
  452. target_include_directories(dgl-system-libs-definitions INTERFACE "${DPF_ROOT_DIR}/khronos")
  453. endif()
  454. target_link_libraries(dgl-system-libs INTERFACE dgl-system-libs-definitions)
  455. endfunction()
  456. # dpf__add_executable
  457. # ------------------------------------------------------------------------------
  458. #
  459. # Adds an executable target, and set some default properties on the target.
  460. #
  461. function(dpf__add_executable NAME)
  462. add_executable("${NAME}" ${ARGN})
  463. dpf__set_target_defaults("${NAME}")
  464. if(MINGW)
  465. target_link_libraries("${NAME}" PRIVATE "-static")
  466. endif()
  467. endfunction()
  468. # dpf__add_module
  469. # ------------------------------------------------------------------------------
  470. #
  471. # Adds a module target, and set some default properties on the target.
  472. #
  473. function(dpf__add_module NAME)
  474. add_library("${NAME}" MODULE ${ARGN})
  475. dpf__set_target_defaults("${NAME}")
  476. if(MINGW)
  477. target_link_libraries("${NAME}" PRIVATE "-static")
  478. endif()
  479. endfunction()
  480. # dpf__add_static_library
  481. # ------------------------------------------------------------------------------
  482. #
  483. # Adds a static library target, and set some default properties on the target.
  484. #
  485. function(dpf__add_static_library NAME)
  486. add_library("${NAME}" STATIC ${ARGN})
  487. dpf__set_target_defaults("${NAME}")
  488. endfunction()
  489. # dpf__set_target_defaults
  490. # ------------------------------------------------------------------------------
  491. #
  492. # Set default properties which must apply to all DPF-defined targets.
  493. #
  494. function(dpf__set_target_defaults NAME)
  495. set_target_properties("${NAME}" PROPERTIES
  496. POSITION_INDEPENDENT_CODE TRUE
  497. C_VISIBILITY_PRESET "hidden"
  498. CXX_VISIBILITY_PRESET "hidden"
  499. VISIBILITY_INLINES_HIDDEN TRUE)
  500. if(WIN32)
  501. target_compile_definitions("${NAME}" PUBLIC "NOMINMAX")
  502. endif()
  503. if (MINGW)
  504. target_compile_options("${NAME}" PUBLIC "-mstackrealign")
  505. endif()
  506. if (MSVC)
  507. target_compile_options("${NAME}" PUBLIC "/UTF-8")
  508. target_compile_definitions("${NAME}" PUBLIC "_CRT_SECURE_NO_WARNINGS")
  509. endif()
  510. endfunction()
  511. # dpf__add_plugin_main
  512. # ------------------------------------------------------------------------------
  513. #
  514. # Adds plugin code to the given target.
  515. #
  516. function(dpf__add_plugin_main NAME TARGET)
  517. target_sources("${NAME}" PRIVATE
  518. "${DPF_ROOT_DIR}/distrho/DistrhoPluginMain.cpp")
  519. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  520. endfunction()
  521. # dpf__add_ui_main
  522. # ------------------------------------------------------------------------------
  523. #
  524. # Adds UI code to the given target (only if the target has UI).
  525. #
  526. function(dpf__add_ui_main NAME TARGET HAS_UI)
  527. if(HAS_UI)
  528. target_sources("${NAME}" PRIVATE
  529. "${DPF_ROOT_DIR}/distrho/DistrhoUIMain.cpp")
  530. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  531. endif()
  532. endfunction()
  533. # dpf__add_plugin_target_definition
  534. # ------------------------------------------------------------------------------
  535. #
  536. # Adds the plugins target macro definition.
  537. # This selects which entry file is compiled according to the target type.
  538. #
  539. function(dpf__add_plugin_target_definition NAME TARGET)
  540. string(TOUPPER "${TARGET}" _upperTarget)
  541. # resolve the alias into the proper name
  542. # the name "vst2" is new, "vst" is legacy
  543. if(_upperTarget STREQUAL "VST2")
  544. set(_upperTarget "VST")
  545. endif()
  546. target_compile_definitions("${NAME}" PRIVATE "DISTRHO_PLUGIN_TARGET_${_upperTarget}")
  547. endfunction()
  548. # dpf__add_lv2_ttl_generator
  549. # ------------------------------------------------------------------------------
  550. #
  551. # Build the LV2 TTL generator.
  552. #
  553. function(dpf__add_lv2_ttl_generator)
  554. if(TARGET lv2_ttl_generator)
  555. return()
  556. endif()
  557. add_executable(lv2_ttl_generator "${DPF_ROOT_DIR}/utils/lv2-ttl-generator/lv2_ttl_generator.c")
  558. if((NOT WIN32) AND (NOT APPLE) AND (NOT HAIKU))
  559. target_link_libraries(lv2_ttl_generator PRIVATE "dl")
  560. endif()
  561. endfunction()
  562. # dpf__ensure_sources_non_empty
  563. # ------------------------------------------------------------------------------
  564. #
  565. # Ensure the given source list contains at least one file.
  566. # The function appends an empty source file to the list if necessary.
  567. # This is useful when CMake does not permit to add targets without sources.
  568. #
  569. function(dpf__ensure_sources_non_empty VAR)
  570. if(NOT "" STREQUAL "${${VAR}}")
  571. return()
  572. endif()
  573. set(_file "${CMAKE_CURRENT_BINARY_DIR}/_dpf_empty.c")
  574. if(NOT EXISTS "${_file}")
  575. file(WRITE "${_file}" "")
  576. endif()
  577. set("${VAR}" "${_file}" PARENT_SCOPE)
  578. endfunction()
  579. # dpf__create_dummy_source_list
  580. # ------------------------------------------------------------------------------
  581. #
  582. # Create a dummy source list which is equivalent to compiling nothing.
  583. # This is only for compatibility with older CMake versions, which refuse to add
  584. # targets without any sources.
  585. #
  586. macro(dpf__create_dummy_source_list VAR)
  587. set("${VAR}")
  588. if(CMAKE_VERSION VERSION_LESS "3.11")
  589. dpf__ensure_sources_non_empty("${VAR}")
  590. endif()
  591. endmacro()
  592. # dpf__warn_once
  593. # ------------------------------------------------------------------------------
  594. #
  595. # Prints a warning message once only.
  596. #
  597. function(dpf__warn_once_only TOKEN MESSAGE)
  598. get_property(_warned GLOBAL PROPERTY "dpf__have_warned_${TOKEN}")
  599. if(NOT _warned)
  600. set_property(GLOBAL PROPERTY "dpf__have_warned_${TOKEN}" TRUE)
  601. message(WARNING "${MESSAGE}")
  602. endif()
  603. endfunction()