# Python bindings (nanobind, stable ABI from Python 3.12). Configure with the interpreter that has nanobind
# installed, for example:
#   -DJKP_BUILD_PYTHON=ON -DPython_EXECUTABLE=bindings/python/.venv/bin/python
# The package is assembled in <build>/bindings/python/just_keypoints; the tests run from there. pip and the
# wheel builds go through pyproject.toml (scikit-build-core), which sets SKBUILD and installs the same files
# into the wheel.
find_package(Python 3.12 REQUIRED COMPONENTS Interpreter Development.Module Development.SABIModule)
execute_process(COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
                OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_ROOT RESULT_VARIABLE _nb)
if(NOT _nb EQUAL 0)
  message(FATAL_ERROR "nanobind not found for ${Python_EXECUTABLE}; install it (bindings/python/requirements.txt) or set Python_EXECUTABLE")
endif()
find_package(nanobind CONFIG REQUIRED)

# Which package this build is (pyproject.toml and packaging/variant.py set it): "cpu" for just-keypoints,
# "cu12" or "cu13" for the CUDA packages, "source" for a CMake build.
set(JKP_PACKAGE_VARIANT source CACHE STRING "Package variant recorded in just_keypoints._build_info")
option(JKP_PYTHON_STUBS "Generate the type stubs (_core.pyi) by importing the built module" ON)

set(_pkg ${CMAKE_CURRENT_BINARY_DIR}/just_keypoints)
nanobind_add_module(_core STABLE_ABI NB_STATIC src/module.cpp)
target_link_libraries(_core PRIVATE jkp)
set_target_properties(_core PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${_pkg})
foreach(_cfg ${CMAKE_CONFIGURATION_TYPES})
  string(TOUPPER ${_cfg} _CFG)
  set_target_properties(_core PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${_CFG} ${_pkg})
endforeach()

set(JKP_CUDA_VERSION "")
if(JKP_HAVE_CUDA)
  set(JKP_CUDA_VERSION ${CMAKE_CUDA_COMPILER_VERSION})
endif()
configure_file(build_info.py.in ${_pkg}/_build_info.py @ONLY)

file(GLOB _py CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/just_keypoints/*.py
     ${CMAKE_CURRENT_SOURCE_DIR}/just_keypoints/py.typed)
foreach(_f ${_py})
  get_filename_component(_n ${_f} NAME)
  add_custom_command(TARGET _core POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${_f} ${_pkg}/${_n})
endforeach()

if(JKP_PYTHON_STUBS)
  # Imports just_keypoints._core from the build directory, so the package's own files must be there first
  # (the copies above run after _core is linked).
  nanobind_add_stub(_core_stub MODULE just_keypoints._core OUTPUT ${_pkg}/_core.pyi
                    PYTHON_PATH ${CMAKE_CURRENT_BINARY_DIR} DEPENDS _core)
endif()

if(SKBUILD)
  # The Python files come from wheel.packages in pyproject.toml; CMake adds what it builds.
  install(TARGETS _core LIBRARY DESTINATION just_keypoints)
  install(FILES ${_pkg}/_build_info.py DESTINATION just_keypoints)
  if(JKP_PYTHON_STUBS)
    install(FILES ${_pkg}/_core.pyi DESTINATION just_keypoints)
  endif()
endif()

if(JKP_BUILD_TESTS)
  add_test(NAME python_tests COMMAND ${Python_EXECUTABLE} -m pytest -q ${CMAKE_CURRENT_SOURCE_DIR}/tests)
  set_tests_properties(python_tests PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}" LABELS python)
endif()
