cmake_minimum_required(VERSION 3.25)
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/VERSION.txt" IB_VERSION LIMIT_COUNT 1) # .txt: a file named VERSION shadows <version> on case-insensitive disks
project(IllbleedRecompiled VERSION ${IB_VERSION} LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if(MSVC)
  # __cplusplus real e fontes em UTF-8
  add_compile_options(/Zc:__cplusplus /utf-8)
endif()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(CTest)

# C++ gerado pelo ib-recomp a partir do executavel do usuario (privado, generated/).
# Vazio: nao compila o jogo nem os testes que dependem dele.
set(IB_GENERATED_DIR "" CACHE PATH "C++ gerado pelo ib-recomp (vazio = nao compilar)")

# Build de validacao (diagnosticos so para testes; o produto recusa as variaveis de diagnostico), como no Blue Stinger.
option(IB_VALIDACAO "Build de validacao" OFF)
option(BUILD_TESTS "Compilar testes" ON)
# SDL3 (zlib, third_party/SDL3): gamepads (IB10) and, later, audio output.
option(IB_ENABLE_SDL3 "Compilar o SDL3 incorporado" ON)
if(IB_ENABLE_SDL3 AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/SDL3/CMakeLists.txt")
  set(SDL_SHARED OFF CACHE BOOL "" FORCE)
  set(SDL_STATIC ON CACHE BOOL "" FORCE)
  set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
  set(SDL_EXAMPLES OFF CACHE BOOL "" FORCE)
  set(SDL_INSTALL OFF CACHE BOOL "" FORCE)
  add_subdirectory(third_party/SDL3 EXCLUDE_FROM_ALL)
endif()
add_subdirectory(native)
if(BUILD_TESTS)
  add_subdirectory(tests)
endif()
add_subdirectory(tools/extractor)
add_subdirectory(translator/sh4)
add_subdirectory(runtime)
add_subdirectory(legacy)
add_subdirectory(builder)
if(IB_GENERATED_DIR)
  if(NOT EXISTS "${IB_GENERATED_DIR}/coverage.txt")
    message(FATAL_ERROR "IB_GENERATED_DIR requires a completed ib-recomp output")
  endif()
  file(STRINGS "${IB_GENERATED_DIR}/coverage.txt" _words REGEX "^image_words=[1-9][0-9]*$")
  if(NOT _words MATCHES "^image_words=([1-9][0-9]*)$")
    message(FATAL_ERROR "Invalid ib-recomp coverage report")
  endif()
  math(EXPR _last_part "(${CMAKE_MATCH_1} - 1) / 8192")
  set(_parts)
  foreach(_i RANGE 0 ${_last_part})
    list(APPEND _parts "${IB_GENERATED_DIR}/part_${_i}.cpp")
  endforeach()
  add_library(ib_game_image STATIC "${IB_GENERATED_DIR}/dispatch.cpp" ${_parts})
  target_link_libraries(ib_game_image PUBLIC ib_sh4_state)
  if(MSVC)
    target_compile_options(ib_game_image PRIVATE /bigobj /MP6)
  endif()
  # The installed game (IB06): recompiled image on the hybrid boot.
  if(WIN32)
    add_executable(Illbleed WIN32 app/illbleed_main.cpp app/settings_menu.cpp)
    target_link_libraries(Illbleed PRIVATE ib_game_image ib_legacy_hybrid ib_native_controls ib_native_files user32 gdi32)
    target_compile_definitions(Illbleed PRIVATE IB_VERSION_TEXT="${IB_VERSION}") # VERSION.txt: 0.<etapa>.<correcao>
    if(WIN32 AND NOT MSVC)
      # MinGW (portable compiler): wWinMain is the entry point; the runtime is linked statically.
      target_link_options(Illbleed PRIVATE -municode -static)
    endif()
    if(TARGET ib_native_vulkan)
      target_link_libraries(Illbleed PRIVATE ib_native_vulkan)
      target_compile_definitions(Illbleed PRIVATE IB_HAS_VULKAN)
    endif()
    if(TARGET ib_native_sdl)
      target_link_libraries(Illbleed PRIVATE ib_native_sdl)
      target_compile_definitions(Illbleed PRIVATE IB_HAS_SDL3)
    endif()
    install(TARGETS Illbleed RUNTIME DESTINATION . COMPONENT Game)
  endif()
endif()

if(BUILD_TESTS)
  set(IB_US_BOOT_IMAGE "" CACHE FILEPATH "1ST_READ.BIN local para validar a entrada USA")
  if(IB_US_BOOT_IMAGE OR TARGET ib_game_image)
    add_subdirectory(tests/integration)
  endif()
endif()
