# Native Shard protocol: C++ schema generation, build wiring and conformance test.
#
# The C++ stubs are generated into the build tree at configure time — they are
# never committed. A C++ consumer already needs a toolchain, so committing
# generated C++ would only create a second copy of the schema that can rot.
#
# Protobuf and gRPC C++ are required together so message and service bindings are
# generated by one exact toolchain. The ignored bootstrap prefix supplies both.
#
# Build:
#   cmake -S packages/node/native -B build/native -DCMAKE_PREFIX_PATH=<protobuf-install>
#   cmake --build build/native -j
#   ctest --test-dir build/native --output-on-failure
#
# `scripts/bootstrap_native_toolchain.sh` builds a protobuf install from source
# when the system has none.

cmake_minimum_required(VERSION 3.24)
project(meshnet_shard_protocol CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Protobuf and gRPC are one pinned generation toolchain. Configure only against
# the ignored prefix produced by scripts/bootstrap_native_toolchain.sh; accepting
# an arbitrary system plugin would make generated service bindings host-dependent.
set(MESHNET_PROTOBUF_VERSION "33.1.0")
set(MESHNET_GRPC_VERSION "1.82.1")
find_package(protobuf ${MESHNET_PROTOBUF_VERSION} EXACT CONFIG REQUIRED)
find_package(gRPC ${MESHNET_GRPC_VERSION} EXACT CONFIG REQUIRED)

if(NOT TARGET gRPC::grpc_cpp_plugin)
  message(FATAL_ERROR "pinned gRPC package does not export grpc_cpp_plugin")
endif()

set(SHARD_PROTO "${CMAKE_CURRENT_SOURCE_DIR}/proto/shard_runtime.proto")

# Message types: always available.
add_library(shard_runtime_proto STATIC "${SHARD_PROTO}")
target_link_libraries(shard_runtime_proto PUBLIC protobuf::libprotobuf)
target_include_directories(shard_runtime_proto PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")
protobuf_generate(
  TARGET shard_runtime_proto
  LANGUAGE cpp
  IMPORT_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/proto"
  PROTOC_OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}"
)

# Service stubs are part of the reproducibility contract, not an optional branch.
add_library(shard_runtime_grpc STATIC "${SHARD_PROTO}")
target_link_libraries(shard_runtime_grpc PUBLIC shard_runtime_proto gRPC::grpc++)
target_include_directories(shard_runtime_grpc PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")
protobuf_generate(
  TARGET shard_runtime_grpc
  LANGUAGE grpc
  GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc
  PLUGIN "protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>"
  IMPORT_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/proto"
  PROTOC_OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}"
)
message(STATUS "Pinned gRPC ${gRPC_VERSION}: building ShardRuntime service stubs")

enable_testing()

add_executable(shard_protocol_conformance tests/test_shard_protocol_conformance.cpp)
target_link_libraries(shard_protocol_conformance PRIVATE shard_runtime_proto)

# The test asserts against the same committed vectors the Python test uses, and
# writes its own re-serialization back out so Python can prove the two languages
# agree byte-for-byte.
add_test(
  NAME shard_protocol_conformance
  COMMAND shard_protocol_conformance
          "${CMAKE_CURRENT_SOURCE_DIR}/testdata"
          "${CMAKE_CURRENT_BINARY_DIR}"
)
