Skip to content

Commit 30c789d

Browse files
committed
Use pybind11-stubgen for generating stubs
1 parent 31e2f54 commit 30c789d

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

python/CMakeLists.txt

+35-14
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ file(COPY "${GTSAM_SOURCE_DIR}/examples/Data" DESTINATION "${GTSAM_MODULE_PATH}"
171171
# Add gtsam as a dependency to the install target
172172
set(GTSAM_PYTHON_DEPENDENCIES ${GTSAM_PYTHON_TARGET})
173173

174+
set(GTSAM_PYTHON_INSTALL_EXTRA "")
174175

175176
if(GTSAM_UNSTABLE_BUILD_PYTHON)
176177
set(ignore
@@ -250,6 +251,22 @@ if(GTSAM_UNSTABLE_BUILD_PYTHON)
250251
VERBATIM
251252
)
252253
endif()
254+
255+
add_custom_target(
256+
python-unstable-stubs
257+
COMMAND
258+
${CMAKE_COMMAND} -E env
259+
"PYTHONPATH=${GTSAM_PYTHON_BUILD_DIRECTORY}/$ENV{PYTHONPATH}"
260+
pybind11-stubgen -o . --enum-class-locations \"KernelFunctionType|NoiseFormat:gtsam.gtsam\" --enum-class-locations \"OrderingType:gtsam.gtsam.Ordering\" gtsam_unstable
261+
DEPENDS ${GTSAM_PYTHON_DEPENDENCIES} ${GTSAM_PYTHON_TEST_FILES} ${GTSAM_PYTHON_UNSTABLE_TARGET}
262+
WORKING_DIRECTORY "${GTSAM_PYTHON_BUILD_DIRECTORY}/"
263+
)
264+
265+
if(NOT WIN32)
266+
# Add the stubgen target as a dependency to the install target
267+
list(APPEND GTSAM_PYTHON_INSTALL_EXTRA python-unstable-stubs)
268+
endif()
269+
253270
# Custom make command to run all GTSAM_UNSTABLE Python tests
254271
add_custom_target(
255272
python-test-unstable
@@ -262,26 +279,30 @@ if(GTSAM_UNSTABLE_BUILD_PYTHON)
262279
)
263280
endif()
264281

282+
add_custom_target(
283+
python-stubs
284+
COMMAND
285+
${CMAKE_COMMAND} -E env
286+
"PYTHONPATH=${GTSAM_PYTHON_BUILD_DIRECTORY}/$ENV{PYTHONPATH}"
287+
pybind11-stubgen -o . --enum-class-locations \"KernelFunctionType|NoiseFormat:gtsam.gtsam\" --enum-class-locations \"OrderingType:gtsam.gtsam.Ordering\" gtsam
288+
DEPENDS ${GTSAM_PYTHON_DEPENDENCIES} ${GTSAM_PYTHON_TEST_FILES} ${GTSAM_PYTHON_TARGET}
289+
WORKING_DIRECTORY "${GTSAM_PYTHON_BUILD_DIRECTORY}/"
290+
)
291+
292+
if(NOT WIN32)
293+
# Add the stubgen target as a dependency to the install target
294+
list(APPEND GTSAM_PYTHON_INSTALL_EXTRA python-stubs)
295+
endif()
296+
265297
# Add custom target so we can install with `make python-install`
266298
# Note below we make sure to install with --user iff not in a virtualenv
267299
set(GTSAM_PYTHON_INSTALL_TARGET python-install)
268-
#TODO(Varun) Maybe move the long command to script?
269-
# https://stackoverflow.com/questions/49053544/how-do-i-run-a-python-script-every-time-in-a-cmake-build
270-
if (NOT WIN32) # WIN32=1 is target platform is Windows
271-
add_custom_target(${GTSAM_PYTHON_INSTALL_TARGET}
272-
COMMAND stubgen -q -p gtsam && cp -a out/gtsam/ gtsam && ${PYTHON_EXECUTABLE} -c "import sys, subprocess; cmd = [sys.executable, '-m', 'pip', 'install']; has_venv = hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix); cmd.append('--user' if not has_venv else ''); cmd.append('.'); subprocess.check_call([c for c in cmd if c])"
273-
DEPENDS ${GTSAM_PYTHON_DEPENDENCIES}
274-
WORKING_DIRECTORY ${GTSAM_PYTHON_BUILD_DIRECTORY}
275-
VERBATIM)
276-
else()
277-
#TODO(Varun) Find equivalent cp on Windows
278-
add_custom_target(${GTSAM_PYTHON_INSTALL_TARGET}
300+
301+
add_custom_target(${GTSAM_PYTHON_INSTALL_TARGET}
279302
COMMAND ${PYTHON_EXECUTABLE} -c "import sys, subprocess; cmd = [sys.executable, '-m', 'pip', 'install']; has_venv = hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix); cmd.append('--user' if not has_venv else ''); cmd.append('.'); subprocess.check_call([c for c in cmd if c])"
280-
DEPENDS ${GTSAM_PYTHON_DEPENDENCIES}
303+
DEPENDS ${GTSAM_PYTHON_DEPENDENCIES} ${GTSAM_PYTHON_INSTALL_EXTRA}
281304
WORKING_DIRECTORY ${GTSAM_PYTHON_BUILD_DIRECTORY}
282305
VERBATIM)
283-
endif()
284-
285306

286307
# Custom make command to run all GTSAM Python tests
287308
add_custom_target(

python/dev_requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-r requirements.txt
22
pyparsing>=2.4.2
3-
mypy==1.4.1 #TODO(Varun) A bug in mypy>=1.5.0 causes an unresolved placeholder error when importing numpy>=2.0.0 (https://github.com/python/mypy/issues/17396)
3+
pybind11-stubgen>=2.5.1

0 commit comments

Comments
 (0)