cmake_minimum_required(VERSION 3.25)
project(xtc_steinberg_examples VERSION 1.0.0 LANGUAGES C CXX)
set(XTC_VST3_SDK_ROOT "" CACHE PATH "External official VST3 SDK checkout (with submodules)")
option(XTC_BUILD_ASIO_EXAMPLE "Build Windows ASIO device diagnostics" OFF)
set(XTC_ASIO_SDK_ROOT "" CACHE PATH "External ASIO SDK root")

if(XTC_VST3_SDK_ROOT)
    if(NOT EXISTS "${XTC_VST3_SDK_ROOT}/public.sdk/source/vst/vstaudioeffect.h")
        message(FATAL_ERROR "XTC_VST3_SDK_ROOT needs the official SDK and its submodules")
    endif()
    # Never install/link example plugins into the user's plugin directories.
    set(SMTG_CREATE_PLUGIN_LINK OFF CACHE BOOL "" FORCE)
    set(SMTG_RUN_VST_VALIDATOR OFF CACHE BOOL "" FORCE)
    set(SMTG_ENABLE_VSTGUI_SUPPORT OFF CACHE BOOL "" FORCE)
    set(SMTG_ENABLE_VST3_PLUGIN_EXAMPLES OFF CACHE BOOL "" FORCE)
    set(SMTG_ENABLE_VST3_HOSTING_EXAMPLES ON CACHE BOOL "" FORCE)
    add_subdirectory("${XTC_VST3_SDK_ROOT}" sdk)
    set(public_sdk_SOURCE_DIR "${XTC_VST3_SDK_ROOT}/public.sdk")
    smtg_add_vst3plugin(xtc-sdk-examples sdk_plugins.cpp)
    target_link_libraries(xtc-sdk-examples PRIVATE sdk)
    target_compile_features(xtc-sdk-examples PRIVATE cxx_std_17)
    target_include_directories(xtc-sdk-examples PRIVATE "${XTC_VST3_SDK_ROOT}")
    if(MSVC)
        target_compile_options(xtc-sdk-examples PRIVATE /W4)
    else()
        target_compile_options(xtc-sdk-examples PRIVATE -Wall -Wextra -Wpedantic)
    endif()
endif()

if(XTC_BUILD_ASIO_EXAMPLE)
    if(NOT WIN32)
        message(FATAL_ERROR "The ASIO example needs Windows and an installed ASIO driver; Linux uses JACK/PipeWire")
    endif()
    foreach(source common/asio.cpp host/asiodrivers.cpp host/pc/asiolist.cpp)
        if(NOT EXISTS "${XTC_ASIO_SDK_ROOT}/${source}")
            message(FATAL_ERROR "XTC_ASIO_SDK_ROOT is missing ${source}")
        endif()
    endforeach()
    add_executable(xtc-asio-devices asio_devices.cpp
        "${XTC_ASIO_SDK_ROOT}/common/asio.cpp"
        "${XTC_ASIO_SDK_ROOT}/host/asiodrivers.cpp"
        "${XTC_ASIO_SDK_ROOT}/host/pc/asiolist.cpp")
    target_include_directories(xtc-asio-devices PRIVATE
        "${XTC_ASIO_SDK_ROOT}/common" "${XTC_ASIO_SDK_ROOT}/host"
        "${XTC_ASIO_SDK_ROOT}/host/pc")
    target_compile_features(xtc-asio-devices PRIVATE cxx_std_17)
    target_link_libraries(xtc-asio-devices PRIVATE ole32 advapi32 uuid user32)
endif()

if(NOT XTC_VST3_SDK_ROOT AND NOT XTC_BUILD_ASIO_EXAMPLE)
    message(FATAL_ERROR "Set XTC_VST3_SDK_ROOT or enable XTC_BUILD_ASIO_EXAMPLE with XTC_ASIO_SDK_ROOT")
endif()
