cmake_minimum_required(VERSION 3.25)
project(FPTAgent VERSION 0.1.0 LANGUAGES C CXX)

if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux" OR NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|amd64|AMD64)$")
  message(FATAL_ERROR "M0 supports Linux x86-64 only")
endif()
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|Clang)$")
  message(FATAL_ERROR "M0 strict FP flags are validated only for GCC and Clang")
endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_subdirectory(core)
option(FPT_BUILD_ANALYZER "Build M1 analysis tools with pinned Clang/LLVM" OFF)
option(FPT_BUILD_TRANSFORM "Build M5 AST precision transformer" OFF)
if(FPT_BUILD_TRANSFORM AND NOT FPT_BUILD_ANALYZER)
  message(FATAL_ERROR "M5 transformer requires FPT_BUILD_ANALYZER")
endif()
option(FPT_BUILD_CESTAC "Build native synchronous CESTAC" OFF)
option(FPT_BUILD_MCA "Build original M2 MCA runtime and instrumentation" OFF)
include(CTest)
if(FPT_BUILD_MCA OR FPT_BUILD_CESTAC)
  find_path(FPT_BOOST_INCLUDE_DIR boost/multiprecision/cpp_int.hpp
    HINTS "$ENV{FPT_BOOST_INCLUDE_DIR}" "${CMAKE_SOURCE_DIR}/work/llvm21/usr/include")
  if(NOT FPT_BOOST_INCLUDE_DIR)
    message(FATAL_ERROR "Boost multiprecision headers are required for verified exp; set FPT_BOOST_INCLUDE_DIR")
  endif()
  add_library(fpt-directed-math INTERFACE)
  target_include_directories(fpt-directed-math SYSTEM INTERFACE "${FPT_BOOST_INCLUDE_DIR}")
endif()
if(FPT_BUILD_MCA)
  add_subdirectory(numerical/mca)
  target_link_libraries(fpt-mca-runtime PUBLIC fpt-directed-math)
endif()
if(FPT_BUILD_CESTAC)
  add_subdirectory(numerical/cestac)
  target_link_libraries(fpt-cestac-runtime PUBLIC fpt-directed-math)
endif()
if(FPT_BUILD_ANALYZER)
  add_subdirectory(compiler)
endif()
if(BUILD_TESTING)
  find_package(Python3 3.11 REQUIRED COMPONENTS Interpreter)
  add_test(NAME fp_demo_contract
    COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tests/check_cpp_demo.py $<TARGET_FILE:fpt-fp-demo>)
  if(FPT_BUILD_ANALYZER)
    add_test(NAME llvm_ir_contract COMMAND $<TARGET_FILE:fpt-ir-analyze>
      ${CMAKE_SOURCE_DIR}/tests/fixtures/fp_ops.ll ctest-input)
    if(FPT_BUILD_TRANSFORM)
      add_test(NAME precision_transform_version COMMAND $<TARGET_FILE:fpt-precision-transform> --version)
    endif()
  endif()
endif()
