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.

565 lines
18KB

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