cmake_minimum_required(VERSION 3.20)
project(ProjectM VERSION 0.0.1 LANGUAGES CXX C)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL") # match Aurora (/MD)

set(PM_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/..")
set(RECOMP_ROOT "${PM_ROOT}/recomp")
# PM_PPLUS=ON: Project+ build (assets/pplus.*, generated_pplus/ from config/pplus_config.toml).
option(PM_PPLUS "Build the Project+ image" OFF)
# PM_REX=ON: Super Smash REX (Project+ based; same mod runtime, assets/rex.*, generated_rex/).
option(PM_REX "Build the Super Smash REX image" OFF)
if(PM_REX)
    set(GENERATED "${PM_ROOT}/generated_rex")
    add_compile_definitions(PM_IMAGE="rex" PM_PPLUS=1 PM_REX=1 PM_MOD_DIR="rex_")
    add_compile_definitions("PM_MOD_SD=\"REX Release 0\\xC2\\xBD Hotfix 1 - Wii/REX Release 0\\xC2\\xBD Hotfix 1 - Wii\"") # 0½, UTF-8
elseif(PM_PPLUS)
    set(GENERATED "${PM_ROOT}/generated_pplus")
    add_compile_definitions(PM_IMAGE="pplus" PM_PPLUS=1 PM_MOD_DIR="Project+" "PM_MOD_SD=\"Project+.v3.2.1.Wii/Project+ v3.2.1 (Wii)\"")
else()
    set(GENERATED "${PM_ROOT}/generated")
    add_compile_definitions(PM_IMAGE="brawl")
endif()

# Portable baseline (no -march): generated code must also build for ARM64 later.
# Windows: clang-cl. Linux: clang (same flags in GCC-style spelling; -rdynamic names functions in crash reports).
if(MSVC)
    add_compile_options(-fno-strict-aliasing /fp:strict /Zi)
    add_link_options(/DEBUG)
else()
    add_compile_options(-fno-strict-aliasing -ffp-model=strict -g)
    add_link_options(-rdynamic -static-libstdc++ -static-libgcc) # prebuilt: no libstdc++ version needed on the player's PC
endif()

# The recompiled game code is not part of this build: tools/game_lib.py compiles it into game.dll / game.so (on the
# player's PC), which projectm loads (src/game_lib.cpp, docs/design_split.md). Only its headers are used here.

# Unit tests (ctest)
enable_testing()
add_executable(test_broadway tests/test_broadway.cpp)
target_include_directories(test_broadway PRIVATE "${GENERATED}" "${RECOMP_ROOT}/XenonUtils" "${RECOMP_ROOT}/thirdparty/simde")
add_test(NAME broadway COMMAND test_broadway)

# App icon: res/local/ (gitignored, user's own art from tools/gen_icon.py) when present, else res/.
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/res/local/icon.ico" AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/res/local/icon_rgba.h")
    set(PM_ICON_DIR "${CMAKE_CURRENT_SOURCE_DIR}/res/local")
else()
    set(PM_ICON_DIR "${CMAKE_CURRENT_SOURCE_DIR}/res")
endif()
if(WIN32)
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/app_icon.rc" "1 ICON \"${PM_ICON_DIR}/icon.ico\"
")
    set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/app_icon.rc" PROPERTIES OBJECT_DEPENDS "${PM_ICON_DIR}/icon.ico")
    set(PM_APP_ICON "${CMAKE_CURRENT_BINARY_DIR}/app_icon.rc")
    set(PM_HOST_SOURCES src/memory_win.cpp src/host_diag_win.cpp)
else()
    set(PM_APP_ICON "")
    set(PM_HOST_SOURCES src/memory_posix.cpp src/host_diag_posix.cpp)
endif()

