Skip to content

Commit

Permalink
Upgrade pybind11
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Nov 5, 2024
1 parent 2ade1d1 commit 3125915
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 87 deletions.
2 changes: 1 addition & 1 deletion QPALM/interfaces/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ option(QPALM_WITH_PY_STUBS
if (QPALM_WITH_PY_STUBS AND NOT CMAKE_CROSSCOMPILING)
include(cmake/Pybind11Stubgen.cmake)
pybind11_stubgen(_qpalm)
pybind11_stubgen_install(_qpalm ${PY_BUILD_CMAKE_MODULE_NAME})
pybind11_stubgen_install(_qpalm)
endif()
21 changes: 13 additions & 8 deletions QPALM/interfaces/python/cmake/Pybind11Stubgen.cmake
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
function(pybind11_stubgen target)

find_package(Python3 REQUIRED COMPONENTS Interpreter)
set_target_properties(${target} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "$<CONFIG>/${PY_BUILD_CMAKE_MODULE_NAME}")
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E touch ${PY_BUILD_CMAKE_MODULE_NAME}/__init__.py
COMMAND ${Python3_EXECUTABLE} -m pybind11_stubgen
$<TARGET_FILE_BASE_NAME:${target}>
--bare-numpy-ndarray
--no-setup-py
-o ${CMAKE_CURRENT_BINARY_DIR}
WORKING_DIRECTORY $<TARGET_FILE_DIR:${target}>
${PY_BUILD_CMAKE_MODULE_NAME}.$<TARGET_FILE_BASE_NAME:${target}>
--numpy-array-remove-parameters
--ignore-invalid-expressions \\\\?
--ignore-unresolved-names m\\|n
--exit-code
-o ${CMAKE_CURRENT_BINARY_DIR}/stubs/$<TARGET_FILE_BASE_NAME:${target}>
WORKING_DIRECTORY $<TARGET_FILE_DIR:${target}>/..
USES_TERMINAL)

endfunction()

function(pybind11_stubgen_install target destination)
function(pybind11_stubgen_install target)

install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_BASE_NAME:${target}>-stubs/
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/stubs/$<TARGET_FILE_BASE_NAME:${target}>/${PY_BUILD_CMAKE_MODULE_NAME}
EXCLUDE_FROM_ALL
COMPONENT python_stubs
DESTINATION ${destination}/$<TARGET_FILE_BASE_NAME:${target}>
DESTINATION .
FILES_MATCHING REGEX "\.pyi$")

endfunction()
77 changes: 11 additions & 66 deletions QPALM/interfaces/python/cmake/QueryPythonForPybind11.cmake
Original file line number Diff line number Diff line change
@@ -1,74 +1,19 @@
option(USE_GLOBAL_PYBIND11 "Don't query Python to find pybind11" Off)
mark_as_advanced(USE_GLOBAL_PYBIND11)

# Find Python
if (CMAKE_CROSSCOMPILING AND FALSE)
find_package(Python3 REQUIRED COMPONENTS Development.Module)
else()
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
endif()

# First tries to find Python 3, then tries to import the pybind11 module to
# query the CMake config location, and finally imports pybind11 using
# find_package(pybind11 REQUIRED CONFIG CMAKE_FIND_ROOT_PATH_BOTH).
function(find_pybind11_python_first)
# find_package(pybind11 ${ARGN} REQUIRED CONFIG CMAKE_FIND_ROOT_PATH_BOTH),
# where ${ARGN} are the arguments passed to this macro.
macro(find_pybind11_python_first)

# Tweak extension suffix and debug ABI when cross-compiling
if (CMAKE_CROSSCOMPILING)
if (NOT PY_BUILD_EXT_SUFFIX AND DEFINED TOOLCHAIN_Python3_EXT_SUFFIX)
set(PY_BUILD_EXT_SUFFIX ${TOOLCHAIN_Python3_EXT_SUFFIX})
endif()
# SETUPTOOLS_EXT_SUFFIX environment variable
if (NOT PY_BUILD_EXT_SUFFIX AND DEFINED ENV{SETUPTOOLS_EXT_SUFFIX})
message(STATUS "Setting PY_BUILD_EXT_SUFFIX to "
"ENV{SETUPTOOLS_EXT_SUFFIX}: $ENV{SETUPTOOLS_EXT_SUFFIX}")
set(PY_BUILD_EXT_SUFFIX $ENV{SETUPTOOLS_EXT_SUFFIX})
endif()
# If that still didn't work, use the Python3_SOABI variable:
if (NOT PY_BUILD_EXT_SUFFIX AND Python3_SOABI)
message(STATUS "Determining Python extension suffix based on "
"Python3_SOABI.")
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(PY_BUILD_EXTENSION ".pyd")
else()
set(PY_BUILD_EXTENSION "${CMAKE_SHARED_MODULE_SUFFIX}")
endif()
set(PY_BUILD_EXT_SUFFIX ".${Python3_SOABI}${PY_BUILD_EXTENSION}")
endif()
# Sanity checks:
if (NOT PY_BUILD_EXT_SUFFIX)
message(FATAL_ERROR "Unable to determine extension suffix.\
Try manually setting PY_BUILD_EXT_SUFFIX.")
endif()
if (Python3_SOABI AND
NOT PY_BUILD_EXT_SUFFIX MATCHES "\\.${Python3_SOABI}\\.")
message(WARNING "PY_BUILD_EXT_SUFFIX (${PY_BUILD_EXT_SUFFIX}) "
"does not match Python3_SOABI (${Python3_SOABI})")
endif()
# Check the debug ABI:
if (NOT PY_BUILD_DEBUG_ABI AND DEFINED TOOLCHAIN_Python3_DEBUG_ABI)
set(PY_BUILD_DEBUG_ABI ${TOOLCHAIN_Python3_DEBUG_ABI})
endif()
# Otherwise, try to deduce it from the SOABI:
if (NOT DEFINED PY_BUILD_DEBUG_ABI)
if (PY_BUILD_EXT_SUFFIX MATCHES "[0-9]+d-")
set(PY_BUILD_DEBUG_ABI true)
else()
set(PY_BUILD_DEBUG_ABI false)
endif()
endif()
# Cache the result:
set(PY_BUILD_EXT_SUFFIX ${PY_BUILD_EXT_SUFFIX} CACHE STRING
"The extension for Python extension modules")
set(PY_BUILD_DEBUG_ABI ${PY_BUILD_DEBUG_ABI} CACHE BOOL
"Whether to compile for a debug version of Python")
# Override pybind11NewTools.cmake's PYTHON_MODULE_EXTENSION variable:
message(STATUS "Python extension suffix: ${PY_BUILD_EXT_SUFFIX}")
message(STATUS "Python debug ABI: ${PY_BUILD_DEBUG_ABI}")
set(PYTHON_MODULE_EXTENSION ${PY_BUILD_EXT_SUFFIX}
CACHE INTERNAL "" FORCE)
set(PYTHON_IS_DEBUG ${PY_BUILD_DEBUG_ABI}
CACHE INTERNAL "" FORCE)
set(PYBIND11_USE_CROSSCOMPILING On)

