cmake_minimum_required(VERSION 3.20)

project(cloud_launcher_chiaki_gateway VERSION 0.3.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(OpenSSL REQUIRED COMPONENTS Crypto SSL)
find_path(NLOHMANN_JSON_INCLUDE_DIR nlohmann/json.hpp
    HINTS "${CMAKE_CURRENT_SOURCE_DIR}/../chiaki-ng/switch/borealis/library/include/borealis/extern"
    REQUIRED)
find_path(CHIAKI_INCLUDE_DIR chiaki/session.h
    HINTS "${CMAKE_CURRENT_SOURCE_DIR}/../chiaki-ng/lib/include"
    DOC "Root include directory containing chiaki/session.h")
if(NOT CHIAKI_INCLUDE_DIR OR
   NOT EXISTS "${CHIAKI_INCLUDE_DIR}/chiaki/session.h")
    message(FATAL_ERROR
        "Chiaki headers not found: set CHIAKI_INCLUDE_DIR to the chiaki-ng/lib/include directory")
endif()

add_library(cloud_gateway_client STATIC src/split_gateway_client_win.cpp)
target_include_directories(cloud_gateway_client
    PUBLIC include "${CHIAKI_INCLUDE_DIR}"
    PRIVATE "${NLOHMANN_JSON_INCLUDE_DIR}")
target_compile_definitions(cloud_gateway_client PUBLIC
	CLOUD_GATEWAY_PROTOCOL_ABI="chiaki_split_v3")
if(WIN32)
    target_compile_definitions(cloud_gateway_client PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
    target_link_libraries(cloud_gateway_client PUBLIC OpenSSL::Crypto OpenSSL::SSL ws2_32)
else()
    message(FATAL_ERROR "The Chiaki gateway client currently requires Windows Winsock")
endif()

include(CTest)
if(BUILD_TESTING AND TARGET chiaki-lib)
    add_executable(cloud_gateway_client_tests tests/split_gateway_client_tests.cpp)
    target_link_libraries(cloud_gateway_client_tests PRIVATE cloud_gateway_client chiaki-lib)
    add_test(NAME cloud_gateway_client_tests COMMAND cloud_gateway_client_tests)

    add_executable(cloud_gateway_bootstrap_contract_tests tests/bootstrap_contract_tests.cpp)
    target_include_directories(cloud_gateway_bootstrap_contract_tests PRIVATE
        include "${CHIAKI_INCLUDE_DIR}" "${NLOHMANN_JSON_INCLUDE_DIR}")
    target_compile_definitions(cloud_gateway_bootstrap_contract_tests PRIVATE
        CLOUD_GATEWAY_PROTOCOL_ABI="chiaki_split_v3" WIN32_LEAN_AND_MEAN NOMINMAX)
    target_link_libraries(cloud_gateway_bootstrap_contract_tests PRIVATE
        chiaki-lib OpenSSL::Crypto OpenSSL::SSL ws2_32)
    add_test(NAME cloud_gateway_bootstrap_contract_tests COMMAND cloud_gateway_bootstrap_contract_tests)
endif()
