# The jdai Python module (pybind11); built with -DJDAI_BUILD_PYTHON=ON, or as a wheel through
# scikit-build-core (pyproject.toml at the top of the repository).

if(NOT pybind11_FOUND)
    find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
    if(NOT pybind11_DIR)
        execute_process(COMMAND ${Python_EXECUTABLE} -m pybind11 --cmakedir
                        OUTPUT_VARIABLE pybind11_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
    endif()
    find_package(pybind11 CONFIG REQUIRED)
endif()

pybind11_add_module(jdai_python MODULE jdai_python.cpp)
set_target_properties(jdai_python PROPERTIES OUTPUT_NAME jdai)
target_link_libraries(jdai_python PRIVATE jdai_static)

if(SKBUILD)
    install(TARGETS jdai_python LIBRARY DESTINATION .)
endif()

# the pytest suite (tests/python), run against the module just built
if(JDAI_BUILD_TESTS AND NOT SKBUILD)
    add_test(NAME python_jdai COMMAND ${Python_EXECUTABLE} -m pytest -q -p no:cacheprovider ${PROJECT_SOURCE_DIR}/tests/python)
    set_tests_properties(python_jdai PROPERTIES ENVIRONMENT "PYTHONPATH=$<TARGET_FILE_DIR:jdai_python>")
    # the Python examples (examples/python), run the same way
    if(JDAI_BUILD_EXAMPLES)
        foreach(e hello_jdai arrays tables)
            add_test(NAME example_py_${e} COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/examples/python/${e}.py)
            set_tests_properties(example_py_${e} PROPERTIES ENVIRONMENT "PYTHONPATH=$<TARGET_FILE_DIR:jdai_python>")
        endforeach()
    endif()
endif()
