# Xenia (xenia-canary, BSD-3-Clause) GPU backend as libraries for this port.
#
# Only the modules the Direct3D 12 GPU backend needs are built: base, memory (+ the
# MMIO exception handler), ui (+ d3d12), gpu (minus SPIR-V/Vulkan) and gpu/d3d12.
# Xenia's kernel / CPU / emulator classes are replaced by the small headers in
# shim/, which come first on the include path. Xenia's sources are not modified.

set(XENIA_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../tools/xenia-canary)
set(XENIA_SRC ${XENIA_ROOT}/src/xenia)
set(XENIA_TP ${XENIA_ROOT}/third_party)
set(XENIA_SHIM ${CMAKE_CURRENT_SOURCE_DIR}/shim)

find_package(Python3 REQUIRED COMPONENTS Interpreter)

# Xenia's build normally generates version.h from git; a fixed one is enough here.
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/version.h
"#pragma once
#define XE_BUILD_BRANCH \"canary-smr\"
#define XE_BUILD_COMMIT \"2da2519\"
#define XE_BUILD_COMMIT_SHORT \"2da2519\"
#define XE_BUILD_DATE __DATE__
")

# ------------------------------------------------------------------ common settings
add_library(xenia_settings INTERFACE)
target_include_directories(xenia_settings INTERFACE
    ${XENIA_SHIM}                 # must win over the real kernel/cpu headers
    ${XENIA_ROOT}
    ${XENIA_ROOT}/src
    ${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(xenia_settings SYSTEM INTERFACE ${XENIA_TP})
target_compile_definitions(xenia_settings INTERFACE
    _CRT_NONSTDC_NO_DEPRECATE _CRT_SECURE_NO_WARNINGS WIN32 _WIN64=1 _AMD64=1 UNICODE _UNICODE
    _LIB VULKAN_HPP_NO_TO_STRING IMGUI_DISABLE_DEFAULT_FONT_BITMAP IMGUI_DISABLE_OBSOLETE_FUNCTIONS
    IMGUI_USE_WCHAR32 USE_CPP17 IMGUI_USE_STB_SPRINTF $<$<CONFIG:Release>:NDEBUG>
    _mm_cvtsi64x_si128=_mm_cvtsi64_si128)  # MSVC-only intrinsic alias
target_compile_options(xenia_settings INTERFACE
    /utf-8 -mavx -Wno-everything)
target_link_libraries(xenia_settings INTERFACE
    ntdll wsock32 ws2_32 xinput comctl32 shcore shlwapi dxguid bcrypt dwmapi dxgi winmm d3d12)

# Xenia's platform source selection: drop tests/mains/demos and non-Windows files.
function(smr_xenia_sources target dir)
  file(GLOB _all "${dir}/*.cc" "${dir}/*.cpp" "${dir}/*.c")
  foreach(src ${_all})
    get_filename_component(n ${src} NAME_WE)
    if(n MATCHES "_(main|test|demo)$" OR n MATCHES "_(linux|posix|gnulinux|x11|gtk|android|mac|arm64)$")
      continue()
    endif()
    foreach(ex ${ARGN})
      if(n MATCHES "${ex}")
        set(skip TRUE)
      endif()
    endforeach()
    if(skip)
      unset(skip)
      continue()
    endif()
    target_sources(${target} PRIVATE ${src})
  endforeach()
endfunction()

# Compiles Xenia's .xesl helper shaders to DXBC headers with its own script (FXC).
function(smr_xenia_dxbc_shaders target shader_dir)
  file(GLOB _sources "${shader_dir}/*.xesl" "${shader_dir}/*.hlsl" "${shader_dir}/*.xesli" "${shader_dir}/*.hlsli")
  set(_stamp "${CMAKE_CURRENT_BINARY_DIR}/${target}_dxbc.stamp")
  set(_script "${XENIA_ROOT}/tools/build/compile_shader_dxbc.py")
  set(_out "${shader_dir}/bytecode/d3d12_5_1")
  set(_commands COMMAND ${CMAKE_COMMAND} -E make_directory "${_out}")
  foreach(src ${_sources})
    get_filename_component(_name ${src} NAME)
    string(REGEX REPLACE "\\.[^.]+$" "" _base "${_name}")
    string(REPLACE "." "_" _id "${_base}")
    string(LENGTH "${_id}" _len)
    if(_len LESS 3)
      continue()
    endif()
    math(EXPR _s "${_len} - 2")
    string(SUBSTRING "${_id}" ${_s} 2 _stage)
    if(NOT _stage MATCHES "^(vs|hs|ds|gs|ps|cs)$")
      continue()
    endif()
    list(APPEND _commands COMMAND ${Python3_EXECUTABLE} "${_script}" "${src}" "${_out}/${_id}.h")
  endforeach()
  add_custom_command(OUTPUT "${_stamp}" ${_commands}
      COMMAND ${CMAKE_COMMAND} -E touch "${_stamp}"
      DEPENDS ${_sources} "${_script}"
      COMMENT "Compiling Xenia DXBC shaders for ${target}" VERBATIM)
  add_custom_target(${target}-dxbc DEPENDS "${_stamp}")
  add_dependencies(${target} ${target}-dxbc)
endfunction()

# ------------------------------------------------------------------ third-party
add_library(xenia_fmt STATIC ${XENIA_TP}/fmt/src/format.cc ${XENIA_TP}/fmt/src/os.cc)
target_include_directories(xenia_fmt PUBLIC ${XENIA_TP}/fmt/include)
target_link_libraries(xenia_fmt PRIVATE xenia_settings)

add_library(xenia_xxhash STATIC ${XENIA_TP}/xxhash/xxhash.c)
target_include_directories(xenia_xxhash PUBLIC ${XENIA_TP}/xxhash)
target_link_libraries(xenia_xxhash PRIVATE xenia_settings)

add_library(xenia_dxbc STATIC ${XENIA_TP}/dxbc/DXBCChecksum.cpp)
target_include_directories(xenia_dxbc PUBLIC ${XENIA_TP}/dxbc)
target_link_libraries(xenia_dxbc PRIVATE xenia_settings)

add_library(xenia_imgui STATIC
    ${XENIA_TP}/imgui/imgui.cpp ${XENIA_TP}/imgui/imgui_demo.cpp ${XENIA_TP}/imgui/imgui_draw.cpp
    ${XENIA_TP}/imgui/imgui_tables.cpp ${XENIA_TP}/imgui/imgui_widgets.cpp)
target_include_directories(xenia_imgui PUBLIC ${XENIA_TP}/imgui PRIVATE ${XENIA_TP}/stb)
target_link_libraries(xenia_imgui PRIVATE xenia_settings)

# snappy needs a generated public header (as Xenia's own build does).
if(NOT EXISTS "${XENIA_TP}/snappy/snappy-stubs-public.h")
  execute_process(COMMAND ${CMAKE_COMMAND} -G Ninja -DSNAPPY_BUILD_TESTS=OFF -DSNAPPY_BUILD_BENCHMARKS=OFF
      -DSNAPPY_REQUIRE_AVX=ON -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
      ${XENIA_TP}/snappy -B${CMAKE_CURRENT_BINARY_DIR}/snappy-config)
  configure_file(${CMAKE_CURRENT_BINARY_DIR}/snappy-config/snappy-stubs-public.h ${XENIA_TP}/snappy/snappy-stubs-public.h COPYONLY)
endif()
add_library(xenia_snappy STATIC
    ${XENIA_TP}/snappy/snappy-sinksource.cc ${XENIA_TP}/snappy/snappy-stubs-internal.cc ${XENIA_TP}/snappy/snappy.cc)
target_include_directories(xenia_snappy PUBLIC ${XENIA_TP}/snappy)
target_link_libraries(xenia_snappy PRIVATE xenia_settings)

# ------------------------------------------------------------------ xenia modules
add_library(xenia_base STATIC)
smr_xenia_sources(xenia_base ${XENIA_SRC}/base "^console_app_main_" "^main_init_" "^main_win$")
target_link_libraries(xenia_base PUBLIC xenia_settings xenia_fmt)

add_library(xenia_core STATIC
    ${XENIA_SRC}/memory.cc
    ${XENIA_SRC}/cpu/mmio_handler.cc
    ${XENIA_SHIM}/shim.cc)
target_link_libraries(xenia_core PUBLIC xenia_base)

add_library(xenia_ui STATIC)
smr_xenia_sources(xenia_ui ${XENIA_SRC}/ui "^windowed_app_main_")
target_link_libraries(xenia_ui PUBLIC xenia_base xenia_imgui)

add_library(xenia_ui_d3d12 STATIC)
smr_xenia_sources(xenia_ui_d3d12 ${XENIA_SRC}/ui/d3d12)
smr_xenia_dxbc_shaders(xenia_ui_d3d12 ${XENIA_SRC}/ui/shaders)
target_link_libraries(xenia_ui_d3d12 PUBLIC xenia_ui)

add_library(xenia_gpu STATIC)
smr_xenia_sources(xenia_gpu ${XENIA_SRC}/gpu "^spirv_" "^trace_(viewer|player|dump)$")
target_link_libraries(xenia_gpu PUBLIC xenia_core xenia_ui xenia_dxbc xenia_snappy xenia_xxhash)

add_library(xenia_gpu_d3d12 STATIC)
smr_xenia_sources(xenia_gpu_d3d12 ${XENIA_SRC}/gpu/d3d12)
smr_xenia_dxbc_shaders(xenia_gpu_d3d12 ${XENIA_SRC}/gpu/shaders)
target_link_libraries(xenia_gpu_d3d12 PUBLIC xenia_gpu xenia_ui_d3d12)

# ------------------------------------------------------------------ XMA audio decoding
# Xenia's XMA context decoder ("master" implementation) on FFmpeg's xmaframes decoder.
# The register block, context allocation and scheduling are this port's (audio/xma.cpp).
include(${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg.cmake)

add_library(xenia_apu STATIC
    ${XENIA_SRC}/apu/xma_context.cc
    ${XENIA_SRC}/apu/xma_context_master.cc)
target_include_directories(xenia_apu PUBLIC ${XENIA_TP}/FFmpeg ${XENIA_TP}/ffmpeg-xenia)
target_link_libraries(xenia_apu PUBLIC xenia_core xenia_avcodec xenia_avutil)
