| 1 |
# This file must have the same content for mypyc/build_setup.py and lib-rt/build_setup.py, |
| 2 |
# it exists to work around absence of support for per-file compile flags in setuptools. |
| 3 |
# The version in mypyc/ is the source of truth, and should be copied to lib-rt if modified. |
| 4 |
|
| 5 |
import os |
| 6 |
import platform |
| 7 |
import sys |
| 8 |
|
| 9 |
try: |
| 10 |
# Import setuptools so that it monkey-patch overrides distutils |
| 11 |
import setuptools # noqa: F401 |
| 12 |
except ImportError: |
| 13 |
pass |
| 14 |
|
| 15 |
if sys.version_info >= (3, 12): |
| 16 |
# From setuptools' monkeypatch |
| 17 |
from distutils import ccompiler # type: ignore[import-not-found] |
| 18 |
else: |
| 19 |
from distutils import ccompiler |
| 20 |
|
| 21 |
EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT = { |
| 22 |
"msvc": { |
| 23 |
"base64/arch/sse42": ["/arch:SSE4.2"], |
| 24 |
"base64/arch/avx2": ["/arch:AVX2"], |
| 25 |
"base64/arch/avx": ["/arch:AVX"], |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
ccompiler.CCompiler.__spawn = ccompiler.CCompiler.spawn # type: ignore[attr-defined] |
| 30 |
X86_64 = platform.machine() in ("x86_64", "AMD64", "amd64") |
| 31 |
PYODIDE = "PYODIDE" in os.environ |
| 32 |
NO_EXTRA_FLAGS = "MYPYC_NO_EXTRA_FLAGS" in os.environ |
| 33 |
|
| 34 |
|
| 35 |
def spawn(self, cmd, **kwargs) -> None: # type: ignore[no-untyped-def] |
| 36 |
new_cmd = list(cmd) |
| 37 |
if PYODIDE: |
| 38 |
for argument in reversed(new_cmd): |
| 39 |
if not str(argument).endswith(".c"): |
| 40 |
continue |
| 41 |
if "base64/arch/" in str(argument): |
| 42 |
new_cmd.extend(["-msimd128"]) |
| 43 |
elif not NO_EXTRA_FLAGS: |
| 44 |
compiler_type: str = self.compiler_type |
| 45 |
extra_options = EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT.get(compiler_type, None) |
| 46 |
if X86_64 and extra_options is not None: |
| 47 |
# filenames are closer to the end of command line |
| 48 |
for argument in reversed(new_cmd): |
| 49 |
# Check if the matching argument contains a source filename. |
| 50 |
if not str(argument).endswith(".c"): |
| 51 |
continue |
| 52 |
|
| 53 |
for path in extra_options.keys(): |
| 54 |
if path in str(argument): |
| 55 |
if compiler_type == "bcpp": |
| 56 |
compiler = new_cmd.pop() |
| 57 |
# Borland accepts a source file name at the end, |
| 58 |
# insert the options before it |
| 59 |
new_cmd.extend(extra_options[path]) |
| 60 |
new_cmd.append(compiler) |
| 61 |
else: |
| 62 |
new_cmd.extend(extra_options[path]) |
| 63 |
|
| 64 |
# path component is found, no need to search any further |
| 65 |
break |
| 66 |
self.__spawn(new_cmd, **kwargs) |
| 67 |
|
| 68 |
|
| 69 |
ccompiler.CCompiler.spawn = spawn # type: ignore[method-assign] |