cmake_minimum_required(VERSION 3.20)
project(ISS2000Recompiled LANGUAGES C CXX)

set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if (NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    message(WARNING "Clang is recommended (the recompiled output and RT64 are tested with it).")
endif()

# clang-cl (Windows) takes GCC-style options that have no cl.exe spelling through /clang:.
if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
    set(ISS_ARCH_FLAGS /clang:-march=nehalem)
else()
    set(ISS_ARCH_FLAGS -march=nehalem)
endif()

# ---- Dependencies -----------------------------------------------------------------------------
set(RT64_STATIC TRUE)
if (NOT WIN32)
    # File dialogs through xdg-desktop-portal (D-Bus) instead of GTK3, so the AppImage doesn't need GTK.
    set(NFD_PORTAL ON CACHE BOOL "" FORCE)
    set(RT64_SDL_WINDOW_VULKAN TRUE)
endif()
add_compile_definitions(HLSL_CPU)
add_subdirectory(${CMAKE_SOURCE_DIR}/ext/rt64 ${CMAKE_BINARY_DIR}/rt64)
target_include_directories(rt64 PRIVATE ${CMAKE_BINARY_DIR}/rt64/src)

add_subdirectory(${CMAKE_SOURCE_DIR}/ext/N64ModernRuntime ${CMAKE_BINARY_DIR}/N64ModernRuntime)

if (WIN32)
    # The SDL2 VC development package bundled with RT64 (the same one RT64 links). Its headers have no SDL2/
    # directory, which the sources include them with, so they are copied under one in the build tree.
    set(SDL2_DIR ${CMAKE_SOURCE_DIR}/ext/rt64/src/contrib/mupen64plus-win32-deps/SDL2-2.26.3/cmake)
    find_package(SDL2 REQUIRED CONFIG)
    file(COPY ${SDL2_INCLUDE_DIRS}/ DESTINATION ${CMAKE_BINARY_DIR}/sdl2-include/SDL2)
    set(SDL2_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/sdl2-include ${SDL2_INCLUDE_DIRS})
else()
    find_package(SDL2 REQUIRED)
endif()
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)

set(RUNTIME_INCLUDES
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_SOURCE_DIR}/ext/N64ModernRuntime/ultramodern/include
    ${CMAKE_SOURCE_DIR}/ext/N64ModernRuntime/librecomp/include
    ${CMAKE_SOURCE_DIR}/ext/N64ModernRuntime/N64Recomp/include
)

# ---- Recompiled game code (generated by N64Recomp, one output directory per ROM version) ---------
# Every version uses the same function names (func_80000400, ...), so each one is built as its own static
# library and all its global symbols get the version id as prefix afterwards (tools/prefix_syms.py):
# us_func_80000400, ende_recomp_entrypoint, ... src/iss_version.cpp refers to them by those names.
find_package(Python3 REQUIRED COMPONENTS Interpreter)
# CMake leaves CMAKE_NM / CMAKE_OBJCOPY empty for clang-cl; use the LLVM tools next to the compiler.
get_filename_component(ISS_COMPILER_DIR ${CMAKE_CXX_COMPILER} DIRECTORY)
if (CMAKE_NM)
    set(ISS_NM ${CMAKE_NM})
else()
    find_program(ISS_NM NAMES llvm-nm nm HINTS ${ISS_COMPILER_DIR} REQUIRED)
endif()
if (CMAKE_OBJCOPY)
    set(ISS_OBJCOPY ${CMAKE_OBJCOPY})
else()
    find_program(ISS_OBJCOPY NAMES llvm-objcopy objcopy HINTS ${ISS_COMPILER_DIR} REQUIRED)
