cmake_minimum_required(VERSION 3.16)
# Match the game's self-contained CRT choice so checks also run with a portable
# MSVC toolchain that does not install the Visual Studio debug runtime DLLs.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
project(sm64ds_frontend_checks LANGUAGES C CXX)

# This builds/tests the portable UI and input layer, NOT the game engine.
option(HANDHELD "Use the SteamOS/handheld menu profile" OFF)
add_executable(coop_frontend_test ../tests/coop_frontend_test.cpp)
target_include_directories(coop_frontend_test PRIVATE ..)
target_compile_features(coop_frontend_test PRIVATE cxx_std_11)
if(HANDHELD)
    target_compile_definitions(coop_frontend_test PRIVATE SM64DS_HANDHELD=1)
endif()
if(MSVC)
    # Standard C APIs keep this component portable. All copies are bounded by
    # the model's fixed field capacities; MSVC's *_s APIs are not portable.
    target_compile_definitions(coop_frontend_test PRIVATE _CRT_SECURE_NO_WARNINGS)
    target_compile_options(coop_frontend_test PRIVATE /W4 /WX /UNDEBUG)
else()
    target_compile_options(coop_frontend_test PRIVATE -Wall -Wextra -Werror -UNDEBUG)
endif()
enable_testing()
add_test(NAME menu_and_multitouch COMMAND coop_frontend_test)

add_executable(coop_palettes_test ../tests/coop_palettes_test.cpp)
target_include_directories(coop_palettes_test PRIVATE ..)
target_compile_features(coop_palettes_test PRIVATE cxx_std_11)
if(MSVC)
    target_compile_definitions(coop_palettes_test PRIVATE _CRT_SECURE_NO_WARNINGS)
    target_compile_options(coop_palettes_test PRIVATE /W4 /WX /UNDEBUG)
else()
    target_compile_options(coop_palettes_test PRIVATE -Wall -Wextra -Werror -UNDEBUG)
endif()
add_test(NAME coopdx_palette_import_export COMMAND coop_palettes_test)

add_executable(coop_staff_roll_test ../tests/coop_staff_roll_test.cpp)
target_include_directories(coop_staff_roll_test PRIVATE ..)
target_compile_features(coop_staff_roll_test PRIVATE cxx_std_17)
if(MSVC)
    target_compile_options(coop_staff_roll_test PRIVATE /W4 /WX /UNDEBUG)
else()
    target_compile_options(coop_staff_roll_test PRIVATE -Wall -Wextra -Werror -UNDEBUG)
endif()
add_test(NAME gameplay_models_and_credits_order COMMAND coop_staff_roll_test)

foreach(memory_test IN ITEMS coop_host_memory_test coop_host_memory_hint_test)
    add_executable(${memory_test} ../tests/coop_host_memory_test.cpp)
    target_include_directories(${memory_test} PRIVATE ../ntr/include)
    target_compile_features(${memory_test} PRIVATE cxx_std_11)
    if(MSVC)
        target_compile_options(${memory_test} PRIVATE /W4 /WX /UNDEBUG)
    else()
        target_compile_options(${memory_test} PRIVATE -Wall -Wextra -Werror -UNDEBUG)
    endif()
    add_test(NAME ${memory_test} COMMAND ${memory_test})
endforeach()
target_compile_definitions(coop_host_memory_hint_test PRIVATE NTR_TEST_MMAP_HINT=1)

add_library(coop_media_decode STATIC ../hal/coop_media_decode.cpp)
target_compile_features(coop_media_decode PRIVATE cxx_std_11)
if(MSVC)
    target_compile_definitions(coop_media_decode PRIVATE _CRT_SECURE_NO_WARNINGS)
endif()
add_executable(coop_media_test ../tests/coop_media_test.cpp)
target_include_directories(coop_media_test PRIVATE ..)
target_compile_features(coop_media_test PRIVATE cxx_std_11)
target_compile_definitions(coop_media_test PRIVATE COOP_MEDIA_FIXTURES="${CMAKE_CURRENT_SOURCE_DIR}/../tests/fixtures")
target_link_libraries(coop_media_test PRIVATE coop_media_decode)
if(MSVC)
    target_compile_options(coop_media_test PRIVATE /UNDEBUG)
else()
    target_compile_options(coop_media_test PRIVATE -UNDEBUG)
endif()
add_test(NAME custom_media_formats COMMAND coop_media_test)

