cmake_minimum_required(VERSION 3.16)
project(DraftorBehaviorTelemetry VERSION 0.1.1 LANGUAGES CXX)
set(TELEMETRY_SOURCES src/Privacy.cpp src/WireEncoder.cpp src/TelemetrySink.cpp src/TelemetryEmitter.cpp)
add_library(draftor_telemetry ${TELEMETRY_SOURCES})
target_include_directories(draftor_telemetry PUBLIC include)
target_compile_features(draftor_telemetry PUBLIC cxx_std_17)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
  target_compile_options(draftor_telemetry PRIVATE -Wall -Wextra -Wpedantic -Werror -fno-exceptions -fno-rtti)
endif()
# A consumer gets only the library and the necessary C++17/header requirements.
set(TELEMETRY_TESTS_DEFAULT OFF)
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
  set(TELEMETRY_TESTS_DEFAULT ON)
endif()
option(TELEMETRY_BUILD_TESTS "Build this module's host validation programs" ${TELEMETRY_TESTS_DEFAULT})
option(TELEMETRY_ASAN "Instrument this module's validation targets only" OFF)
option(TELEMETRY_UBSAN "Instrument this module's validation targets only" OFF)
if(TELEMETRY_BUILD_TESTS)
  set(TELEMETRY_TEST_LIBRARY draftor_telemetry)
  set(TELEMETRY_SANITIZER_FLAGS)
  if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
    if(TELEMETRY_ASAN)
      list(APPEND TELEMETRY_SANITIZER_FLAGS -fsanitize=address -fno-omit-frame-pointer)
    endif()
    if(TELEMETRY_UBSAN)
      list(APPEND TELEMETRY_SANITIZER_FLAGS -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer)
    endif()
  endif()
  if(TELEMETRY_SANITIZER_FLAGS)
    # Separate instrumented copy; even this build's public library stays clean.
    add_library(draftor_telemetry_checked STATIC ${TELEMETRY_SOURCES})
    target_include_directories(draftor_telemetry_checked PUBLIC include)
    target_compile_features(draftor_telemetry_checked PUBLIC cxx_std_17)
    target_compile_options(draftor_telemetry_checked PRIVATE -Wall -Wextra -Wpedantic -Werror -fno-exceptions -fno-rtti ${TELEMETRY_SANITIZER_FLAGS})
    set(TELEMETRY_TEST_LIBRARY draftor_telemetry_checked)
  endif()
  foreach(name core_tests hardening_tests synthetic_workflows resource_benchmark)
    if(name STREQUAL "core_tests" OR name STREQUAL "hardening_tests")
      add_executable(${name} tests/core/${name}.cpp)
    else()
      add_executable(${name} examples/${name}.cpp)
    endif()
    target_link_libraries(${name} PRIVATE ${TELEMETRY_TEST_LIBRARY})
    if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
      target_compile_options(${name} PRIVATE -Wall -Wextra -Wpedantic -Werror -fno-exceptions -fno-rtti ${TELEMETRY_SANITIZER_FLAGS})
      target_link_options(${name} PRIVATE ${TELEMETRY_SANITIZER_FLAGS})
    endif()
  endforeach()
  enable_testing()
  add_test(NAME core_contracts COMMAND core_tests)
  add_test(NAME core_hardening COMMAND hardening_tests)
endif()
