cmake_minimum_required(VERSION 3.20)
if(POLICY CMP0091)
    cmake_policy(SET CMP0091 NEW)
endif()
project(GundamWEndlessDuelFrontend LANGUAGES C)

if(MSVC)
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

option(GUNDAM_W_FRONTEND_REBUILD_CORE
       "Deliberately rebuild the static core from source instead of importing the sealed archive" OFF)
set(GUNDAM_W_CORE_LIBRARY "" CACHE FILEPATH
    "Exact prequalified V10.40 gundam-w-static-core library for host-only builds")
set(GUNDAM_W_CORE_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/../static-core/include" CACHE PATH
    "Directory containing the sealed gundam_w_static_core.h public API")

if(GUNDAM_W_FRONTEND_REBUILD_CORE)
    add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../static-core"
                     "${CMAKE_CURRENT_BINARY_DIR}/static-core-rebuild")
    set(GUNDAM_W_CORE_TARGET gundam-w-static-core)
else()
    if(NOT GUNDAM_W_CORE_LIBRARY)
        message(FATAL_ERROR
            "Host-only builds require -DGUNDAM_W_CORE_LIBRARY=/path/to/the/exact/prequalified/V10.40/library. "
            "Set GUNDAM_W_FRONTEND_REBUILD_CORE=ON only for a deliberate core reproducibility build.")
    endif()
    add_library(gundam-w-static-core-sealed STATIC IMPORTED GLOBAL)
    set_target_properties(gundam-w-static-core-sealed PROPERTIES
        IMPORTED_LOCATION "${GUNDAM_W_CORE_LIBRARY}"
        INTERFACE_INCLUDE_DIRECTORIES "${GUNDAM_W_CORE_INCLUDE_DIR}")
    set(GUNDAM_W_CORE_TARGET gundam-w-static-core-sealed)
endif()

add_library(gundam-w-host-common STATIC
    common/gundam_w_host_core.c
    common/gundam_w_host_evidence.c
    common/gundam_w_input_latch.c
    common/gundam_w_sha256.c)
file(GLOB_RECURSE GUNDAM_W_SNAPSHOT_SOURCE_FILES CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_LIST_DIR}/../static-core/*.c"
    "${CMAKE_CURRENT_LIST_DIR}/../static-core/*.h"
    "${CMAKE_CURRENT_LIST_DIR}/common/*.c"
    "${CMAKE_CURRENT_LIST_DIR}/common/*.h")
list(SORT GUNDAM_W_SNAPSHOT_SOURCE_FILES)
set(GUNDAM_W_SNAPSHOT_ID_INPUT "${CMAKE_C_COMPILER_ID};${CMAKE_C_COMPILER_VERSION};${CMAKE_SIZEOF_VOID_P};${CMAKE_SYSTEM_PROCESSOR};${GUNDAM_W_TEST_HOOKS};${CMAKE_C_FLAGS}")
foreach(GUNDAM_W_SNAPSHOT_SOURCE IN LISTS GUNDAM_W_SNAPSHOT_SOURCE_FILES)
    file(SHA256 "${GUNDAM_W_SNAPSHOT_SOURCE}" GUNDAM_W_SNAPSHOT_SOURCE_HASH)
    string(APPEND GUNDAM_W_SNAPSHOT_ID_INPUT ";${GUNDAM_W_SNAPSHOT_SOURCE_HASH}")
endforeach()
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${GUNDAM_W_SNAPSHOT_SOURCE_FILES})
string(SHA256 GUNDAM_W_SNAPSHOT_BUILD_ID "${GUNDAM_W_SNAPSHOT_ID_INPUT}")
configure_file(common/gundam_w_snapshot_build.h.in gundam_w_snapshot_build.h @ONLY)
target_include_directories(gundam-w-host-common PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_include_directories(gundam-w-host-common PUBLIC
    "${CMAKE_CURRENT_LIST_DIR}/common"
    "${GUNDAM_W_CORE_INCLUDE_DIR}")
target_link_libraries(gundam-w-host-common PUBLIC ${GUNDAM_W_CORE_TARGET})

add_executable(gundam-w-headless linux/gundam_w_headless.c)
target_link_libraries(gundam-w-headless PRIVATE gundam-w-host-common)
add_executable(gundam-w-snapshot-contract EXCLUDE_FROM_ALL tests/snapshot_contract.c)
target_link_libraries(gundam-w-snapshot-contract PRIVATE gundam-w-host-common)

enable_testing()
set(GUNDAM_W_TEST_ROM "" CACHE FILEPATH "Private ROM path for local frontend tests")
set(GUNDAM_W_TEST_SNAPSHOT "" CACHE FILEPATH "Same-build snapshot for local frontend tests")
if(GUNDAM_W_TEST_ROM AND GUNDAM_W_TEST_SNAPSHOT)
    add_test(NAME gundam_w_snapshot_contract COMMAND gundam-w-snapshot-contract
        "${GUNDAM_W_TEST_ROM}" "${GUNDAM_W_TEST_SNAPSHOT}")
endif()
if(WIN32)
    add_subdirectory(windows)
endif()

if(MSVC)
    # Keep the portable C stdio path shared with the Linux headless host.  MSVC
    # marks fopen/_wfopen as deprecated despite their required bounds checks
    # being performed by the surrounding path and error-handling code.
    target_compile_definitions(gundam-w-host-common PRIVATE _CRT_SECURE_NO_WARNINGS=1)
    target_compile_definitions(gundam-w-headless PRIVATE _CRT_SECURE_NO_WARNINGS=1)
    target_compile_options(gundam-w-host-common PRIVATE /W4 /WX)
    target_compile_options(gundam-w-headless PRIVATE /W4 /WX)
else()
    target_compile_options(gundam-w-host-common PRIVATE -Wall -Wextra -Wpedantic -Werror)
    target_compile_options(gundam-w-headless PRIVATE -Wall -Wextra -Wpedantic -Werror)
endif()
