cmake_minimum_required(VERSION 3.16)
cmake_policy(SET CMP0091 NEW)
project(FreeScanCpp VERSION 1.3.0 LANGUAGES CXX)

if(NOT WIN32 OR NOT MSVC)
    message(FATAL_ERROR "FreeScanCpp requires Windows x64 and an MSVC-compatible C++ compiler.")
endif()
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
    message(FATAL_ERROR "Configure an x64 build (for Visual Studio generators: -A x64).")
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# The vendor SDK exchanges STL structures across the DLL boundary. All build
# configurations use the release DLL CRT and iterator ABI used by the SDK.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("#if !defined(_M_X64)\n#error The vendor SDK requires x64.\n#endif\nint main() { return 0; }" FREESCAN_TARGET_X64)
if(NOT FREESCAN_TARGET_X64)
    message(FATAL_ERROR "The official SDK requires x64; ARM64 and x86 targets are unsupported.")
endif()
set(SDK_INCLUDE_DIR "C:/Program Files/Shining3d/FreeTrak_TRAK/sdk/include"
    CACHE PATH "Matching FreeScan Trak SDK include directory")
if(NOT EXISTS "${SDK_INCLUDE_DIR}/sn3dsdkcorepub.h" OR NOT EXISTS "${SDK_INCLUDE_DIR}/Sn3DSDKDataLibpub.h")
    message(FATAL_ERROR "Set SDK_INCLUDE_DIR to the matching official FreeScan SDK headers.")
endif()

set(FREESCAN_SYSTEM_LIBRARIES httpapi advapi32 ole32 shell32 user32 gdiplus shlwapi crypt32 bcrypt ws2_32 version d3d11 dxgi windowsapp windowscodecs dwmapi)
function(freescan_target target)
    target_compile_features(${target} PRIVATE cxx_std_17)
    target_compile_definitions(${target} PRIVATE _ITERATOR_DEBUG_LEVEL=0 _WIN32_WINNT=0x0602 WIN32_LEAN_AND_MEAN NOMINMAX)
    target_compile_options(${target} PRIVATE /EHsc /utf-8 /W4)
    target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src" "${SDK_INCLUDE_DIR}")
    target_link_libraries(${target} PRIVATE ${FREESCAN_SYSTEM_LIBRARIES})
endfunction()

file(GLOB FREESCAN_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
add_executable(FreeScanCpp ${FREESCAN_SOURCES})
freescan_target(FreeScanCpp)

include(CTest)
if(BUILD_TESTING)
    # Separate source lists deliberately omit main.cpp, HTTP and supervisor from
    # offline tests. worker_test always constructs WorkerService(..., true).
    add_executable(contracts_test src/contracts.cpp tests/contracts_test.cpp)
    freescan_target(contracts_test)
    add_test(NAME contracts_offline COMMAND contracts_test)

    add_executable(worker_test src/common.cpp src/contracts.cpp src/sdk_worker.cpp src/calibration_window_capture.cpp tests/worker_test.cpp)
    freescan_target(worker_test)
    add_test(NAME worker_offline COMMAND worker_test)

    add_executable(preview_capture_test src/common.cpp tests/preview_capture_test.cpp)
    freescan_target(preview_capture_test)
    add_test(NAME preview_capture_offline COMMAND preview_capture_test)

    add_executable(battery_test src/common.cpp tests/battery_test.cpp)
    freescan_target(battery_test)
    add_test(NAME battery_offline COMMAND battery_test)

    add_executable(calibration_files_test src/common.cpp tests/calibration_files_test.cpp)
    freescan_target(calibration_files_test)
    add_test(NAME calibration_files_offline COMMAND calibration_files_test)
    add_executable(calibration_recovery_test src/common.cpp tests/calibration_recovery_test.cpp)
    freescan_target(calibration_recovery_test)
    add_test(NAME calibration_recovery_offline COMMAND calibration_recovery_test)
    set_tests_properties(contracts_offline worker_offline preview_capture_offline battery_offline calibration_files_offline calibration_recovery_offline
                         PROPERTIES TIMEOUT 30)
endif()
