gdb/configure.ac: Fix auto-load options to work with Windows path separator

Message ID 20231026234013.937210-1-thiago.bauermann@linaro.org
State New
Headers
Series gdb/configure.ac: Fix auto-load options to work with Windows path separator |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Thiago Jung Bauermann Oct. 26, 2023, 11:40 p.m. UTC
  Both --with-auto-load-dir and --with-auto-load-safe-path accept a list of
directories, which are separated by ':' on Unix and ';' on Windows.
However, as mentioned in PR 18898 this doesn't work on the latter OS.

This is because the AC_DEFINE_DIR macro calls eval twice on the value
provided via these configure options, causing the ';' to be interpreted by
the shell.  E.g.,

  $ ~/src/binutils-gdb/configure \
      --disable-{binutils,ld,gold,gas,sim,gprof,gprofng} \
      --with-auto-load-dir='foo;bar' && make
      ⋮
  checking for default auto-load directory... /home/bauermann/src/binutils-gdb/gdb/configure: line 18004: bar: command not found
  foo;bar
  checking for default auto-load safe-path... /home/bauermann/src/binutils-gdb/gdb/configure: line 18031: bar: command not found
  foo;bar

Line 18004 is:

  ac_define_dir=`eval echo $escape_dir`

Line 18031 is identical.

With some escaping, it's possible to avoid the problem:

  $ ~/src/binutils-gdb/configure \
      --disable-{binutils,ld,gold,gas,sim,gprof,gprofng} \
      --with-auto-load-dir='foo\\\;bar\\\;baz' && make
      ⋮
  checking for default auto-load directory... foo\\\;bar\\\;baz
  checking for default auto-load safe-path... foo\\\;bar\\\;baz
      ⋮
  $ grep AUTO_LOAD gdb/config.h
  #define AUTO_LOAD_DIR "foo;bar;baz"
  #define AUTO_LOAD_SAFE_PATH "foo;bar;baz"

We provide the definition of AC_DEFINE_DIR in gdb/acinclude.m4 so the
simplest approach to fix this problem would be to remove the evals from the
macro.

I don't know why they are there however, so instead I decided to make
gdb/configure.ac automatically add those escaping characters to the values
of the options if the host OS is Windows.  I don't know if Cygwin also
needs this change, so for now I'm only adding it for MinGW.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=18898
---

Hello,

This addresses the problem that Eli has been having with these options
on MinGW. I don't have access to a Windows system, so I tested the patch
on Linux by changing the case to match "linux*" rather than "mingw*" and
using the build and grep commands mentioned in the commit message.

It would be nice if someone could test on an actual Windows system, and
also let me know if other host_os strings should also be matched (e.g.,
"cygwin*" or "windows*")

 gdb/configure    | 20 ++++++++++++++++++--
 gdb/configure.ac | 20 ++++++++++++++++++--
 2 files changed, 36 insertions(+), 4 deletions(-)
  

Patch

diff --git a/gdb/configure b/gdb/configure
index 5361bf42952a..96f0e3da9637 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -17997,7 +17997,15 @@  else
   with_auto_load_dir='$debugdir:$datadir/auto-load'
 fi
 
-escape_dir=`echo $with_auto_load_dir | sed -e 's/[$]datadir\>/\\\\\\\\\\\\&/g' -e 's/[$]debugdir\>/\\\\\\\\\\\\&/g'`
+# If we're on Windows, we need to protect the PATH separator ';' from the couple
+# of eval calls done by AC_DEFINE_DIR.
+extra_sed_args=()
+case "$host_os" in
+  mingw*)
+  extra_sed_args=(-e 's/;/\\\\\\;/g')
+  ;;
+esac
+escape_dir=`echo $with_auto_load_dir | sed -e 's/[$]datadir\>/\\\\\\\\\\\\&/g' -e 's/[$]debugdir\>/\\\\\\\\\\\\&/g' "${extra_sed_args[@]}"`
 
   test "x$prefix" = xNONE && prefix="$ac_default_prefix"
   test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'
@@ -18024,7 +18032,15 @@  else
   with_auto_load_safe_path="$with_auto_load_dir"
 fi
 
-escape_dir=`echo $with_auto_load_safe_path | sed -e 's/[$]datadir\>/\\\\\\\\\\\\&/g' -e 's/[$]debugdir\>/\\\\\\\\\\\\&/g'`
+# If we're on Windows, we need to protect the PATH separator ';' from the couple
+# of eval calls done by AC_DEFINE_DIR.
+extra_sed_args=()
+case "$host_os" in
+  mingw*)
+  extra_sed_args=(-e 's/;/\\\\\\;/g')
+  ;;
+esac
+escape_dir=`echo $with_auto_load_safe_path | sed -e 's/[$]datadir\>/\\\\\\\\\\\\&/g' -e 's/[$]debugdir\>/\\\\\\\\\\\\&/g' "${extra_sed_args[@]}"`
 
   test "x$prefix" = xNONE && prefix="$ac_default_prefix"
   test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 3912b77b27f2..cc8d2bfc5e8b 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -153,7 +153,15 @@  AC_ARG_WITH(auto-load-dir,
 AS_HELP_STRING([--with-auto-load-dir=PATH],
   [directories from which to load auto-loaded scripts @<:@$debugdir:$datadir/auto-load@:>@]),,
   [with_auto_load_dir='$debugdir:$datadir/auto-load'])
-escape_dir=`echo $with_auto_load_dir | sed -e 's/[[$]]datadir\>/\\\\\\\\\\\\&/g' -e 's/[[$]]debugdir\>/\\\\\\\\\\\\&/g'`
+# If we're on Windows, we need to protect the PATH separator ';' from the couple
+# of eval calls done by AC_DEFINE_DIR.
+extra_sed_args=()
+case "$host_os" in
+  mingw*)
+  extra_sed_args=(-e 's/;/\\\\\\;/g')
+  ;;
+esac
+escape_dir=`echo $with_auto_load_dir | sed -e 's/[[$]]datadir\>/\\\\\\\\\\\\&/g' -e 's/[[$]]debugdir\>/\\\\\\\\\\\\&/g' "${extra_sed_args[[@]]}"`
 AC_DEFINE_DIR(AUTO_LOAD_DIR, escape_dir,
 	      [Directories from which to load auto-loaded scripts.])
 AC_MSG_RESULT([$with_auto_load_dir])
@@ -168,7 +176,15 @@  AS_HELP_STRING([--without-auto-load-safe-path],
      with_auto_load_safe_path="/"
      fi],
 [with_auto_load_safe_path="$with_auto_load_dir"])
-escape_dir=`echo $with_auto_load_safe_path | sed -e 's/[[$]]datadir\>/\\\\\\\\\\\\&/g' -e 's/[[$]]debugdir\>/\\\\\\\\\\\\&/g'`
+# If we're on Windows, we need to protect the PATH separator ';' from the couple
+# of eval calls done by AC_DEFINE_DIR.
+extra_sed_args=()
+case "$host_os" in
+  mingw*)
+  extra_sed_args=(-e 's/;/\\\\\\;/g')
+  ;;
+esac
+escape_dir=`echo $with_auto_load_safe_path | sed -e 's/[[$]]datadir\>/\\\\\\\\\\\\&/g' -e 's/[[$]]debugdir\>/\\\\\\\\\\\\&/g' "${extra_sed_args[[@]]}"`
 AC_DEFINE_DIR(AUTO_LOAD_SAFE_PATH, escape_dir,
 	      [Directories safe to hold auto-loaded files.])
 AC_MSG_RESULT([$with_auto_load_safe_path])