option(ASYNCTIMER_SANITIZERS "Enable address and undefined behavior sanitizers" OFF)

add_library(host_test_options INTERFACE)
target_compile_features(host_test_options INTERFACE cxx_std_11)
if(MSVC)
  target_compile_options(host_test_options INTERFACE /W4 /WX)
else()
  target_compile_options(host_test_options INTERFACE -Wall -Wextra -Werror)
endif()
if(ASYNCTIMER_SANITIZERS)
  if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" AND NOT MSVC)
    target_compile_options(host_test_options INTERFACE
      -fsanitize=address,undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer)
    target_link_options(host_test_options INTERFACE -fsanitize=address,undefined)
  else()
    message(FATAL_ERROR "ASYNCTIMER_SANITIZERS requires GCC or Clang")
  endif()
endif()

add_library(asynctimer_host STATIC ../src/AsyncTimer.cpp support/Arduino.cpp)
target_include_directories(asynctimer_host PUBLIC ../src support)
target_link_libraries(asynctimer_host PUBLIC host_test_options)
set_target_properties(asynctimer_host PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO)

function(add_host_test name source)
  add_executable(test_${name} ${source} support/test_main.cpp)
  target_link_libraries(test_${name} PRIVATE asynctimer_host)
  set_target_properties(test_${name} PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO)
  add_test(NAME host.${name} COMMAND test_${name})
  set_tests_properties(host.${name} PROPERTIES TIMEOUT 30 LABELS "host;${name}")
endfunction()

foreach(suite timing capacity cancellation callbacks callback_ids rollover model)
  add_host_test(${suite} ${suite}.cpp)
endforeach()
foreach(example BlinkUsingInterval CancelInterval CancelIntervalFromCallback CancelTimeout DebounceUsingTimeout SerialMsgUsingTimeout)
  add_host_test(${example} examples/${example}.cpp)
endforeach()
