cmake_minimum_required(VERSION 3.20)

# ── Shared object library so tests can link against the same sources ──────────
add_library(trusv_lib STATIC
    ${CMAKE_SOURCE_DIR}/src/io.cpp
    ${CMAKE_SOURCE_DIR}/src/svtype.cpp
    ${CMAKE_SOURCE_DIR}/src/cluster.cpp
    ${CMAKE_SOURCE_DIR}/src/output.cpp
    ${CMAKE_SOURCE_DIR}/src/distance.cpp
)

target_include_directories(trusv_lib PUBLIC
    ${CMAKE_SOURCE_DIR}/src
    ${HTSLIB_INCLUDE_DIRS}
)

target_link_libraries(trusv_lib PUBLIC
    ${HTSLIB_LIBRARIES}
    ${HTSLIB_LDFLAGS_OTHER}
)

if(NOT APPLE)
    target_link_libraries(trusv_lib PUBLIC z bz2 lzma curl)
endif()

if(OpenMP_CXX_FOUND)
    target_link_libraries(trusv_lib PUBLIC OpenMP::OpenMP_CXX)
    target_compile_definitions(trusv_lib PUBLIC HAVE_OPENMP=1)
endif()

target_compile_options(trusv_lib PRIVATE -O2 -Wall -Wextra -Wno-unused-parameter)

# ── Helper macro for test executables ────────────────────────────────────────
macro(add_trusv_test name src)
    add_executable(${name} ${src})
    target_link_libraries(${name} PRIVATE trusv_lib)
    add_test(NAME ${name} COMMAND ${name})
endmacro()

add_trusv_test(test_distance  test_distance.cpp)
add_trusv_test(test_svtype    test_svtype.cpp)
add_trusv_test(test_cluster   test_cluster.cpp)

# End-to-end integration test (mirrors the Makefile `test` target): run the
# built binary on the chr1-slice fixtures via tests/integration.sh.
add_test(NAME test_integration
         COMMAND ${CMAKE_COMMAND} -E env TRUSV_BIN=$<TARGET_FILE:trusv>
                 bash ${CMAKE_SOURCE_DIR}/tests/integration.sh)