endif()
# ISS_TRACE=ON builds the US version from recomp/RecompiledFuncsTrace (generated with recomp/iss2000.trace.toml),
# which counts calls per function for debugging. Use a separate build directory for it.
option(ISS_TRACE "Build the call-counting trace variant" OFF)
function(iss_add_recompiled version funcs_dir)
    set(lib RecompiledFuncs_${version})
    file(GLOB srcs ${funcs_dir}/*.c ${funcs_dir}/*.cpp)
    add_library(${lib} STATIC ${srcs} ${CMAKE_SOURCE_DIR}/src/register_overlays.cpp)
    target_compile_options(${lib} PRIVATE
        -fno-strict-aliasing
        -Wno-unused-variable
        -Wno-unused-but-set-variable
        -Wno-implicit-function-declaration
    )
    target_include_directories(${lib} PRIVATE ${RUNTIME_INCLUDES} ${funcs_dir})
    add_custom_command(TARGET ${lib} POST_BUILD
        COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/prefix_syms.py $<TARGET_FILE:${lib}> ${version}_
            ${ISS_NM} ${ISS_OBJCOPY}
        # Game calls to osGetCount go to the host (src/iss_ultra_replacements.cpp): deterministic clock online.
        COMMAND ${ISS_OBJCOPY} --redefine-sym osGetCount_recomp=iss_game_osGetCount_recomp $<TARGET_FILE:${lib}>
        VERBATIM
    )
endfunction()
if (ISS_TRACE)
    iss_add_recompiled(us ${CMAKE_SOURCE_DIR}/recomp/RecompiledFuncsTrace)
else()
    iss_add_recompiled(us ${CMAKE_SOURCE_DIR}/recomp/RecompiledFuncs)
endif()
iss_add_recompiled(ende ${CMAKE_SOURCE_DIR}/recomp/RecompiledFuncs_ende)
iss_add_recompiled(frit ${CMAKE_SOURCE_DIR}/recomp/RecompiledFuncs_frit)

# ---- Recompiled RSP microcode (generated by RSPRecomp into recomp/rsp) ------------------------
add_library(RecompiledRSP STATIC ${CMAKE_SOURCE_DIR}/recomp/rsp/aspMain.cpp)
target_compile_options(RecompiledRSP PRIVATE ${ISS_ARCH_FLAGS} -fno-strict-aliasing -Wno-unused-variable)
target_include_directories(RecompiledRSP PRIVATE ${RUNTIME_INCLUDES})

# ---- Launcher assets (logo, font, icon) embedded as C arrays ---------------------------------
set(LAUNCHER_ASSETS_H ${CMAKE_BINARY_DIR}/gen/generated/launcher_assets.h)
add_custom_command(
    OUTPUT ${LAUNCHER_ASSETS_H}
    COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/gen/generated
    COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/embed.py ${LAUNCHER_ASSETS_H}
        launcher_logo_png=${CMAKE_SOURCE_DIR}/assets/launcher_logo.png
        launcher_font_ttf=${CMAKE_SOURCE_DIR}/assets/DejaVuSans-Bold.ttf
        app_icon_png=${CMAKE_SOURCE_DIR}/assets/iss2000recomp.png
    DEPENDS ${CMAKE_SOURCE_DIR}/tools/embed.py ${CMAKE_SOURCE_DIR}/assets/launcher_logo.png
        ${CMAKE_SOURCE_DIR}/assets/DejaVuSans-Bold.ttf ${CMAKE_SOURCE_DIR}/assets/iss2000recomp.png
)

# ---- Host executable --------------------------------------------------------------------------
add_executable(ISS2000Recompiled
    src/main.cpp
    src/iss_version.cpp
    src/iss_overlays.cpp
    src/iss_ultra_replacements.cpp
    src/iss_widescreen.cpp
    src/iss_controller_pak.cpp
    src/rt64_render_context.cpp
    src/launcher.cpp
    src/iss_netplay.cpp
    ${LAUNCHER_ASSETS_H}
)
target_compile_options(ISS2000Recompiled PRIVATE ${ISS_ARCH_FLAGS} -fno-strict-aliasing)
if (ISS_TRACE)
    target_compile_definitions(ISS2000Recompiled PRIVATE ISS_TRACE_BUILD)
    target_sources(ISS2000Recompiled PRIVATE src/iss_trace.cpp)
endif()
target_include_directories(ISS2000Recompiled PRIVATE
    ${RUNTIME_INCLUDES}
    ${CMAKE_BINARY_DIR}/gen
    ${SDL2_INCLUDE_DIRS}
    ${CMAKE_SOURCE_DIR}/ext/rt64/src
    ${CMAKE_SOURCE_DIR}/ext/rt64/src/contrib
    ${CMAKE_SOURCE_DIR}/ext/rt64/src/contrib/hlslpp/include
    ${CMAKE_SOURCE_DIR}/ext/rt64/src/contrib/nativefiledialog-extended/src/include
    ${CMAKE_SOURCE_DIR}/ext/rt64/src/contrib/dxc/inc
    ${CMAKE_SOURCE_DIR}/ext/rt64/src/rhi
    ${CMAKE_SOURCE_DIR}/ext/rt64/src/render
    ${CMAKE_BINARY_DIR}/rt64/src
)
target_link_libraries(ISS2000Recompiled PRIVATE
    RecompiledFuncs_us
    RecompiledFuncs_ende
    RecompiledFuncs_frit
    RecompiledRSP
    librecomp
    ultramodern
    rt64
    nfd
    SDL2::SDL2
    Threads::Threads
    ${CMAKE_DL_LIBS}
)

if (WIN32)
    # GUI program (no console window); SDL2main provides WinMain and a UTF-8 argv. Exe icon from the PNG.
    set_target_properties(ISS2000Recompiled PROPERTIES WIN32_EXECUTABLE TRUE)
    # windows.h (pulled in by RT64's headers) otherwise defines min/max macros that break std::min/std::max.
    target_compile_definitions(ISS2000Recompiled PRIVATE NOMINMAX)
    # RT64 links SDL2 by bare name with a directory-scoped link_directories(), which doesn't reach this target.
    target_link_directories(ISS2000Recompiled PRIVATE ${CMAKE_SOURCE_DIR}/ext/rt64/src/contrib/mupen64plus-win32-deps/SDL2-2.26.3/lib/x64)
    target_link_libraries(ISS2000Recompiled PRIVATE SDL2::SDL2main ws2_32)
    set(APP_ICON_ICO ${CMAKE_BINARY_DIR}/gen/iss2000recomp.ico)
    set(APP_ICON_RC ${CMAKE_BINARY_DIR}/gen/iss2000recomp.rc)
    add_custom_command(
        OUTPUT ${APP_ICON_ICO}
        COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/png_to_ico.py ${CMAKE_SOURCE_DIR}/assets/iss2000recomp.png ${APP_ICON_ICO}
        DEPENDS ${CMAKE_SOURCE_DIR}/tools/png_to_ico.py ${CMAKE_SOURCE_DIR}/assets/iss2000recomp.png
    )
    file(WRITE ${APP_ICON_RC} "1 ICON \"iss2000recomp.ico\"
")
    set_source_files_properties(${APP_ICON_RC} PROPERTIES OBJECT_DEPENDS ${APP_ICON_ICO})
    target_sources(ISS2000Recompiled PRIVATE ${APP_ICON_RC})
    # Runtime DLLs next to the exe: SDL2, and DXC (RT64 compiles shaders at runtime on D3D12).
    add_custom_command(TARGET ISS2000Recompiled POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            ${CMAKE_SOURCE_DIR}/ext/rt64/src/contrib/mupen64plus-win32-deps/SDL2-2.26.3/lib/x64/SDL2.dll
            ${CMAKE_SOURCE_DIR}/ext/rt64/src/contrib/dxc/bin/x64/dxcompiler.dll
            ${CMAKE_SOURCE_DIR}/ext/rt64/src/contrib/dxc/bin/x64/dxil.dll
            $<TARGET_FILE_DIR:ISS2000Recompiled>
    )
endif()
