From c4507430c95893ad2be0a1796f8dc7a7962ef789 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:51 +0530
Subject: [PATCH 044/N] mingw _winapi_as_builtin_for_Popen_in_cygwinccompiler
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Алексей <alexey.pawlow@gmail.com>
Co-authored-by: Christoph Reiter <reiter.christoph@gmail.com>
---
 Lib/distutils/cygwinccompiler.py | 14 ++++++++++++--
 Modules/Setup.config.in          |  1 +
 setup.py                         |  4 +++-
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/Lib/distutils/cygwinccompiler.py b/Lib/distutils/cygwinccompiler.py
index 5b281e2..f90af3b 100644
--- a/Lib/distutils/cygwinccompiler.py
+++ b/Lib/distutils/cygwinccompiler.py
@@ -56,6 +56,7 @@ from distutils.errors import (DistutilsExecError, CCompilerError,
         CompileError, UnknownFileError)
 from distutils.version import LooseVersion
 from distutils.spawn import find_executable
+from subprocess import Popen, PIPE, check_output
 
 def get_msvcr():
     """Include the appropriate MSVC runtime library if Python was built
@@ -371,7 +372,7 @@ def check_config_h():
         return (CONFIG_H_UNCERTAIN,
                 "couldn't read '%s': %s" % (fn, exc.strerror))
 
-RE_VERSION = re.compile(br'(\d+\.\d+(\.\d+)*)')
+RE_VERSION = re.compile(br'[\D\s]*(\d+\.\d+(\.\d+)*)[\D\s]*$')
 
 def _find_exe_version(cmd):
     """Find the version of an executable by running `cmd` in the shell.
@@ -400,7 +401,16 @@ def get_versions():
 
     If not possible it returns None for it.
     """
-    commands = ['gcc -dumpversion', 'ld -v', 'dllwrap --version']
+    gcc = os.environ.get('CC') or 'gcc'
+    ld = 'ld'
+    out = Popen(gcc+' --print-prog-name ld', shell=True, stdout=PIPE).stdout
+    try:
+        ld = test=str(out.read(),encoding='utf-8').strip()
+    finally:
+        out.close()
+    dllwrap = os.environ.get('DLLWRAP') or 'dllwrap'
+    # MinGW64 doesn't have i686-w64-mingw32-ld, so instead we ask gcc.
+    commands = [gcc+' -dumpversion', ld+' -v', dllwrap+' --version']
     return tuple([_find_exe_version(cmd) for cmd in commands])
 
 def is_cygwingcc():
diff --git a/Modules/Setup.config.in b/Modules/Setup.config.in
index 825ce5d..b4e7ff7 100644
--- a/Modules/Setup.config.in
+++ b/Modules/Setup.config.in
@@ -12,4 +12,5 @@
 # build-in modules for windows platform:
 @USE_WIN32_MODULE@winreg ../PC/winreg.c
 @USE_WIN32_MODULE@msvcrt -DPy_BUILD_CORE ../PC/msvcrtmodule.c
+@USE_WIN32_MODULE@_winapi _winapi.c
 
diff --git a/setup.py b/setup.py
index 72f1281..36af6e9 100644
--- a/setup.py
+++ b/setup.py
@@ -1607,7 +1607,9 @@ class PyBuildExt(build_ext):
             self.add(Extension('msvcrt', [os.path.join(pc_srcdir, p)
                                for p in ['msvcrtmodule.c']]))
 
-            self.add(Extension('_winapi', ['_winapi.c']))
+            # Added to Setup.config.in as now needed earlier since I
+            # use subprocess (which uses Popen) in cygwinccompiler.py
+            # self.add(Extension('_winapi', ['_winapi.c']))
 
             self.add(Extension('_msi', [os.path.join(pc_srcdir, p)
                                for p in ['_msi.c']],
-- 
2.32.0

