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.

598 lines
20KB

  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)
  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. else()
  128. add_library("${NAME}-ui" INTERFACE)
  129. endif()
  130. ###
  131. foreach(_target ${_dpf_plugin_TARGETS})
  132. if(_target STREQUAL "jack")
  133. dpf__build_jack("${NAME}" "${_dgl_library}")
  134. elseif(_target STREQUAL "ladspa")
  135. dpf__build_ladspa("${NAME}")
  136. elseif(_target STREQUAL "dssi")
  137. dpf__build_dssi("${NAME}" "${_dgl_library}")
  138. elseif(_target STREQUAL "lv2")
  139. dpf__build_lv2("${NAME}" "${_dgl_library}" "${_dpf_plugin_MONOLITHIC}")
  140. elseif(_target STREQUAL "vst2")
  141. dpf__build_vst2("${NAME}" "${_dgl_library}")
  142. else()
  143. message(FATAL_ERROR "Unrecognized target type for plugin: ${_target}")
  144. endif()
  145. endforeach()
  146. endfunction()
  147. # ------------------------------------------------------------------------------
  148. # DPF private functions (prefixed with `dpf__`)
  149. # ------------------------------------------------------------------------------
  150. # Note: The $<0:> trick is to prevent MSVC from appending the build type
  151. # to the output directory.
  152. #
  153. # dpf__build_jack
  154. # ------------------------------------------------------------------------------
  155. #
  156. # Add build rules for a JACK program.
  157. #
  158. function(dpf__build_jack NAME DGL_LIBRARY)
  159. find_package(PkgConfig)
  160. pkg_check_modules(JACK "jack")
  161. if(NOT JACK_FOUND)
  162. dpf__warn_once_only(missing_jack
  163. "JACK is not found, skipping the `jack` plugin targets")
  164. return()
  165. endif()
  166. link_directories(${JACK_LIBRARY_DIRS})
  167. dpf__create_dummy_source_list(_no_srcs)
  168. dpf__add_executable("${NAME}-jack" ${_no_srcs})
  169. dpf__add_plugin_main("${NAME}-jack" "jack")
  170. dpf__add_ui_main("${NAME}-jack" "jack" "${DGL_LIBRARY}")
  171. target_link_libraries("${NAME}-jack" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  172. set_target_properties("${NAME}-jack" PROPERTIES
  173. RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  174. OUTPUT_NAME "${NAME}")
  175. target_include_directories("${NAME}-jack" PRIVATE ${JACK_INCLUDE_DIRS})
  176. target_link_libraries("${NAME}-jack" PRIVATE ${JACK_LIBRARIES})
  177. endfunction()
  178. # dpf__build_ladspa
  179. # ------------------------------------------------------------------------------
  180. #
  181. # Add build rules for a DSSI plugin.
  182. #
  183. function(dpf__build_ladspa NAME)
  184. dpf__create_dummy_source_list(_no_srcs)
  185. dpf__add_module("${NAME}-ladspa" ${_no_srcs})
  186. dpf__add_plugin_main("${NAME}-ladspa" "ladspa")
  187. target_link_libraries("${NAME}-ladspa" PRIVATE "${NAME}-dsp")
  188. set_target_properties("${NAME}-ladspa" PROPERTIES
  189. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  190. OUTPUT_NAME "${NAME}-ladspa"
  191. PREFIX "")
  192. endfunction()
  193. # dpf__build_dssi
  194. # ------------------------------------------------------------------------------
  195. #
  196. # Add build rules for a DSSI plugin.
  197. #
  198. function(dpf__build_dssi NAME DGL_LIBRARY)
  199. find_package(PkgConfig)
  200. pkg_check_modules(LIBLO "liblo")
  201. if(NOT LIBLO_FOUND)
  202. dpf__warn_once_only(missing_liblo
  203. "liblo is not found, skipping the `dssi` plugin targets")
  204. return()
  205. endif()
  206. link_directories(${LIBLO_LIBRARY_DIRS})
  207. dpf__create_dummy_source_list(_no_srcs)
  208. dpf__add_module("${NAME}-dssi" ${_no_srcs})
  209. dpf__add_plugin_main("${NAME}-dssi" "dssi")
  210. target_link_libraries("${NAME}-dssi" PRIVATE "${NAME}-dsp")
  211. set_target_properties("${NAME}-dssi" PROPERTIES
  212. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  213. OUTPUT_NAME "${NAME}-dssi"
  214. PREFIX "")
  215. if(DGL_LIBRARY)
  216. dpf__add_executable("${NAME}-dssi-ui" ${_no_srcs})
  217. dpf__add_ui_main("${NAME}-dssi-ui" "dssi" "${DGL_LIBRARY}")
  218. target_link_libraries("${NAME}-dssi-ui" PRIVATE "${NAME}-ui")
  219. set_target_properties("${NAME}-dssi-ui" PROPERTIES
  220. RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}-dssi/$<0:>"
  221. OUTPUT_NAME "${NAME}_ui")
  222. target_include_directories("${NAME}-dssi-ui" PRIVATE ${LIBLO_INCLUDE_DIRS})
  223. target_link_libraries("${NAME}-dssi-ui" PRIVATE ${LIBLO_LIBRARIES})
  224. endif()
  225. endfunction()
  226. # dpf__build_lv2
  227. # ------------------------------------------------------------------------------
  228. #
  229. # Add build rules for a LV2 plugin.
  230. #
  231. function(dpf__build_lv2 NAME DGL_LIBRARY MONOLITHIC)
  232. dpf__create_dummy_source_list(_no_srcs)
  233. dpf__add_module("${NAME}-lv2" ${_no_srcs})
  234. dpf__add_plugin_main("${NAME}-lv2" "lv2")
  235. target_link_libraries("${NAME}-lv2" PRIVATE "${NAME}-dsp")
  236. set_target_properties("${NAME}-lv2" PROPERTIES
  237. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2/$<0:>"
  238. OUTPUT_NAME "${NAME}_dsp"
  239. PREFIX "")
  240. if(DGL_LIBRARY)
  241. if(MONOLITHIC)
  242. dpf__add_ui_main("${NAME}-lv2" "lv2" "${DGL_LIBRARY}")
  243. target_link_libraries("${NAME}-lv2" PRIVATE "${NAME}-ui")
  244. set_target_properties("${NAME}-lv2" PROPERTIES
  245. OUTPUT_NAME "${NAME}")
  246. else()
  247. dpf__add_module("${NAME}-lv2-ui" ${_no_srcs})
  248. dpf__add_ui_main("${NAME}-lv2-ui" "lv2" "${DGL_LIBRARY}")
  249. target_link_libraries("${NAME}-lv2-ui" PRIVATE "${NAME}-ui")
  250. set_target_properties("${NAME}-lv2-ui" PROPERTIES
  251. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2/$<0:>"
  252. OUTPUT_NAME "${NAME}_ui"
  253. PREFIX "")
  254. endif()
  255. endif()
  256. dpf__add_lv2_ttl_generator()
  257. add_dependencies("${NAME}-lv2" lv2_ttl_generator)
  258. add_custom_command(TARGET "${NAME}-lv2" POST_BUILD
  259. COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR}
  260. "$<TARGET_FILE:lv2_ttl_generator>"
  261. "$<TARGET_FILE:${NAME}-lv2>"
  262. WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2")
  263. endfunction()
  264. # dpf__build_vst2
  265. # ------------------------------------------------------------------------------
  266. #
  267. # Add build rules for a VST2 plugin.
  268. #
  269. function(dpf__build_vst2 NAME DGL_LIBRARY)
  270. dpf__create_dummy_source_list(_no_srcs)
  271. dpf__add_module("${NAME}-vst2" ${_no_srcs})
  272. dpf__add_plugin_main("${NAME}-vst2" "vst2")
  273. dpf__add_ui_main("${NAME}-vst2" "vst2" "${DGL_LIBRARY}")
  274. target_link_libraries("${NAME}-vst2" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  275. set_target_properties("${NAME}-vst2" PROPERTIES
  276. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  277. OUTPUT_NAME "${NAME}-vst2"
  278. PREFIX "")
  279. endfunction()
  280. # dpf__add_dgl_cairo
  281. # ------------------------------------------------------------------------------
  282. #
  283. # Add the Cairo variant of DGL, if not already available.
  284. #
  285. function(dpf__add_dgl_cairo)
  286. if(TARGET dgl-cairo)
  287. return()
  288. endif()
  289. find_package(PkgConfig)
  290. pkg_check_modules(CAIRO "cairo" REQUIRED)
  291. link_directories(${CAIRO_LIBRARY_DIRS})
  292. dpf__add_static_library(dgl-cairo STATIC
  293. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  294. "${DPF_ROOT_DIR}/dgl/src/ApplicationPrivateData.cpp"
  295. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  296. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  297. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  298. "${DPF_ROOT_DIR}/dgl/src/ImageBaseWidgets.cpp"
  299. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  300. "${DPF_ROOT_DIR}/dgl/src/SubWidget.cpp"
  301. "${DPF_ROOT_DIR}/dgl/src/SubWidgetPrivateData.cpp"
  302. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidget.cpp"
  303. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidgetPrivateData.cpp"
  304. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  305. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp"
  306. "${DPF_ROOT_DIR}/dgl/src/Window.cpp"
  307. "${DPF_ROOT_DIR}/dgl/src/WindowPrivateData.cpp"
  308. "${DPF_ROOT_DIR}/dgl/src/Cairo.cpp")
  309. if(NOT APPLE)
  310. target_sources(dgl-cairo PRIVATE
  311. "${DPF_ROOT_DIR}/dgl/src/pugl.cpp")
  312. else()
  313. target_sources(dgl-cairo PRIVATE
  314. "${DPF_ROOT_DIR}/dgl/src/pugl.mm")
  315. endif()
  316. target_include_directories(dgl-cairo PUBLIC
  317. "${DPF_ROOT_DIR}/dgl")
  318. target_include_directories(dgl-cairo PRIVATE
  319. "${DPF_ROOT_DIR}/dgl/src/pugl-upstream/include")
  320. dpf__add_dgl_system_libs()
  321. target_link_libraries(dgl-cairo PRIVATE dgl-system-libs)
  322. add_library(dgl-cairo-definitions INTERFACE)
  323. target_compile_definitions(dgl-cairo-definitions INTERFACE "DGL_CAIRO" "HAVE_CAIRO")
  324. target_include_directories(dgl-cairo PUBLIC ${CAIRO_INCLUDE_DIRS})
  325. target_link_libraries(dgl-cairo PRIVATE dgl-cairo-definitions ${CAIRO_LIBRARIES})
  326. endfunction()
  327. # dpf__add_dgl_opengl
  328. # ------------------------------------------------------------------------------
  329. #
  330. # Add the OpenGL variant of DGL, if not already available.
  331. #
  332. function(dpf__add_dgl_opengl)
  333. if(TARGET dgl-opengl)
  334. return()
  335. endif()
  336. if(NOT OpenGL_GL_PREFERENCE)
  337. set(OpenGL_GL_PREFERENCE "LEGACY")
  338. endif()
  339. find_package(OpenGL REQUIRED)
  340. dpf__add_static_library(dgl-opengl STATIC
  341. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  342. "${DPF_ROOT_DIR}/dgl/src/ApplicationPrivateData.cpp"
  343. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  344. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  345. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  346. "${DPF_ROOT_DIR}/dgl/src/ImageBaseWidgets.cpp"
  347. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  348. "${DPF_ROOT_DIR}/dgl/src/SubWidget.cpp"
  349. "${DPF_ROOT_DIR}/dgl/src/SubWidgetPrivateData.cpp"
  350. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidget.cpp"
  351. "${DPF_ROOT_DIR}/dgl/src/TopLevelWidgetPrivateData.cpp"
  352. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  353. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp"
  354. "${DPF_ROOT_DIR}/dgl/src/Window.cpp"
  355. "${DPF_ROOT_DIR}/dgl/src/WindowPrivateData.cpp"
  356. "${DPF_ROOT_DIR}/dgl/src/OpenGL.cpp"
  357. "${DPF_ROOT_DIR}/dgl/src/NanoVG.cpp")
  358. if(NOT APPLE)
  359. target_sources(dgl-opengl PRIVATE
  360. "${DPF_ROOT_DIR}/dgl/src/pugl.cpp")
  361. else()
  362. target_sources(dgl-opengl PRIVATE
  363. "${DPF_ROOT_DIR}/dgl/src/pugl.mm")
  364. endif()
  365. target_include_directories(dgl-opengl PUBLIC
  366. "${DPF_ROOT_DIR}/dgl")
  367. target_include_directories(dgl-opengl PRIVATE
  368. "${DPF_ROOT_DIR}/dgl/src/pugl-upstream/include")
  369. dpf__add_dgl_system_libs()
  370. target_link_libraries(dgl-opengl PRIVATE dgl-system-libs)
  371. add_library(dgl-opengl-definitions INTERFACE)
  372. target_compile_definitions(dgl-opengl-definitions INTERFACE "DGL_OPENGL" "HAVE_OPENGL")
  373. target_include_directories(dgl-opengl PUBLIC "${OPENGL_INCLUDE_DIR}")
  374. target_link_libraries(dgl-opengl PRIVATE dgl-opengl-definitions "${OPENGL_gl_LIBRARY}")
  375. endfunction()
  376. # dpf__add_dgl_system_libs
  377. # ------------------------------------------------------------------------------
  378. #
  379. # Find system libraries required by DGL and add them as an interface target.
  380. #
  381. function(dpf__add_dgl_system_libs)
  382. if(TARGET dgl-system-libs)
  383. return()
  384. endif()
  385. add_library(dgl-system-libs INTERFACE)
  386. add_library(dgl-system-libs-definitions INTERFACE)
  387. if(HAIKU)
  388. target_link_libraries(dgl-system-libs INTERFACE "be")
  389. elseif(WIN32)
  390. target_link_libraries(dgl-system-libs INTERFACE "gdi32" "comdlg32")
  391. elseif(APPLE)
  392. find_library(APPLE_COCOA_FRAMEWORK "Cocoa")
  393. target_link_libraries(dgl-system-libs INTERFACE "${APPLE_COCOA_FRAMEWORK}")
  394. else()
  395. find_package(X11 REQUIRED)
  396. target_include_directories(dgl-system-libs INTERFACE "${X11_INCLUDE_DIR}")
  397. target_link_libraries(dgl-system-libs INTERFACE "${X11_X11_LIB}")
  398. target_compile_definitions(dgl-system-libs-definitions INTERFACE "HAVE_X11")
  399. endif()
  400. target_link_libraries(dgl-system-libs INTERFACE dgl-system-libs-definitions)
  401. endfunction()
  402. # dpf__add_executable
  403. # ------------------------------------------------------------------------------
  404. #
  405. # Adds an executable target, and set some default properties on the target.
  406. #
  407. function(dpf__add_executable NAME)
  408. add_executable("${NAME}" ${ARGN})
  409. set_target_properties("${NAME}" PROPERTIES
  410. POSITION_INDEPENDENT_CODE TRUE
  411. C_VISIBILITY_PRESET "hidden"
  412. CXX_VISIBILITY_PRESET "hidden"
  413. VISIBILITY_INLINES_HIDDEN TRUE)
  414. endfunction()
  415. # dpf__add_module
  416. # ------------------------------------------------------------------------------
  417. #
  418. # Adds a module target, and set some default properties on the target.
  419. #
  420. function(dpf__add_module NAME)
  421. add_library("${NAME}" MODULE ${ARGN})
  422. set_target_properties("${NAME}" PROPERTIES
  423. POSITION_INDEPENDENT_CODE TRUE
  424. C_VISIBILITY_PRESET "hidden"
  425. CXX_VISIBILITY_PRESET "hidden"
  426. VISIBILITY_INLINES_HIDDEN TRUE)
  427. if ((NOT WIN32 AND NOT APPLE) OR MINGW)
  428. target_link_libraries("${NAME}" PRIVATE "-Wl,--no-undefined")
  429. endif()
  430. endfunction()
  431. # dpf__add_static_library
  432. # ------------------------------------------------------------------------------
  433. #
  434. # Adds a static library target, and set some default properties on the target.
  435. #
  436. function(dpf__add_static_library NAME)
  437. add_library("${NAME}" STATIC ${ARGN})
  438. set_target_properties("${NAME}" PROPERTIES
  439. POSITION_INDEPENDENT_CODE TRUE
  440. C_VISIBILITY_PRESET "hidden"
  441. CXX_VISIBILITY_PRESET "hidden"
  442. VISIBILITY_INLINES_HIDDEN TRUE)
  443. endfunction()
  444. # dpf__add_plugin_main
  445. # ------------------------------------------------------------------------------
  446. #
  447. # Adds plugin code to the given target.
  448. #
  449. function(dpf__add_plugin_main NAME TARGET)
  450. target_sources("${NAME}" PRIVATE
  451. "${DPF_ROOT_DIR}/distrho/DistrhoPluginMain.cpp")
  452. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  453. endfunction()
  454. # dpf__add_ui_main
  455. # ------------------------------------------------------------------------------
  456. #
  457. # Adds UI code to the given target (only if the target has UI).
  458. #
  459. function(dpf__add_ui_main NAME TARGET HAS_UI)
  460. if(HAS_UI)
  461. target_sources("${NAME}" PRIVATE
  462. "${DPF_ROOT_DIR}/distrho/DistrhoUIMain.cpp")
  463. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  464. endif()
  465. endfunction()
  466. # dpf__add_plugin_target_definition
  467. # ------------------------------------------------------------------------------
  468. #
  469. # Adds the plugins target macro definition.
  470. # This selects which entry file is compiled according to the target type.
  471. #
  472. function(dpf__add_plugin_target_definition NAME TARGET)
  473. string(TOUPPER "${TARGET}" _upperTarget)
  474. # resolve the alias into the proper name
  475. # the name "vst2" is new, "vst" is legacy
  476. if(_upperTarget STREQUAL "VST2")
  477. set(_upperTarget "VST")
  478. endif()
  479. target_compile_definitions("${NAME}" PRIVATE "DISTRHO_PLUGIN_TARGET_${_upperTarget}")
  480. endfunction()
  481. # dpf__add_lv2_ttl_generator
  482. # ------------------------------------------------------------------------------
  483. #
  484. # Build the LV2 TTL generator.
  485. #
  486. function(dpf__add_lv2_ttl_generator)
  487. if(TARGET lv2_ttl_generator)
  488. return()
  489. endif()
  490. add_executable(lv2_ttl_generator "${DPF_ROOT_DIR}/utils/lv2-ttl-generator/lv2_ttl_generator.c")
  491. if(NOT WINDOWS AND NOT APPLE AND NOT HAIKU)
  492. target_link_libraries(lv2_ttl_generator "dl")
  493. endif()
  494. endfunction()
  495. # dpf__ensure_sources_non_empty
  496. # ------------------------------------------------------------------------------
  497. #
  498. # Ensure the given source list contains at least one file.
  499. # The function appends an empty source file to the list if necessary.
  500. # This is useful when CMake does not permit to add targets without sources.
  501. #
  502. function(dpf__ensure_sources_non_empty VAR)
  503. if(NOT "" STREQUAL "${${VAR}}")
  504. return()
  505. endif()
  506. set(_file "${CMAKE_CURRENT_BINARY_DIR}/_dpf_empty.c")
  507. if(NOT EXISTS "${_file}")
  508. file(WRITE "${_file}" "")
  509. endif()
  510. set("${VAR}" "${_file}" PARENT_SCOPE)
  511. endfunction()
  512. # dpf__create_dummy_source_list
  513. # ------------------------------------------------------------------------------
  514. #
  515. # Create a dummy source list which is equivalent to compiling nothing.
  516. # This is only for compatibility with older CMake versions, which refuse to add
  517. # targets without any sources.
  518. #
  519. macro(dpf__create_dummy_source_list VAR)
  520. set("${VAR}")
  521. if(CMAKE_VERSION VERSION_LESS "3.11")
  522. dpf__ensure_sources_non_empty("${VAR}")
  523. endif()
  524. endmacro()
  525. # dpf__warn_once
  526. # ------------------------------------------------------------------------------
  527. #
  528. # Prints a warning message once only.
  529. #
  530. function(dpf__warn_once_only TOKEN MESSAGE)
  531. get_property(_warned GLOBAL PROPERTY "dpf__have_warned_${TOKEN}")
  532. if(NOT _warned)
  533. set_property(GLOBAL PROPERTY "dpf__have_warned_${TOKEN}" TRUE)
  534. message(WARNING "${MESSAGE}")
  535. endif()
  536. endfunction()