cmake_minimum_required(VERSION 3.16)
# License: Apache-2.0 (LICENSE, installed with the SDK under share/licenses/).
project(agritrace_c_sdk VERSION 0.3.0
  DESCRIPTION "AgriTrace IoT C SDK: signed device envelopes for microcontrollers"
  HOMEPAGE_URL "https://txng.theio.vn/en/developers"
  LANGUAGES C)
if(POLICY CMP0144)
  cmake_policy(SET CMP0144 NEW) # MBEDTLS_ROOT (set by the build container) is a search root
endif()

option(AT_BUILD_TESTS "Build the host unit tests, the conformance emitter and the examples" ON)
option(AT_SANITIZE "Build with AddressSanitizer and UndefinedBehaviorSanitizer" ON)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

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

set(AT_WARNINGS -Wall -Wextra -Wpedantic -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Werror)

# Core: envelope, canonical JSON, client and outbox. No dependency beyond the C library.
add_library(agritrace src/at_json.c src/at_envelope.c src/at_client.c)
target_include_directories(agritrace PUBLIC include PRIVATE src)
target_compile_options(agritrace PRIVATE ${AT_WARNINGS})
if(NOT AT_NO_DOUBLE)
  find_library(AT_LIBM m)
  if(AT_LIBM)
    target_link_libraries(agritrace PRIVATE ${AT_LIBM})
  endif()
else()
  target_compile_definitions(agritrace PRIVATE AT_NO_DOUBLE)
endif()

# mbedTLS 3.6 backend of at_crypto, plus the POSIX port used by the host tests and the examples.
find_package(MbedTLS 3.6 CONFIG)
if(MbedTLS_FOUND)
  add_library(agritrace_mbedtls src/at_crypto_mbedtls.c)
  target_link_libraries(agritrace_mbedtls PUBLIC agritrace MbedTLS::mbedcrypto)
  target_compile_options(agritrace_mbedtls PRIVATE ${AT_WARNINGS})

  add_library(agritrace_posix ports/posix/at_posix.c)
  target_include_directories(agritrace_posix PUBLIC ports/posix)
  target_link_libraries(agritrace_posix PUBLIC agritrace MbedTLS::mbedcrypto)
  target_compile_options(agritrace_posix PRIVATE ${AT_WARNINGS})
endif()

if(AT_BUILD_TESTS)
  if(NOT MbedTLS_FOUND)
    message(FATAL_ERROR "The tests need mbedTLS 3.6: set CMAKE_PREFIX_PATH (the build container sets MBEDTLS_ROOT)")
  endif()
  if(AT_SANITIZE)
    foreach(t agritrace agritrace_mbedtls agritrace_posix)
      target_compile_options(${t} PRIVATE -fsanitize=address,undefined -fno-sanitize-recover=all)
    endforeach()
    set(AT_SAN -fsanitize=address,undefined -fno-sanitize-recover=all)
  endif()
  enable_testing()
  foreach(name test_json test_envelope test_client test_vectors)
    add_executable(${name} tests/${name}.c)
    target_include_directories(${name} PRIVATE src tests tests/vectors)
    target_link_libraries(${name} PRIVATE agritrace_mbedtls agritrace_posix ${AT_SAN})
    target_compile_options(${name} PRIVATE ${AT_WARNINGS} ${AT_SAN})
    add_test(NAME ${name} COMMAND ${name})
  endforeach()

  add_executable(at_conformance_emit tests/conformance/emit.c)
  target_link_libraries(at_conformance_emit PRIVATE agritrace_mbedtls agritrace_posix ${AT_SAN})
  target_compile_options(at_conformance_emit PRIVATE ${AT_WARNINGS} ${AT_SAN})

  foreach(name knocker scale)
    add_executable(example_${name} examples/${name}.c examples/host_platform.c)
    target_include_directories(example_${name} PRIVATE examples)
    target_link_libraries(example_${name} PRIVATE agritrace_mbedtls agritrace_posix ${AT_SAN})
    target_compile_options(example_${name} PRIVATE ${AT_WARNINGS} ${AT_SAN})
  endforeach()
endif()
