From c96759da7b85fe3100ea018eabcce4fb11008212 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:54 +0530
Subject: [PATCH 047/N] msys convert_path fix and root hack
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>
Co-authored-by: Audrey Dutcher <audrey@rhelmot.io>
---
 Lib/distutils/command/install.py |  3 ++-
 Lib/distutils/util.py            | 26 ++++++++++++++++++++++++--
 Makefile.pre.in                  | 12 +++++++++---
 3 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/Lib/distutils/command/install.py b/Lib/distutils/command/install.py
index aaa300e..eea417e 100644
--- a/Lib/distutils/command/install.py
+++ b/Lib/distutils/command/install.py
@@ -342,7 +342,8 @@ class install(Command):
 
         # Convert directories from Unix /-separated syntax to the local
         # convention.
-        self.convert_paths('lib', 'purelib', 'platlib',
+        self.convert_paths('base', 'platbase',
+                           'lib', 'purelib', 'platlib',
                            'scripts', 'data', 'headers',
                            'userbase', 'usersite')
 
diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py
index 7b2e1e0..75a369d 100644
--- a/Lib/distutils/util.py
+++ b/Lib/distutils/util.py
@@ -131,6 +131,13 @@ def convert_path (pathname):
         paths.remove('.')
     if not paths:
         return os.curdir
+    # On Windows, if paths is ['C:','folder','subfolder'] then
+    # os.path.join(*paths) will return 'C:folder\subfolder' which
+    # is thus relative to the CWD on that drive. So we work around
+    # this by adding a \ to path[0]
+    if (len(paths) > 0 and paths[0].endswith(':') and
+        sys.platform == "win32" and sys.version.find("GCC") >= 0):
+        paths[0] += '\\'
     return os.path.join(*paths)
 
 # convert_path ()
@@ -141,6 +148,10 @@ def change_root (new_root, pathname):
     relative, this is equivalent to "os.path.join(new_root,pathname)".
     Otherwise, it requires making 'pathname' relative and then joining the
     two, which is tricky on DOS/Windows and Mac OS.
+
+    If on Windows or OS/2 and both new_root and pathname are on different
+    drives, raises DistutilsChangeRootError as this is nonsensical,
+    otherwise use drive which can be in either of new_root or pathname.
     """
     if os.name == 'posix':
         if not os.path.isabs(pathname):
@@ -150,9 +161,20 @@ def change_root (new_root, pathname):
 
     elif os.name == 'nt':
         (drive, path) = os.path.splitdrive(pathname)
-        if path[0] == '\\':
+        if path[0] == os.sep:
             path = path[1:]
-        return os.path.join(new_root, path)
+        (drive_r, path_r) = os.path.splitdrive(new_root)
+        if path_r and path_r[0] == os.sep:
+            path_r = path_r[1:]
+        drive_used = ''
+        if len(drive) == 2 and len(drive_r) == 2 and drive != drive_r:
+            raise DistutilsChangeRootError("root and pathname not on same drive (%s, %s)"
+                   % (drive_r,drive))
+        elif len(drive_r) == 2:
+            drive_used = drive_r+os.sep
+        elif len(drive) == 2:
+            drive_used = drive+os.sep
+        return os.path.join(drive_used+path_r, path)
 
     else:
         raise DistutilsPlatformError("nothing known about platform '%s'" % os.name)
diff --git a/Makefile.pre.in b/Makefile.pre.in
index 5271dd8..72be72d 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -1707,6 +1707,12 @@ libainstall:	@DEF_MAKE_RULE@ python-config
 	else true; \
 	fi
 
+ifeq ($(shell uname -o),Msys)
+DESTDIRFINAL=$(DESTDIR)
+else
+DESTDIRFINAL=$(DESTDIR)/
+endif
+
 # Install the dynamically loadable modules
 # This goes into $(exec_prefix)
 sharedinstall: sharedmods
@@ -1714,9 +1720,9 @@ sharedinstall: sharedmods
 	   	--prefix=$(prefix) \
 		--install-scripts=$(BINDIR) \
 		--install-platlib=$(DESTSHARED) \
-		--root=$(DESTDIR)/
-	-rm $(DESTDIR)$(DESTSHARED)/_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH).py
-	-rm -r $(DESTDIR)$(DESTSHARED)/__pycache__
+		--root=$(DESTDIRFINAL)
+	-rm $(DESTDIRFINAL)$(DESTSHARED)/_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH).py
+	-rm -r $(DESTDIRFINAL)$(DESTSHARED)/__pycache__
 
 # Here are a couple of targets for MacOSX again, to install a full
 # framework-based Python. frameworkinstall installs everything, the
-- 
2.32.0

