Skip to content

Commit 8adca9b

Browse files
committed
patch in _compile
1 parent 91127a5 commit 8adca9b

1 file changed

Lines changed: 66 additions & 9 deletions

File tree

setup.py

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,23 @@
4848
from setuptools._distutils._msvccompiler import MSVCCompiler
4949
from setuptools._distutils.command.install_data import install_data
5050
from setuptools._distutils.compilers.C.cygwin import MinGW32Compiler
51+
from setuptools._distutils.compilers.C.errors import CompileError
52+
from setuptools._distutils.errors import DistutilsExecError
5153
else:
5254
from distutils import ccompiler
5355
from distutils._msvccompiler import MSVCCompiler
5456
from distutils.command.install_data import install_data
5557
from distutils.compilers.C.cygwin import MinGW32Compiler
56-
57-
# workaround until https://github.com/pypa/distutils/pull/399 is fixed
58-
MinGW32Compiler.initialize = lambda *_: None
58+
from distutils.compilers.C.errors import CompileError
59+
from distutils.errors import DistutilsExecError
5960

6061
is_mingw = "MSC" not in sys.version
6162

6263
def my_new_compiler(**kw):
63-
if not is_mingw and "compiler" in kw and kw["compiler"] in (None, "msvc"):
64-
return my_compiler()
65-
result = orig_new_compiler(**kw)
66-
print(f"DEBUG compiler: {type(result).__name__}, is_mingw={is_mingw}, kw={kw}")
67-
return result
64+
if is_mingw:
65+
return MyMINGWCompiler()
66+
if "compiler" in kw and kw["compiler"] in (None, "msvc"):
67+
return MyMSVCCompiler()
6868
return orig_new_compiler(**kw)
6969

7070
# No way to cleanly wedge our compiler sub-class in.
@@ -873,7 +873,64 @@ def swig_sources(self, sources, ext):
873873
return new_sources
874874

875875

876-
class my_compiler(MSVCCompiler):
876+
class MyMINGWCompiler(MinGW32Compiler):
877+
# workaround until https://github.com/pypa/distutils/pull/399 is fixed
878+
MinGW32Compiler.initialize = lambda *_: None
879+
880+
# Work around python/cpython#80483 / python/cpython#86175
881+
# it sorts sources but this breaks support for building .mc files etc :(
882+
# See pypa/setuptools#4986 / pypa/distutils#370 for potential upstream fix.
883+
def compile(self, sources, **kwargs):
884+
# re-sort the list of source files but ensure all .mc files come first.
885+
def key_reverse_mc(a):
886+
b, e = os.path.splitext(a)
887+
e = "" if e == ".mc" else e
888+
return (e, b)
889+
890+
sources = sorted(sources, key=key_reverse_mc)
891+
return super().compile(sources, **kwargs)
892+
893+
# Work around missing .mc support in MinGW32Compiler
894+
def _compile(
895+
self,
896+
obj: str | os.PathLike[str],
897+
src: str | os.PathLike[str],
898+
ext: str,
899+
cc_args: list[str],
900+
extra_postargs: list[str],
901+
pp_opts: object,
902+
) -> None:
903+
"""Compiles the source by spawning GCC and windres, and/or windmc if needed."""
904+
try:
905+
if ext == ".mc":
906+
h_dir = os.path.dirname(src)
907+
rc_dir = os.path.dirname(obj)
908+
# first compile .mc to .rc and .h file
909+
self.spawn(["windmc", "-h", h_dir, "-r", rc_dir, src])
910+
# then compile .rc to .res file
911+
src = os.path.join(
912+
rc_dir, os.path.splitext(os.path.basename(src))[0] + ".rc"
913+
)
914+
if ext in (".rc", ".res", ".mc"):
915+
# gcc needs '.res' and '.rc' compiled to object files !!!
916+
self.spawn(["windres", "-i", src, "-o", obj])
917+
else: # for other files use the C-compiler
918+
if self.detect_language(src) == "c++":
919+
self.spawn(
920+
self.compiler_so_cxx
921+
+ cc_args
922+
+ [src, "-o", obj]
923+
+ extra_postargs
924+
)
925+
else:
926+
self.spawn(
927+
self.compiler_so + cc_args + [src, "-o", obj] + extra_postargs
928+
)
929+
except DistutilsExecError as msg:
930+
raise CompileError(msg)
931+
932+
933+
class MyMSVCCompiler(MSVCCompiler):
877934
# Work around python/cpython#80483 / python/cpython#86175
878935
# it sorts sources but this breaks support for building .mc files etc :(
879936
# See pypa/setuptools#4986 / pypa/distutils#370 for potential upstream fix.

0 commit comments

Comments
 (0)