Skip to content

Commit cca1dc2

Browse files
committed
split out the monkeypatching
1 parent a861a3b commit cca1dc2

File tree

3 files changed

+61
-47
lines changed

3 files changed

+61
-47
lines changed

mypyc/build.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from mypyc.ir.pprint import format_modules
4242
from mypyc.namegen import exported_name
4343
from mypyc.options import CompilerOptions
44-
44+
import mypyc.build_setup
4545

4646
class ModDesc(NamedTuple):
4747
module: str
@@ -122,52 +122,6 @@ class ModDesc(NamedTuple):
122122
else:
123123
from distutils import ccompiler, sysconfig
124124

125-
EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT = {
126-
"unix": {
127-
"base64/arch/ssse3": "-mssse3",
128-
"base64/arch/sse41": "-msse4.1",
129-
"base64/arch/sse42": "-msse4.2",
130-
"base64/arch/avx2": "-mavx2",
131-
"base64/arch/avx": "-mavx",
132-
},
133-
"msvc": {
134-
"base64/arch/sse42": "/arch:SSE4.2",
135-
"base64/arch/avx2": "/arch:AVX2",
136-
"base64/arch/avx": "/arch:AVX",
137-
},
138-
}
139-
140-
__spawn = ccompiler.CCompiler.spawn
141-
142-
143-
def spawn(self, cmd, **kwargs): # type: ignore[no-untyped-def]
144-
compiler_type: str = self.compiler_type
145-
extra_options = EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT[compiler_type]
146-
new_cmd = list(cmd)
147-
if extra_options is not None:
148-
# filenames are closer to the end of command line
149-
for argument in reversed(new_cmd):
150-
# Check if argument contains a filename. We must check for all
151-
# possible extensions; checking for target extension is faster.
152-
if self.obj_extension and not str(argument).endswith(self.obj_extension):
153-
continue
154-
155-
for path in extra_options.keys():
156-
if path in str(argument):
157-
if compiler_type == "bcpp":
158-
# Borland accepts a source file name at the end,
159-
# insert the options before it
160-
new_cmd[-1:-1] = extra_options[path]
161-
else:
162-
new_cmd.append(extra_options[path])
163-
164-
# path component is found, no need to search any further
165-
break
166-
__spawn(self, new_cmd, **kwargs)
167-
168-
169-
ccompiler.CCompiler.spawn = spawn # type: ignore[method-assign]
170-
171125

172126
def get_extension() -> type[Extension]:
173127
# We can work with either setuptools or distutils, and pick setuptools

mypyc/build_setup.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import sys
2+
3+
try:
4+
# Import setuptools so that it monkey-patch overrides distutils
5+
import setuptools
6+
except ImportError:
7+
pass
8+
9+
if sys.version_info >= (3, 12):
10+
# From setuptools' monkeypatch
11+
from distutils import ccompiler, sysconfig # type: ignore[import-not-found]
12+
else:
13+
from distutils import ccompiler, sysconfig
14+
15+
EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT = {
16+
"unix": {
17+
"base64/arch/ssse3": "-mssse3",
18+
"base64/arch/sse41": "-msse4.1",
19+
"base64/arch/sse42": "-msse4.2",
20+
"base64/arch/avx2": "-mavx2",
21+
"base64/arch/avx": "-mavx",
22+
},
23+
"msvc": {
24+
"base64/arch/sse42": "/arch:SSE4.2",
25+
"base64/arch/avx2": "/arch:AVX2",
26+
"base64/arch/avx": "/arch:AVX",
27+
},
28+
}
29+
30+
ccompiler.CCompiler.__spawn = ccompiler.CCompiler.spawn # type: ignore[attr-defined]
31+
32+
33+
def spawn(self, cmd, **kwargs) -> None: # type: ignore[no-untyped-def]
34+
compiler_type: str = self.compiler_type
35+
extra_options = EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT[compiler_type]
36+
new_cmd = list(cmd)
37+
if extra_options is not None:
38+
# filenames are closer to the end of command line
39+
for argument in reversed(new_cmd):
40+
# Check if argument contains a filename. We must check for all
41+
# possible extensions; checking for target extension is faster.
42+
if self.obj_extension and not str(argument).endswith(self.obj_extension):
43+
continue
44+
45+
for path in extra_options.keys():
46+
if path in str(argument):
47+
if compiler_type == "bcpp":
48+
# Borland accepts a source file name at the end,
49+
# insert the options before it
50+
new_cmd[-1:-1] = extra_options[path]
51+
else:
52+
new_cmd.append(extra_options[path])
53+
54+
# path component is found, no need to search any further
55+
break
56+
self.__spawn(new_cmd, **kwargs)
57+
58+
59+
ccompiler.CCompiler.spawn = spawn # type: ignore[method-assign]

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def run(self) -> None:
9999
os.path.join("mypyc", "lib-rt", "setup.py"),
100100
# Uses __file__ at top level https://github.com/mypyc/mypyc/issues/700
101101
os.path.join("mypyc", "__main__.py"),
102+
os.path.join("mypyc", "build_setup.py"), # for monkeypatching
102103
)
103104

104105
everything = [os.path.join("mypy", x) for x in find_package_data("mypy", ["*.py"])] + [

0 commit comments

Comments
 (0)