cmake_minimum_required(VERSION 3.20)
project(ISSDNative C CXX)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

set(SNESRECOMP_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/deps/snesrecomp" CACHE PATH "Path to SNESRecomp")
set(SNESRECOMP_SDL_BACKEND "SDL2" CACHE STRING "SDL backend")

# On Linux/SteamOS SDL2 comes from the system (pkg-config finds it). On Windows
# there is no system package, so fall back to the usual scoop location - but
# only as a default the user can still override, never as a hard requirement.
if(WIN32 AND NOT CMAKE_PREFIX_PATH AND NOT DEFINED ENV{SDL2_DIR})
    set(_issd_sdl2_guess "C:/Users/sergi/scoop/apps/sdl2/current")
    if(EXISTS "${_issd_sdl2_guess}")
        set(CMAKE_PREFIX_PATH "${_issd_sdl2_guess}"
            CACHE PATH "Prefix used to locate SDL2")
    endif()
    unset(_issd_sdl2_guess)
endif()

include(${SNESRECOMP_ROOT}/runner/runner.cmake)

# One shared source list for every port (see cmake/issd_sources.cmake).
set(ISSD_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/issd_sources.cmake)

add_executable(ISSDNative
    ${SNESRECOMP_RUNNER_SOURCES}
    ${ISSD_GENERATED_SOURCES}
    ${ISSD_GAME_SOURCES}
)

# Launcher: logo, golden ball and font embedded as C arrays (tools/embed.py),
# so a release cannot lose them.
find_package(Python3 REQUIRED COMPONENTS Interpreter)
set(ISSD_LAUNCHER_ASSETS_H ${CMAKE_BINARY_DIR}/gen/generated/launcher_assets.h)
add_custom_command(
    OUTPUT ${ISSD_LAUNCHER_ASSETS_H}
    COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/gen/generated
    COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/embed.py ${ISSD_LAUNCHER_ASSETS_H}
        launcher_logo_png=${CMAKE_CURRENT_SOURCE_DIR}/assets/launcher/logo.png
        launcher_ball_png=${CMAKE_CURRENT_SOURCE_DIR}/assets/launcher/ball.png
        launcher_font_ttf=${CMAKE_CURRENT_SOURCE_DIR}/assets/launcher/DejaVuSans-Bold.ttf
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/embed.py
        ${CMAKE_CURRENT_SOURCE_DIR}/assets/launcher/logo.png
        ${CMAKE_CURRENT_SOURCE_DIR}/assets/launcher/ball.png
        ${CMAKE_CURRENT_SOURCE_DIR}/assets/launcher/DejaVuSans-Bold.ttf
    VERBATIM)
target_sources(ISSDNative PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/ISSDNative/issd_launcher.c"
    "${CMAKE_CURRENT_SOURCE_DIR}/ISSDNative/issd_online.c"     # needs ISSD_NETPLAY
    ${ISSD_LAUNCHER_ASSETS_H})
target_include_directories(ISSDNative PRIVATE
    "${CMAKE_BINARY_DIR}/gen"
    "${CMAKE_CURRENT_SOURCE_DIR}/third_party/stb")
target_compile_definitions(ISSDNative PRIVATE ISSD_LAUNCHER=1)

# Final build: no in-game menu or hotkeys, and the remaster look applied on
# every start. Developer builds keep the overlay for testing.
option(ISSD_FINAL_BUILD "Build without the in-game menu" OFF)
if(ISSD_FINAL_BUILD)
    target_compile_definitions(ISSDNative PRIVATE ISSD_FINAL_BUILD=1)
endif()

# Delay-sync two-player netplay (recomp-net, LAN transport for now).
option(ISSD_NETPLAY "Build two-player netplay" ON)
if(ISSD_NETPLAY)
    # Internet play: peer-to-peer ICE (libjuice, built in statically). The
    # engine's netplay core is used as is, but its lobby client (which now
    # requires a Discord login) is replaced by issd_signal.c, which meets
    # through public MQTT brokers with the invite code as the key.
    set(SNESRECOMP_NET_ICE ON CACHE BOOL "ICE transport for online play" FORCE)
    _snesrecomp_add_recomp_net()
    target_link_libraries(ISSDNative PRIVATE recomp_net)
    target_sources(ISSDNative PRIVATE
        "${SNESRECOMP_RUNNER_ROOT}/src/netplay/snes_netplay.c"
        "${SNESRECOMP_RUNNER_ROOT}/src/netplay/snes_host_session.c"
        "${CMAKE_CURRENT_SOURCE_DIR}/ISSDNative/issd_signal.c")
    target_include_directories(ISSDNative PRIVATE
        "${SNESRECOMP_RUNNER_ROOT}/src/netplay"
        "${SNESRECOMP_RUNNER_ROOT}/src/lobby")
    target_compile_definitions(ISSDNative PRIVATE SNESRECOMP_NET=1 SNES_HAS_LOBBY_CLIENT=1)
    # Online players must run the same build: the lobby matches this exactly.
    file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" ISSD_VERSION LIMIT_COUNT 1)
    # Builds without git (the Debian 12 AppImage root) pass -DISSD_GIT_HASH.
    if(NOT ISSD_GIT_HASH)
        execute_process(COMMAND git rev-parse --short HEAD
            WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
            OUTPUT_VARIABLE ISSD_GIT_HASH OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
    endif()
    target_compile_definitions(ISSDNative PRIVATE
        ISSD_ONLINE_VERSION="${ISSD_VERSION}-${ISSD_GIT_HASH}")
endif()

target_include_directories(ISSDNative PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/recomp/config"
    "${CMAKE_CURRENT_SOURCE_DIR}/ISSDNative"
    ${SNESRECOMP_RUNNER_INCLUDE_DIRS}
    "${SNESRECOMP_ROOT}/runner/src/desktop"
)

if(MSVC)
    target_compile_options(ISSDNative PRIVATE
        /W3
        /wd4018
        /wd4244
        /wd4267
        /wd4996
        /wd4146
    )
else()
    target_compile_options(ISSDNative PRIVATE
        -w
        -Wno-implicit-function-declaration
    )
endif()

# runner.cmake populates this with the libraries its own sources need: libm on
# every non-Windows host for cx4.c, plus ws2_32/dbghelp for trace builds. The
# project previously ignored it, which only showed up as an undefined cos() at
# link time on Linux because Windows folds libm into the CRT.
target_link_libraries(ISSDNative PRIVATE ${SNESRECOMP_RUNNER_LIBRARIES})

snesrecomp_target_sdl(ISSDNative)

# Without this deny set the SPC700 handshake wedges the reset vector and no
# window is created, so it has to travel with the executable, not just live in
# the source tree. See ApplyDefaultAotBootDenySet in ISSDNative/main.c.
add_custom_command(TARGET ISSDNative POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "${CMAKE_CURRENT_SOURCE_DIR}/recomp/aot_boot_deny.txt"
        $<TARGET_FILE_DIR:ISSDNative>
)

if(WIN32)
    # comdlg32 is no longer used for the ROM picker (the runner's
    # cross-platform snesrecomp_pick_rom_file covers every platform), but winmm
    # and ws2_32 remain runner dependencies on Windows.
    target_link_libraries(ISSDNative PRIVATE winmm ws2_32)
    # Exe icon (the golden ball).
    target_sources(ISSDNative PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/ISSDNative/issd_windows.rc")

    # runner.cmake copies SDL2.dll itself when SDL2 is found as an imported
    # shared target. This covers the plain SDL2_LIBRARIES case, where there is
    # no target to copy from. Silently skipped when the DLL is not where the
    # prefix suggests, rather than baking one machine's layout into the build.
    if(NOT TARGET SDL2::SDL2 AND CMAKE_PREFIX_PATH)
        set(_issd_sdl2_dll "${CMAKE_PREFIX_PATH}/lib/SDL2.dll")
        if(EXISTS "${_issd_sdl2_dll}")
            add_custom_command(TARGET ISSDNative POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_if_different
                    "${_issd_sdl2_dll}" $<TARGET_FILE_DIR:ISSDNative>)
        endif()
        unset(_issd_sdl2_dll)
    endif()
endif()