# Find Python
if (CMAKE_CROSSCOMPILING AND NOT (APPLE AND "$ENV{CIBUILDWHEEL}" STREQUAL "1"))
find_package(Python3 REQUIRED COMPONENTS Development.Module)
else()
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
endif()

# Query Python to see if it knows where the pybind11 root is
Expand All @@ -94,4 +39,4 @@ function(find_pybind11_python_first)
# pybind11 is header-only, so finding a native version is fine
find_package(pybind11 ${ARGN} REQUIRED CONFIG CMAKE_FIND_ROOT_PATH_BOTH)

endfunction()
endmacro()
8 changes: 5 additions & 3 deletions QPALM/interfaces/python/qpalm-debug/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ description = "Debug symbols for the qpalm package."

[build-system]
requires = [
"py-build-cmake~=0.2.0a7",
"pybind11>=2.10.1,!=2.10.2,!=2.10.3,!=2.10.4,!=2.11.*",
"pybind11-stubgen~=0.16.2",
"py-build-cmake~=0.2.0a14",
"pybind11==2.13.6",
"pybind11-stubgen==2.5.1",
"numpy>=1.19,<2",
"scipy>=1.6,<2",
]
build-backend = "py_build_cmake.build_component"

Expand Down
10 changes: 5 additions & 5 deletions QPALM/interfaces/python/qpalm.py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ PYBIND11_MODULE(MODULE_NAME, m) {
#endif
ladel_set_print_config_printf(&print_wrap);

py::class_<::QPALMData>(m, "_QPALMData");
py::class_<::QPALMWorkspace>(m, "_QPALMWorkspace");
py::class_<qpalm::Data>(m, "Data")
.def(py::init<qpalm::index_t, qpalm::index_t>(), "n"_a, "m"_a)
.def_property(
Expand Down Expand Up @@ -264,11 +266,9 @@ PYBIND11_MODULE(MODULE_NAME, m) {
py::cpp_function( // https://github.com/pybind/pybind11/issues/2618
&qpalm::Solver::get_dual_inf_certificate,
py::return_value_policy::copy, py::keep_alive<0, 1>()))
.def(
"_get_c_work_ptr",
[](qpalm::Solver &self) -> const void * { return self.get_c_work_ptr(); },
"Return a pointer to the C workspace struct (of type ::QPALMWorkspace).",
py::return_value_policy::reference_internal);
.def("_get_c_work_ptr", &qpalm::Solver::get_c_work_ptr,
"Return a pointer to the C workspace struct (of type ::QPALMWorkspace).",
py::return_value_policy::reference_internal);
}

static int print_wrap(const char *fmt, ...) {
Expand Down
10 changes: 6 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[build-system]
requires = [
"py-build-cmake~=0.2.0a7",
"pybind11>=2.10.1,!=2.10.2,!=2.10.3,!=2.10.4,!=2.11.*",
"pybind11-stubgen~=0.16.2",
"py-build-cmake~=0.2.0a14",
"pybind11==2.13.6",
"pybind11-stubgen==2.5.1",
"numpy>=1.19,<2",
"scipy>=1.6,<2",
]
build-backend = "py_build_cmake.build"

Expand Down Expand Up @@ -46,7 +48,7 @@ dynamic = ["version", "description"]

[project.optional-dependencies]
debug = ["qpalm-debug==1.2.4.dev0"]
test = ["pytest>=7.2.0,<7.5"]
test = ["pytest>=7.2.0,<9"]

[project.urls]
Source = "https://github.com/kul-optec/QPALM"
Expand Down

0 comments on commit 3125915

Please sign in to comment.