cmake_minimum_required(VERSION 3.29)
project(isaac LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# ccache and mold when installed. The sloppiness lets ccache cache the precompiled header's users.
find_program(CCACHE ccache)
if(CCACHE)
    set(CCACHE_LAUNCHER ${CMAKE_COMMAND} -E env CCACHE_SLOPPINESS=pch_defines,time_macros,include_file_mtime,include_file_ctime ${CCACHE})
    set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_LAUNCHER})
    set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_LAUNCHER})
endif()
find_program(MOLD mold)
if(MOLD)
    set(CMAKE_LINKER_TYPE MOLD)
endif()

if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_BUILD_TYPE Release)
endif()

# SDL: static, with only the window, OpenGL context, audio, events and gamepad pieces the game uses.
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
set(SDL_STATIC ON CACHE BOOL "" FORCE)
set(SDL_OPENGL ON CACHE BOOL "" FORCE)
set(SDL_JOYSTICK ON CACHE BOOL "" FORCE)
set(SDL_HIDAPI ON CACHE BOOL "" FORCE)
set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
set(SDL_EXAMPLES OFF CACHE BOOL "" FORCE)
foreach(OPTION RENDER GPU VULKAN CAMERA HAPTIC HIDAPI_LIBUSB POWER SENSOR DIALOG TRAY OPENGLES OFFSCREEN
        DUMMYVIDEO DISKAUDIO DUMMYAUDIO OSS JACK SNDIO KMSDRM IBUS LIBURING)
    set(SDL_${OPTION} OFF CACHE BOOL "" FORCE)
endforeach()
add_subdirectory(external/sdl EXCLUDE_FROM_ALL)

# Lua 5.3, which mods are written against: static, POSIX, no native modules.
file(GLOB LUA_SOURCES CONFIGURE_DEPENDS external/lua/l*.c)
list(REMOVE_ITEM LUA_SOURCES ${CMAKE_SOURCE_DIR}/external/lua/lua.c ${CMAKE_SOURCE_DIR}/external/lua/ltests.c)
add_library(lua STATIC ${LUA_SOURCES})
target_include_directories(lua SYSTEM PUBLIC external/lua)
target_compile_definitions(lua PRIVATE LUA_USE_POSIX)

# rapidjson, header-only, the library REPENTOGON uses, for REPENTOGON's own JSON save files.
add_library(rapidjson INTERFACE)
target_include_directories(rapidjson SYSTEM INTERFACE external/rapidjson/include)

# pugixml for the game's own XML (the install's data and anm2 files, mods' XML): static, without XPath or exceptions.
add_library(pugixml STATIC external/pugixml/src/pugixml.cpp)
target_include_directories(pugixml SYSTEM PUBLIC external/pugixml/src)
target_compile_definitions(pugixml PUBLIC PUGIXML_NO_XPATH PUGIXML_NO_EXCEPTIONS)

# Xiph's Ogg, Vorbis and Theora for the cutscene videos (.ogv): static, decoders only.
set(INCLUDE_INTTYPES_H 1)
set(INCLUDE_STDINT_H 1)
set(INCLUDE_SYS_TYPES_H 1)
set(SIZE16 int16_t)
set(USIZE16 uint16_t)
set(SIZE32 int32_t)
set(USIZE32 uint32_t)
set(SIZE64 int64_t)
set(USIZE64 uint64_t)
configure_file(external/ogg/include/ogg/config_types.h.in ${CMAKE_BINARY_DIR}/xiph/ogg/config_types.h @ONLY)
add_library(ogg STATIC external/ogg/src/bitwise.c external/ogg/src/framing.c)
target_include_directories(ogg SYSTEM PUBLIC external/ogg/include ${CMAKE_BINARY_DIR}/xiph)

