# scikit-build-core places pybind11 in the isolated build venv.
# Using CONFIG mode ensures cmake finds the pybind11Config.cmake that ships
# with the PyPI package rather than any system-installed copy.
# PYBIND11_FINDPYTHON=ON lets pybind11 locate Python itself; no separate
# find_package(Python) is needed and having both can cause CMake to select
# conflicting Python versions.
set(PYBIND11_FINDPYTHON ON)
find_package(pybind11 CONFIG REQUIRED)

pybind11_add_module(_nabla_core MODULE bindings.cpp)

target_link_libraries(_nabla_core PRIVATE nabla_core)

# Static-link the C++ runtime so the .pyd is self-contained (MinGW)
if(MINGW)
    target_link_options(_nabla_core PRIVATE -static-libgcc -static-libstdc++)
endif()

target_include_directories(_nabla_core PRIVATE
    ${CMAKE_SOURCE_DIR}/include
)

# Local dev: place the built module into python/nabla_quant/ so it's importable
# without installation. Under scikit-build-core (SKBUILD=TRUE) the install rule
# below governs placement inside the wheel; LIBRARY_OUTPUT_DIRECTORY is ignored.
if(NOT SKBUILD)
    set_target_properties(_nabla_core PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/python/nabla_quant"
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/python/nabla_quant"
    )
    foreach(config IN ITEMS DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
        string(TOUPPER ${config} config_upper)
        set_target_properties(_nabla_core PROPERTIES
            LIBRARY_OUTPUT_DIRECTORY_${config_upper} "${CMAKE_SOURCE_DIR}/python/nabla_quant"
            RUNTIME_OUTPUT_DIRECTORY_${config_upper} "${CMAKE_SOURCE_DIR}/python/nabla_quant"
        )
    endforeach()
endif()

# Install rule for scikit-build-core: places the compiled extension into the wheel
install(TARGETS _nabla_core DESTINATION nabla_quant)
