|
48 | 48 | from setuptools._distutils._msvccompiler import MSVCCompiler |
49 | 49 | from setuptools._distutils.command.install_data import install_data |
50 | 50 | from setuptools._distutils.compilers.C.cygwin import MinGW32Compiler |
| 51 | + from setuptools._distutils.compilers.C.errors import CompileError |
| 52 | + from setuptools._distutils.errors import DistutilsExecError |
51 | 53 | else: |
52 | 54 | from distutils import ccompiler |
53 | 55 | from distutils._msvccompiler import MSVCCompiler |
54 | 56 | from distutils.command.install_data import install_data |
55 | 57 | 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 |
59 | 60 |
|
60 | 61 | is_mingw = "MSC" not in sys.version |
61 | 62 |
|
62 | 63 | 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() |
68 | 68 | return orig_new_compiler(**kw) |
69 | 69 |
|
70 | 70 | # No way to cleanly wedge our compiler sub-class in. |
@@ -873,7 +873,64 @@ def swig_sources(self, sources, ext): |
873 | 873 | return new_sources |
874 | 874 |
|
875 | 875 |
|
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): |
877 | 934 | # Work around python/cpython#80483 / python/cpython#86175 |
878 | 935 | # it sorts sources but this breaks support for building .mc files etc :( |
879 | 936 | # See pypa/setuptools#4986 / pypa/distutils#370 for potential upstream fix. |
|
0 commit comments