From 8507f39dc8b27e6e3a2c9e44c41b720d6e10f1a1 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Tue, 23 Jun 2026 16:28:14 -0700 Subject: [PATCH] `setup.py`: Emscripten Support a cross-compiled Python include in WASM Pyodide Emscripten builds in `setup.py`. Try to auto-derive from environment hints and platform in the sysconfig of the Python interpreter. --- setup.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index 94d0acb003..8f7ccee1ad 100644 --- a/setup.py +++ b/setup.py @@ -3,6 +3,7 @@ import re import subprocess import sys +import sysconfig from setuptools import Extension, setup from setuptools.command.build_ext import build_ext @@ -45,14 +46,28 @@ def build_extension(self, ext): 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', + # cross-compiling (e.g. Pyodide)? host & target Python differ + emscripten = sysconfig.get_platform().startswith('emscripten') + cmake_args = [] + # FindPython: pin to the calling interpreter unless the caller + # overrides via openPMD_CMAKE_Python_*. Cross builds keep these host + # hints (to resolve the interpreter/library for Development.Module), + # but override the target headers (below) and relax the exact-version + # match. See: + # https://cmake.org/cmake/help/latest/module/FindPython.html#hints + if emscripten or not \ + any(k.startswith('openPMD_CMAKE_Python_') for k in os.environ): + cmake_args += [ + '-DPython_ROOT_DIR=' + sys.prefix, + f'-DPython_FIND_VERSION={pyv.major}.{pyv.minor}.{pyv.micro}', + '-DPython_FIND_VERSION_EXACT=' + ( + 'FALSE' if emscripten else 'TRUE'), + '-DPython_FIND_STRATEGY=LOCATION', + ] + if emscripten: + cmake_args += ['-DPython_INCLUDE_DIR=' + + sysconfig.get_config_var('INCLUDEPY')] + cmake_args += [ '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + os.path.join(extdir, "openpmd_api"), # '-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=' + extdir,