File: build_setup.py

package info (click to toggle)
python-librt 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 920 kB
  • sloc: ansic: 13,889; python: 293; makefile: 6
file content (76 lines) | stat: -rw-r--r-- 2,830 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# This file must have the same content for mypyc/build_setup.py and lib-rt/build_setup.py,
# it exists to work around absence of support for per-file compile flags in setuptools.
# The version in mypyc/ is the source of truth, and should be copied to lib-rt if modified.

import os
import platform
import sys

try:
    # Import setuptools so that it monkey-patch overrides distutils
    import setuptools  # noqa: F401
except ImportError:
    pass

if sys.version_info >= (3, 12):
    # From setuptools' monkeypatch
    from distutils import ccompiler  # type: ignore[import-not-found]
else:
    from distutils import ccompiler

EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT = {
    "unix": {
        "base64/arch/ssse3": ["-mssse3"],
        "base64/arch/sse41": ["-msse4.1"],
        "base64/arch/sse42": ["-msse4.2"],
        "base64/arch/avx2": ["-mavx2"],
        "base64/arch/avx": ["-mavx"],
    },
    "msvc": {
        "base64/arch/sse42": ["/arch:SSE4.2"],
        "base64/arch/avx2": ["/arch:AVX2"],
        "base64/arch/avx": ["/arch:AVX"],
    },
}

ccompiler.CCompiler.__spawn = ccompiler.CCompiler.spawn  # type: ignore[attr-defined]
X86_64 = platform.machine() in ("x86_64", "AMD64", "amd64")
PYODIDE = "PYODIDE" in os.environ


def spawn(self, cmd, **kwargs) -> None:  # type: ignore[no-untyped-def]
    if PYODIDE:
        new_cmd = list(cmd)
        for argument in reversed(new_cmd):
            if not str(argument).endswith(".c"):
                continue
            if "base64/arch/" in str(argument):
                new_cmd.extend(["-msimd128"])
    else:
        compiler_type: str = self.compiler_type
        extra_options = EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT[compiler_type]
        new_cmd = list(cmd)
        if X86_64 and extra_options is not None:
            # filenames are closer to the end of command line
            for argument in reversed(new_cmd):
                # Check if the matching argument contains a source filename.
                if not str(argument).endswith(".c"):
                    continue

                for path in extra_options.keys():
                    if path in str(argument):
                        if compiler_type == "bcpp":
                            compiler = new_cmd.pop()
                            # Borland accepts a source file name at the end,
                            # insert the options before it
                            new_cmd.extend(extra_options[path])
                            new_cmd.append(compiler)
                        else:
                            new_cmd.extend(extra_options[path])

                        # path component is found, no need to search any further
                        break
    self.__spawn(new_cmd, **kwargs)


ccompiler.CCompiler.spawn = spawn  # type: ignore[method-assign]