File: build_setup.py

package info (click to toggle)
mypy 1.19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,412 kB
  • sloc: python: 114,754; ansic: 13,343; cpp: 11,380; makefile: 257; sh: 28
file content (62 lines) | stat: -rw-r--r-- 2,165 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
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")


def spawn(self, cmd, **kwargs) -> None:  # type: ignore[no-untyped-def]
    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]