Skip to content

Commit

Permalink
Create stubs file and pack in wheel package
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaS20 committed Apr 5, 2023
1 parent bcb353b commit 58c4ae5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ option(BUILD_EXAMPLES "Build the examples" OFF)
option(BUILD_DOC "Build documentation" OFF)
option(BUILD_IN_DEPS "download and build dependencies" ON)
option(CREATE_WIN_INSTALLER "Create windows installer" OFF)
option(CREATE_PYTHON_STUBS "Create the Python stubs" OFF)

# Installation root
if(NOT WIN32)
Expand Down
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## 1.2.7 - 2023-04-05
### Added
- Generate and distribute stubs with python wheel package

## 1.2.6 - 2023-04-05
### Added
- Add MAC address in ifm3d discover command output
Expand Down
54 changes: 54 additions & 0 deletions modules/pybind11/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,57 @@ install(TARGETS ifm3dpy
ARCHIVE DESTINATION ${_python_install_path} COMPONENT ${_component_name}
PUBLIC_HEADER DESTINATION ${_include} COMPONENT ${_component_name}
)

#------------------
# Stubs creation
#------------------

if(CREATE_PYTHON_STUBS)

if(BUILD_SHARED_LIBS)
message (
FATAL_ERROR
"Error while creating stubs as BUILD_SHARED_LIBS is ON")
endif()

# Create a venv
set(venv ${CMAKE_CURRENT_BINARY_DIR}/stubgen-venv)
if(NOT IS_DIRECTORY ${venv})
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -m venv ${venv}
RESULT_VARIABLE status
OUTPUT_VARIABLE output
ERROR_VARIABLE output)
if(status AND NOT status EQUAL 0)
message(
FATAL_ERROR
"Error while creating venv in ${venv}:\n${output}")
endif()
endif()

find_program(venv_python python PATHS ${venv}/bin/ ${venv}/Scripts REQUIRED NO_DEFAULT_PATH)

# Install pybind11-stubgen in the venv
execute_process(
COMMAND ${venv_python} -m pip install pybind11-stubgen
RESULT_VARIABLE status
OUTPUT_VARIABLE output
ERROR_VARIABLE output)
if(status AND NOT status EQUAL 0)
message(
FATAL_ERROR
"Error while installing pybind11-stubgen in ${venv}:\n${output}"
)
endif()

# Generate the stubs
add_custom_command(
TARGET ifm3dpy
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E env PYTHONPATH="$<TARGET_FILE_DIR:ifm3dpy>"
${CMAKE_COMMAND} -E env PYTHONHOME=""
${CMAKE_COMMAND} -E env VIRTUAL_ENV="${venv}"
${venv_python} -m pybind11_stubgen --ignore-invalid=all --skip-signature-downgrade --no-setup-py --output-dir=$<TARGET_FILE_DIR:ifm3dpy> --root-module-suffix="" ifm3dpy
)
endif()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pytest
numpy
setuptools
wheel
pybind11-stubgen
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def build_extension(self, ext):
'-DCMAKE_USE_OPENSSL=OFF',
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_ARCHIVE_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable]
'-DPYTHON_EXECUTABLE=' + sys.executable,
'-DCREATE_PYTHON_STUBS=ON']

cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
Expand Down

0 comments on commit 58c4ae5

Please sign in to comment.