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.

472 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. dpf__add_executable("${NAME}-jack")
  114. dpf__add_plugin_main("${NAME}-jack" "jack")
  115. dpf__add_ui_main("${NAME}-jack" "jack" "${DGL_LIBRARY}")
  116. target_link_libraries("${NAME}-jack" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  117. set_target_properties("${NAME}-jack" PROPERTIES
  118. RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  119. OUTPUT_NAME "${NAME}")
  120. find_package(PkgConfig)
  121. pkg_check_modules(JACK "jack" REQUIRED)
  122. target_include_directories("${NAME}-jack" PRIVATE ${JACK_INCLUDE_DIRS})
  123. target_link_libraries("${NAME}-jack" PRIVATE ${JACK_LIBRARIES})
  124. link_directories(${JACK_LIBRARY_DIRS})
  125. endfunction()
  126. # dpf__build_ladspa
  127. # ------------------------------------------------------------------------------
  128. #
  129. # Add build rules for a DSSI plugin.
  130. #
  131. function(dpf__build_ladspa NAME)
  132. dpf__add_module("${NAME}-ladspa")
  133. dpf__add_plugin_main("${NAME}-ladspa" "ladspa")
  134. target_link_libraries("${NAME}-ladspa" PRIVATE "${NAME}-dsp")
  135. set_target_properties("${NAME}-ladspa" PROPERTIES
  136. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  137. OUTPUT_NAME "${NAME}-ladspa"
  138. PREFIX "")
  139. endfunction()
  140. # dpf__build_dssi
  141. # ------------------------------------------------------------------------------
  142. #
  143. # Add build rules for a DSSI plugin.
  144. #
  145. function(dpf__build_dssi NAME DGL_LIBRARY)
  146. # -- build for DSSI
  147. dpf__add_module("${NAME}-dssi")
  148. dpf__add_plugin_main("${NAME}-dssi" "dssi")
  149. target_link_libraries("${NAME}-dssi" PRIVATE "${NAME}-dsp")
  150. set_target_properties("${NAME}-dssi" PROPERTIES
  151. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  152. OUTPUT_NAME "${NAME}-dssi"
  153. PREFIX "")
  154. if(DGL_LIBRARY)
  155. dpf__add_executable("${NAME}-dssi-ui")
  156. dpf__add_ui_main("${NAME}-dssi-ui" "dssi" "${DGL_LIBRARY}")
  157. target_link_libraries("${NAME}-dssi-ui" PRIVATE "${NAME}-ui")
  158. set_target_properties("${NAME}-dssi-ui" PROPERTIES
  159. RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}-dssi/$<0:>"
  160. OUTPUT_NAME "${NAME}_ui")
  161. find_package(PkgConfig)
  162. pkg_check_modules(LIBLO "liblo" REQUIRED)
  163. target_include_directories("${NAME}-dssi-ui" PRIVATE ${LIBLO_INCLUDE_DIRS})
  164. target_link_libraries("${NAME}-dssi-ui" PRIVATE ${LIBLO_LIBRARIES})
  165. link_directories(${LIBLO_LIBRARY_DIRS})
  166. endif()
  167. endfunction()
  168. # dpf__build_lv2
  169. # ------------------------------------------------------------------------------
  170. #
  171. # Add build rules for a LV2 plugin.
  172. #
  173. function(dpf__build_lv2 NAME DGL_LIBRARY MONOLITHIC)
  174. dpf__add_module("${NAME}-lv2")
  175. dpf__add_plugin_main("${NAME}-lv2" "lv2")
  176. target_link_libraries("${NAME}-lv2" PRIVATE "${NAME}-dsp")
  177. set_target_properties("${NAME}-lv2" PROPERTIES
  178. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2/$<0:>"
  179. OUTPUT_NAME "${NAME}_dsp"
  180. PREFIX "")
  181. if(DGL_LIBRARY)
  182. if(MONOLITHIC)
  183. dpf__add_ui_main("${NAME}-lv2" "lv2" "${DGL_LIBRARY}")
  184. target_link_libraries("${NAME}-lv2" PRIVATE "${NAME}-ui")
  185. set_target_properties("${NAME}-lv2" PROPERTIES
  186. OUTPUT_NAME "${NAME}")
  187. else()
  188. dpf__add_module("${NAME}-lv2-ui")
  189. dpf__add_ui_main("${NAME}-lv2-ui" "lv2" "${DGL_LIBRARY}")
  190. target_link_libraries("${NAME}-lv2-ui" PRIVATE "${NAME}-ui")
  191. set_target_properties("${NAME}-lv2-ui" PROPERTIES
  192. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2/$<0:>"
  193. OUTPUT_NAME "${NAME}_ui"
  194. PREFIX "")
  195. endif()
  196. endif()
  197. dpf__add_lv2_ttl_generator()
  198. add_dependencies("${NAME}-lv2" lv2_ttl_generator)
  199. add_custom_command(TARGET "${NAME}-lv2" POST_BUILD
  200. COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR}
  201. "$<TARGET_FILE:lv2_ttl_generator>"
  202. "$<TARGET_FILE:${NAME}-lv2>"
  203. WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/bin/${NAME}.lv2")
  204. endfunction()
  205. # dpf__build_vst
  206. # ------------------------------------------------------------------------------
  207. #
  208. # Add build rules for a VST plugin.
  209. #
  210. function(dpf__build_vst NAME DGL_LIBRARY)
  211. dpf__add_module("${NAME}-vst")
  212. dpf__add_plugin_main("${NAME}-vst" "vst")
  213. dpf__add_ui_main("${NAME}-vst" "vst" "${DGL_LIBRARY}")
  214. target_link_libraries("${NAME}-vst" PRIVATE "${NAME}-dsp" "${NAME}-ui")
  215. set_target_properties("${NAME}-vst" PROPERTIES
  216. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/$<0:>"
  217. OUTPUT_NAME "${NAME}-vst"
  218. PREFIX "")
  219. endfunction()
  220. # dpf__add_dgl_cairo
  221. # ------------------------------------------------------------------------------
  222. #
  223. # Add the Cairo variant of DGL, if not already available.
  224. #
  225. function(dpf__add_dgl_cairo)
  226. if(TARGET dgl-cairo)
  227. return()
  228. endif()
  229. find_package(PkgConfig)
  230. pkg_check_modules(CAIRO "cairo" REQUIRED)
  231. dpf__add_static_library(dgl-cairo EXCLUDE_FROM_ALL
  232. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  233. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  234. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  235. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  236. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  237. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  238. "${DPF_ROOT_DIR}/dgl/src/Cairo.cpp"
  239. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp")
  240. if(NOT APPLE)
  241. target_sources(dgl-cairo PRIVATE
  242. "${DPF_ROOT_DIR}/dgl/src/Window.cpp")
  243. else()
  244. target_sources(dgl-cairo PRIVATE
  245. "${DPF_ROOT_DIR}/dgl/src/Window.mm")
  246. endif()
  247. target_include_directories(dgl-cairo PUBLIC
  248. "${DPF_ROOT_DIR}/dgl")
  249. dpf__add_dgl_system_libs()
  250. target_link_libraries(dgl-cairo PRIVATE dgl-system-libs)
  251. add_library(dgl-cairo-definitions INTERFACE)
  252. target_compile_definitions(dgl-cairo-definitions INTERFACE "DGL_CAIRO" "HAVE_CAIRO")
  253. target_include_directories(dgl-cairo PUBLIC ${CAIRO_INCLUDE_DIRS})
  254. target_link_libraries(dgl-cairo PRIVATE dgl-cairo-definitions ${CAIRO_LIBRARIES})
  255. link_directories(${CAIRO_LIBRARY_DIRS})
  256. endfunction()
  257. # dpf__add_dgl_opengl
  258. # ------------------------------------------------------------------------------
  259. #
  260. # Add the OpenGL variant of DGL, if not already available.
  261. #
  262. function(dpf__add_dgl_opengl)
  263. if(TARGET dgl-opengl)
  264. return()
  265. endif()
  266. if(NOT OpenGL_GL_PREFERENCE)
  267. set(OpenGL_GL_PREFERENCE "LEGACY")
  268. endif()
  269. find_package(OpenGL REQUIRED)
  270. dpf__add_static_library(dgl-opengl EXCLUDE_FROM_ALL
  271. "${DPF_ROOT_DIR}/dgl/src/Application.cpp"
  272. "${DPF_ROOT_DIR}/dgl/src/Color.cpp"
  273. "${DPF_ROOT_DIR}/dgl/src/Geometry.cpp"
  274. "${DPF_ROOT_DIR}/dgl/src/ImageBase.cpp"
  275. "${DPF_ROOT_DIR}/dgl/src/Resources.cpp"
  276. "${DPF_ROOT_DIR}/dgl/src/Widget.cpp"
  277. "${DPF_ROOT_DIR}/dgl/src/OpenGL.cpp"
  278. "${DPF_ROOT_DIR}/dgl/src/Image.cpp"
  279. "${DPF_ROOT_DIR}/dgl/src/ImageWidgets.cpp"
  280. "${DPF_ROOT_DIR}/dgl/src/NanoVG.cpp"
  281. "${DPF_ROOT_DIR}/dgl/src/WidgetPrivateData.cpp")
  282. if(NOT APPLE)
  283. target_sources(dgl-opengl PRIVATE
  284. "${DPF_ROOT_DIR}/dgl/src/Window.cpp")
  285. else()
  286. target_sources(dgl-opengl PRIVATE
  287. "${DPF_ROOT_DIR}/dgl/src/Window.mm")
  288. endif()
  289. target_include_directories(dgl-opengl PUBLIC
  290. "${DPF_ROOT_DIR}/dgl")
  291. dpf__add_dgl_system_libs()
  292. target_link_libraries(dgl-opengl PRIVATE dgl-system-libs)
  293. add_library(dgl-opengl-definitions INTERFACE)
  294. target_compile_definitions(dgl-opengl-definitions INTERFACE "DGL_OPENGL" "HAVE_OPENGL")
  295. target_include_directories(dgl-opengl PUBLIC "${OPENGL_INCLUDE_DIR}")
  296. target_link_libraries(dgl-opengl PRIVATE dgl-opengl-definitions "${OPENGL_gl_LIBRARY}")
  297. endfunction()
  298. # dpf__add_dgl_system_libs
  299. # ------------------------------------------------------------------------------
  300. #
  301. # Find system libraries required by DGL and add them as an interface target.
  302. #
  303. function(dpf__add_dgl_system_libs)
  304. if(TARGET dgl-system-libs)
  305. return()
  306. endif()
  307. add_library(dgl-system-libs INTERFACE)
  308. if(HAIKU)
  309. target_link_libraries(dgl-system-libs INTERFACE "be")
  310. elseif(WIN32)
  311. target_link_libraries(dgl-system-libs INTERFACE "gdi32" "comdlg32")
  312. elseif(APPLE)
  313. find_library(APPLE_COCOA_FRAMEWORK "Cocoa")
  314. target_link_libraries(dgl-system-libs INTERFACE "${APPLE_COCOA_FRAMEWORK}")
  315. else()
  316. find_package(X11 REQUIRED)
  317. target_include_directories(dgl-system-libs INTERFACE "${X11_INCLUDE_DIR}")
  318. target_link_libraries(dgl-system-libs INTERFACE "${X11_X11_LIB}")
  319. endif()
  320. endfunction()
  321. # dpf__add_executable
  322. # ------------------------------------------------------------------------------
  323. #
  324. # Adds an executable target, and set some default properties on the target.
  325. #
  326. function(dpf__add_executable NAME)
  327. add_executable("${NAME}" ${ARGN})
  328. set_target_properties("${NAME}" PROPERTIES
  329. POSITION_INDEPENDENT_CODE TRUE
  330. C_VISIBILITY_PRESET "hidden"
  331. CXX_VISIBILITY_PRESET "hidden"
  332. VISIBILITY_INLINES_HIDDEN TRUE)
  333. endfunction()
  334. # dpf__add_module
  335. # ------------------------------------------------------------------------------
  336. #
  337. # Adds a module target, and set some default properties on the target.
  338. #
  339. function(dpf__add_module NAME)
  340. add_library("${NAME}" MODULE ${ARGN})
  341. set_target_properties("${NAME}" PROPERTIES
  342. POSITION_INDEPENDENT_CODE TRUE
  343. C_VISIBILITY_PRESET "hidden"
  344. CXX_VISIBILITY_PRESET "hidden"
  345. VISIBILITY_INLINES_HIDDEN TRUE)
  346. if ((NOT WIN32 AND NOT APPLE) OR MINGW)
  347. target_link_libraries("${NAME}" PRIVATE "-Wl,--no-undefined")
  348. endif()
  349. endfunction()
  350. # dpf__add_static_library
  351. # ------------------------------------------------------------------------------
  352. #
  353. # Adds a module target, and set some default properties on the target.
  354. #
  355. function(dpf__add_static_library NAME)
  356. add_library("${NAME}" STATIC ${ARGN})
  357. set_target_properties("${NAME}" PROPERTIES
  358. POSITION_INDEPENDENT_CODE TRUE
  359. C_VISIBILITY_PRESET "hidden"
  360. CXX_VISIBILITY_PRESET "hidden"
  361. VISIBILITY_INLINES_HIDDEN TRUE)
  362. endfunction()
  363. # dpf__add_plugin_main
  364. # ------------------------------------------------------------------------------
  365. #
  366. # Adds plugin code to the given target.
  367. #
  368. function(dpf__add_plugin_main NAME TARGET)
  369. target_sources("${NAME}" PRIVATE
  370. "${DPF_ROOT_DIR}/distrho/DistrhoPluginMain.cpp")
  371. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  372. endfunction()
  373. # dpf__add_ui_main
  374. # ------------------------------------------------------------------------------
  375. #
  376. # Adds UI code to the given target (only if the target has UI).
  377. #
  378. function(dpf__add_ui_main NAME TARGET HAS_UI)
  379. if(HAS_UI)
  380. target_sources("${NAME}" PRIVATE
  381. "${DPF_ROOT_DIR}/distrho/DistrhoUIMain.cpp")
  382. dpf__add_plugin_target_definition("${NAME}" "${TARGET}")
  383. endif()
  384. endfunction()
  385. # dpf__add_plugin_target_definition
  386. # ------------------------------------------------------------------------------
  387. #
  388. # Adds the plugins target macro definition.
  389. # This selects which entry file is compiled according to the target type.
  390. #
  391. function(dpf__add_plugin_target_definition NAME TARGET)
  392. string(TOUPPER "${TARGET}" _upperTarget)
  393. target_compile_definitions("${NAME}" PRIVATE "DISTRHO_PLUGIN_TARGET_${_upperTarget}")
  394. endfunction()
  395. # dpf__add_lv2_ttl_generator
  396. # ------------------------------------------------------------------------------
  397. #
  398. # Build the LV2 TTL generator.
  399. #
  400. function(dpf__add_lv2_ttl_generator)
  401. if(TARGET lv2_ttl_generator)
  402. return()
  403. endif()
  404. add_executable(lv2_ttl_generator "${DPF_ROOT_DIR}/utils/lv2-ttl-generator/lv2_ttl_generator.c")
  405. if(NOT WINDOWS AND NOT APPLE AND NOT HAIKU)
  406. target_link_libraries(lv2_ttl_generator "dl")
  407. endif()
  408. endfunction()
  409. # dpf__ensure_sources_non_empty
  410. # ------------------------------------------------------------------------------
  411. #
  412. # Ensure the given source list contains at least one file.
  413. # The function appends an empty source file to the list if necessary.
  414. # This is useful when CMake does not permit to add targets without sources.
  415. #
  416. function(dpf__ensure_sources_non_empty VAR)
  417. if(NOT "" STREQUAL "${${VAR}}")
  418. return()
  419. endif()
  420. set(_file "${CMAKE_CURRENT_BINARY_DIR}/_dpf_empty.cpp")
  421. if(NOT EXISTS "${_file}")
  422. file(WRITE "${_file}" "")
  423. endif()
  424. set("${VAR}" "${_file}" PARENT_SCOPE)
  425. endfunction()