From ffeebbc4866c45b9fcde46be9d1f24423f21cc70 Mon Sep 17 00:00:00 2001 From: Bernie Hackett Date: Wed, 1 Mar 2017 14:46:08 -0800 Subject: [PATCH] PYTHON-1247 - Work around distutils MingW32 issues --- setup.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/setup.py b/setup.py index 6e1944fdb..eeb5da6fa 100755 --- a/setup.py +++ b/setup.py @@ -188,6 +188,18 @@ else: build_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError) +_COMPILER_ATTRS = ( + 'compiler', 'compiler_so', 'compiler_cxx', 'linker_exe', 'linker_so') + + +# From distutils.cygwinccompiler in recent pythons. +def _is_cygwingcc(): + out = os.popen('gcc -dumpmachine', 'r') + out_string = out.read() + out.close() + return out_string.strip().endswith('cygwin') + + class custom_build_ext(build_ext): """Allow C extension building to fail. @@ -256,6 +268,31 @@ http://api.mongodb.org/python/current/installation.html#osx write_nose_config() def build_extension(self, ext): + # http://bugs.python.org/issue12641 + # This makes a number of well researched assumptions about distutils + # but is written in a defensive style to guard against those + # assumptions failing. + compiler = getattr(self, "compiler", None) + if compiler and getattr(compiler, "compiler_type", None) == "mingw32": + try: + from distutils import cygwinccompiler + except ImportError: + pass + else: + # If cygwinccompiler.is_cygwingcc exists the problem is + # already solved for us. + if not hasattr(cygwinccompiler, "is_cygwingcc"): + gcc_version = getattr(compiler, "gcc_version", None) + # If gcc_version is None assume we need to strip + # -mno-cygwin. + if (not _is_cygwingcc() and + (not gcc_version or gcc_version >= "4.6")): + for att in [getattr(compiler, attrname) + for attrname in _COMPILER_ATTRS + if hasattr(compiler, attrname)]: + if isinstance(att, list) and '-mno-cygwin' in att: + att.remove('-mno-cygwin') + name = ext.name if sys.version_info[:3] >= (2, 4, 0): try: