# The REAPER extension — a loadable module REAPER dlopen()s, never linked against. Paths
# below are rooted at src/, not relative to this directory.
# No core/ TU is ever compiled into this source list; every core/ TU enters through a link edge
# instead. Compiling one here too would give it its own copy, built under this target's own
# compile definitions and include dirs — free to diverge from the library copy every other
# consumer (the <module>_tests targets, reasampler_vst) links, with nothing to detect it.

add_library(reaper_reasampler MODULE
    ${REASAMPLER_SRC_DIR}/app/main.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/capture.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/capture_orchestrator.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/capture_batch.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/bake_land.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/bake_landing.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/scope_resolve.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/render_selection.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/render_isolation.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/render_bounds_gate.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/render_in_place.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/realtime_lifecycle.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/capture_realtime_shell.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/capture_realtime_finalize.cpp
    ${REASAMPLER_SRC_DIR}/shell/persist/session.cpp
    ${REASAMPLER_SRC_DIR}/shell/persist/ext_state_io.cpp
    ${REASAMPLER_SRC_DIR}/shell/persist/prune_fs.cpp
    ${REASAMPLER_SRC_DIR}/shell/bank_ops/bank_ops.cpp
    ${REASAMPLER_SRC_DIR}/shell/panel/panel_audition.cpp
    ${REASAMPLER_SRC_DIR}/shell/panel/panel_bank_ops.cpp
    ${REASAMPLER_SRC_DIR}/shell/panel/panel_drag.cpp
    ${REASAMPLER_SRC_DIR}/shell/panel/panel_input.cpp
    ${REASAMPLER_SRC_DIR}/shell/panel/panel_layout.cpp
    ${REASAMPLER_SRC_DIR}/shell/panel/panel_render.cpp
    ${REASAMPLER_SRC_DIR}/shell/panel/panel_thumbnails.cpp
    ${REASAMPLER_SRC_DIR}/shell/panel/panel_window.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
    ${LICE_SRC}
    ${REASAMPLER_SRC_DIR}/shell/capture/insert.cpp
    ${REASAMPLER_SRC_DIR}/shell/view/view.cpp
    ${REASAMPLER_SRC_DIR}/shell/view/view_fx_park.cpp
    ${REASAMPLER_SRC_DIR}/shell/view/view_solo.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/track_guid.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/provenance_shell.cpp
    ${REASAMPLER_SRC_DIR}/shell/capture/item_read.cpp
    ${REASAMPLER_SRC_DIR}/shell/actions/action_registry.cpp
    ${REASAMPLER_SRC_DIR}/shell/actions/design_view_actions.cpp
    ${REASAMPLER_SRC_DIR}/shell/actions/bank_actions.cpp
    ${REASAMPLER_SRC_DIR}/shell/actions/prune_action.cpp
    ${REASAMPLER_SRC_DIR}/shell/actions/package_export_action.cpp
    ${REASAMPLER_SRC_DIR}/shell/actions/ingest.cpp
    ${REASAMPLER_SRC_DIR}/shell/actions/arrange_drop_win.cpp
    ${REASAMPLER_SRC_DIR}/shell/actions/drag_out_win.cpp
    ${REASAMPLER_SRC_DIR}/shell/actions/instrument_drop_win.cpp
    ${REASAMPLER_SRC_DIR}/shell/persist/usage_scan.cpp
)
target_link_libraries(reaper_reasampler PRIVATE json wire file_bytes bank_model capture_paths capture_name peaks bank_grid mode_switch tab_strip view_mode_model view_tree guid_diff lane_keys solo_cache insert_plan render_settings render_window track_topology batch_capture tail_control capture_realtime bank_book wav_codec origin_ledger tracking_authority prune_reconcile prune_button app_version provenance drag_out instrument_drop theme component_geometry action_bar footer_bar overflow_menu mode_enable tooltip card_meta card_drag assignment_request bank_sync sample_usage bake_wire resample_name export_bank package_pickers)
# NOT linked here, deliberately: sampler_core / pitch_shift / the filter / limiter. The
# instrument renders its own bake in its own process, which is what keeps the extension's
# link graph free of the voice engine — a link edge to it here means the design drifted.
target_include_directories(reaper_reasampler PRIVATE ${SDK_INC} ${WDL_INC})

