cmake_minimum_required(VERSION 3.16)
# License: Apache-2.0 (LICENSE, installed with the SDK under share/licenses/).
project(agritrace_cpp_sdk VERSION 0.3.0
  DESCRIPTION "AgriTrace IoT C++ SDK: a header-only C++17 wrapper over the C SDK"
  HOMEPAGE_URL "https://txng.theio.vn/en/developers"
  LANGUAGES C CXX)
if(POLICY CMP0144)
  cmake_policy(SET CMP0144 NEW) # MBEDTLS_ROOT (set by the build container) is a search root
endif()

option(AT_CPP_BUILD_TESTS "Build the C++ unit tests, the vectors test and the conformance emitter" ON)
option(AT_CPP_SANITIZE "Build the tests (and the C SDK under them) with ASan and UBSan" ON)
set(AT_C_SDK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../c" CACHE PATH "The C SDK this wrapper links (sdk/c)")

# The C SDK builds its own tests from sdk/c; here it only provides the libraries.
set(AT_BUILD_TESTS OFF)
add_subdirectory(${AT_C_SDK_DIR} c-sdk)

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

include(GNUInstallDirs)
install(FILES LICENSE DESTINATION ${CMAKE_INSTALL_DATADIR}/licenses/${PROJECT_NAME})

# Header-only: agritrace.hpp over the C core, agritrace_mbedtls.hpp over the mbedTLS backend.
add_library(agritrace_cpp INTERFACE)
target_include_directories(agritrace_cpp INTERFACE include)
target_link_libraries(agritrace_cpp INTERFACE agritrace)
target_compile_features(agritrace_cpp INTERFACE cxx_std_17)
if(TARGET agritrace_mbedtls)
  add_library(agritrace_cpp_mbedtls INTERFACE)
  target_link_libraries(agritrace_cpp_mbedtls INTERFACE agritrace_cpp agritrace_mbedtls)
endif()

if(AT_CPP_BUILD_TESTS)
  if(NOT TARGET agritrace_mbedtls)
    message(FATAL_ERROR "The tests need mbedTLS 3.6: set CMAKE_PREFIX_PATH (the build container sets MBEDTLS_ROOT)")
  endif()
  set(AT_CPP_WARNINGS -Wall -Wextra -Wpedantic -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Werror)
  set(AT_CPP_SAN)
  if(AT_CPP_SANITIZE)
    set(AT_CPP_SAN -fsanitize=address,undefined -fno-sanitize-recover=all)
    foreach(t agritrace agritrace_mbedtls agritrace_posix)
      target_compile_options(${t} PRIVATE ${AT_CPP_SAN})
    endforeach()
  endif()
  enable_testing()
  foreach(name test_envelope test_client test_vectors)
    add_executable(cpp_${name} tests/${name}.cpp)
    target_include_directories(cpp_${name} PRIVATE tests tests/vectors)
    target_link_libraries(cpp_${name} PRIVATE agritrace_cpp_mbedtls agritrace_posix ${AT_CPP_SAN})
    target_compile_options(cpp_${name} PRIVATE ${AT_CPP_WARNINGS} ${AT_CPP_SAN})
    add_test(NAME cpp_${name} COMMAND cpp_${name})
  endforeach()

  # Firmware often builds C++ without exceptions (ESP-IDF's default): the header must compile and work there.
  add_executable(cpp_test_no_exceptions tests/test_no_exceptions.cpp)
  target_include_directories(cpp_test_no_exceptions PRIVATE tests)
  target_link_libraries(cpp_test_no_exceptions PRIVATE agritrace_cpp_mbedtls ${AT_CPP_SAN})
  target_compile_options(cpp_test_no_exceptions PRIVATE ${AT_CPP_WARNINGS} ${AT_CPP_SAN} -fno-exceptions)
  add_test(NAME cpp_test_no_exceptions COMMAND cpp_test_no_exceptions)

  add_executable(at_cpp_conformance_emit tests/conformance/emit.cpp)
  target_link_libraries(at_cpp_conformance_emit PRIVATE agritrace_cpp_mbedtls agritrace_posix ${AT_CPP_SAN})
  target_compile_options(at_cpp_conformance_emit PRIVATE ${AT_CPP_WARNINGS} ${AT_CPP_SAN})
endif()
