|
36 | 36 | from setuptools import setup, Extension |
37 | 37 | from setuptools.command.build_ext import build_ext |
38 | 38 |
|
39 | | -def get_python_include(): |
| 39 | +def get_python_include(is_emscripten=False): |
40 | 40 | temp = os.getenv('PYTHON_INCLUDE_DIR') |
41 | 41 | 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 |
43 | 65 |
|
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 |
49 | 67 |
|
50 | 68 | def get_win_python_lib(): |
51 | 69 | vars = sysconfig.get_config_vars() |
@@ -187,7 +205,8 @@ def build_cmake(self, extension): |
187 | 205 | suffix = suffix[:suffix.rfind('/')] |
188 | 206 | if '\\' in suffix: |
189 | 207 | suffix = suffix[:suffix.rfind('\\')] |
190 | | - |
| 208 | + |
| 209 | + is_emscripten = 'emscripten' in suffix |
191 | 210 | ext_dir = self.get_ext_fullpath(extension.name) |
192 | 211 | makedirs(build_temp) |
193 | 212 | target_lib_path = abspath(ext_dir) |
@@ -272,7 +291,7 @@ def build_cmake(self, extension): |
272 | 291 | '-DWITH_ZLIB=ON', |
273 | 292 | '-DWITH_PYTHON=ON', |
274 | 293 | '-DPYTHON_EXECUTABLE=' + sys.executable, |
275 | | - '-DPYTHON_INCLUDE_DIR=' + get_python_include() |
| 294 | + '-DPYTHON_INCLUDE_DIR=' + get_python_include(is_emscripten) |
276 | 295 | ] |
277 | 296 |
|
278 | 297 |
|
|
0 commit comments