cmake_minimum_required(VERSION 3.25)
project(abyssport VERSION 0.1.2 LANGUAGES C CXX RC)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT MINGW OR NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" OR NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
    message(FATAL_ERROR "abyssport is built with llvm-mingw for 64-bit Windows: use the 'pc-x64' preset "
                        "(tools/get_toolchain.py fetches the compiler). The Vita's 4 GiB of address space is one "
                        "block of the process's memory, and the game's recompiled code is GNU C.")
endif()

# Optimised without -DNDEBUG, as Project Nativ builds: the runtime's own checks stay in.
set(CMAKE_C_FLAGS_RELEASE "-O2")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
# One program, nothing beside it but FFmpeg's DLLs: the C and C++ libraries and winpthreads are linked in.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")

# The game's code, recompiled from the player's own copy: never in the repository (game/ is ignored).
set(ABYSS_RECOMP_DIR "${CMAKE_SOURCE_DIR}/game/library/decrypted/PCSA00029/recomp" CACHE PATH
    "The recompiler's output for PCSA00029 1.00 (tools/make_game.py): game*.c, the libraries' mod_*.c, the .img images")
file(GLOB ABYSS_GAME_SOURCES CONFIGURE_DEPENDS "${ABYSS_RECOMP_DIR}/game.c" "${ABYSS_RECOMP_DIR}/game_part*.c"
                                               "${ABYSS_RECOMP_DIR}/mod_*.c")
# Without it only the launcher and the runtime are built: what the release ships (tools/make_package.py), and all the
# CI can build, which has no copy of the game.
if(NOT ABYSS_GAME_SOURCES)
    message(WARNING "The game's recompiled code is missing: no game.c in ${ABYSS_RECOMP_DIR}, so abyss.exe and its test "
                    "are left out. It is made from your own copy of the game by tools/make_game.py; set "
                    "ABYSS_RECOMP_DIR if it is somewhere else.")
endif()

# The POSIX names Windows spells differently, in front of every C file of ours (src/platform/win32_compat.h):
# the runtime and the recompiled code are written for POSIX. 64-bit file offsets; mingw-w64's localtime_r.
set(ABYSS_COMPAT -D_FILE_OFFSET_BITS=64 -include "${CMAKE_SOURCE_DIR}/src/platform/win32_compat.h")

# The game: 32,000 functions of ARM code as C, 425 MB of it, and Sony's libraries recompiled the same way: libc and
# libfios2 from the game, libgxm, libgpu and libscemp4 from the firmware. An object library: each library registers
# its tables from a constructor nothing refers to, which a static library would leave out of the program.
if(ABYSS_GAME_SOURCES)
add_library(abyssgame OBJECT ${ABYSS_GAME_SOURCES})
target_include_directories(abyssgame PUBLIC src/recomp)
# No fused multiply-adds: the Vita's VFP rounds the product before it adds (rt.h). No warnings: generated code.
target_compile_options(abyssgame PRIVATE -ffp-contract=off -w ${ABYSS_COMPAT} -D_POSIX_THREAD_SAFE_FUNCTIONS)
endif()

include(FetchContent)

