From 0eee28b4ad46bcf4ed60d6bb612dc1e477b639b1 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Mon, 22 Mar 2021 08:10:23 +0100
Subject: [PATCH 30/N] uname: limit sysname to "MSYS" or "MINGW"

uname just included the content of the MSYSTEM env var as sysname
which leads to problems in case build tools use it detect a mingw build
and MSYSTEM is something like UCRT or CLANG.

To work around that, in case MSYSTEM is not set, empty, or not MSYS, then
we just output a hardcoded "MINGW".

And because various scripts hardcode checks for MINGW32/MINGW64 we return MINGW64,
unless there is a "32" in MSYSTEM, in which case we return MINGW32.
---
 winsup/cygwin/uname.cc | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/uname.cc b/winsup/cygwin/uname.cc
index 28bcd63..daed73f 100644
--- a/winsup/cygwin/uname.cc
+++ b/winsup/cygwin/uname.cc
@@ -37,9 +37,11 @@ uname_x (struct utsname *name)
       memset (name, 0, sizeof (*name));
       /* sysname */
       char* msystem = getenv("MSYSTEM");
-      const char *msystem_msys = "MSYS";
+      const char* msystem_sysname = "MSYS";
+      if (msystem != NULL && *msystem && strcmp(msystem, "MSYS") != 0)
+        msystem_sysname = (strstr(msystem, "32") != NULL) ? "MINGW32" : "MINGW64";;
       __small_sprintf (name->sysname, "%s_%s-%u%s",
-		       msystem ? msystem : msystem_msys,
+		       msystem_sysname,
 		       wincap.osname (), wincap.build_number (),
 		       wincap.is_wow64 () ? "-WOW64" : "");
       /* nodename */
@@ -103,8 +105,10 @@ uname (struct utsname *in_name)
       memset (name, 0, sizeof (*name));
 #ifdef __MSYS__
       char* msystem = getenv("MSYSTEM");
-      const char *msystem_msys = "MSYS";
-      __small_sprintf (name->sysname, "%s_%s", msystem ? msystem : msystem_msys, wincap.osname ());
+      const char* msystem_sysname = "MSYS";
+      if (msystem != NULL && *msystem && strcmp(msystem, "MSYS") != 0)
+        msystem_sysname = (strstr(msystem, "32") != NULL) ? "MINGW32" : "MINGW64";
+      __small_sprintf (name->sysname, "%s_%s", msystem_sysname, wincap.osname ());
 #else
       __small_sprintf (name->sysname, "CYGWIN_%s", wincap.osname ());
 #endif
-- 
2.31.1

