set(REXSYSTEM_SOURCES
    runtime.cpp
    kernel_state.cpp
    xmemory.cpp
    kernel_module.cpp
    user_module.cpp
    xthread.cpp
    xevent.cpp
    xfile.cpp
    xmutant.cpp
    xsemaphore.cpp
    xtimer.cpp
    xiocompletion.cpp
    xnotifylistener.cpp
    xenumerator.cpp
    xsession.cpp
    xlive_web_client.cpp
    xsocket.cpp
    xsymboliclink.cpp
    xobject.cpp
    xmodule.cpp
    shared_library.cpp
    guest_path.cpp
    debug_hud.cpp
    discord_presence.cpp

    # Utilities
    util/object_table.cpp
    util/native_list.cpp
    util/xdbf_utils.cpp
    util/gameinfo_utils.cpp

    # XAM services
    xam/app_manager.cpp
    xam/content_manager.cpp
    xam/user_profile.cpp

    ppc_types.cpp
    elf_module.cpp
    entry_table.cpp
    export_resolver.cpp
    lzx.cpp
    mmio_handler.cpp
    map_parser.cpp
    module.cpp
    function_dispatcher.cpp
    thread.cpp
    thread_state.cpp
    xex_module.cpp
)

if(REXGLUE_BUILD_MONOLITHIC_RUNTIME)
    add_library(rexruntime STATIC ${REXSYSTEM_SOURCES})
else()
    add_library(rexruntime SHARED ${REXSYSTEM_SOURCES})
endif()
add_library(rex::runtime ALIAS rexruntime)
add_library(rex::system ALIAS rexruntime)
add_library(rex::kernel ALIAS rexruntime)

if(NOT REXGLUE_BUILD_MONOLITHIC_RUNTIME)
    set_target_properties(rexruntime PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()

target_include_directories(rexruntime PUBLIC
    $<BUILD_INTERFACE:${REXGLUE_ROOT}/include>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_include_directories(rexruntime PRIVATE
    ${REXGLUE_ROOT}/thirdparty
    ${REXGLUE_ROOT}/thirdparty/inja/third_party/include
)

target_link_libraries(rexruntime
    PUBLIC
        simde
        fmt::fmt
        spdlog::spdlog
        tomlplusplus
        SDL3::SDL3
    PRIVATE
        aes128
        mspack
)

if(REXGLUE_ENABLE_TRACY)
    target_link_libraries(rexruntime PUBLIC TracyClient)
    target_compile_definitions(rexruntime PUBLIC
        $<$<NOT:$<CONFIG:Release>>:REXGLUE_ENABLE_PROFILING>)
endif()

if(REXGLUE_ENABLE_PERF_COUNTERS)
    target_compile_definitions(rexruntime PUBLIC
        $<$<NOT:$<CONFIG:Release>>:REXGLUE_ENABLE_PERF_COUNTERS>)
endif()

if(WIN32)
    target_link_libraries(rexruntime PRIVATE ws2_32 winhttp ole32 oleaut32 uuid)
endif()

option(REXGLUE_ENABLE_DISCORD_SOCIAL_SDK
    "Enable Discord Social SDK Rich Presence integration when the SDK is available"
    ON)
set(REXGLUE_DISCORD_SDK_DIR "${REXGLUE_ROOT}/../Discord SDK" CACHE PATH
    "Path to the Discord Social SDK root")
if(REXGLUE_ENABLE_DISCORD_SOCIAL_SDK AND WIN32)
    set(REXGLUE_DISCORD_SDK_INCLUDE "${REXGLUE_DISCORD_SDK_DIR}/include")
    set(REXGLUE_DISCORD_SDK_RELEASE_LIB "${REXGLUE_DISCORD_SDK_DIR}/lib/release/discord_partner_sdk.lib")
    set(REXGLUE_DISCORD_SDK_DEBUG_LIB "${REXGLUE_DISCORD_SDK_DIR}/lib/debug/discord_partner_sdk.lib")
    if(EXISTS "${REXGLUE_DISCORD_SDK_INCLUDE}/cdiscord.h" AND
       EXISTS "${REXGLUE_DISCORD_SDK_RELEASE_LIB}" AND
       EXISTS "${REXGLUE_DISCORD_SDK_DEBUG_LIB}")
        target_include_directories(rexruntime PRIVATE "${REXGLUE_DISCORD_SDK_INCLUDE}")
        target_link_libraries(rexruntime PRIVATE
            "$<$<CONFIG:Debug>:${REXGLUE_DISCORD_SDK_DEBUG_LIB}>"
            "$<$<NOT:$<CONFIG:Debug>>:${REXGLUE_DISCORD_SDK_RELEASE_LIB}>"
        )
        target_compile_definitions(rexruntime PRIVATE REX_HAS_DISCORD_SOCIAL_SDK=1)
        message(STATUS "Discord Social SDK enabled: ${REXGLUE_DISCORD_SDK_DIR}")
    else()
        target_compile_definitions(rexruntime PRIVATE REX_HAS_DISCORD_SOCIAL_SDK=0)
        message(WARNING "Discord Social SDK requested but not found at ${REXGLUE_DISCORD_SDK_DIR}")
    endif()
else()
    target_compile_definitions(rexruntime PRIVATE REX_HAS_DISCORD_SOCIAL_SDK=0)
endif()

if(REXGLUE_USE_D3D12)
    target_link_libraries(rexruntime PUBLIC dxgi dxguid dxc-headers)
    target_compile_definitions(rexruntime PUBLIC REX_HAS_D3D12=1)
endif()
if(REXGLUE_USE_VULKAN)
    target_link_libraries(rexruntime PUBLIC
        volk::volk
        GPUOpen::VulkanMemoryAllocator
        SPIRV-Tools-static
    )
    target_compile_definitions(rexruntime PUBLIC REX_HAS_VULKAN=1)
endif()
