From 386008948b544e74ebe06620dc41a6a555195170 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9?=
 <alexey.pawlow@gmail.com>
Date: Thu, 17 Jun 2021 18:51:34 +0530
Subject: [PATCH 024/N] MINGW build extensions with GCC
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Алексей <alexey.pawlow@gmail.com>
---
 Lib/distutils/command/build_ext.py | 16 +++++++++++++++-
 Lib/distutils/util.py              |  2 ++
 Lib/sysconfig.py                   |  2 ++
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/Lib/distutils/command/build_ext.py b/Lib/distutils/command/build_ext.py
index 1a9bd12..1c9d471 100644
--- a/Lib/distutils/command/build_ext.py
+++ b/Lib/distutils/command/build_ext.py
@@ -186,7 +186,7 @@ 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':
+        if os.name == 'nt' and not self.plat_name.startswith(('mingw')):
             # 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.
@@ -712,6 +712,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 4b002ec..7b2e1e0 100644
--- a/Lib/distutils/util.py
+++ b/Lib/distutils/util.py
@@ -36,6 +36,8 @@ def get_host_platform():
 
     """
     if os.name == 'nt':
+        if 'GCC' in sys.version:
+            return 'mingw'
         if 'amd64' in sys.version.lower():
             return 'win-amd64'
         if '(arm)' in sys.version.lower():
diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py
index 505d538..aa49d80 100644
--- a/Lib/sysconfig.py
+++ b/Lib/sysconfig.py
@@ -656,6 +656,8 @@ def get_platform():
 
     """
     if os.name == 'nt':
+        if 'GCC' in sys.version:
+            return 'mingw'
         if 'amd64' in sys.version.lower():
             return 'win-amd64'
         if '(arm)' in sys.version.lower():
-- 
2.32.0

