Subject: bug#75939: setting $MSYS2_ARG_CONV_EXCL in compile script?

From: Bruno Haible <[email-masked]>
Date: Mon, 03 Feb 2025 11:56:29 +0100

Kirill Makurin wrote:
> I applied patches and they work for Msys2.
> 
> I also tested it with (what I assume is) "Msys-based MinGW" (https://osdn.net/projects/mingw/) and it fails. Its `uname -s` reports `MINGW32_NT-6.2` and it has `MSYSTEM` set , and it lacks `cygpath`.
> ...
> I explicitly set `file_conv` to `mingw` in compile just for testing with this MinGW and there are no issues with double conversion.

Thanks for testing.

In the download area of https://osdn.net/projects/mingw/, I cannot see an
"Msys-based MinGW". Rather, the page says
  "This is the official download site for the latest packages originating
   from the MinGW.OSDN Project, (formerly the MinGW.org Project; however,
   that domain is no longer associated with this project)."

Maybe the mix between the original MinGW and MSYS occurred on your machine?

Anyway, here's an update of the patch series, that should make things work
also in this situation (regardless how it originated).

I wouldn't want to test for the presence of 'cygpath' _without_ also testing
$MSYSTEM, because that could malfunction for people who access the original
MinGW binaries through a Cygwin environment (which is another mix-up that
people might do).

Bruno

>From fa6a98993ea95ce576c79e597cda5e166f36ba5e Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email-masked]>
Date: Mon, 3 Feb 2025 06:02:07 +0100
Subject: [PATCH 1/3] compile: Simplify.

* lib/compile (func_file_conv): Remove unnecessary code, added on 2019-11-11.
* lib/ar-lib (func_file_conv): Likewise.
---
 lib/ar-lib  | 4 ++--
 lib/compile | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/ar-lib b/lib/ar-lib
index 7d62dea99..85761fbf1 100755
--- a/lib/ar-lib
+++ b/lib/ar-lib
@@ -2,7 +2,7 @@
 # Wrapper for Microsoft lib.exe
 
 me=ar-lib
-scriptversion=2024-06-19.01; # UTC
+scriptversion=2025-02-03.05; # UTC
 
 # Copyright (C) 2010-2025 Free Software Foundation, Inc.
 # Written by Peter Rosin <[email-masked]>.
@@ -65,7 +65,7 @@ func_file_conv ()
 	mingw)
 	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
 	  ;;
-	cygwin | msys)
+	cygwin)
 	  file=`cygpath -m "$file" || echo "$file"`
 	  ;;
 	wine)
diff --git a/lib/compile b/lib/compile
index 14aec5621..e80b054a0 100755
--- a/lib/compile
+++ b/lib/compile
@@ -1,7 +1,7 @@
 #! /bin/sh
 # Wrapper for compilers which do not understand '-c -o'.
 
-scriptversion=2024-12-03.03; # UTC
+scriptversion=2025-02-03.05; # UTC
 
 # Copyright (C) 1999-2025 Free Software Foundation, Inc.
 # Written by Tom Tromey <[email-masked]>.
