cmake_minimum_required(VERSION 3.20)
project(AlienResurrectionPort C CXX)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()

find_package(SDL3 REQUIRED CONFIG)

if(MINGW)
  # C99/GNU printf (%zu, %lld) instead of the Microsoft runtime's
  add_compile_definitions(__USE_MINGW_ANSI_STDIO=1)
endif()

# How the game's code runs:
#   recompiled   the C translated from the game executables (src/gen, made by
#                tools/regen.sh from your disc) is compiled in. Fastest; for
#                development. The binary contains the game's code.
#   interpreted  no game code in the binary: the executables are read from the
#                player's disc at run time and interpreted. Used for releases,
#                which can be built without a disc image.
#   auto         recompiled if src/gen exists, otherwise interpreted.
set(GAME_CODE auto CACHE STRING "Game code: auto, recompiled or interpreted")
set_property(CACHE GAME_CODE PROPERTY STRINGS auto recompiled interpreted)
set(GAME_CODE_MODE ${GAME_CODE})
if(GAME_CODE_MODE STREQUAL "auto")
  if(EXISTS ${CMAKE_SOURCE_DIR}/src/gen/alien/alien_funcs.h)
    set(GAME_CODE_MODE recompiled)
  else()
    set(GAME_CODE_MODE interpreted)
  endif()
endif()
message(STATUS "Game code: ${GAME_CODE_MODE}")