# Runtime executable
file(GLOB PM_HLE_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/hle/*.cpp")
add_executable(projectm src/main.cpp src/game_lib.cpp ${PM_HOST_SOURCES} src/net/rollback.cpp src/net/session.cpp src/net/transport.cpp src/net/online_overlay.cpp src/controls_overlay.cpp src/settings_overlay.cpp src/loader.cpp src/timebase.cpp src/guest_call.cpp src/scheduler.cpp ${PM_HLE_SOURCES}
    src/audio/axdsp.cpp src/audio/audio_out_sdl.cpp src/gpu/gx_bridge.cpp ${PM_APP_ICON})
target_include_directories(projectm PRIVATE src "${PM_ICON_DIR}" "${GENERATED}" "${RECOMP_ROOT}/thirdparty/simde")
target_compile_definitions(projectm PRIVATE PM_RUNTIME)
if(WIN32)
    target_link_libraries(projectm PRIVATE winmm ws2_32)
    target_sources(projectm PRIVATE res/utf8.manifest) # UTF-8 paths
endif()

# Aurora (GX on WebGPU), prebuilt into external/aurora/build (Windows: tools/build_aurora_test.bat) or
# external/aurora/build_linux (tools/build_aurora_linux.sh).
set(AURORA_ROOT "${PM_ROOT}/external/aurora")
if(WIN32)
    set(AURORA_BUILD "${AURORA_ROOT}/build")
else()
    set(AURORA_BUILD "${AURORA_ROOT}/build_linux")
endif()
if(NOT WIN32)
    # Linux: Aurora's static libs + Dawn/SDL3 (shared or static) in one link group (static libs reference each
    # other in no fixed order). Runtime .so files resolve next to the executable ($ORIGIN, copied post-build).
    file(GLOB_RECURSE AURORA_LIBS "${AURORA_BUILD}/*.a" "${AURORA_BUILD}/*.so")
    list(FILTER AURORA_LIBS EXCLUDE REGEX "glfw3|SDL3_test|aurora_main|imgui-src/examples|CMakeFiles")
    file(GLOB_RECURSE AURORA_SDL_INC LIST_DIRECTORIES true "${AURORA_BUILD}/_deps/*/include/SDL3")
    list(FILTER AURORA_SDL_INC INCLUDE REGEX "/include/SDL3$")
    list(TRANSFORM AURORA_SDL_INC REPLACE "/SDL3$" "")
    target_include_directories(projectm PRIVATE "${AURORA_ROOT}/include" ${AURORA_SDL_INC} "${AURORA_BUILD}/_deps/imgui-src"
        "${AURORA_BUILD}/_deps/sdl3-src/include")
    target_link_libraries(projectm PRIVATE -Wl,--start-group ${AURORA_LIBS} -Wl,--end-group z dl pthread) # system zlib (libpng/freetype)
    set_target_properties(projectm PROPERTIES BUILD_RPATH "$ORIGIN" INSTALL_RPATH "$ORIGIN")
    foreach(so ${AURORA_LIBS})
        if(so MATCHES "\.so$")
            add_custom_command(TARGET projectm POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${so}" "$<TARGET_FILE_DIR:projectm>")
        endif()
    endforeach()
    target_precompile_headers(projectm PRIVATE "${GENERATED}/ppc_recomp_shared.h")
    # Launcher: SDL3 (static, from source) + Dear ImGui from the Aurora build. Online panel (pm_net) Windows only.
    add_executable(launcher launcher/launcher.cpp)
    target_include_directories(launcher PRIVATE "${PM_ICON_DIR}" "${AURORA_BUILD}/_deps/tracy-src/profiler/src"
        "${AURORA_BUILD}/_deps/imgui-src" "${AURORA_BUILD}/_deps/imgui-src/backends" ${AURORA_SDL_INC} "${AURORA_BUILD}/_deps/sdl-src/include")
    target_link_libraries(launcher PRIVATE "${AURORA_BUILD}/extern/libimgui_backends.a" "${AURORA_BUILD}/extern/libimgui.a"
        "${AURORA_BUILD}/_deps/sdl-build/libSDL3.a" "${AURORA_BUILD}/_deps/freetype-build/libfreetype.a"
        "${AURORA_BUILD}/_deps/png-build/libpng16.a" z dl m pthread)
    # pm_net (Linux): libsteam_api.so linked, rpath $ORIGIN, copied next to pm_net; TCP-only without the SDK.
    add_executable(pm_net netrelay/relay.cpp)
    target_link_libraries(pm_net PRIVATE pthread)
    target_link_options(pm_net PRIVATE -rdynamic) # crash backtraces with names
    set(PM_STEAM_SDK "${CMAKE_CURRENT_SOURCE_DIR}/../external/steamworks" CACHE PATH "Steamworks SDK (public/, redistributable_bin/)")
    if(EXISTS "${PM_STEAM_SDK}/public/steam/steam_api.h")
        target_compile_definitions(pm_net PRIVATE PM_HAVE_STEAM)
        target_include_directories(pm_net PRIVATE "${PM_STEAM_SDK}/public")
        target_link_libraries(pm_net PRIVATE "${PM_STEAM_SDK}/redistributable_bin/linux64/libsteam_api.so")
        set_target_properties(pm_net PROPERTIES BUILD_RPATH "$ORIGIN" INSTALL_RPATH "$ORIGIN")
        add_custom_command(TARGET pm_net POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${PM_STEAM_SDK}/redistributable_bin/linux64/libsteam_api.so" "$<TARGET_FILE_DIR:pm_net>")
    endif()
    return() # aurora_quad: Windows only (docs/design_linux.md)
endif()
file(GLOB_RECURSE AURORA_LIBS "${AURORA_BUILD}/*.lib")
list(FILTER AURORA_LIBS EXCLUDE REGEX "glfw3|SDL3_test|aurora_main|imgui-src/examples")
target_include_directories(projectm PRIVATE "${AURORA_ROOT}/include" "${AURORA_BUILD}/_deps/sdl3_prebuilt-src/include" "${AURORA_BUILD}/_deps/imgui-src")
target_link_libraries(projectm PRIVATE ${AURORA_LIBS} imm32 version setupapi ole32 oleaut32 uuid advapi32 shell32 user32 gdi32 dwmapi dxgi d3d12 d3d11 dxguid wbemuuid ntdll comsuppw)
file(GLOB AURORA_DLLS "${AURORA_BUILD}/_deps/dawn_prebuilt-src/bin/*.dll" "${AURORA_BUILD}/_deps/sdl3_prebuilt-src/bin/SDL3.dll"
    "${AURORA_BUILD}/_deps/png-build/libpng16.dll" "${AURORA_BUILD}/_deps/zlib-build/zlib1.dll")
foreach(dll ${AURORA_DLLS})
    add_custom_command(TARGET projectm POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${dll}" "$<TARGET_FILE_DIR:projectm>")
endforeach()
target_precompile_headers(projectm PRIVATE "${GENERATED}/ppc_recomp_shared.h")

# Standalone Aurora draw test (tests/aurora_quad.cpp)
add_executable(aurora_quad tests/aurora_quad.cpp)
target_compile_definitions(aurora_quad PRIVATE TARGET_PC)
target_include_directories(aurora_quad PRIVATE "${AURORA_ROOT}/include" "${AURORA_BUILD}/_deps/sdl3_prebuilt-src/include")
target_link_libraries(aurora_quad PRIVATE ${AURORA_LIBS} imm32 version setupapi ole32 oleaut32 uuid advapi32 shell32 user32 gdi32 dwmapi dxgi d3d12 d3d11 dxguid wbemuuid ntdll comsuppw winmm dbghelp)




# Launcher (launcher/launcher.cpp): picks Brawl / Project+ / REX and starts runtime/<build>/projectm.
# SDL3 + Dear ImGui from the Aurora build; lives next to the vanilla projectm.exe (SDL3.dll is copied there).
set(IMGUI_SRC "${AURORA_BUILD}/_deps/imgui-src")
add_executable(launcher WIN32 launcher/launcher.cpp res/utf8.manifest "${CMAKE_CURRENT_BINARY_DIR}/app_icon.rc")
target_include_directories(launcher PRIVATE "${PM_ICON_DIR}" "${AURORA_BUILD}/_deps/tracy-src/profiler/src" "${IMGUI_SRC}" "${IMGUI_SRC}/backends" "${AURORA_BUILD}/_deps/sdl3_prebuilt-src/include")
target_link_libraries(launcher PRIVATE winhttp shell32 ole32 "${AURORA_BUILD}/extern/imgui.lib" "${AURORA_BUILD}/extern/imgui_backends.lib"
    "${AURORA_BUILD}/_deps/sdl3_prebuilt-src/lib/SDL3.lib" "${AURORA_BUILD}/_deps/freetype-build/freetype.lib"
    "${AURORA_BUILD}/_deps/png-build/libpng16.lib" "${AURORA_BUILD}/_deps/zlib-build/zlib.lib")

# Online relay helper (netrelay/relay.cpp, docs/design_online.md phase 3): the game's TCP stream to a peer over
# Steam (SteamNetworkingMessages, app 480) or TCP. Steamworks SDK is user-supplied in external/steamworks
# (public/ + redistributable_bin/, not in git); without it pm_net builds TCP-only. steam_api64.dll is
# delay-loaded and copied next to pm_net.exe.
add_executable(pm_net netrelay/relay.cpp res/utf8.manifest)
target_link_libraries(pm_net PRIVATE ws2_32)
set(PM_STEAM_SDK "${CMAKE_CURRENT_SOURCE_DIR}/../external/steamworks" CACHE PATH "Steamworks SDK (public/, redistributable_bin/)")
if(EXISTS "${PM_STEAM_SDK}/public/steam/steam_api.h")
    target_compile_definitions(pm_net PRIVATE PM_HAVE_STEAM)
    target_include_directories(pm_net PRIVATE "${PM_STEAM_SDK}/public")
    target_link_libraries(pm_net PRIVATE "${PM_STEAM_SDK}/redistributable_bin/win64/steam_api64.lib" delayimp)
    target_link_options(pm_net PRIVATE "/DELAYLOAD:steam_api64.dll")
    add_custom_command(TARGET pm_net POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "${PM_STEAM_SDK}/redistributable_bin/win64/steam_api64.dll" "$<TARGET_FILE_DIR:pm_net>")
endif()
