Skip to content

Commit

Permalink
WIP: generates stubs on usual CMake builds, having issues when buildi…
Browse files Browse the repository at this point in the history
…ng through pip
  • Loading branch information
joaovbs96 committed Sep 26, 2024
1 parent 80e1b56 commit 6da6881
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ option(MATERIALX_BUILD_SHARED_LIBS "Build MaterialX libraries as shared rather t
option(MATERIALX_BUILD_MONOLITHIC "Build a single monolithic MaterialX library." OFF)
option(MATERIALX_BUILD_USE_CCACHE "Enable the use of ccache to speed up build time, if present." ON)
option(MATERIALX_PYTHON_LTO "Enable link-time optimizations for MaterialX Python." ON)
option(MATERIALX_PYTHON_STUBS "Enable the automated generation of MaterialX Python '.pyi' stub files through mypy's stubgen." ON)
option(MATERIALX_INSTALL_PYTHON "Install the MaterialX Python package as a third-party library when the install target is built." ON)
option(MATERIALX_INSTALL_RESOURCES "Install the resources folder when building render modules." ON)
option(MATERIALX_TEST_RENDER "Run rendering tests for MaterialX Render module. GPU required for graphics validation." ON)
Expand Down Expand Up @@ -142,6 +143,7 @@ mark_as_advanced(MATERIALX_BUILD_USE_CCACHE)
mark_as_advanced(MATERIALX_NAMESPACE_SUFFIX)
mark_as_advanced(MATERIALX_LIBNAME_SUFFIX)
mark_as_advanced(MATERIALX_PYTHON_LTO)
mark_as_advanced(MATERIALX_PYTHON_STUBS)
mark_as_advanced(MATERIALX_INSTALL_PYTHON)
mark_as_advanced(MATERIALX_INSTALL_RESOURCES)
mark_as_advanced(MATERIALX_TEST_RENDER)
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
# Use a fixed version because we use an experimental feature
# (a custom plugin) and for now that functionality has
# no compatibility promises.
requires = ["scikit-build-core==0.4.4"]
requires = [
"scikit-build-core==0.4.4",
# For generating .pyi stub files:
"mypy==1.11.2"
]
build-backend = "scikit_build_core.build"

[project]
Expand Down Expand Up @@ -71,6 +75,7 @@ sdist.exclude = [
[tool.scikit-build.cmake.define]
MATERIALX_BUILD_SHARED_LIBS = 'OFF' # Be explicit
MATERIALX_BUILD_PYTHON = 'ON'
MATERIALX_PYTHON_STUBS = 'ON'
MATERIALX_TEST_RENDER = 'OFF'
MATERIALX_WARNINGS_AS_ERRORS = 'ON'
MATERIALX_BUILD_TESTS = 'OFF'
Expand Down
32 changes: 32 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ if(MATERIALX_PYTHON_OCIO_DIR)
install(DIRECTORY "${MATERIALX_PYTHON_OCIO_DIR}/" DESTINATION "${MATERIALX_PYTHON_FOLDER_NAME}/config/" MESSAGE_NEVER)
endif()

if (MATERIALX_PYTHON_STUBS)
# attempts to find mypy stubgen executable
find_program(_MYPY_STUBGEN_EXECUTABLE "stubgen")
message("Found stubgen executable: _MYPY_STUBGEN_EXECUTABLE=${MATERIALX_PYTHON_STUBS}")

# check if stubgen can be accessed
execute_process(
COMMAND ${_MYPY_STUBGEN_EXECUTABLE} --help
OUTPUT_VARIABLE _MYPY_STUBGEN_HELP_OUTPUT
ERROR_VARIABLE _MYPY_STUBGEN_HELP_OUTPUT
RESULT_VARIABLE _MYPY_STUBGEN_HELP_EXITCODE
)

# build stubs if stubgen could be found properly
if (_MYPY_STUBGEN_HELP_EXITCODE EQUAL "0")
install(CODE "
execute_process(
COMMAND ${_MYPY_STUBGEN_EXECUTABLE} -p MaterialX --inspect-mode -o \"${CMAKE_INSTALL_PREFIX}/python\" -v --ignore-errors
WORKING_DIRECTORY \"${CMAKE_INSTALL_PREFIX}/python\")
")
else ()
# prints errors if mypy couldn't be found
# TODO: should this be a fatal error or?
message(
FATAL_ERROR
"MATERIALX_PYTHON_STUBS=${MATERIALX_PYTHON_STUBS} but"
" mypy stubgen not found: status=${_MYPY_STUBGEN_HELP_EXITCODE}:"
" ${_MYPY_STUBGEN_HELP_OUTPUT}"
)
endif ()
endif()

if(MATERIALX_INSTALL_PYTHON AND PYTHON_EXECUTABLE AND NOT SKBUILD)
set(SETUP_PY "${CMAKE_INSTALL_PREFIX}/python/setup.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in" "${SETUP_PY}")
Expand Down

0 comments on commit 6da6881

Please sign in to comment.