From 96eef92849a136c740462a6d4e2561d535155f98 Mon Sep 17 00:00:00 2001
From: Iain Sandoe <iain@sandoe.co.uk>
Date: Fri, 16 Apr 2021 20:01:40 +0100
Subject: [PATCH 4/4] Testsuite: allow non-installed testing on darwin

DYLD_LIBRARY_PATH is now removed from the environment for all system
tools, including the shell. Adapt the testsuite and pass the right
options to allow testing, even when the compiler and libraries have not
been installed.

gcc/ChangeLog:

	* Makefile.in: set ENABLE_DARWIN_AT_RPATH in site.tmp.

gcc/testsuite/ChangeLog:

	* gfortran.dg/coarray/caf.exp: Correctly set
	libatomic flags.
	* gfortran.dg/dg.exp: Likewise.
	* lib/asan-dg.exp: Set correct -B flags.
	* lib/atomic-dg.exp: Likewise.
	* lib/target-libpath.exp: Handle ENABLE_DARWIN_AT_RPATH.

libatomic/ChangeLog:

	* testsuite/lib/libatomic.exp: Pass correct flags on darwin.

libffi/ChangeLog:

	* testsuite/lib/libffi.exp: Likewise.

libitm/ChangeLog:

	* testsuite/lib/libitm.exp: Likewise.
	* testsuite/libitm.c++/c++.exp: Likewise.
---
 gcc/Makefile.in                           |  3 +++
 gcc/testsuite/gfortran.dg/coarray/caf.exp | 16 +++++++++---
 gcc/testsuite/gfortran.dg/dg.exp          | 32 ++++++++++++++++++++---
 gcc/testsuite/lib/asan-dg.exp             |  2 +-
 gcc/testsuite/lib/atomic-dg.exp           |  2 +-
 gcc/testsuite/lib/target-libpath.exp      | 23 +++++++++++++---
 libatomic/testsuite/lib/libatomic.exp     |  8 ++++--
 libffi/testsuite/lib/libffi.exp           | 11 +++++---
 libitm/testsuite/lib/libitm.exp           |  1 +
 libitm/testsuite/libitm.c++/c++.exp       |  4 ++-
 10 files changed, 84 insertions(+), 18 deletions(-)

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index 6809a7781a1..0abd43e5e3f 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -4202,6 +4202,9 @@ site.exp: ./config.status Makefile
 	  echo "set COMPAT_OPTIONS \"$(COMPAT_OPTIONS)\"" >> ./site.tmp; \
 	else true; \
 	fi
+	@if test "x@enable_darwin_at_rpath@" = "xyes" ; then \
+	  echo "set ENABLE_DARWIN_AT_RPATH 1" >> ./site.tmp; \
+	fi
 	@echo "## All variables above are generated by configure. Do Not Edit ##" >> ./site.tmp
 	@cat ./site.tmp > site.exp
 	@cat site.bak | sed \
diff --git a/gcc/testsuite/gfortran.dg/coarray/caf.exp b/gcc/testsuite/gfortran.dg/coarray/caf.exp
index d232be2fa90..1c94743a977 100644
--- a/gcc/testsuite/gfortran.dg/coarray/caf.exp
+++ b/gcc/testsuite/gfortran.dg/coarray/caf.exp
@@ -28,6 +28,7 @@
 
 # Load procedures from common libraries. 
 load_lib gfortran-dg.exp
+load_lib atomic-dg.exp
 
 # If a testcase doesn't have special options, use these.
 global DEFAULT_FFLAGS
@@ -47,6 +48,7 @@ global gfortran_test_path
 global gfortran_aux_module_flags
 set gfortran_test_path $srcdir/$subdir
 set gfortran_aux_module_flags $DEFAULT_FFLAGS
