cmake_minimum_required(VERSION 3.22)
project(cutout_native VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(CUTOUT_BUILD_CLI "Build desktop command line interface" ON)
option(CUTOUT_BUILD_TESTS "Build operator regression tests" ON)
option(CUTOUT_SANITIZE "Enable address and undefined-behavior sanitizers" OFF)
option(CUTOUT_VERBOSE_LOGGING "Print recovered operator timings" OFF)
find_package(OpenCV 4 REQUIRED COMPONENTS core imgproc)
find_package(Threads REQUIRED)
file(GLOB ANN_SOURCES CONFIGURE_DEPENDS vendor/ann/*.cpp)
add_library(cutout_ann STATIC ${ANN_SOURCES})
target_include_directories(cutout_ann PRIVATE vendor/include)
set_target_properties(cutout_ann PROPERTIES POSITION_INDEPENDENT_CODE ON CXX_STANDARD 14)
add_library(cutout_figtree STATIC vendor/figtree/figtree.cpp vendor/figtree/KCenterClustering.cpp)
target_include_directories(cutout_figtree PUBLIC vendor/include/figtree PRIVATE vendor/include src)
target_link_libraries(cutout_figtree PRIVATE cutout_ann)
set_target_properties(cutout_figtree PROPERTIES POSITION_INDEPENDENT_CODE ON CXX_STANDARD 14)
add_library(cutout_core STATIC src/core.cpp src/platform.cpp legacy/KernelEstimation.cpp legacy/Dijkstra.cpp
    legacy/mw/fimage.cpp legacy/mw/flst.cpp legacy/mw/list.cpp legacy/mw/mw.cpp
    legacy/mw/openCVWithMW.cpp legacy/mw/shape.cpp)
target_include_directories(cutout_core PUBLIC include ${OpenCV_INCLUDE_DIRS} PRIVATE legacy vendor/include vendor/include/figtree src)
target_link_libraries(cutout_core PUBLIC opencv_core opencv_imgproc Threads::Threads PRIVATE cutout_figtree)
set_target_properties(cutout_core PROPERTIES POSITION_INDEPENDENT_CODE ON)
if(CUTOUT_VERBOSE_LOGGING)
    target_compile_definitions(cutout_core PRIVATE CUTOUT_VERBOSE_LOGGING)
endif()
# Vendor sources retain their old interfaces/notices; do not hide sanitizer errors.
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
    target_compile_options(cutout_core PRIVATE -Wall -Wextra -Wno-unused-parameter -Wno-unused-variable -Wno-sign-compare -Wno-write-strings)
    target_compile_options(cutout_ann PRIVATE -Wno-write-strings)
    target_compile_options(cutout_figtree PRIVATE -Wno-write-strings)
endif()
if(CUTOUT_SANITIZE)
    foreach(target cutout_ann cutout_figtree cutout_core)
        target_compile_options(${target} PUBLIC -fsanitize=address,undefined -fno-omit-frame-pointer)
        target_link_options(${target} PUBLIC -fsanitize=address,undefined)
    endforeach()
endif()
if(CUTOUT_BUILD_CLI)
    find_package(OpenCV 4 REQUIRED COMPONENTS core imgproc imgcodecs)
    add_executable(cutout_cli src/cli.cpp)
    target_link_libraries(cutout_cli PRIVATE cutout_core opencv_imgcodecs)
endif()
if(CUTOUT_BUILD_TESTS)
    enable_testing()
    if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../validation/native_regressions.cpp")
        message(FATAL_ERROR "CUTOUT_BUILD_TESTS requires updated/validation/native_regressions.cpp")
    endif()
    add_executable(cutout_regressions ../validation/native_regressions.cpp)
    target_include_directories(cutout_regressions PRIVATE legacy src)
    target_link_libraries(cutout_regressions PRIVATE cutout_core)
    add_test(NAME cutout_regressions COMMAND cutout_regressions)
endif()
