find_package(
  Python 3.9
  REQUIRED COMPONENTS Interpreter Development.Module
  OPTIONAL_COMPONENTS Development.SABIModule
)

execute_process(
  COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
  OUTPUT_STRIP_TRAILING_WHITESPACE
  OUTPUT_VARIABLE nanobind_ROOT
)

find_package(
  nanobind
  CONFIG REQUIRED
)

nanobind_add_module(
  bindings
  STABLE_ABI
  NB_STATIC src/bindings.cpp
)

nanobind_add_stub(
  cecpy_bindings_stub
  MODULE bindings
  MARKER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/cecpy/py.typed"
  OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/cecpy/bindings.pyi"
  PYTHON_PATH $<TARGET_FILE_DIR:bindings>
  DEPENDS bindings
)

add_custom_target(
  build_stubs
  ALL DEPENDS cecpy_bindings_stub
)

target_compile_features(
  bindings
  PRIVATE cxx_std_23
)

target_link_libraries(
  bindings
  PRIVATE cecxx
)

install(
  TARGETS bindings
  LIBRARY DESTINATION cecpy
)

install(
  FILES "${CMAKE_CURRENT_SOURCE_DIR}/cecpy/py.typed" "${CMAKE_CURRENT_SOURCE_DIR}/cecpy/bindings.pyi"
  DESTINATION "."
)

install(
  DIRECTORY ${CMAKE_SOURCE_DIR}/data/
  DESTINATION cecpy/data
)
