Skip to content

Commit aea2f69

Browse files
committed
- adjust sizeof long and void_p for emscripten if needed
1 parent c5ab56f commit aea2f69

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

‎setup.py‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,34 @@
3636
from setuptools import setup, Extension
3737
from setuptools.command.build_ext import build_ext
3838

39-
def get_python_include():
39+
def get_python_include(is_emscripten=False):
4040
temp = os.getenv('PYTHON_INCLUDE_DIR')
4141
if temp:
42-
return temp
42+
path = temp
43+
else:
44+
path = sysconfig.get_paths().get('include')
45+
if not path or not exists(path):
46+
# for whatever reason 2.7 on centos returns a wrong path here
47+
path = sysconfig.get_config_vars().get('INCLUDEPY')
48+
49+
# Automatically patch pyconfig.h if 32-bit wasm sizes are incorrectly set to 8
50+
if is_emscripten and path and exists(path):
51+
pyconfig_path = os.path.join(path, 'pyconfig.h')
52+
if exists(pyconfig_path):
53+
try:
54+
with open(pyconfig_path, 'r', encoding='utf-8') as f:
55+
content = f.read()
56+
57+
updated = content.replace('#define SIZEOF_LONG 8', '#define SIZEOF_LONG 4')
58+
updated = updated.replace('#define SIZEOF_VOID_P 8', '#define SIZEOF_VOID_P 4')
59+
60+
if updated != content:
61+
with open(pyconfig_path, 'w', encoding='utf-8') as f:
62+
f.write(updated)
63+
except Exception:
64+
pass
4365

44-
path = sysconfig.get_paths()['include']
45-
if exists(path):
46-
return path
47-
# for whatever reason 2.7 on centos returns a wrong path here
48-
return sysconfig.get_config_vars()['INCLUDEPY']
66+
return path
4967

5068
def get_win_python_lib():
5169
vars = sysconfig.get_config_vars()
@@ -187,7 +205,8 @@ def build_cmake(self, extension):
187205
suffix = suffix[:suffix.rfind('/')]
188206
if '\\' in suffix:
189207
suffix = suffix[:suffix.rfind('\\')]
190-
208+
209+
is_emscripten = 'emscripten' in suffix
191210
ext_dir = self.get_ext_fullpath(extension.name)
192211
makedirs(build_temp)
193212
target_lib_path = abspath(ext_dir)
@@ -272,7 +291,7 @@ def build_cmake(self, extension):
272291
'-DWITH_ZLIB=ON',
273292
'-DWITH_PYTHON=ON',
274293
'-DPYTHON_EXECUTABLE=' + sys.executable,
275-
'-DPYTHON_INCLUDE_DIR=' + get_python_include()
294+
'-DPYTHON_INCLUDE_DIR=' + get_python_include(is_emscripten)
276295
]
277296

278297

0 commit comments

Comments
 (0)