LynxPlugins.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # LYNX's own plugins as VST3 / CLAP (round 180b, docs/PLUGIN_EXPORT.md; LV2 on Linux behind LYNX_PLUGINS_LV2, round 190ov). Included from CMakeLists.txt only with
  2. # -DLYNX_BUILD_PLUGINS=ON. One static library holds JUCE and the app's sources (compiled once); each VST3 adds JUCE's wrapper
  3. # for its catalog entry, and one CLAP module holds every entry.
  4. add_library(lynx_plugin_runtime STATIC src/plugin/ExportProcessor.cpp ${LYNX_APP_SOURCES})
  5. set(LYNX_PLUGIN_DEFINES
  6. JUCE_WEB_BROWSER=0 JUCE_USE_CURL=0 JUCE_PLUGINHOST_VST3=1 JUCE_USE_MP3AUDIOFORMAT=0
  7. JUCE_STANDALONE_APPLICATION=0 JUCE_GLOBAL_MODULE_SETTINGS_INCLUDED=1
  8. JUCE_APPLICATION_NAME_STRING="LYNX" JUCE_APPLICATION_VERSION_STRING="${PROJECT_VERSION}"
  9. LYNX_EXPORT_VERSION="${PROJECT_VERSION}")
  10. foreach(m juce_core juce_data_structures juce_events juce_graphics juce_gui_basics juce_gui_extra juce_audio_basics
  11. juce_audio_devices juce_audio_formats juce_audio_processors juce_audio_utils juce_dsp juce_opengl)
  12. list(APPEND LYNX_PLUGIN_DEFINES JUCE_MODULE_AVAILABLE_${m}=1)
  13. endforeach()
  14. if(WIN32)
  15. list(APPEND LYNX_PLUGIN_DEFINES JUCE_ASIO=1)
  16. target_include_directories(lynx_plugin_runtime PRIVATE third_party/asiosdk/common)
  17. target_link_libraries(lynx_plugin_runtime PRIVATE winhttp ws2_32 iphlpapi) # (iphlpapi, ws2_32: Ableton Link, round 190gl)
  18. target_link_libraries(lynx_plugin_runtime PRIVATE mfplat mfreadwrite mfuuid) # (Media Foundation: video decoding, M4A export)
  19. elseif(APPLE)
  20. list(APPEND LYNX_PLUGIN_DEFINES JUCE_PLUGINHOST_AU=1)
  21. # the system's video decoder, as in the app (ui/VideoMediaMac.mm; manual retain / release)
  22. target_sources(lynx_plugin_runtime PRIVATE src/ui/VideoMediaMac.mm)
  23. set_source_files_properties(src/ui/VideoMediaMac.mm PROPERTIES COMPILE_OPTIONS "-fno-objc-arc")
  24. target_link_libraries(lynx_plugin_runtime PRIVATE "-framework AVFoundation" "-framework CoreMedia" "-framework CoreVideo"
  25. "-framework AudioToolbox" "-framework CoreFoundation" ${CMAKE_DL_LIBS}) # (AudioToolbox: M4A export)
  26. # the moving marks' backend, as in the app: ui/MotionOverlay.cpp (in LYNX_APP_SOURCES) calls it (round 190nk)
  27. target_sources(lynx_plugin_runtime PRIVATE src/ui/MotionOverlayMac.mm)
  28. set_source_files_properties(src/ui/MotionOverlayMac.mm PROPERTIES COMPILE_OPTIONS "-fno-objc-arc")
  29. target_link_libraries(lynx_plugin_runtime PRIVATE "-framework QuartzCore")
  30. else()
  31. list(APPEND LYNX_PLUGIN_DEFINES JUCE_ALSA=1 JUCE_JACK=1 JUCE_PLUGINHOST_LV2=1)
  32. target_include_directories(lynx_plugin_runtime SYSTEM PRIVATE third_party/pipewire/include/pipewire-0.3 third_party/pipewire/include/spa-0.2)
  33. target_link_libraries(lynx_plugin_runtime PRIVATE ${CMAKE_DL_LIBS})
  34. # the moving marks' X11 backend, as in the app: ui/MotionOverlay.cpp (in LYNX_APP_SOURCES) calls it (round 190nk; the
  35. # 0.1.30 release run's Linux plugin link failed without it)
  36. target_sources(lynx_plugin_runtime PRIVATE src/ui/MotionOverlayX11.cpp)
  37. endif()
  38. target_compile_definitions(lynx_plugin_runtime PUBLIC ${LYNX_PLUGIN_DEFINES})
  39. target_include_directories(lynx_plugin_runtime
  40. PUBLIC src third_party/clap/include "${juce_SOURCE_DIR}/modules" "${juce_SOURCE_DIR}/modules/juce_audio_processors/format_types/VST3_SDK"
  41. PRIVATE third_party/onnxruntime ${CMAKE_CURRENT_BINARY_DIR}/lynx_build)
  42. target_link_libraries(lynx_plugin_runtime
  43. PUBLIC lynx_core
  44. PRIVATE juce::juce_audio_utils juce::juce_audio_processors juce::juce_dsp juce::juce_opengl)
  45. set_target_properties(lynx_plugin_runtime PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON)
  46. # the host's symbols are never taken for ours, and only the entry points are visible
  47. function(lynx_finish_plugin target)
  48. set_target_properties(${target} PROPERTIES PREFIX "" CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON)
  49. target_link_libraries(${target} PRIVATE lynx_plugin_runtime)
  50. if(TARGET ${target}_entry)
  51. # (round 190sh: the entry object, compiled exactly as when it was the module's own source: the module's definition
  52. # <target>_EXPORTS, its visibility, and the runtime's include folders and definitions after the caller's own)
  53. set_target_properties(${target}_entry PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON)
  54. target_compile_definitions(${target}_entry PRIVATE ${target}_EXPORTS)
  55. target_link_libraries(${target}_entry PRIVATE lynx_plugin_runtime)
  56. endif()
  57. if(WIN32)
  58. target_sources(${target} PRIVATE resources/LynxPlugin.rc)
  59. endif()
  60. if(APPLE)
  61. target_link_options(${target} PRIVATE -Wl,-dead_strip)
  62. endif()
  63. if(UNIX AND NOT APPLE)
  64. target_link_options(${target} PRIVATE -static-libstdc++ -static-libgcc -Wl,--gc-sections -Wl,--exclude-libs,ALL -Wl,-z,defs)
  65. target_compile_options(${target} PRIVATE -ffunction-sections -fdata-sections)
  66. if(TARGET ${target}_entry)
  67. target_compile_options(${target}_entry PRIVATE -ffunction-sections -fdata-sections)
  68. endif()
  69. endif()
  70. endfunction()
  71. # Round 190sh: the binary's own entry object (<entry source>, compiled as before into an OBJECT library <target>_entry) is linked
  72. # LAST, after the engine's static libraries, and the module's first object is plugin/LinkAnchor.cpp, the same in every binary:
  73. # so the shared engine is laid out the same way in the CLAP, each VST3 and each LV2, and the release archives' solid xz stream
  74. # finds more of each file in the ones before it. The libraries come again after the entry object for what only it needs.
  75. # (GCC / Clang with GNU-style linkers only; with MSVC or clang-cl the entry stays the module's own source.) Returns the OBJECT library's name in <out>.
  76. set(LYNX_PLUGIN_STATIC_LIBS lynx_plugin_runtime lynx_core pocketpy lynx_ysfx monocypher libxmp_lite lame_encoder wavpack_decoder
  77. vorbis_decoder)
  78. function(lynx_plugin_module target entry_source out)
  79. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" AND NOT MSVC)
  80. add_library(${target} MODULE src/plugin/LinkAnchor.cpp)
  81. add_library(${target}_entry OBJECT ${entry_source}) # (lynx_finish_plugin gives it the module's settings)
  82. target_link_libraries(${target} PRIVATE ${LYNX_PLUGIN_STATIC_LIBS} "-Wl,$<TARGET_OBJECTS:${target}_entry>"
  83. ${LYNX_PLUGIN_STATIC_LIBS})
  84. add_dependencies(${target} ${target}_entry)
  85. set_property(TARGET ${target} APPEND PROPERTY LINK_DEPENDS "$<TARGET_OBJECTS:${target}_entry>")
  86. set(${out} ${target}_entry PARENT_SCOPE)
  87. else()
  88. add_library(${target} MODULE ${entry_source})
  89. set(${out} ${target} PARENT_SCOPE)
  90. endif()
  91. endfunction()
  92. if(WIN32)
  93. set(LYNX_VST3_ARCH x86_64-win)
  94. elseif(APPLE)
  95. set(LYNX_VST3_ARCH MacOS)
  96. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|ARM64")
  97. set(LYNX_VST3_ARCH aarch64-linux)
  98. else()
  99. set(LYNX_VST3_ARCH x86_64-linux)
  100. endif()
  101. set(LYNX_PLUGIN_OUT "${CMAKE_BINARY_DIR}/plugins")
  102. # round 190ov: the VST3 short list also as LV2 bundles on Linux (JUCE's LV2 wrapper, plugin/Lv2Entry.cpp). On since round
  103. # 190pk (0.1.31), which fixed what made JUCE's LV2 host fail on them (cmake/LynxPatchJuce.cmake, fix 50)
  104. option(LYNX_PLUGINS_LV2 "Linux: build the VST3 short list as LV2 bundles too" ON)
  105. # every built entry of the one table, src/plugin/plugins.def; the index counts the built entries (plugin/ExportCatalog.h).
  106. # built = 2: a VST3 of its own as well (built = 1: in the CLAP only)
  107. file(STRINGS src/plugin/plugins.def LYNX_PLUGIN_LINES REGEX "^LYNX_PLUGIN .*, [12][)]$")
  108. # (round 190pq: an edit of the table re-runs CMake, so a newly built entry gets its targets in an existing build folder)
  109. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/plugin/plugins.def")
  110. set(index 0)
  111. set(LYNX_PLUGIN_TARGETS lynx_clap)
  112. foreach(line IN LISTS LYNX_PLUGIN_LINES)
  113. # (round 190ov: the LV2 URI comes from the clap id, read first: a REGEX REPLACE resets CMAKE_MATCH_<n>)
  114. string(REGEX REPLACE "^LYNX_PLUGIN [(]\"[^\"]+\", [01], [^,]+, [^,]+, \"com[.]ka1-s[.]lynx[.]([^\"]+)\", .*$" "\\1" lv2_id "${line}")
  115. if(NOT line MATCHES "^LYNX_PLUGIN [(]\"([^\"]+)\", ([01]), [^,]+, [^,]+, \"[^\"]+\", (0x[0-9a-f]+), \"([^\"]+)\", ([12])[)]$")
  116. message(FATAL_ERROR "src/plugin/plugins.def: can't read: ${line}")
  117. endif()
  118. if(CMAKE_MATCH_5 STREQUAL "2")
  119. set(name "${CMAKE_MATCH_1}")
  120. set(target "lynx_vst3_${index}")
  121. lynx_plugin_module(${target} src/plugin/Vst3Entry.cpp entry)
  122. target_compile_definitions(${entry} PRIVATE LYNX_EXPORT_INDEX=${index} JucePlugin_Name="${name}" JucePlugin_IsSynth=${CMAKE_MATCH_2}
  123. JucePlugin_PluginCode=${CMAKE_MATCH_3} JucePlugin_Vst3Category="${CMAKE_MATCH_4}")
  124. lynx_finish_plugin(${target})
  125. if(APPLE)
  126. # a bundle: <name>.vst3/Contents/{Info.plist, MacOS/<name>}
  127. set_source_files_properties(src/plugin/Vst3Entry.cpp PROPERTIES COMPILE_OPTIONS "-xobjective-c++")
  128. string(REGEX REPLACE "[^A-Za-z0-9]" "-" bundle_id "${name}")
  129. set_target_properties(${target} PROPERTIES BUNDLE TRUE BUNDLE_EXTENSION vst3 OUTPUT_NAME "${name}"
  130. LIBRARY_OUTPUT_DIRECTORY "${LYNX_PLUGIN_OUT}"
  131. MACOSX_BUNDLE_GUI_IDENTIFIER "com.ka1-s.lynx.vst3.${bundle_id}"
  132. MACOSX_BUNDLE_BUNDLE_NAME "${name}"
  133. MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION}"
  134. MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION}"
  135. MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/resources/plugin/Info.plist.in")
  136. else()
  137. set_target_properties(${target} PROPERTIES
  138. OUTPUT_NAME "${name}"
  139. LIBRARY_OUTPUT_DIRECTORY "${LYNX_PLUGIN_OUT}/${name}.vst3/Contents/${LYNX_VST3_ARCH}")
  140. endif()
  141. if(WIN32)
  142. set_target_properties(${target} PROPERTIES SUFFIX ".vst3")
  143. endif()
  144. list(APPEND LYNX_PLUGIN_TARGETS ${target})
  145. # round 190ov: on Linux the same short list as LV2 bundles (JUCE's LV2 wrapper, plugin/Lv2Entry.cpp):
  146. # <name with dashes>.lv2/{<name>.so, manifest.ttl, dsp.ttl, ui.ttl}, the .ttl files written by JUCE's juce_lv2_helper
  147. if(UNIX AND NOT APPLE AND LYNX_PLUGINS_LV2)
  148. set(lv2_defs LYNX_EXPORT_INDEX=${index} JucePlugin_Name="${name}" JucePlugin_IsSynth=${CMAKE_MATCH_2}
  149. JucePlugin_PluginCode=${CMAKE_MATCH_3} JucePlugin_Vst3Category="${CMAKE_MATCH_4}"
  150. JucePlugin_LV2URI="https://lynx-share.com/plugins/lv2/${lv2_id}")
  151. # (round 190pk: an effect's LV2 class from its VST3 category, so LV2 hosts file it; JUCE wrote lv2:Plugin)
  152. set(lv2_class "")
  153. set(lv2_category "${CMAKE_MATCH_4}") # (copied: the MATCHES below resets CMAKE_MATCH_<n>)
  154. if(CMAKE_MATCH_2 STREQUAL "0")
  155. foreach(pair "EQ=EQPlugin" "Dynamics=DynamicsPlugin" "Reverb=ReverbPlugin" "Delay=DelayPlugin" "Pitch=PitchPlugin")
  156. string(REPLACE "=" ";" pair "${pair}")
  157. list(GET pair 0 word)
  158. list(GET pair 1 cls)
  159. if(lv2_class STREQUAL "" AND lv2_category MATCHES "(^|[|])${word}")
  160. set(lv2_class "lv2:${cls}")
  161. endif()
  162. endforeach()
  163. endif()
  164. if(NOT lv2_class STREQUAL "")
  165. list(APPEND lv2_defs JucePlugin_LV2Class="${lv2_class}")
  166. endif()
  167. string(REGEX REPLACE "[^A-Za-z0-9]" "-" lv2_name "${name}")
  168. set(lv2_target "lynx_lv2_${index}")
  169. lynx_plugin_module(${lv2_target} src/plugin/Lv2Entry.cpp entry)
  170. target_compile_definitions(${entry} PRIVATE ${lv2_defs})
  171. target_include_directories(${entry} PRIVATE src/plugin/lv2)
  172. target_link_libraries(${entry} PRIVATE juce_lilv_headers)
  173. lynx_finish_plugin(${lv2_target})
  174. set_target_properties(${lv2_target} PROPERTIES OUTPUT_NAME "${lv2_name}" LIBRARY_OUTPUT_DIRECTORY "${LYNX_PLUGIN_OUT}/${lv2_name}.lv2")
  175. if(NOT TARGET lynx_lv2_helper)
  176. add_executable(lynx_lv2_helper "${juce_SOURCE_DIR}/modules/juce_audio_plugin_client/LV2/juce_LV2ManifestHelper.cpp")
  177. target_compile_features(lynx_lv2_helper PRIVATE cxx_std_17)
  178. target_link_libraries(lynx_lv2_helper PRIVATE ${CMAKE_DL_LIBS})
  179. endif()
  180. add_custom_command(TARGET ${lv2_target} POST_BUILD COMMAND lynx_lv2_helper "$<TARGET_FILE:${lv2_target}>" VERBATIM)
  181. list(APPEND LYNX_PLUGIN_TARGETS ${lv2_target})
  182. endif()
  183. endif()
  184. math(EXPR index "${index} + 1")
  185. endforeach()
  186. lynx_plugin_module(lynx_clap src/plugin/ClapEntry.cpp entry)
  187. lynx_finish_plugin(lynx_clap)
  188. if(APPLE)
  189. # a bundle: LYNX Plugins.clap/Contents/{Info.plist, MacOS/LYNX Plugins}
  190. set_target_properties(lynx_clap PROPERTIES BUNDLE TRUE BUNDLE_EXTENSION clap OUTPUT_NAME "LYNX Plugins"
  191. LIBRARY_OUTPUT_DIRECTORY "${LYNX_PLUGIN_OUT}"
  192. MACOSX_BUNDLE_GUI_IDENTIFIER "com.ka1-s.lynx.clap"
  193. MACOSX_BUNDLE_BUNDLE_NAME "LYNX Plugins"
  194. MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION}"
  195. MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION}"
  196. MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/resources/plugin/Info.plist.in")
  197. else()
  198. set_target_properties(lynx_clap PROPERTIES OUTPUT_NAME "LYNX Plugins" SUFFIX ".clap" LIBRARY_OUTPUT_DIRECTORY "${LYNX_PLUGIN_OUT}")
  199. endif()
  200. # the check: loads the built plugins as a host does (docs/PLUGIN_EXPORT.md)
  201. add_executable(plugexporttest tools/plugexporttest.cpp)
  202. target_link_libraries(plugexporttest PRIVATE lynx_plugin_runtime)
  203. # every plugin of the table: cmake --build <dir> --target lynx_plugins (scripts/package-plugins.sh packs them)
  204. add_custom_target(lynx_plugins DEPENDS ${LYNX_PLUGIN_TARGETS} plugexporttest)