# ReaSampler 9000 — the second build artifact: a Windows-only VST3 instrument the user
# instantiates on an instrument track. Additive; the extension builds unchanged.
#
# The nested vst3sdk slice (pluginterfaces / base / public.sdk) is a one-time submodule
# step documented in CLAUDE.md. When it is absent — a fresh clone that ran only the
# top-level init — skip this module rather than fail configure on missing sources: the pure
# libraries and their CTest targets still build and test without the SDK. Probe one
# representative source file.
if(WIN32 AND EXISTS "${VST3_SDK}/public.sdk/source/main/pluginfactory.cpp")

    # The bounded slice of the Steinberg VST3 SDK this instrument needs. Enumerated rather
    # than add_subdirectory of the whole SDK to keep the build hermetic and lean: no VSTGUI,
    # no examples, no SDK-global CMake helpers or install machinery. The submodule is pinned
    # to tag v3.7.9_build_61, so the list is fixed — re-verify this set if the tag is bumped.
    add_library(vst3_sdk STATIC
        ${VST3_SDK}/pluginterfaces/base/funknown.cpp
        ${VST3_SDK}/pluginterfaces/base/coreiids.cpp
        ${VST3_SDK}/pluginterfaces/base/conststringtable.cpp
        ${VST3_SDK}/pluginterfaces/base/ustring.cpp
        ${VST3_SDK}/base/source/fobject.cpp
        ${VST3_SDK}/base/source/fstring.cpp
        ${VST3_SDK}/base/source/fbuffer.cpp
        ${VST3_SDK}/base/source/fstreamer.cpp
        ${VST3_SDK}/base/source/fdebug.cpp
        ${VST3_SDK}/base/source/baseiids.cpp
        ${VST3_SDK}/base/source/updatehandler.cpp
        ${VST3_SDK}/base/thread/source/flock.cpp
        # vstsinglecomponenteffect.cpp #includes vsteditcontroller.cpp unity-style, so
        # vsteditcontroller.cpp must NOT be listed separately (double definition).
        ${VST3_SDK}/public.sdk/source/vst/vstsinglecomponenteffect.cpp
        ${VST3_SDK}/public.sdk/source/vst/vstcomponentbase.cpp
        ${VST3_SDK}/public.sdk/source/vst/vstbus.cpp
        ${VST3_SDK}/public.sdk/source/vst/vstparameters.cpp
        ${VST3_SDK}/public.sdk/source/vst/vstinitiids.cpp
        ${VST3_SDK}/public.sdk/source/common/pluginview.cpp
        ${VST3_SDK}/public.sdk/source/common/commoniids.cpp
        # dllmain.cpp + moduleinit.cpp are NOT here — see the module target below.
        ${VST3_SDK}/public.sdk/source/main/pluginfactory.cpp
    )
    target_include_directories(vst3_sdk PUBLIC ${VST3_SDK})
    # The SDK requires exactly one of RELEASE / DEVELOPMENT (fdebug.cpp keys off it).
    target_compile_definitions(vst3_sdk PUBLIC $<IF:$<CONFIG:Debug>,DEVELOPMENT=1,RELEASE=1>)

    add_library(reasampler_vst MODULE
        vst_entry.cpp
        reasampler_processor.cpp
        processor_state.cpp
        processor_reload.cpp
        # The editor family is split on the Sample face's band axis: session/bridge state,
        # param plumbing plus the shared band-layout resolve, then paint and input in
        # matching sets, plus the two band-independent surfaces and the platform TU.
        editor_session.cpp
        editor_controls.cpp
        editor_models.cpp
        editor_paint.cpp
        editor_paint_chrome.cpp
        editor_paint_waveform.cpp
        editor_paint_deck.cpp
        editor_paint_browse.cpp
        editor_paint_curve.cpp
        editor_input.cpp
        editor_input_chrome.cpp
        editor_input_waveform.cpp
        editor_input_deck.cpp
        editor_input_browse.cpp
        editor_input_curve.cpp
        editor_stroke.cpp
        editor_platform.cpp
        reasampler_embed.cpp
        reaper_bridge.cpp
        # draw_kit is compiled into each module rather than being a static library — see root
        # CMakeLists.txt's LICE_SRC comment for why.
        ${REASAMPLER_SRC_DIR}/shell/panel/draw_kit.cpp
        # Compiled into the module, NOT into vst3_sdk: their SMTG_EXPORT_SYMBOL functions
        # (InitDll/ExitDll) have no internal referrer, so the linker strips them from a
        # static library. Compiling them here keeps the exports.
        ${VST3_SDK}/public.sdk/source/main/dllmain.cpp
        ${VST3_SDK}/public.sdk/source/main/moduleinit.cpp
        ${LICE_SRC}
    )
    # sample_map's build yields plain SampleData, so it does NOT pull the voice engine —
    # sampler_core is linked directly. app_version's PUBLIC include dir carries the
    # generated version header, so the instrument reads the SAME channel-derived ext-state
    # namespace the extension writes.
    target_link_libraries(reasampler_vst PRIVATE vst3_sdk editor_geometry bridge_marshal
        sampler_core sample_map component_state_io capture_paths embed_strip app_version
        capture_browser keyboard_strip sample_bands sample_chrome
        waveform_view bank_sync browser_scroll param_slider tooltip
        theme component_geometry bank_grid trigger_seam envelope_overlay envelope_edit
        knob_deck deck_groups deck_values curve_popup spline_edit master_gain sample_usage
        file_bytes curve_law stroke_aa)
    # SDK_INC gives the REAPER VST3 interfaces + API header for the bridge; WDL_INC gives
    # LICE for the editor. The VST3 SDK headers arrive via vst3_sdk PUBLIC.
    target_include_directories(reasampler_vst PRIVATE ${REASAMPLER_SRC_DIR} ${SDK_INC} ${WDL_INC})

    # A .vst3 is a DLL with a .vst3 extension and no lib prefix. OUTPUT_NAME is channel-
    # forked from the one channel decision at the root and must match
    # app_version::vstOutputName(); the two channels install side-by-side, and the
    # per-channel VST3 class UID keeps a saved instance bound to its own channel.
    # LIBRARY_OUTPUT_DIRECTORY / ARCHIVE_OUTPUT_DIRECTORY pin the module to the top of the
    # build tree even though this target is declared in a subdirectory — see src/app/CMakeLists.txt's
    # set_target_properties comment for why both properties are needed.
    set_target_properties(reasampler_vst PROPERTIES
        PREFIX ""
        SUFFIX ".vst3"
        OUTPUT_NAME "${REASAMPLER_VST_OUTPUT_NAME}"
        LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}"
        ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
endif()
