From b4530c313fcd743611db7e126e482bae8620205b Mon Sep 17 00:00:00 2001 From: Allan Leal Date: Tue, 16 Jan 2024 18:54:22 +0100 Subject: [PATCH] Enable python stubgen for Python bindings of Optima --- CMakeLists.txt | 8 ++++++++ python/package/CMakeLists.txt | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index ff033d88..1931957a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,14 @@ add_subdirectory(Optima) if(OPTIMA_BUILD_PYTHON) find_package(Python COMPONENTS Interpreter Development) find_package(pybind11 REQUIRED) + find_program(PYBIND11_STUBGEN pybind11-stubgen) + if(NOT pybind11_FOUND) + message(WARNING "Could not find pybind11. The Python package optima will not be built!") + set(OPTIMA_BUILD_PYTHON OFF) + endif() + if(NOT PYBIND11_STUBGEN) + message(WARNING "Could not find pybind11-stubgen (available via pip or conda). There will be no stubs generated for the python package optima.") + endif() add_subdirectory(python) endif() diff --git a/python/package/CMakeLists.txt b/python/package/CMakeLists.txt index c9396cfa..ec32bd17 100644 --- a/python/package/CMakeLists.txt +++ b/python/package/CMakeLists.txt @@ -1,6 +1,15 @@ # Configure the setup.py file configure_file(setup.py.in ${CMAKE_CURRENT_BINARY_DIR}/setup.py) +# Create pybind11-stubgen command string depending if this program has been found or not +if(PYBIND11_STUBGEN) + set(STUBGEN_COMMAND1 ${CMAKE_COMMAND} -E env "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}" ${PYBIND11_STUBGEN} --ignore-all-errors optima) + set(STUBGEN_COMMAND2 ${CMAKE_COMMAND} -E copy_directory stubs/optima/ optima) +else() + set(STUBGEN_COMMAND1 "") # do nothing when it it's time to generate python stubs for optima in the target below + set(STUBGEN_COMMAND2 "") # do nothing when it it's time to copy the generated stub directory +endif() + # Create a custom target to build the python package during build stage add_custom_target(optima-setuptools ALL DEPENDS optima4py @@ -8,6 +17,8 @@ add_custom_target(optima-setuptools ALL COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/optima optima COMMAND ${CMAKE_COMMAND} -E copy $ optima COMMAND ${CMAKE_COMMAND} -E copy $ optima + COMMAND ${STUBGEN_COMMAND1} + COMMAND ${STUBGEN_COMMAND2} COMMAND ${PYTHON_EXECUTABLE} setup.py --quiet build --force COMMAND ${CMAKE_COMMAND} -E rm optima/$ COMMAND ${CMAKE_COMMAND} -E rm optima/$