# This folder should mirror the structure of the wheel with i.e. dynamic dependencies, NOTICE, licenses and other stuff.

file(GENERATE
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/__init__.py
    INPUT ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py.in
)

add_custom_target(
    copy_cpython_so ALL
    COMMAND ${CMAKE_COMMAND} -E rm -f ${CMAKE_CURRENT_BINARY_DIR}/*.cpython*.so
    COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:aeveon-control-module> ${CMAKE_CURRENT_BINARY_DIR}
    DEPENDS aeveon-control-module
)

set(AEVEON_CONTROL_PACKAGE_FILES
    ${CMAKE_CURRENT_BINARY_DIR}/__init__.py
    ${CMAKE_CURRENT_SOURCE_DIR}/aeveon_bringup_board.py
    ${CMAKE_CURRENT_SOURCE_DIR}/i2c_aeveon_bringup_board.py
)

# The CI fails when trying to build the typing stubs on Windows, so we skip it there.
if(NOT WIN32)
    # NOTE: This actually reads the python package (i.e. the __init__.py file) and not the compiled module (.so) file.
    # Because the path points to an aeveon_control package directory rather next to the compiled module itself.
    nanobind_add_stub(
        aeveon_control_stub
        MODULE aeveon_control
        PYTHON_PATH $<TARGET_FILE_DIR:aeveon-control-module>
        DEPENDS copy_cpython_so
        RECURSIVE
        OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}
        OUTPUT
            __init__.pyi
            config.pyi
            registers.pyi
    )

    set(AEVEON_CONTROL_STUB_FILES
        ${CMAKE_CURRENT_BINARY_DIR}/__init__.pyi
        ${CMAKE_CURRENT_BINARY_DIR}/config.pyi
        ${CMAKE_CURRENT_BINARY_DIR}/registers.pyi
    )
endif()

if(DEFINED SKBUILD)
    install(TARGETS aeveon-control-module DESTINATION ${SKBUILD_PLATLIB_DIR}/aeveon_control)
    install(
        FILES ${AEVEON_CONTROL_PACKAGE_FILES} ${AEVEON_CONTROL_STUB_FILES}
        DESTINATION ${SKBUILD_PLATLIB_DIR}/aeveon_control)
endif()