# The REAPER extension — a loadable module REAPER dlopen()s, never linked against.
# Its source list is app/ + shell/ only, so the paths below are rooted at src/ rather than
# being relative to this directory. Every core/ TU enters through a link edge instead: a TU
# compiled here AND linked from its library would be built twice under two different sets of
# per-target compile definitions and include dirs — two definitions of one symbol in one link.

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/scope_resolve.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/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/ingest.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 peaks bank_grid mode_switch tab_strip view_mode_model view_tree guid_diff lane_keys insert_plan render_settings batch_capture tail_control capture_realtime bank_book wav_codec owned_manifest 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 bridge_marshal sample_usage)
target_include_directories(reaper_reasampler PRIVATE ${SDK_INC} ${WDL_INC})

# 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()
