From 78ba60f537dc443ef5bc912e650e688619b9e798 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:33 +0530
Subject: [PATCH 023/N] MINGW setup msvcrt and _winapi modules
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Алексей <alexey.pawlow@gmail.com>
---
 Modules/_winapi.c            |  2 ++
 PC/msvcrtmodule.c            |  2 ++
 Python/dynamic_annotations.c |  2 +-
 setup.py                     | 21 +++++++++++++++++++++
 4 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/Modules/_winapi.c b/Modules/_winapi.c
index e1672c4..ba318a1 100644
--- a/Modules/_winapi.c
+++ b/Modules/_winapi.c
@@ -39,7 +39,9 @@
 
 #define WINDOWS_LEAN_AND_MEAN
 #include "windows.h"
+#if defined(Py_DEBUG)
 #include <crtdbg.h>
+#endif
 #include "winreparse.h"
 
 #if defined(MS_WIN32) && !defined(MS_WIN64)
diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c
index faceb03..28e1957 100644
--- a/PC/msvcrtmodule.c
+++ b/PC/msvcrtmodule.c
@@ -21,7 +21,9 @@
 #include <io.h>
 #include <conio.h>
 #include <sys/locking.h>
+#ifdef _DEBUG
 #include <crtdbg.h>
+#endif
 #include <windows.h>
 
 #ifdef _MSC_VER
diff --git a/Python/dynamic_annotations.c b/Python/dynamic_annotations.c
index 7febaa0..70d5b3d 100644
--- a/Python/dynamic_annotations.c
+++ b/Python/dynamic_annotations.c
@@ -27,7 +27,7 @@
  * Author: Kostya Serebryany
  */
 
-#ifdef _MSC_VER
+#ifdef MS_WINDOWS
 # include <windows.h>
 #endif
 
diff --git a/setup.py b/setup.py
index 5280ea0..736b0f0 100644
--- a/setup.py
+++ b/setup.py
@@ -1599,6 +1599,27 @@ class PyBuildExt(build_ext):
                                    '-framework', 'SystemConfiguration',
                                    '-framework', 'CoreFoundation']))
 
+        # Modules with some Windows dependencies:
+        if MS_WINDOWS:
+            srcdir = sysconfig.get_config_var('srcdir')
+            pc_srcdir = os.path.abspath(os.path.join(srcdir, 'PC'))
+
+            self.add(Extension('msvcrt', [os.path.join(pc_srcdir, p)
+                               for p in ['msvcrtmodule.c']]))
+
+            self.add(Extension('_winapi', ['_winapi.c']))
+
+            self.add(Extension('_msi', [os.path.join(pc_srcdir, p)
+                               for p in ['_msi.c']],
+                               libraries=['msi','cabinet','rpcrt4'])) # To link with lib(msi|cabinet|rpcrt4).a
+
+            self.add(Extension('winsound', [os.path.join(pc_srcdir, p)
+                               for p in ['winsound.c']],
+                               libraries=['winmm']))
+
+            self.add(Extension('_overlapped', ['overlapped.c'],
+                               libraries=['ws2_32']))
+
     def detect_compress_exts(self):
         # Andrew Kuchling's zlib module.  Note that some versions of zlib
         # 1.1.3 have security problems.  See CERT Advisory CA-2002-07:
-- 
2.32.0