+
 proc dg-compile-aux-modules { args } {
     global gfortran_test_path
     global gfortran_aux_module_flags
@@ -71,7 +73,15 @@ proc dg-compile-aux-modules { args } {
 # Add -latomic only where supported.  Assume built-in support elsewhere.
 set maybe_atomic_lib ""
 if [check_effective_target_libatomic_available] {
-    set maybe_atomic_lib "-latomic"
+    if ![is_remote host] {
+	if [info exists TOOL_OPTIONS] {
+	    set maybe_atomic_lib "[atomic_link_flags [get_multilibs ${TOOL_OPTIONS}]]"
+	} else {
+	    set maybe_atomic_lib "[atomic_link_flags [get_multilibs]]"
+	}
+    }
+    set t [get_multilibs]
+    puts "maybe al $maybe_atomic_lib ml $t"
 }
 
 # Main loop.
@@ -97,14 +107,14 @@ foreach test [lsort [glob -nocomplain $srcdir/$subdir/*.\[fF\]{,90,95,03,08} ]]
     foreach flags $option_list {
 	verbose "Testing $nshort (single), $flags" 1
         set gfortran_aux_module_flags "-fcoarray=single $flags"
-	dg-test $test "-fcoarray=single $flags $maybe_atomic_lib" "" 
+	dg-test $test "-fcoarray=single $flags" $maybe_atomic_lib 
 	cleanup-modules ""
     }
 
     foreach flags $option_list {
 	verbose "Testing $nshort (libcaf_single), $flags" 1
         set gfortran_aux_module_flags "-fcoarray=lib $flags -lcaf_single"
-	dg-test $test "-fcoarray=lib $flags -lcaf_single $maybe_atomic_lib" ""
+	dg-test $test "-fcoarray=lib $flags -lcaf_single" $maybe_atomic_lib
 	cleanup-modules ""
     }
 }
diff --git a/gcc/testsuite/gfortran.dg/dg.exp b/gcc/testsuite/gfortran.dg/dg.exp
index ee2760327dc..73541ea7301 100644
--- a/gcc/testsuite/gfortran.dg/dg.exp
+++ b/gcc/testsuite/gfortran.dg/dg.exp
@@ -18,6 +18,7 @@
 
 # Load support procs.
 load_lib gfortran-dg.exp
+load_lib atomic-dg.exp
 
 # If a testcase doesn't have special options, use these.
 global DEFAULT_FFLAGS
@@ -53,13 +54,38 @@ proc dg-compile-aux-modules { args } {
     }
 }
 
+# coarray tests might need libatomic.  Assume that it is either not needed or
+# provided by builtins if it's not available.
+set maybe_atomic_lib ""
+if [check_effective_target_libatomic_available] {
+    if ![is_remote host] {
+	if [info exists TOOL_OPTIONS] {
+	    set maybe_atomic_lib "[atomic_link_flags [get_multilibs ${TOOL_OPTIONS}]]"
+	} else {
+	    set maybe_atomic_lib "[atomic_link_flags [get_multilibs]]"
+	}
+    } else {
+        set maybe_atomic_lib ""
+    }
+  set t [get_multilibs]
+  puts "dg set al $maybe_atomic_lib ml $t"
+}
+
+set all_flags $DEFAULT_FFLAGS
+if { $maybe_atomic_lib != "" } {
+   foreach f $maybe_atomic_lib {
+     lappend all_flags $f
+   }
+}
+
+puts "revised FFLAGS $all_flags"
+
 # Main loop.
 gfortran-dg-runtest [lsort \
-       [glob -nocomplain $srcdir/$subdir/*.\[fF\]{,90,95,03,08} ] ] "" $DEFAULT_FFLAGS
+       [glob -nocomplain $srcdir/$subdir/*.\[fF\]{,90,95,03,08} ] ] "" $all_flags
 
 gfortran-dg-runtest [lsort \
-       [glob -nocomplain $srcdir/$subdir/g77/*.\[fF\] ] ] "" $DEFAULT_FFLAGS
-
+       [glob -nocomplain $srcdir/$subdir/g77/*.\[fF\] ] ] "" $all_flags
 
 # All done.
 dg-finish
diff --git a/gcc/testsuite/lib/asan-dg.exp b/gcc/testsuite/lib/asan-dg.exp
index 35e60eaaed5..157b60908d6 100644
--- a/gcc/testsuite/lib/asan-dg.exp
+++ b/gcc/testsuite/lib/asan-dg.exp
@@ -78,7 +78,7 @@ proc asan_link_flags_1 { paths lib } {
 	   || [file exists "${gccpath}/libsanitizer/${lib}/.libs/lib${lib}.${shlib_ext}"] } {
 	  append flags " -B${gccpath}/libsanitizer/ "
 	  append flags " -B${gccpath}/libsanitizer/${lib}/ "
-	  append flags " -L${gccpath}/libsanitizer/${lib}/.libs "
+	  append flags " -B${gccpath}/libsanitizer/${lib}/.libs "
 	  append ld_library_path ":${gccpath}/libsanitizer/${lib}/.libs"
       }
     } else {
diff --git a/gcc/testsuite/lib/atomic-dg.exp b/gcc/testsuite/lib/atomic-dg.exp
index 1589acd8eaf..ce1799cef2d 100644
--- a/gcc/testsuite/lib/atomic-dg.exp
+++ b/gcc/testsuite/lib/atomic-dg.exp
@@ -33,7 +33,7 @@ proc atomic_link_flags { paths } {
       if { [file exists "${gccpath}/libatomic/.libs/libatomic.a"]
 	   || [file exists "${gccpath}/libatomic/.libs/libatomic.${shlib_ext}"] } {
 	  append flags " -B${gccpath}/libatomic/ "
-	  append flags " -L${gccpath}/libatomic/.libs"
+	  append flags " -B${gccpath}/libatomic/.libs"
 	  append ld_library_path ":${gccpath}/libatomic/.libs"
       }
     } else {
diff --git a/gcc/testsuite/lib/target-libpath.exp b/gcc/testsuite/lib/target-libpath.exp
index 6d530fb4af6..5de039b4fc2 100644
--- a/gcc/testsuite/lib/target-libpath.exp
+++ b/gcc/testsuite/lib/target-libpath.exp
@@ -67,6 +67,7 @@ proc set_ld_library_path_env_vars { } {
   global orig_dyld_library_path
   global orig_path
   global orig_gcc_exec_prefix
+  global ENABLE_DARWIN_AT_RPATH
   global env
 
   # Save the original GCC_EXEC_PREFIX.
@@ -133,6 +134,7 @@ proc set_ld_library_path_env_vars { } {
   #
   # Doing this is somewhat of a hack as ld_library_path gets repeated in
   # SHLIB_PATH and LD_LIBRARY_PATH when unix_load sets these variables.
+  if { ![istarget *-*-darwin*] } {
   if { $orig_ld_library_path_saved } {
     setenv LD_LIBRARY_PATH "$ld_library_path:$orig_ld_library_path"
   } else {
@@ -166,11 +168,23 @@ proc set_ld_library_path_env_vars { } {
   } else {
     setenv LD_LIBRARY_PATH_64 "$ld_library_path"
   }
-  if { $orig_dyld_library_path_saved } {
-    setenv DYLD_LIBRARY_PATH "$ld_library_path:$orig_dyld_library_path"
-  } else {
-    setenv DYLD_LIBRARY_PATH "$ld_library_path"
   }
+  if { [istarget *-*-darwin*] } {
+    if { [info exists ENABLE_DARWIN_AT_RPATH] || [istarget *-*-darwin1\[5-9\]*]
+         || [istarget *-*-darwin20*] } {
+      # Either we are not using DYLD_LIBRARY_PATH or we're on a version of the
+      # OS for which it is not passed through system exes.
+      if [info exists env(DYLD_LIBRARY_PATH)] {
+        unsetenv DYLD_LIBRARY_PATH
+      }
+    } else {
+      if { $orig_dyld_library_path_saved } {
+        setenv DYLD_LIBRARY_PATH "$ld_library_path:$orig_dyld_library_path"
+      } else {
+        setenv DYLD_LIBRARY_PATH "$ld_library_path"
+      }
+    }
+  } 
   if { [istarget *-*-cygwin*] || [istarget *-*-mingw*] } {
     if { $orig_path_saved } {
       setenv PATH "$ld_library_path:$orig_path"
@@ -179,6 +193,7 @@ proc set_ld_library_path_env_vars { } {
     }
   }
 
+  verbose -log "set paths"
   verbose -log "LD_LIBRARY_PATH=[getenv LD_LIBRARY_PATH]"
   verbose -log "LD_RUN_PATH=[getenv LD_RUN_PATH]"
   verbose -log "SHLIB_PATH=[getenv SHLIB_PATH]"
diff --git a/libatomic/testsuite/lib/libatomic.exp b/libatomic/testsuite/lib/libatomic.exp
index 10f38475bc8..c6d645e9ae3 100644
--- a/libatomic/testsuite/lib/libatomic.exp
+++ b/libatomic/testsuite/lib/libatomic.exp
@@ -148,11 +148,15 @@ proc libatomic_init { args } {
     if { $blddir != "" } {
 	lappend ALWAYS_CFLAGS "additional_flags=-B${blddir}/"
 	lappend ALWAYS_CFLAGS "additional_flags=-I${blddir}"
-	lappend ALWAYS_CFLAGS "ldflags=-L${blddir}/.libs"
+        if [istarget *-*-darwin*] {
+            lappend ALWAYS_CFLAGS "additional_flags=-B${blddir}/.libs"
+	} else {
+	    lappend ALWAYS_CFLAGS "ldflags=-L${blddir}/.libs"
+	}
     }
     lappend ALWAYS_CFLAGS "additional_flags=-I${srcdir}/.."
 
-    if [istarget *-*-darwin*] {
+    if [istarget *-*-darwin\[89\]*] {
 	lappend ALWAYS_CFLAGS "additional_flags=-shared-libgcc"
     }
 
diff --git a/libffi/testsuite/lib/libffi.exp b/libffi/testsuite/lib/libffi.exp
index 15d3d5ebd73..611f5177c7a 100644
--- a/libffi/testsuite/lib/libffi.exp
+++ b/libffi/testsuite/lib/libffi.exp
@@ -337,8 +337,13 @@ proc libffi-init { args } {
     verbose "libffi_dir $libffi_dir"
     if { $libffi_dir != "" } {
 	set libffi_dir [file dirname ${libffi_dir}]
-	set libffi_link_flags "-L${libffi_dir}/.libs"
-	lappend libffi_link_flags "-L${blddircxx}/src/.libs"
+        if [istarget *-*-darwin*] {
+            set libffi_link_flags "-B${libffi_dir}/.libs"
+	    lappend libffi_link_flags "-B${blddircxx}/src/.libs"
+	} else {
+	    set libffi_link_flags "-L${libffi_dir}/.libs"
+	    lappend libffi_link_flags "-L${blddircxx}/src/.libs"
+	}
     }
 
     set_ld_library_path_env_vars
@@ -382,7 +387,7 @@ proc libffi_target_compile { source dest type options } {
     # Darwin needs a stack execution allowed flag.
 
     if { [istarget "*-*-darwin9*"] || [istarget "*-*-darwin1*"]
-	 || [istarget "*-*-darwin2*"] } {
+	 || [istarget "x86_64-*-darwin2*"] } {
 	lappend options "additional_flags=-Wl,-allow_stack_execute"
 	lappend options "additional_flags=-Wl,-search_paths_first"
     }
diff --git a/libitm/testsuite/lib/libitm.exp b/libitm/testsuite/lib/libitm.exp
index da918d1ee8d..61bbfa0c923 100644
--- a/libitm/testsuite/lib/libitm.exp
+++ b/libitm/testsuite/lib/libitm.exp
@@ -159,6 +159,7 @@ proc libitm_init { args } {
     }
 
     if [istarget *-*-darwin*] {
+	lappend ALWAYS_CFLAGS "additional_flags=-B${blddir}/.libs"
 	lappend ALWAYS_CFLAGS "additional_flags=-shared-libgcc"
     }
 
diff --git a/libitm/testsuite/libitm.c++/c++.exp b/libitm/testsuite/libitm.c++/c++.exp
index de45e7e5480..1b0ead05fee 100644
--- a/libitm/testsuite/libitm.c++/c++.exp
+++ b/libitm/testsuite/libitm.c++/c++.exp
@@ -56,8 +56,10 @@ if { $lang_test_file_found } {
     # Gather a list of all tests.
     set tests [lsort [glob -nocomplain $srcdir/$subdir/*.C]]
 
+    set stdcxxadder ""
     if { $blddir != "" } {
 	set ld_library_path "$always_ld_library_path:${blddir}/${lang_library_path}"
+	set stdcxxadder "-B ${blddir}/${lang_library_path}"
     } else {
 	set ld_library_path "$always_ld_library_path"
     }
@@ -72,7 +74,7 @@ if { $lang_test_file_found } {
     }
 
     # Main loop.
-    dg-runtest $tests "" $libstdcxx_includes
+    dg-runtest $tests $stdcxxadder $libstdcxx_includes
 }
 
 # All done.
-- 
2.39.2 (Apple Git-143)

