# CMakeLists.txt
#
# Author: Vojtěch Dvořák


# ---------------------------- Fetching dependencies --------------------------

FetchContent_Declare(
    pybind11
    GIT_REPOSITORY https://github.com/pybind/pybind11.git
    GIT_TAG 5b0a6fc
    GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(pybind11)


# ------------------------- Shared object with python module ------------------

pybind11_add_module(${PYTHON_MODULE_NAME} ${PYTHON_SRCS})
target_link_libraries(${PYTHON_MODULE_NAME} PRIVATE ${LIBRARY_NAME})
add_custom_command(
    TARGET ${PYTHON_MODULE_NAME}
    POST_BUILD
    COMMAND python3 ${CMAKE_BINARY_DIR}/setup.py build_ext --inplace
)


# ---------------------------- Python bindings tests --------------------------

add_custom_target(${PYTHON_MODULE_NAME}_tests DEPENDS ${PYTHON_MODULE_NAME})
add_custom_command(
    TARGET ${PYTHON_MODULE_NAME}_tests
    COMMAND export PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR} && python3 -m pytest -s "${CMAKE_CURRENT_LIST_DIR}/tests/*"
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)


# ----------------------------------- .whl file -------------------------------

configure_file(${CMAKE_CURRENT_LIST_DIR}/setup.py.in ${CMAKE_BINARY_DIR}/setup.py)
add_custom_target(${PYTHON_MODULE_NAME}_wheel DEPENDS ${PYTHON_MODULE_NAME} ${CMAKE_BINARY_DIR}/setup.py)
add_custom_command(
    TARGET ${PYTHON_MODULE_NAME}_wheel
    POST_BUILD
    COMMAND python3 ${CMAKE_BINARY_DIR}/setup.py bdist_wheel
)

