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.

518 lines
17KB

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