From 93df9d5d0bc6454e4922541cfaa5bad95057550a Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Fri, 11 Oct 2024 14:05:14 -0700 Subject: [PATCH] `PYBIND11_FINDPYTHON=ON` (#1684) Reuse our `find_package(Python ...)` call and use new CMake logic in pybind11. https://pybind11.readthedocs.io/en/stable/compiling.html#modules-with-cmake https://cmake.org/cmake/help/latest/command/find_package.html#config-mode-version-selection Signed-off-by: Axel Huebl --- cmake/dependencies/pybind11.cmake | 5 +++++ setup.py | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/cmake/dependencies/pybind11.cmake b/cmake/dependencies/pybind11.cmake index 6719a38c37..70899de816 100644 --- a/cmake/dependencies/pybind11.cmake +++ b/cmake/dependencies/pybind11.cmake @@ -16,6 +16,11 @@ function(find_pybind11) message(STATUS "pybind11 repository: ${openPMD_pybind11_repo} (${openPMD_pybind11_branch})") endif() endif() + + # rely on our find_package(Python ...) call + # https://pybind11.readthedocs.io/en/stable/compiling.html#modules-with-cmake + set(PYBIND11_FINDPYTHON ON) + if(TARGET pybind11::module) # nothing to do, target already exists in the superbuild elseif(openPMD_USE_INTERNAL_PYBIND11 AND openPMD_pybind11_src) diff --git a/setup.py b/setup.py index 4ecbfc5c68..ac0d86fcb7 100644 --- a/setup.py +++ b/setup.py @@ -44,12 +44,19 @@ def build_extension(self, ext): if not extdir.endswith(os.path.sep): extdir += os.path.sep + pyv = sys.version_info cmake_args = [ + # Python: use the calling interpreter in CMake + # https://cmake.org/cmake/help/latest/module/FindPython.html#hints + # https://cmake.org/cmake/help/latest/command/find_package.html#config-mode-version-selection + '-DPython_ROOT_DIR=' + sys.prefix, + f'-DPython_FIND_VERSION={pyv.major}.{pyv.minor}.{pyv.micro}', + '-DPython_FIND_VERSION_EXACT=TRUE', + '-DPython_FIND_STRATEGY=LOCATION', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + os.path.join(extdir, "openpmd_api"), # '-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=' + extdir, '-DopenPMD_PYTHON_OUTPUT_DIRECTORY=' + extdir, - '-DPython_EXECUTABLE=' + sys.executable, '-DopenPMD_USE_PYTHON:BOOL=ON', # variants '-DopenPMD_USE_MPI:BOOL=' + openPMD_USE_MPI,