if(GAME_CODE_MODE STREQUAL "recompiled")
  # Statically recompiled game code (generated by tools/recomp.py)
  file(GLOB GEN_ALIEN ${CMAKE_SOURCE_DIR}/src/gen/alien/*.c)
  file(GLOB GEN_SLUS ${CMAKE_SOURCE_DIR}/src/gen/slus/*.c)
  if(NOT GEN_ALIEN)
    message(FATAL_ERROR "src/gen is missing: run tools/regen.sh, or use -DGAME_CODE=interpreted")
  endif()
  add_library(game_code STATIC ${GEN_ALIEN} ${GEN_SLUS})
  target_include_directories(game_code PRIVATE src/runtime)
  target_compile_options(game_code PRIVATE -O2 -fno-strict-aliasing
    -Wno-unused-label -Wno-unused-variable -Wno-unused-but-set-variable)
else()
  add_library(game_code STATIC src/runtime/nogen.c)
  target_include_directories(game_code PRIVATE src/runtime)
endif()

include(cmake/overrides_table.cmake)
if(GAME_CODE_MODE STREQUAL "interpreted")
  port_generate_overrides(${CMAKE_BINARY_DIR}/overrides_table.c ON)
else()
  port_generate_overrides(${CMAKE_BINARY_DIR}/overrides_table.c OFF)
endif()

# Dear ImGui (third_party/imgui, MIT) for the launcher
set(IMGUI_DIR ${CMAKE_SOURCE_DIR}/third_party/imgui)
add_library(imgui STATIC
  ${IMGUI_DIR}/imgui.cpp
  ${IMGUI_DIR}/imgui_draw.cpp
  ${IMGUI_DIR}/imgui_tables.cpp
  ${IMGUI_DIR}/imgui_widgets.cpp
  ${IMGUI_DIR}/backends/imgui_impl_sdl3.cpp
  ${IMGUI_DIR}/backends/imgui_impl_sdlgpu3.cpp
)
target_include_directories(imgui PUBLIC ${IMGUI_DIR} ${IMGUI_DIR}/backends)
target_link_libraries(imgui PUBLIC SDL3::SDL3)
target_compile_options(imgui PRIVATE -O2 -w)

set(RUNTIME_SRC
  src/runtime/cpu.c
  src/runtime/bus.c
  src/runtime/bios.c
  src/runtime/gte.c
  src/runtime/interp.c
  src/runtime/verify.c
  src/runtime/widescreen.c
  src/runtime/automap.c
  src/runtime/native.c
  src/runtime/region.c
  src/runtime/pgxp.c
  src/runtime/disc.c
  src/runtime/memcard.c
  src/runtime/savestate.c
  src/hw/dma.c
  src/hw/timers.c
  src/hw/gpu.c
  src/hw/texrep.c
  src/hw/gpu_sw.c
  src/hw/cdrom.c
  src/hw/spu.c
  src/hw/sio.c
  src/hw/mdec.c
  src/render/gpu_hw.c
  src/render/gpu_device.c
  src/render/texpack.c
  src/platform/main.c
  src/platform/settings.c
  src/platform/discinfo.c
  src/platform/launcher.cpp
  src/platform/png.c
)

add_executable(alien ${RUNTIME_SRC} ${CMAKE_BINARY_DIR}/overrides_table.c)
if(GAME_CODE_MODE STREQUAL "interpreted")
  target_compile_definitions(alien PRIVATE GAME_CODE_INTERPRETED)
endif()
target_include_directories(alien PRIVATE src/runtime src/hw)
target_compile_options(alien PRIVATE -Wall -Wextra -Wno-unused-parameter
  -fno-strict-aliasing)
target_link_libraries(alien PRIVATE game_code imgui SDL3::SDL3 m)

# Port version: PORT_VERSION in src/platform/launcher.h, stamped by CI. A
# suffix such as "-pre" is left out of the numeric version used here.
file(STRINGS ${CMAKE_SOURCE_DIR}/src/platform/launcher.h _port_version_line
     REGEX "#define PORT_VERSION \"")
string(REGEX MATCH "\"([0-9]+)\\.([0-9]+)\\.([0-9]+)(-[0-9A-Za-z.]+)?\"" _m "${_port_version_line}")
set(PORT_VERSION_MAJOR ${CMAKE_MATCH_1})
set(PORT_VERSION_MINOR ${CMAKE_MATCH_2})
set(PORT_VERSION_PATCH ${CMAKE_MATCH_3})
set(PORT_VERSION ${PORT_VERSION_MAJOR}.${PORT_VERSION_MINOR}.${PORT_VERSION_PATCH})

if(APPLE)
  # macOS release: AlienResurrection.app (tools/build_macos_app.sh copies
  # SDL3.framework into Contents/Frameworks and signs the bundle)
  option(PORT_MACOS_BUNDLE "Build AlienResurrection.app" OFF)
  if(PORT_MACOS_BUNDLE)
    set(_icon ${CMAKE_SOURCE_DIR}/packaging/icon/AppIcon.icns)
    target_sources(alien PRIVATE ${_icon})
    set_source_files_properties(${_icon} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
    set_target_properties(alien PROPERTIES
      OUTPUT_NAME AlienResurrection
      MACOSX_BUNDLE TRUE
      MACOSX_BUNDLE_INFO_PLIST ${CMAKE_SOURCE_DIR}/packaging/macos/Info.plist.in
      MACOSX_BUNDLE_BUNDLE_NAME "Alien Resurrection"
      MACOSX_BUNDLE_GUI_IDENTIFIER io.github.DiegoDeDios.AlienReRecomp
      MACOSX_BUNDLE_ICON_FILE AppIcon
      MACOSX_BUNDLE_SHORT_VERSION_STRING ${PORT_VERSION}
      MACOSX_BUNDLE_BUNDLE_VERSION ${PORT_VERSION}
      MACOSX_BUNDLE_COPYRIGHT "Native port; requires your own Alien Resurrection (USA) disc image."
      BUILD_WITH_INSTALL_RPATH TRUE
      INSTALL_RPATH "@executable_path/../Frameworks")
  endif()
endif()

if(UNIX AND NOT APPLE)
  # Linux release (tools/build_linux.sh): the program finds the bundled
  # libSDL3.so.0 in lib/ next to it; the C++ runtime is linked statically so
  # the package does not depend on the system's libstdc++ version.
  option(PORT_LINUX_PACKAGE "Build the relocatable Linux release program" OFF)
  if(PORT_LINUX_PACKAGE)
    set_target_properties(alien PROPERTIES
      OUTPUT_NAME AlienResurrection
      BUILD_WITH_INSTALL_RPATH TRUE
      INSTALL_RPATH "$ORIGIN/lib")
    target_link_options(alien PRIVATE -static-libstdc++ -static-libgcc)
  endif()
endif()

if(WIN32)
  # Windows: GUI program (SDL provides WinMain), manifest and version info,
  # MinGW runtime linked statically so only SDL3.dll ships next to the exe,
  # and a 16 MB stack for deep guest call chains (the default is 1 MB).
  enable_language(RC)
  configure_file(src/platform/win32/alien.manifest.in ${CMAKE_BINARY_DIR}/alien.manifest @ONLY)
  configure_file(src/platform/win32/alien.rc.in ${CMAKE_BINARY_DIR}/alien.rc @ONLY)
  target_sources(alien PRIVATE src/platform/win32.c ${CMAKE_BINARY_DIR}/alien.rc)
  set_target_properties(alien PROPERTIES OUTPUT_NAME AlienResurrection WIN32_EXECUTABLE TRUE)
  target_link_options(alien PRIVATE -static -Wl,--stack,16777216)
endif()

# Texture pack next to the program (in the bundle's Resources on macOS)
if(PORT_MACOS_BUNDLE)
  set(PORT_TEXTURES_DIR $<TARGET_BUNDLE_CONTENT_DIR:alien>/Resources/textures)
else()
  set(PORT_TEXTURES_DIR $<TARGET_FILE_DIR:alien>/textures)
endif()
add_custom_command(TARGET alien POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/assets/textures ${PORT_TEXTURES_DIR})