# zlib (zlib licence): the game's archives (PSARC) are deflated. Downloaded at configure time, pinned by version
# and hash, linked statically.
FetchContent_Declare(zlib
    URL https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz
    URL_HASH SHA256=9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23
)
set(ZLIB_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(zlib)

# SDL3 (zlib licence): the window, the GPU API the renderer draws through, audio output, controllers and sensors.
# Downloaded at configure time, pinned by version and hash, linked statically.
FetchContent_Declare(SDL3
    URL https://github.com/libsdl-org/SDL/releases/download/release-3.4.20/SDL3-3.4.20.tar.gz
    URL_HASH SHA256=f3bb06f55bed9557227a700af2ea62e49497ebdf58037732a731a5fb3ef8581f
)
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
set(SDL_STATIC ON CACHE BOOL "" FORCE)
set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
set(SDL_INSTALL OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(SDL3)

# FFmpeg (LGPL 2.1 or later, this build of it): the H.264 decoder behind the game's films. A ready-made build for
# Windows with shared libraries, downloaded at configure time, pinned by release and hash; only libavcodec and
# libavutil are used, and their DLLs are copied beside the program.
FetchContent_Declare(ffmpeg
    URL https://github.com/BtbN/FFmpeg-Builds/releases/download/autobuild-2026-10-31-12-59/ffmpeg-n8.1.3-27-g8dca1586f4-win64-lgpl-shared-8.1.zip
    URL_HASH SHA256=5cef7bc48e513f3cee7c881e8b2999d835541edb1846c3e89443a67f8f8556ab
)
FetchContent_MakeAvailable(ffmpeg)

# Dear ImGui (MIT, third_party/imgui): the in-game overlay's interface, drawn through SDL's GPU API.
add_library(imgui STATIC
    third_party/imgui/imgui.cpp
    third_party/imgui/imgui_draw.cpp
    third_party/imgui/imgui_tables.cpp
    third_party/imgui/imgui_widgets.cpp
    third_party/imgui/backends/imgui_impl_sdl3.cpp
    third_party/imgui/backends/imgui_impl_sdlgpu3.cpp
)
target_include_directories(imgui PUBLIC third_party/imgui third_party/imgui/backends)
target_compile_options(imgui PRIVATE -w)
target_link_libraries(imgui PUBLIC SDL3::SDL3-static)

# LibAtrac9 (MIT, third_party/libatrac9): the decoder of the game's ATRAC9 audio.
file(GLOB ABYSS_ATRAC9_SOURCES CONFIGURE_DEPENDS third_party/libatrac9/*.c)
add_library(atrac9 STATIC ${ABYSS_ATRAC9_SOURCES})
target_compile_options(atrac9 PRIVATE -w)

# The touch card's input icons (third_party/kenney-prompts, CC0) as bytes the overlay includes.
file(READ third_party/kenney-prompts/prompts.png ABYSS_PROMPTS HEX)
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," ABYSS_PROMPTS "${ABYSS_PROMPTS}")
file(WRITE "${CMAKE_BINARY_DIR}/generated/prompts_png.h" "static const unsigned char prompts_png[] = {${ABYSS_PROMPTS}};\n")
# The version res/utf8.rc writes into both programs' version information.
configure_file(res/version.h.in "${CMAKE_BINARY_DIR}/generated/abyss_version.h" @ONLY)

# The program: the host of the recompiled code and the Vita's system: kernel, files, graphics, audio, input.
# The runtime is adapted from Project Nativ.
set(ABYSS_C_SOURCES
    src/audio/ngs.c
    src/core/apputil.c
    src/core/fios.c
    src/core/hle.c
    src/core/hooks.c
    src/core/io.c
    src/core/kernel.c
    src/core/libc.c
    src/core/main.c
    src/core/metrics.c
    src/core/net.c
    src/core/psarc.c
    src/core/settings.c
    src/core/sysmem.c
    src/debug/gfx_capture.c
    src/debug/gfx_coverage.c
    src/debug/memscan.c
    src/debug/stallprof.c
    src/debug/waitstats.c
    src/game/hooks.c
    src/gfx/display.c
    src/gfx/fsr/fsr.c
    src/gfx/gfx_dispatch.c
    src/gfx/gfx_gpu.c
    src/gfx/gfx_soft.c
    src/gfx/gpu.c
    src/gfx/gpulock.c
    src/gfx/guestwatch.c
    src/gfx/gxm_sony.c
    src/gfx/gxp_index.c
    src/gfx/post.c
    src/gfx/ssao.c
    src/gfx/usse.c
    src/gfx/usse_spirv.c
    src/input/gesture.c
    src/input/motion.c
    src/media/camera.c
    src/media/jpeg.c
    src/media/video.c
    src/platform/platform_win32.c
    src/platform/win32_compat.c
    src/ui/keycap.c
    src/ui/window.c
)
# The runtime is a library of its own: the release ships it compiled, and the player's PC links the game's code to it
# (tools/build_game.py) with the same compiler. Linked whole: some of its files register themselves from constructors.
# The launcher's pages are the game's menu too (F1, src/launcher/launcher_game.h): launcher.cpp built again, as the
# launcher itself is (without win32_compat.h, whose names clash with <filesystem>), its objects inside the runtime.
add_library(abyssmenu OBJECT src/launcher/launcher_in_game.cpp)
target_include_directories(abyssmenu PRIVATE src/core src/launcher "${CMAKE_SOURCE_DIR}")
target_compile_options(abyssmenu PRIVATE -w)
target_compile_definitions(abyssmenu PRIVATE ABYSS_VERSION="${PROJECT_VERSION}")
target_link_libraries(abyssmenu PRIVATE imgui SDL3::SDL3-static)
add_library(abyssrt STATIC ${ABYSS_C_SOURCES} src/ui/overlay.cpp $<TARGET_OBJECTS:abyssmenu> res/utf8.rc)
target_include_directories(abyssrt PRIVATE
    src/audio src/core src/debug src/gfx src/gfx/fsr src/input src/launcher src/media src/platform src/ui src/recomp
    "${CMAKE_SOURCE_DIR}"                       # ("third_party/...")
    "${CMAKE_BINARY_DIR}/generated"
    "${zlib_SOURCE_DIR}" "${zlib_BINARY_DIR}"
    "${ffmpeg_SOURCE_DIR}/include"
)
# No warnings for now: the adapted code is kept as it is upstream. (_POSIX_THREAD_SAFE_FUNCTIONS: C only -- with it
# mingw-w64 defines the localtime_r that Dear ImGui's headers also define.)
target_compile_options(abyssrt PRIVATE -w ${ABYSS_COMPAT})
set_source_files_properties(${ABYSS_C_SOURCES} PROPERTIES COMPILE_DEFINITIONS _POSIX_THREAD_SAFE_FUNCTIONS)
# The settings the game plays with, as Project Nativ's pack for it sets them (tools/PCSA00029.toml RUN_ENV).
set_property(SOURCE src/core/main.c APPEND PROPERTY COMPILE_DEFINITIONS
    "RECOMP_BUILTIN_ENV=\"RECOMP_GAME_OPTIONS=fov,view_distance RECOMP_SRGB=1 RECOMP_ASPECT_HOOK=1 RECOMP_HUD_DRAWS=2d RECOMP_TARGET_SCALE=720x408:1.3333 RECOMP_CAMERA_LAMP=1 RECOMP_FSR_SUPPORTED=1\"")
# The films: sceAvcdec through libavcodec.
set_property(SOURCE src/media/video.c APPEND PROPERTY COMPILE_DEFINITIONS RECOMP_AVCODEC)
target_link_libraries(abyssrt PUBLIC atrac9 imgui zlibstatic SDL3::SDL3-static
    "${ffmpeg_SOURCE_DIR}/lib/libavcodec.dll.a" "${ffmpeg_SOURCE_DIR}/lib/libavutil.dll.a"
    pthread winmm ws2_32 bcrypt)
if(ABYSS_GAME_SOURCES)
# The program: the game's code linked to the runtime, as tools/build_game.py links it on the player's PC.
add_executable(abyss)
target_link_libraries(abyss PRIVATE abyssgame "$<LINK_LIBRARY:WHOLE_ARCHIVE,abyssrt>")
file(GLOB ABYSS_FFMPEG_DLLS "${ffmpeg_SOURCE_DIR}/bin/avcodec-*.dll" "${ffmpeg_SOURCE_DIR}/bin/avutil-*.dll"
                            "${ffmpeg_SOURCE_DIR}/bin/swresample-*.dll")
add_custom_command(TARGET abyss POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ABYSS_FFMPEG_DLLS} "$<TARGET_FILE_DIR:abyss>"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different "${ffmpeg_SOURCE_DIR}/LICENSE.txt" "$<TARGET_FILE_DIR:abyss>/FFmpeg-LICENSE.txt")

# The check: the game starts and draws its first frames -- headless, on the software renderer, which is the reference
# (81009ffc: module_start of 1.00). The game loads its front end first.
enable_testing()
add_test(NAME abyss_first_frames COMMAND abyss game.img 81009ffc 100000000000 WORKING_DIRECTORY "${ABYSS_RECOMP_DIR}")
set_tests_properties(abyss_first_frames PROPERTIES
    ENVIRONMENT "RECOMP_MODULES=libc.img,libgxm_es4.img,libgpu_es4.img,libfios2.img,libscemp4.img;RECOMP_GFX=soft;RECOMP_SHADERS=interp;RECOMP_NO_MSPACE=1;RECOMP_SRGB=1;RECOMP_TITLE=PCSA00029;RECOMP_HEADLESS=1;RECOMP_FS=fs;RECOMP_LOG=0;RECOMP_SHOT=3;RECOMP_SHOT_LAST=3"
    PASS_REGULAR_EXPRESSION "frame 3 shot: the last one asked for"
    TIMEOUT 300)
endif()

# The launcher: the first screen (the game ready or not, settings, controls, how the console's touch and tilt are
# played on a PC); Play starts abyss. In the look of mc3port's and sm2port's launchers, on SDL3. A window program:
# no console behind it.
add_executable(abyssport WIN32 src/launcher/launcher.cpp res/utf8.rc src/core/settings.c src/platform/win32_compat.c src/platform/platform_win32.c)
target_include_directories(abyssport PRIVATE src/core src/platform "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}/generated")
target_compile_options(abyssport PRIVATE -w)
# (ABYSS_RC_LAUNCHER: res/utf8.rc's version information names the launcher, not the game)
target_compile_definitions(abyssport PRIVATE ABYSS_VERSION="${PROJECT_VERSION}" ABYSS_RC_LAUNCHER)
# Stripped: the debug sections the static C and C++ libraries bring (700 KB, odd section names /4, /18...) are no use
# to a player and count against the launcher with antivirus heuristics.
target_link_options(abyssport PRIVATE -s)
# settings.c is the runtime's, written for POSIX: the Windows layer in front of it here too.
set_property(SOURCE src/core/settings.c src/platform/win32_compat.c src/platform/platform_win32.c APPEND PROPERTY COMPILE_OPTIONS ${ABYSS_COMPAT})
target_link_libraries(abyssport PRIVATE imgui SDL3::SDL3-static pthread winhttp)
# Barlow Semi Condensed (SIL Open Font License, third_party/fonts): the launcher's letters, beside the program.
add_custom_command(TARGET abyssport POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_SOURCE_DIR}/third_party/fonts" "$<TARGET_FILE_DIR:abyssport>/fonts"
    # abyssport's own artwork (tools/art/make_art.py): the backgrounds before there is a copy of the game, the emblem
    COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_SOURCE_DIR}/res/launcher" "$<TARGET_FILE_DIR:abyssport>/art")
set_property(SOURCE res/utf8.rc APPEND PROPERTY OBJECT_DEPENDS "${CMAKE_SOURCE_DIR}/res/utf8.manifest;${CMAKE_SOURCE_DIR}/res/abyssport.ico;${CMAKE_BINARY_DIR}/generated/abyss_version.h")
