Do not build pybind11-based python module(will be deprecated). Use C API + ctypes based python module instead.

This commit is contained in:
Syoyo Fujita
2023-05-05 23:09:46 +09:00
parent 685eda7841
commit 59d16e5041
6 changed files with 62 additions and 29 deletions

View File

@@ -980,37 +980,53 @@ endif()
if(TINYUSDZ_WITH_PYTHON)
# For the time beging, we stick with pybind11, since PyPI manylinux2014(probably mostly used architectrue as of 2022/Aug) does not support C++17.
# We may switch to nanobind at some point(probably around 2024?)
# TODO: Fully Remove pybind11-based python binding
## For the time beging, we stick with pybind11, since PyPI manylinux2014(probably mostly used architectrue as of 2022/Aug) does not support C++17.
## We may switch to nanobind at some point(probably around 2024?)
# build monolithic .dll
#nanobind_add_module(${BUILD_TARGET_PY} ${TINYUSDZ_PYTHON_BINDING_SOURCES})
## build monolithic .dll
##nanobind_add_module(${BUILD_TARGET_PY} ${TINYUSDZ_PYTHON_BINDING_SOURCES})
pybind11_add_module(${BUILD_TARGET_PY} ${TINYUSDZ_PYTHON_BINDING_SOURCES})
add_sanitizers(${BUILD_TARGET_PY})
target_include_directories(
${BUILD_TARGET_PY} PRIVATE ${PROJECT_SOURCE_DIR}/src)
#pybind11_add_module(${BUILD_TARGET_PY} ${TINYUSDZ_PYTHON_BINDING_SOURCES})
#add_sanitizers(${BUILD_TARGET_PY})
#target_include_directories(
# ${BUILD_TARGET_PY} PRIVATE ${PROJECT_SOURCE_DIR}/src)
target_link_libraries(${BUILD_TARGET_PY} PRIVATE ${TINYUSDZ_TARGET_STATIC})
#target_link_libraries(${BUILD_TARGET_PY} PRIVATE ${TINYUSDZ_TARGET_STATIC})
# Use 'c' prefix
# TODO: Use `cpp` prefix?
set_target_properties(${BUILD_TARGET_PY} PROPERTIES OUTPUT_NAME "ctinyusdz")
## Use 'c' prefix
## TODO: Use `cpp` prefix?
#set_target_properties(${BUILD_TARGET_PY} PROPERTIES OUTPUT_NAME "ctinyusdz")
# copy python binding .so file to python/
## copy python binding .so file to python/
## For developer
## NOTE: `POST_BUILD` command is not triggered when building python module using
## `python setup.py build`
#add_custom_command(
# TARGET ${BUILD_TARGET_PY}
# POST_BUILD
# COMMAND "${CMAKE_COMMAND}" -E copy "$<TARGET_FILE:${BUILD_TARGET_PY}>"
# "${CMAKE_SOURCE_DIR}/python/tinyusdz/$<TARGET_FILE_NAME:${BUILD_TARGET_PY}>"
# COMMENT "copying python module file to python/tinyusdz"
# VERBATIM)
## For pypi packaging
#install(TARGETS ${BUILD_TARGET_PY} LIBRARY DESTINATION tinyusdz)
# copy C tinyusd .so file to python/tinyusdz/
# For developer
# NOTE: `POST_BUILD` command is not triggered when building python module using
# `python setup.py build`
add_custom_command(
TARGET ${BUILD_TARGET_PY}
TARGET ${BUILD_TARGET_C}
POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy "$<TARGET_FILE:${BUILD_TARGET_PY}>"
"${CMAKE_SOURCE_DIR}/python/$<TARGET_FILE_NAME:${BUILD_TARGET_PY}>"
COMMENT "copying python module file to python/"
COMMAND "${CMAKE_COMMAND}" -E copy "$<TARGET_FILE:${BUILD_TARGET_C}>"
"${CMAKE_SOURCE_DIR}/python/tinyusdz/$<TARGET_FILE_NAME:${BUILD_TARGET_C}>"
COMMENT "Copying libc-tinyusd.so/c-tinyusd.dll file to <tinyusdz>/python/tinyusdz"
VERBATIM)
# For pypi packaging
install(TARGETS ${BUILD_TARGET_PY} LIBRARY DESTINATION tinyusdz)
# TODO: Run ctypesgen?
endif()

View File

@@ -73,6 +73,20 @@ $ python setup.py build
# Then copy `./_skbuild/<arch>-<version>/cmake-install/tinyusdz/ctinyusdz.*.so/dll to `<tinyusdz>/python` folder.
```
### Re-generate ctinyusdz.py
When TinyUSDZ C API has been updated, need to re-genrerate `<tinyusdz>/python/tinyusdz/ctinyusd.py`
using ctypesgen https://github.com/ctypesgen/ctypesgen .
```
# if you do not install ctypesgen
$ python -m pip install ctypesgen
$ cd <tinyusdz>/python
$ sh gen-ctypes.sh
```
### Asan support
If you built ctinyusdz with ASAN enabled, use `LD_PRELOAD` to load asan modules.

1
python/gen-ctypes.sh Normal file
View File

@@ -0,0 +1 @@
ctypesgen -lc-tinyusd ../src/c-tinyusd.h -o tinyusdz/ctinyusd.py

View File

@@ -19,6 +19,13 @@ from .compat_typing_extensions import Literal, TypeAlias
from . import version
from .prims import Prim
try:
from . import ctinyusd
except ImportError:
import warnings
warnings.warn(
"Failed to import native module `ctinyusd`(No corresponding dll/so exists?). Loading USDA/USDC/USDZ feature is disabled.")
FILE_LIKE: TypeAlias = Union[str, os.PathLike, IO[str], IO[bytes]]
try:
@@ -32,12 +39,6 @@ except ImportError:
return cls
try:
import ctinyusdz
except ImportError:
import warnings
warnings.warn(
"Failed to import native module `ctinyusdz`(No corresponding dll/so exists?). Loading USDA/USDC/USDZ feature is disabled.")
try:
import numpy as np
@@ -49,10 +50,11 @@ try:
except:
pass
def is_ctinyusdz_available():
def is_ctinyusd_available():
import importlib.util
if importlib.util.find_spec("ctinyusdz"):
# Seems '.' prefix required for relative module
if importlib.util.find_spec(".ctinyusd", package='tinyusdz'):
return True
return False

View File

@@ -5,7 +5,8 @@ print("TinyUSDZ major_version", tinyusdz.version.major_version) # int
print("TinyUSDZ minor_version", tinyusdz.version.minor_version) # int
print("TinyUSDZ micro_version", tinyusdz.version.micro_version) # int
print("ctinyusdz(Native TinyUSDZ module) available? ", tinyusdz.is_ctinyusdz_available()) # bool
# NOTE: no 'z' suffix.
print("ctinyusd(Native TinyUSDZ module) available? ", tinyusdz.is_ctinyusd_available()) # bool
print("typeguard available? ", tinyusdz.is_typeguard_available()) # bool
print("numpy available? ", tinyusdz.is_numpy_available()) # bool
print("pandas available? ", tinyusdz.is_pandas_available()) # bool

View File

@@ -1 +0,0 @@
ctypesgen -lc-tinyusd ../../src/c-tinyusd.h -o ctinyusd.py