cmake_minimum_required(VERSION 3.15...3.27)
project(alignable LANGUAGES CXX)

if (NOT SKBUILD)
  message(WARNING "\
  This CMake file is meant to be executed using 'scikit-build-core'. \
  Running it directly will almost certainly not produce the desired result. \
  If you are a user trying to install this package, use: \
    $ pip install . \
  If you are a developer, use: \
    $ pip install nanobind scikit-build-core[pyproject] \
    $ pip install --no-build-isolation -ve .")
endif()

if (CMAKE_VERSION VERSION_LESS 3.18)
  set(DEV_MODULE Development)
else()
  set(DEV_MODULE Development.Module)
endif()

find_package(Python 3.10
  REQUIRED COMPONENTS Interpreter ${DEV_MODULE})

# Import nanobind
execute_process(
  COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
  OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_ROOT)
find_package(nanobind CONFIG REQUIRED)

# Build with speed optimization — this is a hot inner loop, not just bindings
nanobind_add_module(_tiler_fast NOMINSIZE
  src/alignable/_tiler_fast.cpp
)

install(TARGETS _tiler_fast LIBRARY DESTINATION alignable)

# ── Scorer (htslib-backed BAM reader + scoring loop) ──────────────────
find_path(HTS_INCLUDE htslib/sam.h
  HINTS "$ENV{CONDA_PREFIX}/include" "$ENV{PREFIX}/include")
find_library(HTS_LIB hts
  HINTS "$ENV{CONDA_PREFIX}/lib" "$ENV{PREFIX}/lib")

if (HTS_INCLUDE AND HTS_LIB)
  nanobind_add_module(_scorer_fast NOMINSIZE
    src/alignable/_scorer_fast.cpp
  )
  target_include_directories(_scorer_fast PRIVATE ${HTS_INCLUDE})
  target_link_libraries(_scorer_fast PRIVATE ${HTS_LIB})
  install(TARGETS _scorer_fast LIBRARY DESTINATION alignable)
else()
  message(WARNING "htslib not found — _scorer_fast will not be built. "
                  "Install htslib (conda install -c bioconda htslib) for "
                  "the native scoring extension.")
endif()
