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.

494 lines
16KB

  1. include(CMakeParseArguments)
  2. # ------------------------------------------------------------------------------
  3. # DPF public functions
  4. # ------------------------------------------------------------------------------
  5. # dpf_add_plugin(name <args...>)
  6. # ------------------------------------------------------------------------------
  7. #
  8. # Add a plugin built using the DISTRHO Plugin Framework.
  9. #
  10. # ------------------------------------------------------------------------------
  11. # Created targets:
  12. #
  13. # `<name>`
  14. # static library: the common part of the plugin
  15. # The public properties set on this target apply to both DSP and UI.
  16. #
  17. # `<name>-dsp`
  18. # static library: the DSP part of the plugin
  19. # The public properties set on this target apply to the DSP only.
  20. #
  21. # `<name>-ui`
  22. # static library: the UI part of the plugin
  23. # The public properties set on this target apply to the UI only.
  24. #
  25. # `<name>-<target>` for each target specified with the `TARGETS` argument.
  26. # This is target-dependent and not intended for public use.
  27. #
  28. # ------------------------------------------------------------------------------
  29. # Arguments:
  30. #
  31. # `TARGETS` <tgt1>...<tgtN>
  32. # a list of one of more of the following target types:
  33. # `jack`, `ladspa`, `dssi`, `lv2`, `vst`
  34. #
  35. # `UI_TYPE` <type>
  36. # the user interface type, if any: `cairo`, `opengl`
  37. #
  38. # `MONOLITHIC`
  39. # build LV2 as a single binary for UI and DSP
  40. #
  41. # `FILES_DSP` <file1>...<fileN>
  42. # list of sources which are part of the DSP
  43. #
  44. # `FILES_UI` <file1>...<fileN>
  45. # list of sources which are part of the UI
  46. #
  47. # `FILES_COMMON` <file1>...<fileN>
  48. # list of sources which are part of both DSP and UI
  49. #
  50. function(dpf_add_plugin NAME)
  51. set(options MONOLITHIC)
  52. set(oneValueArgs UI_TYPE)
  53. set(multiValueArgs TARGETS FILES_DSP FILES_UI)
  54. cmake_parse_arguments(_dpf_plugin "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  55. if(NOT _dpf_plugin_UI_TYPE)
  56. set(_dgl_library)
  57. elseif(_dpf_plugin_UI_TYPE STREQUAL "cairo")
  58. dpf__add_dgl_cairo()
  59. set(_dgl_library dgl-cairo)
  60. elseif(_dpf_plugin_UI_TYPE STREQUAL "opengl")
  61. dpf__add_dgl_opengl()
  62. set(_dgl_library dgl-opengl)
  63. else()
  64. message(FATAL_ERROR "Unrecognized UI type for plugin: ${UI_TYPE}")
  65. endif()
  66. ###
  67. dpf__ensure_sources_non_empty(_dpf_plugin_FILES_COMMON)
  68. dpf__ensure_sources_non_empty(_dpf_plugin_FILES_DSP)
  69. dpf__ensure_sources_non_empty(_dpf_plugin_FILES_UI)
  70. ###
  71. dpf__add_static_library("${NAME}" ${_dpf_plugin_FILES_COMMON})
  72. target_include_directories("${NAME}" PUBLIC
  73. "${DPF_ROOT_DIR}/distrho")
  74. if(_dgl_library)
  75. # make sure that all code will see DGL_* definitions
  76. target_link_libraries("${NAME}" PUBLIC "${_dgl_library}-definitions")
  77. endif()
  78. dpf__add_static_library("${NAME}-dsp" ${_dpf_plugin_FILES_DSP})
  79. target_link_libraries("${NAME}-dsp" PUBLIC "${NAME}")
  80. if(_dgl_library)
  81. dpf__add_static_library("${NAME}-ui" ${_dpf_plugin_FILES_UI})
  82. target_link_libraries("${NAME}-ui" PUBLIC "${NAME}" ${_dgl_library})
  83. else()
  84. add_library("${NAME}-ui" INTERFACE)
  85. endif()
  86. ###
  87. foreach(_target ${_dpf_plugin_TARGETS})
  88. if(_target STREQUAL "jack")
  89. dpf__build_jack("${NAME}" "${_dgl_library}")
  90. elseif(_target STREQUAL "ladspa")
  91. dpf__build_ladspa("${NAME}")
  92. elseif(_target STREQUAL "dssi")
  93. dpf__build_dssi("${NAME}" "${_dgl_library}")
  94. elseif(_target STREQUAL "lv2")
  95. dpf__build_lv2("${NAME}" "${_dgl_library}" "${_dpf_plugin_MONOLITHIC}")
  96. elseif(_target STREQUAL "vst")
  97. dpf__build_vst("${NAME}" "${_dgl_library}")
  98. endif()
  99. endforeach()
  100. endfunction()
  101. # ------------------------------------------------------------------------------
  102. # DPF private functions (prefixed with `dpf__`)
  103. # ------------------------------------------------------------------------------
  104. # Note: The $<0:> trick is to prevent MSVC from appending the build type
  105. # to the output directory.
  106. #
  107. # dpf__build_jack
  108. # ------------------------------------------------------------------------------
  109. #
  110. # Add build rules for a JACK program.
  111. #
  112. function(dpf__build_jack NAME DGL_LIBRARY)
  113. find_package(PkgConfig)
  114. pkg_check_modules(JACK "jack")
  115. if(NOT JACK_FOUND)
  116. dpf__warn_once_only(missing_jack
  117. "JACK is not found, skipping the `jack` plugin targets")
  118. return()
  119. endif()
  120. dpf__add_executable("${NAME}-jack")
  121. dpf__add_plugin_main("${NAME}-jack" "jack")
  122. dpf__add_ui_main("${NAME}-jack" "jack" "${DGL_LIBRARY}")
  123. target_link_libraries("${NAME}-jack" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  124. set_target_properties("${NAME}-jack" PROPERTIES
  125. RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  126. OUTPUT_NAME "${NAME}")
  127. target_include_directories("${NAME}-jack" PRIVATE ${JACK_INCLUDE_DIRS})
  128. target_link_libraries("${NAME}-jack" PRIVATE ${JACK_LIBRARIES})
  129. link_directories(${JACK_LIBRARY_DIRS})
  130. endfunction()
  131. # dpf__build_ladspa
  132. # ------------------------------------------------------------------------------
  133. #
  134. # Add build rules for a DSSI plugin.
  135. #
  136. function(dpf__build_ladspa NAME)
  137. dpf__add_module("${NAME}-ladspa")
  138. dpf__add_plugin_main("${NAME}-ladspa" "ladspa")
  139. target_link_libraries("${NAME}-ladspa" PRIVATE "${NAME}-dsp")
  140. set_target_properties("${NAME}-ladspa" PROPERTIES
  141. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  142. OUTPUT_NAME "${NAME}-ladspa"
  143. PREFIX "")
  144. endfunction()
  145. # dpf__build_dssi
  146. # ------------------------------------------------------------------------------
  147. #
  148. # Add build rules for a DSSI plugin.
  149. #
  150. function(dpf__build_dssi NAME DGL_LIBRARY)
  151. find_package(PkgConfig)
  152. pkg_check_modules(LIBLO "liblo")
  153. if(NOT LIBLO_FOUND)
  154. dpf__warn_once_only(missing_liblo
  155. "liblo is not found, skipping the `dssi` plugin targets")
  156. return()
  157. endif()
  158. dpf__add_module("${NAME}-dssi")
  159. dpf__add_plugin_main("${NAME}-dssi" "dssi")
  160. target_link_libraries("${NAME}-dssi" PRIVATE "${NAME}-dsp")
  161. set_target_properties("${NAME}-dssi" PROPERTIES
  162. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  163. OUTPUT_NAME "${NAME}-dssi"
  164. PREFIX "")
  165. if(DGL_LIBRARY)
  166. dpf__add_executable("${NAME}-dssi-ui")
  167. dpf__add_ui_main("${NAME}-dssi-ui" "dssi" "${DGL_LIBRARY}")
  168. target_link_libraries("${NAME}-dssi-ui" PRIVATE "${NAME}-ui")
  169. set_target_properties("${NAME}-dssi-ui" PROPERTIES
  170. RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}-dssi/$<0:>"
  171. OUTPUT_NAME "${NAME}_ui")
  172. target_include_directories("${NAME}-dssi-ui" PRIVATE ${LIBLO_INCLUDE_DIRS})
  173. target_link_libraries("${NAME}-dssi-ui" PRIVATE ${LIBLO_LIBRARIES})
  174. link_directories(${LIBLO_LIBRARY_DIRS})
  175. endif()
  176. endfunction()
  177. # dpf__build_lv2
  178. # ------------------------------------------------------------------------------
  179. #
  180. # Add build rules for a LV2 plugin.
  181. #
  182. function(dpf__build_lv2 NAME DGL_LIBRARY MONOLITHIC)
  183. dpf__add_module("${NAME}-lv2")
  184. dpf__add_plugin_main("${NAME}-lv2" "lv2")
  185. target_link_libraries("${NAME}-lv2" PRIVATE "${NAME}-dsp")
  186. set_target_properties("${NAME}-lv2" PROPERTIES
  187. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2/$<0:>"
  188. OUTPUT_NAME "${NAME}_dsp"
  189. PREFIX "")
  190. if(DGL_LIBRARY)
  191. if(MONOLITHIC)
  192. dpf__add_ui_main("${NAME}-lv2" "lv2" "${DGL_LIBRARY}")
  193. target_link_libraries("${NAME}-lv2" PRIVATE "${NAME}-ui")
  194. set_target_properties("${NAME}-lv2" PROPERTIES
  195. OUTPUT_NAME "${NAME}")
  196. else()
  197. dpf__add_module("${NAME}-lv2-ui")
  198. dpf__add_ui_main("${NAME}-lv2-ui" "lv2" "${DGL_LIBRARY}")
  199. target_link_libraries("${NAME}-lv2-ui" PRIVATE "${NAME}-ui")
  200. set_target_properties("${NAME}-lv2-ui" PROPERTIES
  201. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2/$<0:>"
  202. OUTPUT_NAME "${NAME}_ui"
  203. PREFIX "")
  204. endif()
  205. endif()
  206. dpf__add_lv2_ttl_generator()
  207. add_dependencies("${NAME}-lv2" lv2_ttl_generator)
  208. add_custom_command(TARGET "${NAME}-lv2" POST_BUILD
  209. COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR}
  210. "$<TARGET_FILE:lv2_ttl_generator>"
  211. "$<TARGET_FILE:${NAME}-lv2>"
  212. WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2")
  213. endfunction()
  214. # dpf__build_vst
  215. # ------------------------------------------------------------------------------
  216. #
  217. # Add build rules for a VST plugin.
  218. #
  219. function(dpf__build_vst NAME DGL_LIBRARY)
  220. dpf__add_module("${NAME}-vst")
  221. dpf__add_plugin_main("${NAME}-vst" "vst")
  222. dpf__add_ui_main("${NAME}-vst" "vst" "${DGL_LIBRARY}")
  223. target_link_libraries("${NAME}-vst" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  224. set_target_properties("${NAME}-vst" PROPERTIES
  225. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  226. OUTPUT_NAME "${NAME}-vst"
  227. PREFIX "")
  228. endfunction()
  229. # dpf__add_dgl_cairo
  230. # ------------------------------------------------------------------------------
  231. #
  232. # Add the Cairo variant of DGL, if not already available.
  233. #
  234. function(dpf__add_dgl_cairo)
  235. if(TARGET dgl-cairo)
  236. return()
  237. endif()
  238. find_package(PkgConfig)
  239. pkg_check_modules(CAIRO "cairo" REQUIRED)
  240. dpf__add_static_library(dgl-cairo EXCLUDE_FROM_ALL
  241. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  242. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  243. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  244. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  245. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  246. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  247. "${DPF_ROOT_DIR}/dgl/src/Cairo.cpp"
  248. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp")
  249. if(NOT APPLE)
  250. target_sources(dgl-cairo PRIVATE
  251. "${DPF_ROOT_DIR}/dgl/src/Window.cpp")
  252. else()
  253. target_sources(dgl-cairo PRIVATE
  254. "${DPF_ROOT_DIR}/dgl/src/Window.mm")
  255. endif()
  256. target_include_directories(dgl-cairo PUBLIC
  257. "${DPF_ROOT_DIR}/dgl")
  258. dpf__add_dgl_system_libs()
  259. target_link_libraries(dgl-cairo PRIVATE dgl-system-libs)
  260. add_library(dgl-cairo-definitions INTERFACE)
  261. target_compile_definitions(dgl-cairo-definitions INTERFACE "DGL_CAIRO" "HAVE_CAIRO")
  262. target_include_directories(dgl-cairo PUBLIC ${CAIRO_INCLUDE_DIRS})
  263. target_link_libraries(dgl-cairo PRIVATE dgl-cairo-definitions ${CAIRO_LIBRARIES})
  264. link_directories(${CAIRO_LIBRARY_DIRS})
  265. endfunction()
  266. # dpf__add_dgl_opengl
  267. # ------------------------------------------------------------------------------
  268. #
  269. # Add the OpenGL variant of DGL, if not already available.
  270. #
  271. function(dpf__add_dgl_opengl)
  272. if(TARGET dgl-opengl)
  273. return()
  274. endif()
  275. if(NOT OpenGL_GL_PREFERENCE)
  276. set(OpenGL_GL_PREFERENCE "LEGACY")
  277. endif()
  278. find_package(OpenGL REQUIRED)
  279. dpf__add_static_library(dgl-opengl EXCLUDE_FROM_ALL
  280. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  281. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  282. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  283. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  284. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  285. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  286. "${DPF_ROOT_DIR}/dgl/src/OpenGL.cpp"
  287. "${DPF_ROOT_DIR}/dgl/src/Image.cpp"
  288. "${DPF_ROOT_DIR}/dgl/src/ImageWidgets.cpp"
  289. "${DPF_ROOT_DIR}/dgl/src/NanoVG.cpp"
  290. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp")
  291. if(NOT APPLE)
  292. target_sources(dgl-opengl PRIVATE
  293. "${DPF_ROOT_DIR}/dgl/src/Window.cpp")
  294. else()
  295. target_sources(dgl-opengl PRIVATE
  296. "${DPF_ROOT_DIR}/dgl/src/Window.mm")
  297. endif()
  298. target_include_directories(dgl-opengl PUBLIC
  299. "${DPF_ROOT_DIR}/dgl")
  300. dpf__add_dgl_system_libs()
  301. target_link_libraries(dgl-opengl PRIVATE dgl-system-libs)
  302. add_library(dgl-opengl-definitions INTERFACE)
  303. target_compile_definitions(dgl-opengl-definitions INTERFACE "DGL_OPENGL" "HAVE_OPENGL")
  304. target_include_directories(dgl-opengl PUBLIC "${OPENGL_INCLUDE_DIR}")
  305. target_link_libraries(dgl-opengl PRIVATE dgl-opengl-definitions "${OPENGL_gl_LIBRARY}")
  306. endfunction()
  307. # dpf__add_dgl_system_libs
  308. # ------------------------------------------------------------------------------
  309. #
  310. # Find system libraries required by DGL and add them as an interface target.
  311. #
  312. function(dpf__add_dgl_system_libs)
  313. if(TARGET dgl-system-libs)
  314. return()
  315. endif()
  316. add_library(dgl-system-libs INTERFACE)
  317. if(HAIKU)
  318. target_link_libraries(dgl-system-libs INTERFACE "be")
  319. elseif(WIN32)
  320. target_link_libraries(dgl-system-libs INTERFACE "gdi32" "comdlg32")
  321. elseif(APPLE)
  322. find_library(APPLE_COCOA_FRAMEWORK "Cocoa")
  323. target_link_libraries(dgl-system-libs INTERFACE "${APPLE_COCOA_FRAMEWORK}")
  324. else()
  325. find_package(X11 REQUIRED)
  326. target_include_directories(dgl-system-libs INTERFACE "${X11_INCLUDE_DIR}")
  327. target_link_libraries(dgl-system-libs INTERFACE "${X11_X11_LIB}")
  328. endif()
  329. endfunction()
  330. # dpf__add_executable
  331. # ------------------------------------------------------------------------------
  332. #
  333. # Adds an executable target, and set some default properties on the target.
  334. #
  335. function(dpf__add_executable NAME)
  336. add_executable("${NAME}" ${ARGN})
  337. set_target_properties("${NAME}" PROPERTIES
  338. POSITION_INDEPENDENT_CODE TRUE
  339. C_VISIBILITY_PRESET "hidden"
  340. CXX_VISIBILITY_PRESET "hidden"
  341. VISIBILITY_INLINES_HIDDEN TRUE)
  342. endfunction()
  343. # dpf__add_module
  344. # ------------------------------------------------------------------------------
  345. #
  346. # Adds a module target, and set some default properties on the target.
  347. #
  348. function(dpf__add_module NAME)
  349. add_library("${NAME}" MODULE ${ARGN})
  350. set_target_properties("${NAME}" PROPERTIES
  351. POSITION_INDEPENDENT_CODE TRUE
  352. C_VISIBILITY_PRESET "hidden"
  353. CXX_VISIBILITY_PRESET "hidden"
  354. VISIBILITY_INLINES_HIDDEN TRUE)
  355. if ((NOT WIN32 AND NOT APPLE) OR MINGW)
  356. target_link_libraries("${NAME}" PRIVATE "-Wl,--no-undefined")
  357. endif()
  358. endfunction()
  359. # dpf__add_static_library
  360. # ------------------------------------------------------------------------------
  361. #
  362. # Adds a module target, and set some default properties on the target.
  363. #
  364. function(dpf__add_static_library NAME)
  365. add_library("${NAME}" STATIC ${ARGN})
  366. set_target_properties("${NAME}" PROPERTIES
  367. POSITION_INDEPENDENT_CODE TRUE
  368. C_VISIBILITY_PRESET "hidden"
  369. CXX_VISIBILITY_PRESET "hidden"
  370. VISIBILITY_INLINES_HIDDEN TRUE)
  371. endfunction()
  372. # dpf__add_plugin_main
  373. # ------------------------------------------------------------------------------
  374. #
  375. # Adds plugin code to the given target.
  376. #
  377. function(dpf__add_plugin_main NAME TARGET)
  378. target_sources("${NAME}" PRIVATE
  379. "${DPF_ROOT_DIR}/distrho/DistrhoPluginMain.cpp")
  380. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  381. endfunction()
  382. # dpf__add_ui_main
  383. # ------------------------------------------------------------------------------
  384. #
  385. # Adds UI code to the given target (only if the target has UI).
  386. #
  387. function(dpf__add_ui_main NAME TARGET HAS_UI)
  388. if(HAS_UI)
  389. target_sources("${NAME}" PRIVATE
  390. "${DPF_ROOT_DIR}/distrho/DistrhoUIMain.cpp")
  391. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  392. endif()
  393. endfunction()
  394. # dpf__add_plugin_target_definition
  395. # ------------------------------------------------------------------------------
  396. #
  397. # Adds the plugins target macro definition.
  398. # This selects which entry file is compiled according to the target type.
  399. #
  400. function(dpf__add_plugin_target_definition NAME TARGET)
  401. string(TOUPPER "${TARGET}" _upperTarget)
  402. target_compile_definitions("${NAME}" PRIVATE "DISTRHO_PLUGIN_TARGET_${_upperTarget}")
  403. endfunction()
  404. # dpf__add_lv2_ttl_generator
  405. # ------------------------------------------------------------------------------
  406. #
  407. # Build the LV2 TTL generator.
  408. #
  409. function(dpf__add_lv2_ttl_generator)
  410. if(TARGET lv2_ttl_generator)
  411. return()
  412. endif()
  413. add_executable(lv2_ttl_generator "${DPF_ROOT_DIR}/utils/lv2-ttl-generator/lv2_ttl_generator.c")
  414. if(NOT WINDOWS AND NOT APPLE AND NOT HAIKU)
  415. target_link_libraries(lv2_ttl_generator "dl")
  416. endif()
  417. endfunction()
  418. # dpf__ensure_sources_non_empty
  419. # ------------------------------------------------------------------------------
  420. #
  421. # Ensure the given source list contains at least one file.
  422. # The function appends an empty source file to the list if necessary.
  423. # This is useful when CMake does not permit to add targets without sources.
  424. #
  425. function(dpf__ensure_sources_non_empty VAR)
  426. if(NOT "" STREQUAL "${${VAR}}")
  427. return()
  428. endif()
  429. set(_file "${CMAKE_CURRENT_BINARY_DIR}/_dpf_empty.cpp")
  430. if(NOT EXISTS "${_file}")
  431. file(WRITE "${_file}" "")
  432. endif()
  433. set("${VAR}" "${_file}" PARENT_SCOPE)
  434. endfunction()
  435. # dpf__warn_once
  436. # ------------------------------------------------------------------------------
  437. #
  438. # Prints a warning message once only.
  439. #
  440. function(dpf__warn_once_only TOKEN MESSAGE)
  441. get_property(_warned GLOBAL PROPERTY "dpf__have_warned_${TOKEN}")
  442. if(NOT _warned)
  443. set_property(GLOBAL PROPERTY "dpf__have_warned_${TOKEN}" TRUE)
  444. message(WARNING "${MESSAGE}")
  445. endif()
  446. endfunction()