cmake_minimum_required(VERSION 3.20)

# The emulation core (software 3D rasterizer, 2D, SPU) is far too slow unoptimized.
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()
project(DQ9-Decomp LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

enable_testing()

option(DSR_RET_CHECK "Verify guest return addresses at run time (debug)" OFF)
option(DSR_TRACE "Generated code logs every guest function entry to $DSR_TRACE_FILE (debug)" OFF)
option(DQ9_BUILD_SDL "Build the SDL2 frontend and dq9 executable" ON)
option(DQ9_BUILD_GODOT "Build the Godot 4.5 GDExtension" OFF)
set(GODOT_CPP_DIR "" CACHE PATH "External godot-cpp 4.5 checkout")
if(DQ9_BUILD_GODOT AND (NOT GODOT_CPP_DIR OR NOT EXISTS "${GODOT_CPP_DIR}/CMakeLists.txt"))
  message(FATAL_ERROR "DQ9_BUILD_GODOT requires -DGODOT_CPP_DIR=/path/to/godot-cpp")
endif()
set(DSR_SDL2_RUNTIME_DLL "" CACHE FILEPATH "SDL2 runtime DLL installed alongside dq9 on Windows")
set(DQ9_TEST_DATA_ROOT "${PROJECT_SOURCE_DIR}")
set(DQ9_TEST_ROM "$ENV{DQ9_ROM}" CACHE FILEPATH "Locally available compatible image for optional integration tests")
set(DQ9_TEST_SAVE "" CACHE FILEPATH "Caller-supplied save for optional integration tests")
set(DQ9_TEST_INPUT "" CACHE FILEPATH "Caller-supplied input script for optional integration tests")
set(DQ9_BIOS_REFERENCE_SOURCE "" CACHE FILEPATH "Optional reference emulator table source for extended tests")

function(dsr_target_compile_options target)
  if(MSVC)
    target_compile_options(${target} PRIVATE /W4)
  else()
    target_compile_options(${target} PRIVATE ${ARGN})
  endif()
endfunction()
function(dsr_copy_sdl2_runtime target)
  if(WIN32)
    if(NOT DSR_SDL2_RUNTIME_DLL OR NOT EXISTS "${DSR_SDL2_RUNTIME_DLL}")
      message(FATAL_ERROR "Set DSR_SDL2_RUNTIME_DLL to the matching SDL2.dll")
    endif()
    # Several executables in one directory used to run copy_if_different on the same SDL2.dll
    # concurrently, which intermittently failed ("file in use") under parallel builds. One copy
    # target per directory, built before every executable that lives there.
    string(MD5 dsr_dir_key "${CMAKE_CURRENT_BINARY_DIR}")
    set(dsr_copy_target dsr_sdl2_runtime_${dsr_dir_key})
    if(NOT TARGET ${dsr_copy_target})
      add_custom_target(${dsr_copy_target}
        COMMAND "${CMAKE_COMMAND}" -E make_directory "$<TARGET_FILE_DIR:${target}>"
        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${DSR_SDL2_RUNTIME_DLL}"
                "$<TARGET_FILE_DIR:${target}>"
        VERBATIM)
    endif()
    add_dependencies(${target} ${dsr_copy_target})
  endif()
endfunction()
# A separately supplied generated-code directory (never committed).
# Empty: build the runtime and its tests only.
set(DQ9_GENERATED_DIR "" CACHE PATH "Separately supplied generated-code directory")
if(DQ9_BUILD_GODOT)
  foreach(cpu arm9 arm7)
    if(NOT EXISTS "${DQ9_GENERATED_DIR}/${cpu}/fn_table.cpp")
      message(FATAL_ERROR "DQ9_BUILD_GODOT requires generated ${cpu} code; set DQ9_GENERATED_DIR to a separately supplied generated-code directory")
    endif()
  endforeach()
endif()

add_library(dsr_headers INTERFACE)
target_include_directories(dsr_headers INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/include)
target_compile_features(dsr_headers INTERFACE cxx_std_20)
if(WIN32)
  target_compile_definitions(dsr_headers INTERFACE WIN32_LEAN_AND_MEAN NOMINMAX SDL_MAIN_HANDLED)
  if(MSVC)
    target_compile_options(dsr_headers INTERFACE /fp:strict)
  else()
    target_compile_options(dsr_headers INTERFACE -ffp-contract=off)
  endif()
  # Zig already supplies static libc++/compiler-rt; native MinGW needs this.
  if(MINGW AND NOT DSR_ZIG_TOOLCHAIN)
    add_link_options(-static -static-libgcc -static-libstdc++)
  endif()
endif()
if(DSR_RET_CHECK)
  target_compile_definitions(dsr_headers INTERFACE DSR_RET_CHECK)
endif()
if(DSR_TRACE)
  target_compile_definitions(dsr_headers INTERFACE DSR_TRACE)
endif()
foreach(component core gpu2d gpu3d spu periph)
  if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/runtime/${component}/CMakeLists.txt")
    add_subdirectory(runtime/${component})
  endif()
endforeach()
if(DQ9_BUILD_SDL)
  add_subdirectory(runtime/frontend)
endif()
foreach(target dsr_core dsr_gpu2d dsr_gpu3d dsr_spu dsr_periph)
  if(TARGET ${target})
    set_target_properties(${target} PROPERTIES POSITION_INDEPENDENT_CODE ON)
  endif()
endforeach()
add_subdirectory(runtime/session)

# Host-side frame selection is testable without generated code, SDL or a ROM.
add_executable(dq9_frame_spec_tests game/frame_spec_tests.cpp)
target_link_libraries(dq9_frame_spec_tests PRIVATE dsr_headers)
dsr_target_compile_options(dq9_frame_spec_tests -Wall -Wextra)
add_test(NAME dq9_frame_spec_tests COMMAND dq9_frame_spec_tests)
set_tests_properties(dq9_frame_spec_tests PROPERTIES TIMEOUT 30)

if(DQ9_GENERATED_DIR AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/game/CMakeLists.txt")
  add_subdirectory(game)
endif()
if(DQ9_BUILD_GODOT)
  add_subdirectory(godot)
endif()
foreach(test_target dsr_core_tests dsr_swi_diff_tests dsr_gpu2d_tests dsr_gpu3d_tests
        dsr_spu_tests dsr_periph_tests dsr_band_pool_tests dsr_texture_rgba_tests
        dsr_texture_cache_tests dsr_vram_tests dsr_render_timing_tests
        dsr_rom_path_test dsr_ui_screenshot_capture_test
        dsr_frontend_logic_test dsr_ui_logic_test dsr_session_tests dsr_render_frame_tests)
  if(TARGET ${test_target})
    if(MSVC)
      target_compile_options(${test_target} PRIVATE /UNDEBUG)
    else()
      target_compile_options(${test_target} PRIVATE -UNDEBUG)
    endif()
  endif()
endforeach()

