From 7116a7c308e9dcd8fcefdba77a6551694bd397a5 Mon Sep 17 00:00:00 2001 From: Roumen Petrov Date: Sat, 2 Mar 2013 22:27:02 +0200 Subject: [PATCH 15/24] MINGW: build extensions with GCC --- Lib/distutils/command/build_ext.py | 22 ++++++++++++++++++++-- Lib/distutils/util.py | 2 ++ Lib/sysconfig.py | 2 ++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Lib/distutils/command/build_ext.py b/Lib/distutils/command/build_ext.py index 495de2a..6cea693 100644 --- a/Lib/distutils/command/build_ext.py +++ b/Lib/distutils/command/build_ext.py @@ -190,7 +190,11 @@ class build_ext(Command): # for extensions under windows use different directories # for Release and Debug builds. # also Python's library directory must be appended to library_dirs - if os.name == 'nt': + + use_libdir = ( self.plat_name.startswith(('mingw')) + or sys.platform.startswith(('linux', 'gnu', 'sunos')) ) + + if os.name == 'nt' and not use_libdir: # the 'libs' directory is for binary installs - we assume that # must be the *native* platform. But we don't really support # cross-compiling via a binary install anyway, so we let it go. @@ -245,7 +249,7 @@ class build_ext(Command): # for extensions under Linux or Solaris with a shared Python library, # Python's library directory must be appended to library_dirs sysconfig.get_config_var('Py_ENABLE_SHARED') - if (sys.platform.startswith(('linux', 'gnu', 'sunos')) + if (use_libdir and sysconfig.get_config_var('Py_ENABLE_SHARED')): if not sysconfig.python_build: # building third party extensions @@ -692,6 +696,20 @@ class build_ext(Command): # pyconfig.h that MSVC groks. The other Windows compilers all seem # to need it mentioned explicitly, though, so that's what we do. # Append '_d' to the python import library on debug builds. + + # Use self.plat_name as it works even in case of + # cross-compilation (at least for mingw build). + if self.plat_name.startswith('mingw'): + from distutils import sysconfig + extra = [] + for lib in ( + sysconfig.get_config_var('BLDLIBRARY').split() + + sysconfig.get_config_var('SHLIBS').split() + ): + if lib.startswith('-l'): + extra.append(lib[2:]) + return ext.libraries + extra + if sys.platform == "win32": from distutils.msvccompiler import MSVCCompiler if not isinstance(self.compiler, MSVCCompiler): diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py index efb3834..e5af80c 100644 --- a/Lib/distutils/util.py +++ b/Lib/distutils/util.py @@ -40,6 +40,8 @@ def get_platform (): For other non-POSIX platforms, currently just returns 'sys.platform'. """ if os.name == 'nt': + if 'GCC' in sys.version: + return 'mingw' # sniff sys.version for architecture. prefix = " bit (" i = sys.version.find(prefix) diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py index ee38a20..8720e50 100644 --- a/Lib/sysconfig.py +++ b/Lib/sysconfig.py @@ -607,6 +607,8 @@ def get_platform(): For other non-POSIX platforms, currently just returns 'sys.platform'. """ if os.name == 'nt': + if 'GCC' in sys.version: + return 'mingw' # sniff sys.version for architecture. prefix = " bit (" i = sys.version.find(prefix) -- 1.7.12.1