cmake_minimum_required(VERSION 3.22)
project(SRSpinfall VERSION 0.5.0 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(MSVC)
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
option(SPINFALL_BUILD_PLUGIN "Build JUCE plugin targets" ON)
include(CTest)
if(BUILD_TESTING)
    find_package(Threads REQUIRED)
    add_executable(spinfall_visualizer_tests Tests/VisualizerTests.cpp)
    target_include_directories(spinfall_visualizer_tests PRIVATE Source)
    target_link_libraries(spinfall_visualizer_tests PRIVATE Threads::Threads)
    add_test(NAME spinfall_visualizer COMMAND spinfall_visualizer_tests)
    add_executable(spinfall_tests Tests/EngineTests.cpp)
    target_include_directories(spinfall_tests PRIVATE Source)
    add_test(NAME spinfall_dsp COMMAND spinfall_tests)
    add_executable(spinfall_modern_tests Tests/ModernTests.cpp)
    target_include_directories(spinfall_modern_tests PRIVATE Source)
    add_test(NAME spinfall_modern COMMAND spinfall_modern_tests)
    add_executable(spinfall_loop_tests Tests/LoopTests.cpp)
    target_include_directories(spinfall_loop_tests PRIVATE Source)
    add_test(NAME spinfall_loop COMMAND spinfall_loop_tests)
    add_executable(spinfall_preview_data Tests/ExportPreview.cpp)
    target_include_directories(spinfall_preview_data PRIVATE Source)
    add_executable(spinfall_comparison Tests/RenderComparison.cpp)
    target_include_directories(spinfall_comparison PRIVATE Source)
    add_executable(spinfall_benchmark Tests/Benchmark.cpp)
    target_include_directories(spinfall_benchmark PRIVATE Source)
    add_executable(spinfall_render Tests/RenderDemo.cpp)
    target_include_directories(spinfall_render PRIVATE Source)
endif()
if(SPINFALL_BUILD_PLUGIN)
    if(NOT JUCE_PATH)
        message(FATAL_ERROR "Set JUCE_PATH to a JUCE 8 checkout; dependencies are not downloaded implicitly.")
    endif()
    add_subdirectory("${JUCE_PATH}" JUCE)
    set(formats VST3 Standalone)
    if(APPLE)
        list(APPEND formats AU)
    endif()
    juce_add_plugin(SRSpinfall
        COMPANY_NAME "System Revolution" BUNDLE_ID "music.systemrevolution.spinfall"
        PLUGIN_MANUFACTURER_CODE SyRv PLUGIN_CODE SpFl
        PRODUCT_NAME "SR Spinfall" FORMATS ${formats}
        ICON_BIG "Assets/Spinfall.png" ICON_SMALL "Assets/Spinfall.png"
        IS_SYNTH FALSE NEEDS_MIDI_INPUT FALSE NEEDS_MIDI_OUTPUT FALSE
        IS_MIDI_EFFECT FALSE EDITOR_WANTS_KEYBOARD_FOCUS FALSE COPY_PLUGIN_AFTER_BUILD FALSE)
    juce_add_binary_data(SpinfallAssets SOURCES Assets/DejaVuSans.ttf)
    target_sources(SRSpinfall PRIVATE Source/PluginProcessor.cpp Source/PluginEditor.cpp)
    target_compile_definitions(SRSpinfall PUBLIC JUCE_WEB_BROWSER=0 JUCE_USE_CURL=0 JUCE_VST3_CAN_REPLACE_VST2=0)
    if(BUILD_TESTING)
        # Reuse the plugin's compiled JUCE modules instead of building them twice.
        add_executable(spinfall_processor_tests Tests/ProcessorTests.cpp)
        target_include_directories(spinfall_processor_tests PRIVATE Source "$<TARGET_PROPERTY:SRSpinfall,INCLUDE_DIRECTORIES>")
        target_compile_definitions(spinfall_processor_tests PRIVATE "$<TARGET_PROPERTY:SRSpinfall,COMPILE_DEFINITIONS>")
        target_link_libraries(spinfall_processor_tests PRIVATE SRSpinfall SpinfallAssets)
        add_test(NAME spinfall_processor COMMAND spinfall_processor_tests)
    endif()
    target_link_libraries(SRSpinfall PRIVATE juce::juce_audio_utils SpinfallAssets PUBLIC
        juce::juce_recommended_config_flags juce::juce_recommended_warning_flags)
endif()