# The deferred FX-park queue's re-entrancy rule is pure (header-inline, no REAPER
# types), so it is CTest-covered like a core/ module. Declared here rather than in a
# src/shell/view/CMakeLists.txt because that directory deliberately has none — its
# TUs are compiled into this target directly. view_mode_model is linked for
# makeRestorePlan alone: the cancel path's round trip is a contract between the two,
# and pinning it against hand-built ops would not catch a change to either half.
reasampler_test(view_fx_park LINK fx_offline view_mode_model)

# Bank-package import: the promptless verb plus its action skin. Kept as its own
# appended block rather than merged into the lists above, so the two package
# directions stay textually independent.
target_sources(reaper_reasampler PRIVATE
    ${REASAMPLER_SRC_DIR}/shell/package/import_bank.cpp
    ${REASAMPLER_SRC_DIR}/shell/actions/package_import_action.cpp)
target_link_libraries(reaper_reasampler PRIVATE import_landing package_pickers)

# OUTPUT_NAME is channel-derived; the CMake target name stays "reaper_reasampler" for both
# configs, since REAPER dlopen's any reaper_* module and the two channels' artifacts load
# side-by-side. LIBRARY_OUTPUT_DIRECTORY pins the module to the top of the build tree even
# though this target is declared in a subdirectory — the install step copies it from there.
# ARCHIVE_OUTPUT_DIRECTORY pins the same for MODULE targets: CMake emits an import-lib
# sidecar (.lib/.exp on MSVC) keyed off ARCHIVE_OUTPUT_DIRECTORY, not LIBRARY_OUTPUT_DIRECTORY,
# so it needs pinning too even though nothing links against this import lib.
set_target_properties(reaper_reasampler PROPERTIES
    PREFIX ""
    OUTPUT_NAME "${REASAMPLER_OUTPUT_NAME}"
    LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}"
    ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")

if(WIN32)
    # Native Win32; REAPER provides nothing extra to link. The bank-panel dialog template
    # is compiled from resource.rc by the platform RC compiler.
    target_sources(reaper_reasampler PRIVATE ${REASAMPLER_SRC_DIR}/resource.rc)

elseif(APPLE)
    # Use REAPER's OWN SWELL at runtime via the modstub. Do NOT build full SWELL —
    # SWELL_PROVIDED_BY_APP routes calls to the host.
    target_sources(reaper_reasampler PRIVATE ${SWELL}/swell-modstub.mm)
    target_compile_definitions(reaper_reasampler PRIVATE SWELL_PROVIDED_BY_APP)
    target_link_libraries(reaper_reasampler PRIVATE "-framework AppKit")
    set_target_properties(reaper_reasampler PROPERTIES SUFFIX ".dylib")
    # SWELL can't read a Win32 .rc directly. Run resgen once to turn resource.rc into a
    # C++ source, then add it here:
    #   php ${WDL_INC}/swell/mac_resgen.php src/resource.rc
    # target_sources(reaper_reasampler PRIVATE ${REASAMPLER_SRC_DIR}/resource.rc_mac_dlg.h)

else()
    # Linux: REAPER's libSwell.so is used at runtime via the generic modstub. With
    # SWELL_PROVIDED_BY_APP you can drop pkg-config / -lX11 entirely.
    target_sources(reaper_reasampler PRIVATE ${SWELL}/swell-modstub-generic.cpp)
    target_compile_definitions(reaper_reasampler PRIVATE SWELL_PROVIDED_BY_APP)
    set_target_properties(reaper_reasampler PROPERTIES SUFFIX ".so")
    # Reuse the macOS resgen output (see CLAUDE.md, SWELL dialog resources), then add the
    # generated source:
    #   php ${WDL_INC}/swell/mac_resgen.php src/resource.rc
    # target_sources(reaper_reasampler PRIVATE ${REASAMPLER_SRC_DIR}/resource.rc_mac_dlg.h)
endif()