file(GLOB VORBIS_SOURCES CONFIGURE_DEPENDS external/vorbis/lib/*.c)
foreach(TOOL barkmel psytune tone vorbisenc)
    list(REMOVE_ITEM VORBIS_SOURCES ${CMAKE_SOURCE_DIR}/external/vorbis/lib/${TOOL}.c)
endforeach()
add_library(vorbis STATIC ${VORBIS_SOURCES})
target_include_directories(vorbis SYSTEM PUBLIC external/vorbis/include PRIVATE external/vorbis/lib)
target_link_libraries(vorbis PUBLIC ogg)

set(THEORA_DECODER apiwrapper bitpack decapiwrapper decinfo decode dequant fragment huffdec idct info internal quant state)
list(TRANSFORM THEORA_DECODER PREPEND external/theora/lib/)
list(TRANSFORM THEORA_DECODER APPEND .c)
add_library(theora STATIC ${THEORA_DECODER})
target_include_directories(theora SYSTEM PUBLIC external/theora/include)
target_link_libraries(theora PUBLIC ogg)
target_compile_options(theora PRIVATE -w)

# The developer overlay (src/overlay/) in place of the drop-down console, on Dear ImGui's docking branch: its core,
# the std::string widgets and the SDL3 and OpenGL 3 backends, static. OFF builds the game without either.
option(ISAAC_OVERLAY "Build the developer overlay in place of the drop-down console" ON)
if(ISAAC_OVERLAY)
    set(IMGUI_SOURCES imgui imgui_draw imgui_tables imgui_widgets misc/cpp/imgui_stdlib backends/imgui_impl_sdl3
        backends/imgui_impl_opengl3)
    list(TRANSFORM IMGUI_SOURCES PREPEND external/imgui/)
    list(TRANSFORM IMGUI_SOURCES APPEND .cpp)
    add_library(imgui STATIC ${IMGUI_SOURCES})
    target_include_directories(imgui SYSTEM PUBLIC external/imgui external/imgui/backends external/imgui/misc/cpp)
    target_link_libraries(imgui PRIVATE SDL3::SDL3-static ${CMAKE_DL_LIBS})
    target_compile_options(imgui PRIVATE -w)
endif()

# REPENTOGON's modding API built in natively (src/repentogon/, its scripts in assets/repentogon/): inert without mods,
# with every change to the vanilla game behind its options and the vanilla parity switch. OFF builds the vanilla game.
option(ISAAC_REPENTOGON "Build REPENTOGON's modding API into the game" ON)

# The game: every .cpp under src/, sorted into subsystem folders and including each other by path from src/.
# src/generated/ holds the frozen decompiler output: classes.h declares every original class, and stubs.cpp defines
# each original function src/ does not as a stub that stops the game where it is reached.
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.cpp)
if(NOT ISAAC_OVERLAY)
    list(FILTER SOURCES EXCLUDE REGEX "/src/overlay/")
endif()
if(NOT ISAAC_REPENTOGON)
    list(FILTER SOURCES EXCLUDE REGEX "/src/repentogon/")
endif()
add_executable(isaac ${SOURCES})

# The game goes alone into build/<platform>/, beside links to the resources it reads, the mods folder and assets/.
# ISAAC_RESOURCES picks the resources: the import tool's folder, or an install's resources folder.
set(ISAAC_RESOURCES ${CMAKE_SOURCE_DIR}/resources CACHE PATH "The resources folder the game reads")
string(TOLOWER ${CMAKE_SYSTEM_NAME} PLATFORM)
set(GAME_DIRECTORY ${CMAKE_BINARY_DIR}/${PLATFORM})
set_target_properties(isaac PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${GAME_DIRECTORY})
file(MAKE_DIRECTORY ${GAME_DIRECTORY})
foreach(LINK resources mods assets)
    set(TARGET ${CMAKE_SOURCE_DIR}/${LINK})
    if(LINK STREQUAL "resources")
        set(TARGET ${ISAAC_RESOURCES})
    endif()
    file(REMOVE ${GAME_DIRECTORY}/${LINK})
    file(CREATE_LINK ${TARGET} ${GAME_DIRECTORY}/${LINK} SYMBOLIC)
endforeach()
target_include_directories(isaac PRIVATE src)
target_include_directories(isaac SYSTEM PRIVATE src/generated external/stb)
target_link_libraries(isaac PRIVATE SDL3::SDL3-static lua rapidjson pugixml theora vorbis ogg)

# The standard headers and classes.h make up most of every file's parse, so they are compiled once.
# ISAAC_UNITY also merges the files into batches: faster clean builds, slower incremental ones.
target_precompile_headers(isaac PRIVATE <algorithm> <array> <cmath> <cstdint> <deque> <functional> <list> <map> <memory>
    <optional> <set> <span> <string> <string_view> <unordered_map> <vector> src/generated/classes.h)
set_source_files_properties(src/engine/kage_sound.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON SKIP_UNITY_BUILD_INCLUSION ON)
option(ISAAC_UNITY "Build the game in unity batches" OFF)
set_target_properties(isaac PROPERTIES UNITY_BUILD ${ISAAC_UNITY} UNITY_BUILD_BATCH_SIZE 24)
if(ISAAC_OVERLAY)
    target_compile_definitions(isaac PRIVATE ISAAC_OVERLAY)
    target_link_libraries(isaac PRIVATE imgui)
endif()
if(ISAAC_REPENTOGON)
    target_compile_definitions(isaac PRIVATE ISAAC_REPENTOGON)
endif()
