Skip to content

Commit

Permalink
Enable python stubgen for Python bindings of Optima
Browse files Browse the repository at this point in the history
  • Loading branch information
allanleal committed Jan 16, 2024
1 parent b9cb212 commit b4530c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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

0 comments on commit b4530c3

Please sign in to comment.