From e5719bec6bd661f18c1200b38997b5c5df39b521 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:25 +0530
Subject: [PATCH 012/N] Add dynamic loading support for MINGW
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Алексей <alexey.pawlow@gmail.com>
---
 Makefile.pre.in      |  6 ++++++
 Python/dynload_win.c |  9 +++++++--
 configure.ac         | 16 ++++++++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/Makefile.pre.in b/Makefile.pre.in
index 7ca3dc6..2d8145f 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -1378,6 +1378,12 @@ Python/dynload_hpux.o: $(srcdir)/Python/dynload_hpux.c Makefile
 		-DSHLIB_EXT='"$(EXT_SUFFIX)"' \
 		-o $@ $(srcdir)/Python/dynload_hpux.c
 
+Python/dynload_win.o: $(srcdir)/Python/dynload_win.c Makefile
+	$(CC) -c $(PY_CORE_CFLAGS) \
+		-DSHLIB_SUFFIX='"$(SHLIB_SUFFIX)"' \
+		-DEXT_SUFFIX='"$(EXT_SUFFIX)"' \
+		-o $@ $(srcdir)/Python/dynload_win.c
+
 Python/sysmodule.o: $(srcdir)/Python/sysmodule.c Makefile $(srcdir)/Include/pydtrace.h
 	$(CC) -c $(PY_CORE_CFLAGS) \
 		-DABIFLAGS='"$(ABIFLAGS)"' \
diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index acab05e..4bb90ba 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -29,6 +29,12 @@
 #define PYD_UNTAGGED_SUFFIX PYD_DEBUG_SUFFIX ".pyd"
 
 const char *_PyImport_DynLoadFiletab[] = {
+#ifdef EXT_SUFFIX
+    EXT_SUFFIX, /* include SOABI flags where is encoded debug */
+#endif
+#ifdef SHLIB_SUFFIX
+    "-abi" PYTHON_ABI_STRING SHLIB_SUFFIX,
+#endif
     PYD_TAGGED_SUFFIX,
     PYD_UNTAGGED_SUFFIX,
     NULL
@@ -251,8 +257,7 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
            ensure DLLs adjacent to the PYD are preferred. */
         Py_BEGIN_ALLOW_THREADS
         hDLL = LoadLibraryExW(wpathname, NULL,
-                              LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
-                              LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
+                              LOAD_WITH_ALTERED_SEARCH_PATH);
         Py_END_ALLOW_THREADS
         PyMem_Free(wpathname);
 
diff --git a/configure.ac b/configure.ac
index d4e8b5f..1863fa8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3424,6 +3424,9 @@ if test -z "$SHLIB_SUFFIX"; then
 	CYGWIN*)   SHLIB_SUFFIX=.dll;;
 	*)	   SHLIB_SUFFIX=.so;;
 	esac
+	case $host_os in
+	mingw*)    SHLIB_SUFFIX=.dll;;
+	esac
 fi
 AC_MSG_RESULT([$SHLIB_SUFFIX])
 
@@ -5036,6 +5039,13 @@ then
 	fi
 	;;
 	esac
+	case $host in
+	*-*-mingw*)
+	DYNLOADFILE="dynload_win.o"
+	extra_machdep_objs="$extra_machdep_objs PC/dl_nt.o"
+	CFLAGS_NODIST="$CFLAGS_NODIST -DMS_DLL_ID='\"$VERSION\"'"
+	;;
+	esac
 fi
 AC_MSG_RESULT([$DYNLOADFILE])
 if test "$DYNLOADFILE" != "dynload_stub.o"
@@ -6925,6 +6935,12 @@ case "$ac_cv_computed_gotos" in yes*)
   AC_DEFINE([HAVE_COMPUTED_GOTOS], [1],
   [Define if the C compiler supports computed gotos.])
 esac
+case $host_os in
+    mingw*)
+	dnl Synchronized with _PyImport_DynLoadFiletab (dynload_win.c)
+	dnl Do not use more then one dot on this platform !
+	EXT_SUFFIX=-$SOABI$SHLIB_SUFFIX;;
+esac
 
 case $ac_sys_system in
 AIX*)