if(WIN32 AND MSVC)
    # Separate processes catch differences between the saved cache and a fresh
    # load. Keep all files in an isolated fixture beside the test executable.
    add_executable(coop_settings_test ../tests/coop_settings_test.cpp ../hal/host_settings.cpp)
    target_include_directories(coop_settings_test PRIVATE ..)
    target_compile_features(coop_settings_test PRIVATE cxx_std_17)
    target_compile_definitions(coop_settings_test PRIVATE _CRT_SECURE_NO_WARNINGS)
    target_compile_options(coop_settings_test PRIVATE /UNDEBUG)
    set_target_properties(coop_settings_test PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/settings-fixture")
    foreach(rate IN ITEMS native 144)
        add_test(NAME settings_save_${rate} COMMAND coop_settings_test save ${rate})
        add_test(NAME settings_restart_${rate} COMMAND coop_settings_test read ${rate})
        set_tests_properties(settings_save_${rate} settings_restart_${rate} PROPERTIES
            WORKING_DIRECTORY "$<TARGET_FILE_DIR:coop_settings_test>"
            RUN_SERIAL TRUE)
        set_tests_properties(settings_restart_${rate} PROPERTIES DEPENDS settings_save_${rate})
    endforeach()
    set_tests_properties(settings_save_144 PROPERTIES DEPENDS settings_restart_native)
endif()

file(GLOB LUA_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/../vendor/lua/src/*.c")
list(FILTER LUA_SOURCES EXCLUDE REGEX "/(lua|luac|ltests|onelua)\\.c$")
add_library(coop_lua STATIC ${LUA_SOURCES})
target_include_directories(coop_lua PUBLIC ../vendor/lua/src)
if(UNIX)
    target_link_libraries(coop_lua PUBLIC m)
endif()
include(../hal/coop_mods.cmake)
add_executable(coop_mods_test ../tests/coop_mods_test.cpp)
coop_add_mods(coop_mods_test coop_lua)
target_compile_definitions(coop_mods_test PRIVATE _CRT_SECURE_NO_WARNINGS)
if(MSVC)
    target_compile_options(coop_mods_test PRIVATE /UNDEBUG)
else()
    target_compile_options(coop_mods_test PRIVATE -UNDEBUG)
endif()
add_test(NAME lua_gameplay_mods COMMAND coop_mods_test)

add_executable(coop_texture_tools_test ../tests/coop_texture_tools_test.cpp
    ../hal/coop_texture_tools.cpp ../hal/resource_pack.cpp)
target_include_directories(coop_texture_tools_test PRIVATE ..)
target_compile_features(coop_texture_tools_test PRIVATE cxx_std_17)
target_link_libraries(coop_texture_tools_test PRIVATE coop_lua)
target_compile_definitions(coop_texture_tools_test PRIVATE _CRT_SECURE_NO_WARNINGS
    COOP_MEDIA_FIXTURES="${CMAKE_CURRENT_SOURCE_DIR}/../tests/fixtures")
if(MSVC)
    target_compile_options(coop_texture_tools_test PRIVATE /UNDEBUG)
else()
    target_compile_options(coop_texture_tools_test PRIVATE -UNDEBUG)
endif()
add_test(NAME editable_texture_pack COMMAND coop_texture_tools_test)

add_executable(resource_pack_probe ../tests/resource_pack_probe.cpp ../hal/resource_pack.cpp)
target_include_directories(resource_pack_probe PRIVATE ../hal)
target_link_libraries(resource_pack_probe PRIVATE coop_lua)
target_compile_features(resource_pack_probe PRIVATE cxx_std_17)
target_compile_definitions(resource_pack_probe PRIVATE _CRT_SECURE_NO_WARNINGS)

# Production UDP carrier adapter for native Linux/SteamOS and macOS.
# This is a transport check, not a playable native game build.
if(UNIX)
    add_executable(coop_native_socket_test ../tests/coop_native_socket_test.cpp)
    target_include_directories(coop_native_socket_test PRIVATE ..)
    target_compile_features(coop_native_socket_test PRIVATE cxx_std_11)
    target_compile_options(coop_native_socket_test PRIVATE -Wall -Wextra -Werror -UNDEBUG)
    add_test(NAME native_udp_seven_endpoints COMMAND coop_native_socket_test)
    add_library(coop_native_carrier_compile OBJECT ../hal/comms_loopback.cpp)
    target_include_directories(coop_native_carrier_compile PRIVATE .. ../hal)
    target_compile_features(coop_native_carrier_compile PRIVATE cxx_std_17)
endif()
