From c4e2de6cd7e3e139cfc5016256970e876b89a1c9 Mon Sep 17 00:00:00 2001
From: Naveen M K <naveen521kk@gmail.com>
Date: Thu, 12 Sep 2024 17:42:31 +0530
Subject: [PATCH 116/N] `sysconfig.get_platform()`: use consistent naming

Currently, the platform names are hardcoded in many places,
and are not named consistently. This commit fixes it by
standardizing the names to be returned by `sysconfig.get_platform`.
The naming is based on https://github.com/msys2-contrib/cpython-mingw/issues/167#issuecomment-1952215164

Similarly, `EXT_SUFFIX` is also standardized to be consistent with the platform names.

Signed-off-by: Naveen M K <naveen521kk@gmail.com>
---
 Lib/site.py                             | 32 +++++++++------
 Lib/sysconfig.py                        | 32 +++++++++------
 Lib/test/test_importlib/test_windows.py | 38 +++++++++--------
 configure.ac                            | 54 ++++++++++---------------
 mingw_smoketests.py                     |  2 +-
 5 files changed, 83 insertions(+), 75 deletions(-)

diff --git a/Lib/site.py b/Lib/site.py
index c1ecd6e..16c7c41 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -314,21 +314,27 @@ def _getuserbase():
 def _get_platform():
     if os.name == 'nt':
         if 'gcc' in sys.version.lower():
+            platform = 'mingw'
+            if 'amd64' in sys.version.lower():
+                platform += '_x86_64'
+            elif 'arm64' in sys.version.lower():
+                platform += '_aarch64'
+            elif 'arm' in sys.version.lower():
+                platform += '_armv7'
+            else:
+                platform += '_i686'
+
             if 'ucrt' in sys.version.lower():
-                if 'amd64' in sys.version.lower():
-                    return 'mingw_x86_64_ucrt'
-                return 'mingw_i686_ucrt'
+                platform += '_ucrt'
+            else:
+                platform += "_msvcrt"
+
             if 'clang' in sys.version.lower():
-                if 'amd64' in sys.version.lower():
-                    return 'mingw_x86_64_clang'
-                if 'arm64' in sys.version.lower():
-                    return 'mingw_aarch64'
-                if 'arm' in sys.version.lower():
-                    return 'mingw_armv7'
-                return 'mingw_i686_clang'
-            if 'amd64' in sys.version.lower():
-                return 'mingw_x86_64'
-            return 'mingw_i686'
+                platform += "_llvm"
+            else:
+                platform += "_gnu"
+            
+            return platform
     return sys.platform
 
 # Same to sysconfig.get_path('purelib', os.name+'_user')
diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py
index 916c1e8..c12ce38 100644
--- a/Lib/sysconfig.py
+++ b/Lib/sysconfig.py
@@ -818,21 +818,27 @@ def get_platform():
     """
     if os.name == 'nt':
         if 'gcc' in sys.version.lower():
+            platform = 'mingw'
+            if 'amd64' in sys.version.lower():
+                platform += '_x86_64'
+            elif 'arm64' in sys.version.lower():
+                platform += '_aarch64'
+            elif 'arm' in sys.version.lower():
+                platform += '_armv7'
+            else:
+                platform += '_i686'
+
             if 'ucrt' in sys.version.lower():
-                if 'amd64' in sys.version.lower():
-                    return 'mingw_x86_64_ucrt'
-                return 'mingw_i686_ucrt'
+                platform += '_ucrt'
+            else:
+                platform += "_msvcrt"
+
             if 'clang' in sys.version.lower():
-                if 'amd64' in sys.version.lower():
-                    return 'mingw_x86_64_clang'
-                if 'arm64' in sys.version.lower():
-                    return 'mingw_aarch64'
-                if 'arm' in sys.version.lower():
-                    return 'mingw_armv7'
-                return 'mingw_i686_clang'
-            if 'amd64' in sys.version.lower():
-                return 'mingw_x86_64'
-            return 'mingw_i686'
+                platform += "_llvm"
+            else:
+                platform += "_gnu"
+            
+            return platform
         if 'amd64' in sys.version.lower():
             return 'win-amd64'
         if '(arm)' in sys.version.lower():
diff --git a/Lib/test/test_importlib/test_windows.py b/Lib/test/test_importlib/test_windows.py
index 03b5a98..0b1254c 100644
--- a/Lib/test/test_importlib/test_windows.py
+++ b/Lib/test/test_importlib/test_windows.py
@@ -25,22 +25,28 @@ def get_platform():
             'arm' : 'win-arm32',
         }
     if os.name == 'nt':
-        if 'gcc' in sys.version.lower():
-            if 'ucrt' in sys.version.lower():
-                if 'amd64' in sys.version.lower():
-                    return 'mingw_x86_64_ucrt'
-                return 'mingw_i686_ucrt'
-            if 'clang' in sys.version.lower():
-                if 'amd64' in sys.version.lower():
-                    return 'mingw_x86_64_clang'
-                if 'arm64' in sys.version.lower():
-                    return 'mingw_aarch64'
-                if 'arm' in sys.version.lower():
-                    return 'mingw_armv7'
-                return 'mingw_i686_clang'
-            if 'amd64' in sys.version.lower():
-                return 'mingw_x86_64'
-            return 'mingw_i686'
+        if "gcc" in sys.version.lower():
+            platform = "mingw"
+            if "amd64" in sys.version.lower():
+                platform += "_x86_64"
+            elif "arm64" in sys.version.lower():
+                platform += "_aarch64"
+            elif "arm" in sys.version.lower():
+                platform += "_armv7"
+            else:
+                platform += "_i686"
+
+            if "ucrt" in sys.version.lower():
+                platform += "_ucrt"
+            else:
+                platform += "_msvcrt"
+
+            if "clang" in sys.version.lower():
+                platform += "_llvm"
+            else:
+                platform += "_gnu"
+
+            return platform
     if ('VSCMD_ARG_TGT_ARCH' in os.environ and
         os.environ['VSCMD_ARG_TGT_ARCH'] in TARGET_TO_PLAT):
         return TARGET_TO_PLAT[os.environ['VSCMD_ARG_TGT_ARCH']]
diff --git a/configure.ac b/configure.ac
index 5d1b6e2..264bae6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6335,16 +6335,12 @@ AC_C_BIGENDIAN
 
 AC_SUBST(PYD_PLATFORM_TAG)
 # Special case of PYD_PLATFORM_TAG with python build with mingw. 
-# Python can with compiled with clang or gcc and linked
-# to msvcrt or ucrt. To avoid conflicts between them
-# we are selecting the extension as based on the compiler
-# and the runtime they link to
-#   gcc + x86_64 + msvcrt = cp{version number}-x86_64
-#   gcc + i686 + msvcrt = cp{version number}-i686
-#   gcc + x86_64 + ucrt = cp{version number}-x86_64-ucrt
-#   clang + x86_64 + ucrt = cp{version number}-x86_64-clang
-#   clang + i686 + ucrt = cp{version number}-i686-clang
-
+# Python can with different cpu arch and c runtime as well as different
+# toolchain. We follow this`mingw_<cpu_arch>_<c_runtime>_<toolchain>`
+# convention for PYD_PLATFORM_TAG. Where:
+# `cpu_arch` = `x86_64`, `aarch64` or `i686`
+# `c_runtime` = `msvcrt` or `ucrt`
+# `toolchain` = `gnu` or `llvm`
 PYD_PLATFORM_TAG=""
 case $host in
   *-*-mingw*)
@@ -6363,38 +6359,32 @@ esac
 case $host_os in
     mingw*)
   AC_MSG_CHECKING(PYD_PLATFORM_TAG)
+  PYD_PLATFORM_TAG="mingw"
   case $host in
   i686-*-mingw*)
-    if test -n "${cc_is_clang}"; then
-      # it is CLANG32
-      PYD_PLATFORM_TAG="mingw_i686_clang"
-    else
-      if test $linking_to_ucrt = no; then
-        PYD_PLATFORM_TAG="mingw_i686"
-      else
-        PYD_PLATFORM_TAG="mingw_i686_ucrt"
-      fi
-    fi
+    PYD_PLATFORM_TAG+="_i686"
     ;;
   x86_64-*-mingw*)
-    if test -n "${cc_is_clang}"; then
-      # it is CLANG64
-      PYD_PLATFORM_TAG="mingw_x86_64_clang"
-    else
-      if test $linking_to_ucrt = no; then
-        PYD_PLATFORM_TAG="mingw_x86_64"
-      else
-        PYD_PLATFORM_TAG="mingw_x86_64_ucrt"
-      fi
-    fi
+    PYD_PLATFORM_TAG+="_x86_64"
     ;;
   aarch64-*-mingw*)
-    PYD_PLATFORM_TAG+="mingw_aarch64"
+    PYD_PLATFORM_TAG+="_aarch64"
     ;;
   armv7-*-mingw*)
-    PYD_PLATFORM_TAG+="mingw_armv7"
+    PYD_PLATFORM_TAG+="_armv7"
     ;;
   esac
+  if test $linking_to_ucrt = no; then
+    PYD_PLATFORM_TAG+="_msvcrt"
+  else
+    PYD_PLATFORM_TAG += "_ucrt"
+  fi
+  if test -n "${cc_is_clang}"; then
+    # it is CLANG32
+    PYD_PLATFORM_TAG+="_llvm"
+  else
+    PYD_PLATFORM_TAG+="_gnu"
+  fi
   AC_MSG_RESULT($PYD_PLATFORM_TAG)
 esac
 
diff --git a/mingw_smoketests.py b/mingw_smoketests.py
index 0ab5d3b..9ad32b3 100644
--- a/mingw_smoketests.py
+++ b/mingw_smoketests.py
@@ -37,7 +37,7 @@ else:
 if sysconfig.is_python_build():
     os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"
 
-_UCRT = sysconfig.get_platform() not in ('mingw_x86_64', 'mingw_i686')
+_UCRT = 'ucrt' in sysconfig.get_platform()
 
 
 class Tests(unittest.TestCase):
