From 3c642b927b9f71071aff679f299a84f8803f19d1 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:50 +0530
Subject: [PATCH 022/N] msys cygwin semi native build sysconfig
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Алексей <alexey.pawlow@gmail.com>
---
 Lib/sysconfig.py |  8 +++++++
 Makefile.pre.in  |  7 ++++++
 configure.ac     | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+)

diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py
index e05a67d..60626d2 100644
--- a/Lib/sysconfig.py
+++ b/Lib/sysconfig.py
@@ -451,6 +451,14 @@ def _parse_makefile(filename, vars=None, keep_unresolved=True):
         if isinstance(v, str):
             done[k] = v.strip()
 
+    # any keys that have one with the same name suffixed with _b2h
+    # need to be replaced with the value of the _b2h key.
+    # This converts from MSYS*/Cygwin paths to Windows paths.
+    for k, v in dict(done).items():
+        if isinstance(k, str):
+            if k.endswith("_b2h"):
+                done[k[:-4]]=v
+ 
     # save the results in the global dictionary
     vars.update(done)
     return vars
diff --git a/Makefile.pre.in b/Makefile.pre.in
index 5e005b6..63ca7ad 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -142,6 +142,13 @@ exec_prefix=	@exec_prefix@
 # Install prefix for data files
 datarootdir=    @datarootdir@
 
+# Locations needed for semi-native fixup of sysconfig.
+srcdir_b2h=		@srcdir_b2h@
+VPATH_b2h=		@VPATH_b2h@
+abs_srcdir_b2h=		@abs_srcdir_b2h@
+abs_builddir_b2h=	@abs_builddir_b2h@
+prefix_b2h=		@prefix_b2h@
+
 # Expanded directories
 BINDIR=		@bindir@
 LIBDIR=		@libdir@
diff --git a/configure.ac b/configure.ac
index 8ccec5e..c8407ad 100644
--- a/configure.ac
+++ b/configure.ac
@@ -755,6 +755,65 @@ then
             [Define to include mbstate_t for mbrtowc])
 fi
 
+# On 'semi-native' build systems (MSYS*/Cygwin targeting MinGW-w64)
+# _sysconfigdata.py will contain paths that are correct only in the
+# build environment. This means external modules will fail to build
+# without setting up the same env and also that the build of Python
+# itself will fail as the paths are not correct for the host tools.
+#
+# Also, getpath.c uses GetModuleFileNameW (replacing \ with /) and
+# compares that with the define VPATH (passed in via command-line)
+# to determine whether it's the build- or the installed-Python.
+#
+# To work around these issues a set of _b2h variables are created:
+# VPATH_b2h, prefix_b2h, srcdir_b2h, abs_srcdir_b2h
+# and abs_builddir_b2h
+# .. where b2h stands for build to host. sysconfig.py replaces path
+# prefixes matching the non-b2h versions with the b2h equivalents.
+#
+# (note this assumes the host compilers are native and *not* cross
+#  - in the 'semi-native' scenario only that is.)
+
+AC_DEFUN([ABS_PATH_HOST],
+[$1=$(cd $$2 && pwd)
+  case $build_os in
+    mingw*)
+      case $host_os in
+        mingw*) $1=$(cd $$2 && pwd -W) ;;
+        *) ;;
+      esac
+      ;;
+    cygwin*)
+      case $host_os in
+        mingw*) $1=$(cygpath -w -m $$2) ;;
+        *) ;;
+      esac
+      ;;
+  esac
+AC_SUBST([$1])
+])
+
+AC_MSG_CHECKING(absolute host location of VPATH)
+ABS_PATH_HOST([VPATH_b2h],[srcdir])
+AC_MSG_RESULT([$VPATH_b2h])
+
+AC_MSG_CHECKING(absolute host location of prefix)
+ABS_PATH_HOST([prefix_b2h],[prefix])
+AC_MSG_RESULT([$prefix_b2h])
+
+AC_MSG_CHECKING(absolute host location of srcdir)
+ABS_PATH_HOST([srcdir_b2h],[srcdir])
+AC_MSG_RESULT([$srcdir_b2h])
+
+AC_MSG_CHECKING(absolute host location of abs_srcdir)
+ABS_PATH_HOST([abs_srcdir_b2h],[srcdir])
+AC_MSG_RESULT([$abs_srcdir_b2h])
+
+my_builddir=.
+AC_MSG_CHECKING(Absolute host location of abs_builddir)
+ABS_PATH_HOST([abs_builddir_b2h],[my_builddir])
+AC_MSG_RESULT([$abs_builddir_b2h])
+
 AC_MSG_CHECKING([for init system calls])
 AC_SUBST(INITSYS)
 case $host in
