Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Python stubgen #24

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
11 changes: 11 additions & 0 deletions python/package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
# 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
COMMAND ${CMAKE_COMMAND} -E rm -rf build # remove build dir created by previous `python setup.py install` commands (see next) to ensure fresh rebuild since changed python files are not overwritten even with --force option
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/optima optima
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:optima4py> optima
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:Optima> optima
COMMAND ${STUBGEN_COMMAND1}
COMMAND ${STUBGEN_COMMAND2}
COMMAND ${PYTHON_EXECUTABLE} setup.py --quiet build --force
COMMAND ${CMAKE_COMMAND} -E rm optima/$<TARGET_FILE_NAME:optima4py>
COMMAND ${CMAKE_COMMAND} -E rm optima/$<TARGET_FILE_NAME:Optima>
Expand Down
Loading