cmake_minimum_required(VERSION 3.20)
project(virtual_kinect_core LANGUAGES CXX)

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

# ---------------------------------------------------------------------------
# libnui_core: OS-agnostic static library aggregating the pure core modules.
# Real logic arrives per plan todos 2 (contract), 6 (mapping), 7 (synth);
# these sources are compile/link placeholders today.
# ---------------------------------------------------------------------------
add_library(libnui_core STATIC
  core/calibration/calibration.cpp
  core/contract/contract.cpp
  core/mapping/mapping.cpp
  core/synth/mask_stabilizer.cpp
  core/synth/synth.cpp
  core/protocol/commands.cpp
  core/protocol/descriptors.cpp
  core/protocol/protocol.cpp
  core/protocol/streams.cpp
)
# Keep the on-disk name libnui_core.a (not liblibnui_core.a).
set_target_properties(libnui_core PROPERTIES PREFIX "" OUTPUT_NAME "libnui_core")
target_include_directories(libnui_core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/core)

# ---------------------------------------------------------------------------
# contract_layout_dump: prints the frozen contract's sizeof/offsetof golden
# table so tests/test_contract_py.py can check the Python ctypes mirror
# against the CURRENT C layout instead of a hardcoded, driftable copy.
# ---------------------------------------------------------------------------
add_executable(contract_layout_dump core/contract/contract_layout_dump.cpp)
target_link_libraries(contract_layout_dump PRIVATE libnui_core)

add_executable(contract_reader_probe tests/contract_reader_probe.cpp)
target_link_libraries(contract_reader_probe PRIVATE libnui_core)

# ---------------------------------------------------------------------------
# Tests: GoogleTest via FetchContent + CTest.
# ---------------------------------------------------------------------------
option(NUI_CORE_BUILD_TESTS "Build libnui_core unit tests" ON)
if(NUI_CORE_BUILD_TESTS)
  include(FetchContent)
  FetchContent_Declare(
    googletest
    URL https://github.com/google/googletest/archive/refs/tags/v1.15.2.tar.gz
    URL_HASH SHA256=7b42b4d6ed48810c5362c265a17faebe90dc2373c885e5216439d37927f02926
    DOWNLOAD_EXTRACT_TIMESTAMP TRUE
  )
  # Honour the parent project's C runtime selection on Windows/MSVC.
  set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  FetchContent_MakeAvailable(googletest)

  enable_testing()
  add_executable(libnui_core_tests
    tests/test_core.cpp
    tests/test_calibration.cpp
    tests/test_contract.cpp
    tests/test_mapping.cpp
    tests/test_synth.cpp
    tests/test_protocol.cpp
  )
  target_link_libraries(libnui_core_tests PRIVATE libnui_core GTest::gtest_main)

  include(GoogleTest)
  gtest_discover_tests(libnui_core_tests)
  add_test(
    NAME Contract.PythonProducerCppReaderInterop
    COMMAND
      ${CMAKE_CURRENT_SOURCE_DIR}/tracker/.venv/bin/python
      ${CMAKE_CURRENT_SOURCE_DIR}/tests/contract_interop_driver.py
      $<TARGET_FILE:contract_reader_probe>
  )
endif()