@@ -67,7 +67,7 @@ func_file_conv ()
 	mingw/*)
 	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
 	  ;;
-	cygwin/* | msys/*)
+	cygwin/*)
 	  file=`cygpath -m "$file" || echo "$file"`
 	  ;;
 	wine/*)
-- 
2.43.0

>From afa958e0d4382f5fc6e1be236ae4718b83ec42b0 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email-masked]>
Date: Mon, 3 Feb 2025 06:10:09 +0100
Subject: [PATCH 2/3] compile: Distinguish various MinGW, MSYS, MSYS2
 environments correctly.

Reported by Kirill Makurin <[email-masked]> in
<https://debbugs.gnu.org/cgi/bugreport.cgi?bug=75939>.

* lib/compile (func_file_conv): Use not only "uname -s", but also $MSYSTEM and
the presence of cygpath, in order to distinguish the original MinGW and MSYS2.
* lib/ar-lib (func_file_conv): Likewise.
---
 lib/ar-lib  | 15 +++++++++++++--
 lib/compile | 23 ++++++++++++++++++-----
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/lib/ar-lib b/lib/ar-lib
index 85761fbf1..f85f879d8 100755
--- a/lib/ar-lib
+++ b/lib/ar-lib
@@ -51,9 +51,20 @@ func_file_conv ()
 	# lazily determine how to convert abs files
 	case `uname -s` in
 	  MINGW*)
-	    file_conv=mingw
+	    if test -n "$MSYSTEM" && (cygpath --version) >/dev/null 2>&1; then
+	      # MSYS2 environment.
+	      file_conv=cygwin
+	    else
+	      # Original MinGW environment.
+	      file_conv=mingw
+	    fi
 	    ;;
-	  CYGWIN* | MSYS*)
+	  MSYS*)
+	    # Old MSYS environment, or MSYS2 with 32-bit MSYS2 shell.
+	    file_conv=cygwin
+	    ;;
+	  CYGWIN*)
+	    # Cygwin environment.
 	    file_conv=cygwin
 	    ;;
 	  *)
diff --git a/lib/compile b/lib/compile
index e80b054a0..fc738d3d9 100755
--- a/lib/compile
+++ b/lib/compile
@@ -37,11 +37,11 @@ IFS=" ""	$nl"
 
 file_conv=
 
-# func_file_conv build_file lazy
+# func_file_conv build_file unneeded_conversions
 # Convert a $build file to $host form and store it in $file
 # Currently only supports Windows hosts. If the determined conversion
-# type is listed in (the comma separated) LAZY, no conversion will
-# take place.
+# type is listed in (the comma separated) UNNEEDED_CONVERSIONS, no
+# conversion will take place.
 func_file_conv ()
 {
   file=$1
@@ -51,9 +51,20 @@ func_file_conv ()
 	# lazily determine how to convert abs files
 	case `uname -s` in
 	  MINGW*)
-	    file_conv=mingw
+	    if test -n "$MSYSTEM" && (cygpath --version) >/dev/null 2>&1; then
+	      # MSYS2 environment.
+	      file_conv=cygwin
+	    else
+	      # Original MinGW environment.
+	      file_conv=mingw
+	    fi
 	    ;;
-	  CYGWIN* | MSYS*)
+	  MSYS*)
+	    # Old MSYS environment, or MSYS2 with 32-bit MSYS2 shell.
+	    file_conv=cygwin
+	    ;;
+	  CYGWIN*)
+	    # Cygwin environment.
 	    file_conv=cygwin
 	    ;;
 	  *)
@@ -63,6 +74,8 @@ func_file_conv ()
       fi
       case $file_conv/,$2, in
 	*,$file_conv,*)
+	  # This is the optimization mentioned above:
+	  # If UNNEEDED_CONVERSIONS contains $file_conv, don't convert.
 	  ;;
 	mingw/*)
 	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
-- 
2.43.0

>From cb3d9062422613d7363ada31451334f858fb710a Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email-masked]>
Date: Mon, 3 Feb 2025 06:11:37 +0100
Subject: [PATCH 3/3] compile: Improve support for C++ compilations on MSYS2.

Reported by Kirill Makurin <[email-masked]> in
<https://debbugs.gnu.org/cgi/bugreport.cgi?bug=75939>.

* lib/compile (func_file_conv): Use 'cygpath -w', not 'cygpath -m'.
* lib/ar-lib (func_file_conv): Likewise.
---
 lib/ar-lib  | 2 +-
 lib/compile | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/ar-lib b/lib/ar-lib
index f85f879d8..d0a7b5c8a 100755
--- a/lib/ar-lib
+++ b/lib/ar-lib
@@ -77,7 +77,7 @@ func_file_conv ()
 	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
 	  ;;
 	cygwin)
-	  file=`cygpath -m "$file" || echo "$file"`
+	  file=`cygpath -w "$file" || echo "$file"`
 	  ;;
 	wine)
 	  file=`winepath -w "$file" || echo "$file"`
diff --git a/lib/compile b/lib/compile
index fc738d3d9..c404e89e4 100755
--- a/lib/compile
+++ b/lib/compile
@@ -81,7 +81,7 @@ func_file_conv ()
 	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
 	  ;;
 	cygwin/*)
-	  file=`cygpath -m "$file" || echo "$file"`
+	  file=`cygpath -w "$file" || echo "$file"`
 	  ;;
 	wine/*)
 	  file=`winepath -w "$file" || echo "$file"`
-- 
2.43.0