cmake_minimum_required(VERSION 3.20)

project(CrystalDefendersRecomp C CXX)

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

if(NOT WIN32)
    # See the header: keeps the program startable on systems a little older than the build machine.
    add_compile_options(-include ${CMAKE_CURRENT_SOURCE_DIR}/runtime/linux/glibc_compat.h)
endif()

set(XENONRECOMP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../tools/XenonRecomp-main)
set(THIRDPARTY_ROOT ${XENONRECOMP_ROOT}/thirdparty)

# XEX loading (decryption + LZX decompression) comes from XenonUtils.
add_subdirectory(${THIRDPARTY_ROOT}/disasm disasm)
add_subdirectory(${XENONRECOMP_ROOT}/XenonUtils XenonUtils)

# Recompiled PowerPC code, generated by XenonRecomp from config/crystal_defenders.toml.
file(GLOB PPC_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ppc/*.cpp)
add_library(ppc OBJECT ${PPC_SOURCES})
target_include_directories(ppc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/ppc ${CMAKE_CURRENT_SOURCE_DIR}/runtime ${THIRDPARTY_ROOT}/simde)
# Pulls runtime/ppc_detail.h into the generated ppc_config.h.
target_compile_definitions(ppc PUBLIC PPC_INCLUDE_DETAIL)
target_compile_options(ppc
    PUBLIC -march=x86-64-v2
    # Line tables let llvm-symbolizer map a crash address back to the generated
    # C++ line, whose comment is the original PowerPC instruction.
    PRIVATE -O2 -fno-strict-aliasing -w -gline-tables-only)

# Everything that is the same on every system.
set(RUNTIME_SOURCES
    runtime/main.cpp
    runtime/achievements.cpp
    runtime/png.cpp
    runtime/config.cpp
    runtime/kernel.cpp
    runtime/kernel_objects.cpp
    runtime/kernel_io.cpp
    runtime/game_state.cpp
    runtime/video.cpp
    runtime/gpu.cpp
    runtime/audio.cpp
    runtime/xam.cpp
    runtime/xam_content.cpp
    runtime/setup.cpp
    runtime/launcher.cpp
    runtime/translations.cpp
    runtime/imports.cpp
    runtime/imports.gen.cpp)

# DarkLauncher (kit/launcher, copied from the DarkLauncher kit): the window, the settings screen, the
# controls and the sound output of the game; the game's side is runtime/darklauncher.cpp.
option(CD_DARKLAUNCHER "Run the game inside DarkLauncher (OFF: its own window and start screen)" ON)
set(KIT ${CMAKE_CURRENT_SOURCE_DIR}/../kit/launcher)
# The project's pictures for DarkLauncher (resources/art), built into the program by runtime/art.cpp
set(ART_DIR ${CMAKE_CURRENT_SOURCE_DIR}/resources/art)
file(GLOB_RECURSE ART_FILES CONFIGURE_DEPENDS ${ART_DIR}/*.png)
set_source_files_properties(runtime/art.cpp PROPERTIES COMPILE_DEFINITIONS "CD_ART_DIR=\"${ART_DIR}\"" OBJECT_DEPENDS "${ART_FILES}")

if(WIN32)
    enable_language(RC)
    set(PLATFORM_SOURCES runtime/renderer.cpp resources/crystal_defenders.rc)
    if(CD_DARKLAUNCHER)
        file(GLOB DARKLAUNCHER_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../kit/launcher/*.c)
        set_source_files_properties(${DARKLAUNCHER_SOURCES} PROPERTIES COMPILE_OPTIONS "-std=c11;-Wno-missing-braces")
        list(APPEND PLATFORM_SOURCES runtime/darklauncher.cpp runtime/art.cpp runtime/debug_script.cpp ${DARKLAUNCHER_SOURCES})
    endif()
    add_executable(crystal_defenders ${RUNTIME_SOURCES} ${PLATFORM_SOURCES})
    if(CD_DARKLAUNCHER)
        target_compile_definitions(crystal_defenders PRIVATE CD_DARKLAUNCHER)
        # dl.h, and the project's project.h at the root of the repository
        target_include_directories(crystal_defenders PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../kit/launcher ${CMAKE_CURRENT_SOURCE_DIR}/..)
        target_link_libraries(crystal_defenders PRIVATE dwmapi gdi32 user32 shell32)
    endif()

    # FFmpeg (LGPL, shared build) decodes the game's XMA audio.
    set(FFMPEG_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../tools/ffmpeg/ffmpeg-n8.1-latest-win64-lgpl-shared-8.1)
    target_include_directories(crystal_defenders PRIVATE runtime ${FFMPEG_ROOT}/include)
    target_link_libraries(crystal_defenders PRIVATE ppc XenonUtils winmm d3d11 dxgi dxguid msimg32
        ${FFMPEG_ROOT}/lib/libavcodec.dll.a ${FFMPEG_ROOT}/lib/libavutil.dll.a)
    # -mwindows: a windowed program, so no console window opens next to the game.
    target_link_options(crystal_defenders PRIVATE -static -mwindows)

    # The FFmpeg DLLs must sit next to the executable.
    foreach(FFMPEG_DLL avcodec-62.dll avutil-60.dll swresample-6.dll)
        add_custom_command(TARGET crystal_defenders POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different ${FFMPEG_ROOT}/bin/${FFMPEG_DLL} $<TARGET_FILE_DIR:crystal_defenders>)
    endforeach()
else()
    # Linux (Steam Deck): the runtime is written against the Windows API, of
    # which runtime/linux provides the small part it uses, over OpenGL; and
    # SDL2 for the classic build, or SDL3 under DarkLauncher (the two cannot be
    # in one program: their functions have the same names).
    find_package(PkgConfig REQUIRED)
    if(CD_DARKLAUNCHER)
        pkg_check_modules(SDL REQUIRED IMPORTED_TARGET sdl3)
    else()
        pkg_check_modules(SDL REQUIRED IMPORTED_TARGET sdl2)
    endif()
    pkg_check_modules(TEXT REQUIRED IMPORTED_TARGET freetype2 fontconfig)
    # CD_FFMPEG_ROOT: a static FFmpeg built with only the XMA decoders, for a
    # program that does not depend on the system's FFmpeg version (releases).
    # Without it the system's shared FFmpeg is used.
    set(CD_FFMPEG_ROOT "" CACHE PATH "Prefix of a static FFmpeg build")
    if(CD_FFMPEG_ROOT)
        add_library(cd_ffmpeg INTERFACE)
        target_include_directories(cd_ffmpeg INTERFACE ${CD_FFMPEG_ROOT}/include)
        target_link_libraries(cd_ffmpeg INTERFACE ${CD_FFMPEG_ROOT}/lib/libavcodec.a ${CD_FFMPEG_ROOT}/lib/libavutil.a m)
    else()
        pkg_check_modules(FFMPEG REQUIRED IMPORTED_TARGET libavcodec libavutil)
        add_library(cd_ffmpeg INTERFACE)
        target_link_libraries(cd_ffmpeg INTERFACE PkgConfig::FFMPEG)
    endif()
    find_package(OpenGL REQUIRED COMPONENTS OpenGL EGL)
    find_package(Threads REQUIRED)

    set(PLATFORM_SOURCES runtime/linux/win32.cpp runtime/linux/overlay.cpp runtime/linux/renderer_sdl.cpp)
    if(CD_DARKLAUNCHER)
        # DarkLauncher's shared code (but its Direct3D and HID parts) on what kit/launcher/linux gives
        # it of Windows. A library of its own: it has its own windows.h, not the runtime's.
        add_library(darklauncher STATIC
            ${KIT}/dl_app.c ${KIT}/dl_menu.c ${KIT}/dl_draw.c ${KIT}/dl_text.c ${KIT}/dl_saves.c ${KIT}/dl_achieve.c
            ${KIT}/dl_packs.c ${KIT}/dl_input.c ${KIT}/linux/dl_gdi.c ${KIT}/linux/dl_icons.c ${KIT}/linux/dl_win32.c
            ${KIT}/linux/dl_pads.c ${KIT}/linux/dl_gl.c)
        target_include_directories(darklauncher PRIVATE ${KIT}/linux/include ${KIT} ${CMAKE_CURRENT_SOURCE_DIR}/..)
        target_compile_options(darklauncher PRIVATE -std=gnu11 -Wno-missing-braces -Wno-unused-parameter)
        target_link_libraries(darklauncher PUBLIC PkgConfig::SDL PkgConfig::TEXT OpenGL::GL m)
        list(APPEND PLATFORM_SOURCES runtime/darklauncher.cpp runtime/art.cpp runtime/debug_script.cpp)
    endif()
    add_executable(crystal_defenders ${RUNTIME_SOURCES} ${PLATFORM_SOURCES})
    target_include_directories(crystal_defenders BEFORE PRIVATE runtime/linux runtime)
    target_link_libraries(crystal_defenders PRIVATE ppc XenonUtils PkgConfig::SDL PkgConfig::TEXT cd_ffmpeg OpenGL::GL Threads::Threads)
    if(CD_DARKLAUNCHER)
        target_compile_definitions(crystal_defenders PRIVATE CD_DARKLAUNCHER)
        # dl.h, and the project's project.h at the root of the repository
        target_include_directories(crystal_defenders PRIVATE ${KIT} ${CMAKE_CURRENT_SOURCE_DIR}/..)
        target_link_libraries(crystal_defenders PRIVATE darklauncher OpenGL::EGL)
        # SDL3, which few systems have yet, is looked for in the program's lib folder first
        target_link_options(crystal_defenders PRIVATE "-Wl,-rpath,$ORIGIN/lib")
    endif()
    # The C++ library is built in, so the program only needs the system's C library, SDL and OpenGL.
    target_link_options(crystal_defenders PRIVATE -static-libstdc++ -static-libgcc)
endif()
