diff --git a/CMakeLists.txt b/CMakeLists.txt index c9a87bc4c4a..8dccfa65bfa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,8 @@ option(BUILD_EXAMPLES "Build Open3D examples programs" ON option(BUILD_UNIT_TESTS "Build Open3D unit tests" OFF) option(BUILD_BENCHMARKS "Build the micro benchmarks" OFF) option(BUILD_PYTHON_MODULE "Build the python module" ON ) +option(WITH_STUBGEN "Use pybind11-stubgen to generate stubs" OFF) +option(IGNORE_STUBGEN_ERRORS "Ignore all errors during stub generation" ON ) option(BUILD_CUDA_MODULE "Build the CUDA module" OFF) option(BUILD_WITH_CUDA_STATIC "Build with static CUDA libraries" ON ) option(BUILD_COMMON_CUDA_ARCHS "Build for common CUDA GPUs (for release)" OFF) diff --git a/cmake/Open3DPrintConfigurationSummary.cmake b/cmake/Open3DPrintConfigurationSummary.cmake index c12946af62c..f9274280fec 100644 --- a/cmake/Open3DPrintConfigurationSummary.cmake +++ b/cmake/Open3DPrintConfigurationSummary.cmake @@ -37,6 +37,10 @@ function(open3d_print_configuration_summary) open3d_aligned_print("Build Unit Tests" "${BUILD_UNIT_TESTS}") open3d_aligned_print("Build Examples" "${BUILD_EXAMPLES}") open3d_aligned_print("Build Python Module" "${BUILD_PYTHON_MODULE}") + open3d_aligned_print("Generate Typing Stubs" "${WITH_STUBGEN}") + if(WITH_STUBGEN) + open3d_aligned_print("Ignore All Stubgen Errors" "${IGNORE_STUBGEN_ERRORS}") + endif() open3d_aligned_print("Build Jupyter Extension" "${BUILD_JUPYTER_EXTENSION}") open3d_aligned_print("Build TensorFlow Ops" "${BUILD_TENSORFLOW_OPS}") open3d_aligned_print("Build PyTorch Ops" "${BUILD_PYTORCH_OPS}") diff --git a/cpp/pybind/CMakeLists.txt b/cpp/pybind/CMakeLists.txt index 4da6d51aebd..528eee1d0f6 100644 --- a/cpp/pybind/CMakeLists.txt +++ b/cpp/pybind/CMakeLists.txt @@ -223,6 +223,8 @@ add_custom_target(python-package -DPYTHON_VERSION=${PYTHON_VERSION} "-DCOMPILED_MODULE_PATH_LIST=${COMPILED_MODULE_PATH_LIST}" "-DPYTHON_EXTRA_LIBRARIES=${PYTHON_EXTRA_LIBRARIES}" + -DWITH_STUBGEN=${WITH_STUBGEN} + -DIGNORE_STUBGEN_ERRORS=${IGNORE_STUBGEN_ERRORS} -DBUILD_JUPYTER_EXTENSION=${BUILD_JUPYTER_EXTENSION} -DBUILD_TENSORFLOW_OPS=${BUILD_TENSORFLOW_OPS} -DBUILD_PYTORCH_OPS=${BUILD_PYTORCH_OPS} diff --git a/cpp/pybind/make_python_package.cmake b/cpp/pybind/make_python_package.cmake index aa4171414b0..fd7d79d9b93 100644 --- a/cpp/pybind/make_python_package.cmake +++ b/cpp/pybind/make_python_package.cmake @@ -139,3 +139,19 @@ file(COPY "${PYTHON_PACKAGE_SRC_DIR}/../examples/python/" DESTINATION "${PYTHON_PACKAGE_DST_DIR}/open3d/examples") file(COPY "${PYTHON_PACKAGE_SRC_DIR}/../examples/python/" DESTINATION "${PYTHON_PACKAGE_DST_DIR}/open3d/examples") + +# Generate typing stub files (.pyi) and py.typed marker file. +if(WITH_STUBGEN) + if(NOT IGNORE_STUBGEN_ERRORS) + list(APPEND PYBIND11_STUBGEN_FLAGS "--exit-code") + endif() + message(STATUS "Generating typing stubs...") + execute_process( + COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PYTHON_PACKAGE_DST_DIR} + pybind11-stubgen open3d -o ${PYTHON_PACKAGE_DST_DIR} + ${PYBIND11_STUBGEN_FLAGS} + COMMAND_ECHO STDOUT + COMMAND_ERROR_IS_FATAL ANY + ) + file(WRITE "${PYTHON_PACKAGE_DST_DIR}/open3d/py.typed" "") +endif() diff --git a/python/open3d/__init__.py b/python/open3d/__init__.py index 87608701d5c..0a1a00af3a9 100644 --- a/python/open3d/__init__.py +++ b/python/open3d/__init__.py @@ -211,4 +211,4 @@ def _jupyter_nbextension_paths(): if sys.platform == "win32": _win32_dll_dir.close() -del os, sys, CDLL, find_library, Path, warnings, _insert_pybind_names +del os, sys, CDLL, find_library, Path, warnings, _insert_pybind_names, open3d diff --git a/python/requirements_stubgen.txt b/python/requirements_stubgen.txt new file mode 100644 index 00000000000..da78393bcf0 --- /dev/null +++ b/python/requirements_stubgen.txt @@ -0,0 +1 @@ +pybind11-stubgen==2.5.*