diff --git a/autogen.sh b/autogen.sh
index 3ddd2e4d6cd1e95349f7a7a8fd371a5e5f4637c0..a4f31cbe532aed7cf0a77c1bc52e2960991299e0 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -1,23 +1,29 @@
 #!/bin/sh
 
 autoreconf -fvi || exit $?
-patch --forward --no-backup-if-mismatch -p1 -r - -i patch/libtool.m4.nag_wrapper.patch
-# The program 'patch' exits with exitcode=1 if the patch has already been applied.
-# Consider this a normal scenario:
-exitcode=$?; test $exitcode -ne 0 && test $exitcode -ne 1 && exit $exitcode
 
-patch --forward --no-backup-if-mismatch -p1 -r - -i patch/libtool.m4.arg_spaces.patch
-exitcode=$?; test $exitcode -ne 0 && test $exitcode -ne 1 && exit $exitcode
+# Patch libtool.m4:
+for patch in \
+  ; do
+    echo "applying $patch"
+    patch -p 1 --no-backup-if-mismatch --forward -r - -i $patch -d .
+    # The program 'patch' exits with exitcode=1 if the patch has already been applied.
+    # Consider this a normal scenario:
+    exitcode=$?; if test $exitcode -ne 0 && test $exitcode -ne 1; then exit $exitcode; fi
+done
 
-# Rebuild configure if you need to patch M4 macros:
-autoconf -f || exit $?
-
-# Reset libtool.m4 timestamps to avoid confusing make:
-touch -r m4/ltversion.m4 m4/libtool.m4 || exit $?
-
-patch --forward --no-backup-if-mismatch -p1 -r - -i patch/ltmain.sh.nag_pthread.patch
-exitcode=$?; test $exitcode -ne 0 && test $exitcode -ne 1 && exit $exitcode
-
-# All went fine since we have not exited before:
-exit 0
+# The following is not needed as long as we do not patch libtool.m4:
+# autoconf -f || exit $?
+# # Reset libtool.m4 timestamps to avoid confusing make:
+# touch -r m4/ltversion.m4 m4/libtool.m4 || exit $?
 
+# Patch the rest of the files:
+for patch in \
+  contrib/00nagfor-libtool-patch/nagfor-libtool-2.4.6.patch \
+  contrib/05macos-nagfor-patch/macos-nagfor.patch \
+  contrib/06ltmain_nag_pthread-patch/ltmain_nag_pthread.patch \
+  ; do
+    echo "applying $patch"
+    patch -p 1 --no-backup-if-mismatch --forward -r - -i $patch -d .
+    exitcode=$?; if test $exitcode -ne 0 && test $exitcode -ne 1; then exit $exitcode; fi
+done
diff --git a/contrib/00nagfor-libtool-patch/README.txt b/contrib/00nagfor-libtool-patch/README.txt
new file mode 100644
index 0000000000000000000000000000000000000000..660461961f5f238f040cd17c0c62a1586bbb32cd
--- /dev/null
+++ b/contrib/00nagfor-libtool-patch/README.txt
@@ -0,0 +1,9 @@
+This patch is needed so that -Wl,-rpath option arguments make it into
+the dependent libraries of a library and also ensures that -Wl,-Wl,,
+options are passed to the compiler unaltered.
+
+It is intended for libtool 2.4.2 and can be applied after
+e.g. autoreconf by
+
+$ patch -p1 <contrib/nagfor-libtool-patch/nagfor-libtool.patch
+
diff --git a/contrib/00nagfor-libtool-patch/nagfor-libtool-2.4.2.patch b/contrib/00nagfor-libtool-patch/nagfor-libtool-2.4.2.patch
new file mode 100644
index 0000000000000000000000000000000000000000..5a5a4af38a4ef1c4e770a283d63813ba3c0aa51d
--- /dev/null
+++ b/contrib/00nagfor-libtool-patch/nagfor-libtool-2.4.2.patch
@@ -0,0 +1,137 @@
+--- a/config/ltmain.sh	2012-06-12 00:20:43.000000000 +0200
++++ b/config/ltmain.sh	2014-08-19 16:34:48.161235251 +0200
+@@ -3229,6 +3229,25 @@
+ 
+ test "$opt_mode" = install && func_mode_install ${1+"$@"}
+ 
++func_add2xrpath ()
++{
++  dir="$1"
++  # We need an absolute path.
++  case $dir in
++    [\\/]* | [A-Za-z]:[\\/]*) ;;
++    =*)
++    func_stripname '=' '' "$dir"
++    dir=$lt_sysroot$func_stripname_result
++      ;;
++    *)
++      func_fatal_error "only absolute run-paths are allowed"
++      ;;
++  esac
++  case "$xrpath " in
++    *" $dir "*) ;;
++    *) func_append xrpath " $dir" ;;
++  esac
++}
+ 
+ # func_generate_dlsyms outputname originator pic_p
+ # Extract symbols from dlprefiles and create ${outputname}S.o with
+@@ -5727,20 +5746,7 @@
+ 	func_stripname '-R' '' "$arg"
+ 	dir=$func_stripname_result
+ 	# We need an absolute path.
+-	case $dir in
+-	[\\/]* | [A-Za-z]:[\\/]*) ;;
+-	=*)
+-	  func_stripname '=' '' "$dir"
+-	  dir=$lt_sysroot$func_stripname_result
+-	  ;;
+-	*)
+-	  func_fatal_error "only absolute run-paths are allowed"
+-	  ;;
+-	esac
+-	case "$xrpath " in
+-	*" $dir "*) ;;
+-	*) func_append xrpath " $dir" ;;
+-	esac
++	func_add2xrpath "$dir"
+ 	continue
+ 	;;
+ 
+@@ -5801,18 +5807,58 @@
+ 	;;
+ 
+       -Wl,*)
++	wlprefix=`expr x"$wl" : '.*' - 1`
++	wlprefix=`expr x"$arg" : 'x\(.\{'"$wlprefix"'\}\)'`
++	if test x"$wlprefix" = x"$wl" -a x"$wl" != x"-Wl,"; then
++	  func_append compiler_flags " $arg"
++	  func_append compile_command " $arg"
++	  func_append finalize_command " $arg"
++	  if expr "x$arg" : '^x'"$wl"'-rpath,,' >/dev/null ; then
++	    func_stripname "$wl"'-rpath,,' '' "$arg"
++	    dir=$func_stripname_result
++	    func_add2xrpath "$dir"
++	  elif expr "x$arg" : '^x'"$wl"'-rpath$' >/dev/null ; then
++	    func_stripname "$wl" '' "$1"
++	    dir=$func_stripname_result
++	    if ! expr "x$1" : '^x'"$wl" >/dev/null \
++	    || ! test -d "$dir" ; then
++	    func_fatal_error "$wl"'-rpath must be followed by '"$wl"'-escaped directory'
++	    fi
++	    shift
++	    func_add2xrpath "$dir"
++	    continue
++	  else
++	    func_stripname "$wl" '' "$arg"
++	    func_append new_inherited_linker_flags " -Wl,$func_stripname_result"
++	  fi
++	  continue
++	elif expr "x$arg" : '^x-Wl,-rpath,' >/dev/null ; then
++	  func_stripname '-Wl,-rpath,' '' "$arg"
++	  dir=$func_stripname_result
++	  func_add2xrpath "$dir"
++	  continue
++	elif expr "x$arg" : '^x-Wl,-rpath$' >/dev/null ; then
++	  func_stripname '-Wl,' '' "$1"
++	  dir=$func_stripname_result
++	  if ! expr "x$1" : '^x-Wl,' >/dev/null \
++	    || ! test -d "$dir" ; then
++	    func_fatal_error '-Wl,-rpath must be followed by -Wl-escaped directory'
++	  fi
++	  func_quote_for_eval "$1"
++	  func_append libtool_args " $func_quote_for_eval_result"
++	  shift
++	  func_add2xrpath "$dir"
++	  continue
++	fi
+ 	func_stripname '-Wl,' '' "$arg"
+ 	args=$func_stripname_result
+ 	arg=
+-	save_ifs="$IFS"; IFS=','
++	save_ifs=$IFS; IFS=,
+ 	for flag in $args; do
+-	  IFS="$save_ifs"
++	  IFS=$save_ifs
+           func_quote_for_eval "$flag"
+ 	  func_append arg " $wl$func_quote_for_eval_result"
+ 	  func_append compiler_flags " $wl$func_quote_for_eval_result"
+ 	  func_append linker_flags " $func_quote_for_eval_result"
+ 	done
+-	IFS="$save_ifs"
++	IFS=$save_ifs
+ 	func_stripname ' ' '' "$arg"
+ 	arg=$func_stripname_result
+ 	;;
+@@ -9515,6 +9562,10 @@
+ 	  *) func_append new_libs " $deplib" ;;
+ 	  esac
+ 	  ;;
++	-Wl,*)
++	  func_stripname -Wl, '' "$deplib"
++	  func_append new_libs " $wl$func_stripname_result"
++	  ;;
+ 	*) func_append new_libs " $deplib" ;;
+ 	esac
+       done
+@@ -10247,6 +10298,10 @@
+ 	  *) func_append new_libs " $deplib" ;;
+ 	  esac
+ 	  ;;
++	-Wl,*)
++	  func_stripname -Wl, '' "$deplib"
++	  func_append new_libs " $wl$func_stripname_result"
++	  ;;
+ 	*) func_append new_libs " $deplib" ;;
+ 	esac
+       done
diff --git a/contrib/00nagfor-libtool-patch/nagfor-libtool-2.4.6.patch b/contrib/00nagfor-libtool-patch/nagfor-libtool-2.4.6.patch
new file mode 100644
index 0000000000000000000000000000000000000000..32d0f1e6d772536a57f25ce3699d915889171606
--- /dev/null
+++ b/contrib/00nagfor-libtool-patch/nagfor-libtool-2.4.6.patch
@@ -0,0 +1,122 @@
+--- a/config/ltmain.sh	2012-06-12 00:20:43.000000000 +0200
++++ b/config/ltmain.sh	2014-08-19 16:34:48.161235251 +0200
+@@ -3217,6 +3217,25 @@
+ }
+ # end func_convert_path_msys_to_cygwin
+ 
++func_add2xrpath ()
++{
++  dir="$1"
++  # We need an absolute path.
++  case $dir in
++    [\\/]* | [A-Za-z]:[\\/]*) ;;
++    =*)
++    func_stripname '=' '' "$dir"
++    dir=$lt_sysroot$func_stripname_result
++      ;;
++    *)
++      func_fatal_error "only absolute run-paths are allowed"
++      ;;
++  esac
++  case "$xrpath " in
++    *" $dir "*) ;;
++    *) func_append xrpath " $dir" ;;
++  esac
++}
+ 
+ # func_convert_path_nix_to_cygwin ARG
+ # Convert path ARG from *nix to Cygwin format.  Requires Cygwin installed in a
+@@ -7147,20 +7166,7 @@
+ 	func_stripname '-R' '' "$arg"
+ 	dir=$func_stripname_result
+ 	# We need an absolute path.
+-	case $dir in
+-	[\\/]* | [A-Za-z]:[\\/]*) ;;
+-	=*)
+-	  func_stripname '=' '' "$dir"
+-	  dir=$lt_sysroot$func_stripname_result
+-	  ;;
+-	*)
+-	  func_fatal_error "only absolute run-paths are allowed"
+-	  ;;
+-	esac
+-	case "$xrpath " in
+-	*" $dir "*) ;;
+-	*) func_append xrpath " $dir" ;;
+-	esac
++	func_add2xrpath "$dir"
+ 	continue
+ 	;;
+ 
+@@ -7221,6 +7227,49 @@
+ 	;;
+ 
+       -Wl,*)
++	wlprefix=`expr x"$wl" : '.*' - 1`
++	wlprefix=`expr x"$arg" : 'x\(.\{'"$wlprefix"'\}\)'`
++	if test x"$wlprefix" = x"$wl" -a x"$wl" != x"-Wl,"; then
++	  func_append compiler_flags " $arg"
++	  func_append compile_command " $arg"
++	  func_append finalize_command " $arg"
++	  if expr "x$arg" : '^x'"$wl"'-rpath,,' >/dev/null ; then
++	    func_stripname "$wl"'-rpath,,' '' "$arg"
++	    dir=$func_stripname_result
++	    func_add2xrpath "$dir"
++	  elif expr "x$arg" : '^x'"$wl"'-rpath$' >/dev/null ; then
++	    func_stripname "$wl" '' "$1"
++	    dir=$func_stripname_result
++	    if ! expr "x$1" : '^x'"$wl" >/dev/null \
++	    || ! test -d "$dir" ; then
++	    func_fatal_error "$wl"'-rpath must be followed by '"$wl"'-escaped directory'
++	    fi
++	    shift
++	    func_add2xrpath "$dir"
++	    continue
++	  else
++	    func_stripname "$wl" '' "$arg"
++	    func_append new_inherited_linker_flags " -Wl,$func_stripname_result"
++	  fi
++	  continue
++	elif expr "x$arg" : '^x-Wl,-rpath,' >/dev/null ; then
++	  func_stripname '-Wl,-rpath,' '' "$arg"
++	  dir=$func_stripname_result
++	  func_add2xrpath "$dir"
++	  continue
++	elif expr "x$arg" : '^x-Wl,-rpath$' >/dev/null ; then
++	  func_stripname '-Wl,' '' "$1"
++	  dir=$func_stripname_result
++	  if ! expr "x$1" : '^x-Wl,' >/dev/null \
++	    || ! test -d "$dir" ; then
++	    func_fatal_error '-Wl,-rpath must be followed by -Wl-escaped directory'
++	  fi
++	  func_quote_for_eval "$1"
++	  func_append libtool_args " $func_quote_for_eval_result"
++	  shift
++	  func_add2xrpath "$dir"
++	  continue
++	fi
+ 	func_stripname '-Wl,' '' "$arg"
+ 	args=$func_stripname_result
+ 	arg=
+@@ -9515,6 +9562,10 @@
+ 	  *) func_append new_libs " $deplib" ;;
+ 	  esac
+ 	  ;;
++	-Wl,*)
++	  func_stripname -Wl, '' "$deplib"
++	  func_append new_libs " $wl$func_stripname_result"
++	  ;;
+ 	*) func_append new_libs " $deplib" ;;
+ 	esac
+       done
+@@ -10247,6 +10298,10 @@
+ 	  *) func_append new_libs " $deplib" ;;
+ 	  esac
+ 	  ;;
++	-Wl,*)
++	  func_stripname -Wl, '' "$deplib"
++	  func_append new_libs " $wl$func_stripname_result"
++	  ;;
+ 	*) func_append new_libs " $deplib" ;;
+ 	esac
+       done
diff --git a/contrib/00nagfor-libtool-patch/nagfor-libtool.patch b/contrib/00nagfor-libtool-patch/nagfor-libtool.patch
new file mode 100644
index 0000000000000000000000000000000000000000..9677b1666963501864636f115774ed319fff844c
--- /dev/null
+++ b/contrib/00nagfor-libtool-patch/nagfor-libtool.patch
@@ -0,0 +1,112 @@
+--- a/config/ltmain.sh	2012-06-12 00:20:43.000000000 +0200
++++ b/config/ltmain.sh	2014-08-19 16:34:48.161235251 +0200
+@@ -3229,6 +3229,25 @@
+ 
+ test "$opt_mode" = install && func_mode_install ${1+"$@"}
+ 
++func_add2xrpath ()
++{
++  dir="$1"
++  # We need an absolute path.
++  case $dir in
++    [\\/]* | [A-Za-z]:[\\/]*) ;;
++    =*)
++    func_stripname '=' '' "$dir"
++    dir=$lt_sysroot$func_stripname_result
++      ;;
++    *)
++      func_fatal_error "only absolute run-paths are allowed"
++      ;;
++  esac
++  case "$xrpath " in
++    *" $dir "*) ;;
++    *) func_append xrpath " $dir" ;;
++  esac
++}
+ 
+ # func_generate_dlsyms outputname originator pic_p
+ # Extract symbols from dlprefiles and create ${outputname}S.o with
+@@ -5727,20 +5746,7 @@
+ 	func_stripname '-R' '' "$arg"
+ 	dir=$func_stripname_result
+ 	# We need an absolute path.
+-	case $dir in
+-	[\\/]* | [A-Za-z]:[\\/]*) ;;
+-	=*)
+-	  func_stripname '=' '' "$dir"
+-	  dir=$lt_sysroot$func_stripname_result
+-	  ;;
+-	*)
+-	  func_fatal_error "only absolute run-paths are allowed"
+-	  ;;
+-	esac
+-	case "$xrpath " in
+-	*" $dir "*) ;;
+-	*) func_append xrpath " $dir" ;;
+-	esac
++	func_add2xrpath "$dir"
+ 	continue
+ 	;;
+ 
+@@ -5801,18 +5807,58 @@
+ 	;;
+ 
+       -Wl,*)
++	wlprefix=`expr x"$wl" : '.*' - 1`
++	wlprefix=`expr x"$arg" : 'x\(.\{'"$wlprefix"'\}\)'`
++	if test x"$wlprefix" = x"$wl" -a x"$wl" != x"-Wl,"; then
++	  func_append compiler_flags " $arg"
++	  func_append compile_command " $arg"
++	  func_append finalize_command " $arg"
++	  if expr "x$arg" : '^x'"$wl"'-rpath,,' >/dev/null ; then
++           func_stripname "$wl"'-rpath,,' '' "$arg"
++	    dir=$func_stripname_result
++	    func_add2xrpath "$dir"
++	  elif expr "x$arg" : '^x'"$wl"'-rpath$' >/dev/null ; then
++	    func_stripname "$wl" '' "$1"
++	    dir=$func_stripname_result
++	    if ! expr "x$1" : '^x'"$wl" >/dev/null \
++	    || ! test -d "$dir" ; then
++	    func_fatal_error "$wl"'-rpath must be followed by '"$wl"'-escaped directory'
++           fi
++	    shift
++	    func_add2xrpath "$dir"
++	    continue
++	  else
++	    func_append new_inherited_linker_flags " $arg"
++	  fi
++	  continue
++	elif expr "x$arg" : '^x-Wl,-rpath,' >/dev/null ; then
++	  func_stripname '-Wl,-rpath,' '' "$arg"
++	  dir=$func_stripname_result
++	  func_add2xrpath "$dir"
++	  continue
++	elif expr "x$arg" : '^x-Wl,-rpath$' >/dev/null ; then
++	  func_stripname '-Wl,' '' "$1"
++	  dir=$func_stripname_result
++	  if ! expr "x$1" : '^x-Wl,' >/dev/null \
++	    || ! test -d "$dir" ; then
++	    func_fatal_error '-Wl,-rpath must be followed by -Wl-escaped directory'
++         fi
++	  shift
++	  func_add2xrpath "$dir"
++	  continue
++	fi
+ 	func_stripname '-Wl,' '' "$arg"
+ 	args=$func_stripname_result
+ 	arg=
+-	save_ifs="$IFS"; IFS=','
++	save_ifs=$IFS; IFS=,
+ 	for flag in $args; do
+-	  IFS="$save_ifs"
++	  IFS=$save_ifs
+           func_quote_for_eval "$flag"
+ 	  func_append arg " $wl$func_quote_for_eval_result"
+ 	  func_append compiler_flags " $wl$func_quote_for_eval_result"
+ 	  func_append linker_flags " $func_quote_for_eval_result"
+ 	done
+-	IFS="$save_ifs"
++	IFS=$save_ifs
+ 	func_stripname ' ' '' "$arg"
+ 	arg=$func_stripname_result
+ 	;;
diff --git a/contrib/05macos-nagfor-patch/macos-nagfor.patch b/contrib/05macos-nagfor-patch/macos-nagfor.patch
new file mode 100644
index 0000000000000000000000000000000000000000..befdb41228fa2551824267b237573afcb10579f6
--- /dev/null
+++ b/contrib/05macos-nagfor-patch/macos-nagfor.patch
@@ -0,0 +1,13 @@
+--- a/config/ltmain.sh	2019-10-18 09:59:55.967567005 +0200
++++ b/config/ltmain.sh	2019-10-18 10:00:11.403887362 +0200
+@@ -8958,8 +8958,8 @@
+ 	  xlcverstring="$wl-compatibility_version $wl$minor_current $wl-current_version $wl$minor_current.$revision"
+ 	  verstring="-compatibility_version $minor_current -current_version $minor_current.$revision"
+           # On Darwin other compilers
+-          case $CC in
+-              nagfor*)
++          case $with_nagfor in
++              yes)
+                   verstring="$wl-compatibility_version $wl$minor_current $wl-current_version $wl$minor_current.$revision"
+                   ;;
+               *)
diff --git a/patch/ltmain.sh.nag_pthread.patch b/contrib/06ltmain_nag_pthread-patch/ltmain_nag_pthread.patch
similarity index 67%
rename from patch/ltmain.sh.nag_pthread.patch
rename to contrib/06ltmain_nag_pthread-patch/ltmain_nag_pthread.patch
index 63b3d3c66dd379150633c99c24e80358171e20f2..c267e68ec1e8c5b9afb709f5398d4fd632411cbc 100644
--- a/patch/ltmain.sh.nag_pthread.patch
+++ b/contrib/06ltmain_nag_pthread-patch/ltmain_nag_pthread.patch
@@ -1,20 +1,19 @@
 --- a/config/ltmain.sh
 +++ b/config/ltmain.sh
-@@ -7862,6 +7862,13 @@ func_mode_link ()
+@@ -7936,6 +7936,12 @@ func_mode_link ()
  	# Convert "-framework foo" to "foo.ltframework"
  	if test -n "$inherited_linker_flags"; then
  	  tmp_inherited_linker_flags=`$ECHO "$inherited_linker_flags" | $SED 's/-framework \([^ $]*\)/\1.ltframework/g'`
 +
 +	  # Additionally convert " -pthread" to " -Wl,-pthread" for nagfor
-+	  func_cc_basename $CC
-+	  case $func_cc_basename_result in
-+	    nagfor*) tmp_inherited_linker_flags=`$ECHO " $tmp_inherited_linker_flags" | $SED 's/ -pthread/ -Wl,-pthread/g'` ;;
-+	  esac
++	  if test yes = $with_nagfor ; then
++	    tmp_inherited_linker_flags=`$ECHO " $tmp_inherited_linker_flags" | $SED 's/ -pthread\b/ -Wl,-pthread/g'`
++	  fi
 +
  	  for tmp_inherited_linker_flag in $tmp_inherited_linker_flags; do
  	    case " $new_inherited_linker_flags " in
  	      *" $tmp_inherited_linker_flag "*) ;;
-@@ -9494,6 +9501,22 @@ EOF
+@@ -9570,6 +9577,20 @@ EOF
  	  ;;
        esac
  
@@ -28,12 +27,19 @@
 +      # better not to revert the changes, otherwise, uncomment the following
 +      # lines.
 +
-+      func_cc_basename $CC
-+      case $func_cc_basename_result in
-+        nagfor*)
-+          new_inherited_linker_flags=`$ECHO " $new_inherited_linker_flags" | $SED 's% -Wl,-pthread% -pthread%g'` ;;
-+      esac
++      if test yes = $with_nagfor ; then
++        new_inherited_linker_flags=`$ECHO " $new_inherited_linker_flags" | $SED 's/ -Wl,-pthread\b/ -pthread/g;s/^ *//'`
++      fi
 +
        # move library search paths that coincide with paths to not yet
        # installed libraries to the beginning of the library search list
        new_libs=
+@@ -10328,6 +10348,8 @@
+ 	  *) func_append new_libs " $deplib" ;;
+ 	  esac
+ 	  ;;
++	-Wl,-pthread)
++	  func_append new_libs " $deplib" ;;
+ 	-Wl,*)
+ 	  func_stripname -Wl, '' "$deplib"
+ 	  func_append new_libs " $wl$func_stripname_result"
diff --git a/m4/acx_use_libtool_configuration.m4 b/m4/acx_use_libtool_configuration.m4
index a9993745a05b49f301094537d9a564eb8b82e79e..03d6780cb08a17381ba1ee995f9972a469b02f2c 100644
--- a/m4/acx_use_libtool_configuration.m4
+++ b/m4/acx_use_libtool_configuration.m4
@@ -38,7 +38,9 @@ dnl _ACX_LT_FLAGS_MANGLE
 m4_define([nag_filter_flag_var],
   [AS_IF([test x${$1+set} = xset],
      [$1=`echo "$][$1" | tr ' ' '\n' | sed -e '/^-W@<:@lc@:>@/{' \
--e 's/^\(-Wl\)/-XCClinker \1/;s/^\(-Wc\)/-Xcompiler \1/' -e '}'     | tr '\n' ' '`])])
+-e 's/^\(-Wl\)/-XCClinker \1/;s/^\(-Wc,\)/-Xcompiler \1/' \
+-e 's/^\(-Wc=.*\)/-Xcompiler \1 -XCClinker \1/' -e '}' \
+   | tr '\n' ' ' | sed -e 's/ $//'`])])
 AC_DEFUN([_ACX_LT_FORT_FLAGS_MANGLE],
   [_AC_FORTRAN_ASSERT
    AC_LANG_CASE([Fortran],
@@ -49,7 +51,7 @@ AC_DEFUN([_ACX_LT_FORT_FLAGS_MANGLE],
      [AS_VAR_PUSHDEF([acx_FC],[F77])dnl
       AS_VAR_PUSHDEF([acx_FCFLAGS],[FFLAGS])dnl
       AS_VAR_PUSHDEF([acx_LDFLAGS],[F77LDFLAGS])])
-   acx_temp=`$acx_FC -V 2>&1 | sed 5q`
+   acx_temp=`$acx_FC -V 2>&1 | sed -n 1,5p`
 dnl fix problems from NAG compiler
    AS_CASE(["$acx_temp"],
      [*NAG\ Fortran\ Compiler\ Release*],
@@ -74,16 +76,19 @@ AC_DEFUN([ACX_USE_LIBTOOL_CONFIGURATION],
 dnl add some monkey patching for older libtool versions that don't handle
 dnl newer PGI or NAG configurations particularly well
    m4_if(m4_cmp(m4_version_compare(LT_PACKAGE_VERSION,[2.4.6]),1),-1,
-     [m4_pushdef([_LT_COMPILER_PIC],m4_bpatsubst(m4_dquote(m4_defn([_LT_COMPILER_PIC])),[;;
+     [m4_pushdef([_LT_COMPILER_PIC],m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(m4_defn([_LT_COMPILER_PIC])),[;;
 	\*Portland\\ ],[;; @%:@(
 	*NAG\\ Fortran\\ Compiler*)
 	  _LT_TAGVAR(lt_prog_compiler_wl, $][1)='-Wl,-Wl,,'
 	  _LT_TAGVAR(lt_prog_compiler_pic, $][1)='-PIC'
 	  _LT_TAGVAR(lt_prog_compiler_static, $][1)='-Bstatic'
 	  ;;
-	*PGI\\ Compilers\\ and\\ Tools*|*Port][land\\ ]))dnl
-      m4_pushdef([_LT_LINKER_SHLIBS],m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(
-          m4_bpatsubst(m4_dquote(m4_defn(
+	*PGI\\ Compilers\\ and\\ Tools*|*NVIDIA\\ Compilers\\ and\\ Tools*|*Port][land\\ ])),[sed 5q`],[sed -n 1,5p`]))dnl
+      m4_pushdef([_LT_LANG_CXX_CONFIG],m4_bpatsubst(m4_dquote(m4_defn([_LT_LANG_CXX_CONFIG])),
+        [sed 5q`],[sed -n 1,5p`]))dnl
+      m4_pushdef([_LT_LINKER_SHLIBS],m4_bpatsubst(m4_dquote(
+        m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(
+        m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(m4_defn(
           [_LT_LINKER_SHLIBS])),[tmp_sharedflag='-shared'],
           [tmp_sharedflag='-shared'
 	tmp_compiler_flags='$compiler_flags'])),
@@ -91,36 +96,72 @@ dnl newer PGI or NAG configurations particularly well
         [$CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs '"$tmp_compiler_flags"' $wl-soname])),
         [  tmp_sharedflag='-Wl,-shared'],
         [  tmp_sharedflag='-Wl,-shared'
-	  tmp_compiler_flags='`echo \$compiler_flags | sed -e '"'"'s/ -W@<:@cl@:>@,-no-pie\\b//g'"'"'`']))dnl
+	  tmp_compiler_flags='`echo \$compiler_flags | sed -e '"'"'s/ -W@<:@cl@:>@,-no-pie\\b//g'"'"'`'])),
+        [\*Sun\\ F\*\(.\)[ 	]*# Sun Fortran 8\.3
+[ 	]*tmp_sharedflag='-G' ;;
+],[\&	*NAG\\ Fortran\\ Compiler*\1
+	  tmp_sharedflag='-Wl,-shared'
+	  tmp_compiler_flags='`echo \$compiler_flags | sed -e '"'"'s/ -W@<:@cl@:>@,-no-pie\\b//g'"'"'`' ;;
+])),[sed 5q`],[sed -n 1,5p`]))dnl
       m4_pushdef([_LT_SYS_HIDDEN_LIBDEPS],[AS_UNSET([output_verbose_link_cmd])]
-    m4_bpatsubst(m4_dquote(
-        m4_defn([_LT_SYS_HIDDEN_LIBDEPS])),[test x-\([LR]\) = "\$p"],
-          [test x-\1 = x"$p"]))])dnl
+        m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(
+          m4_defn([_LT_SYS_HIDDEN_LIBDEPS])),[test x-\([LR]\) = "\$p"],
+            [test x-\1 = x"$p"])),
+          [test x-R = x"\$p"],[\& ||
+	   test x-l = x"$p"]))])dnl
+   m4_foreach([acx_ltconfig],[[[_LT_LANG_C_CONFIG]],[[_LT_LANG_F77_CONFIG]],[[_LT_LANG_FC_CONFIG]]],
+     [m4_pushdef(acx_ltconfig,m4_bpatsubst(m4_dquote(
+        m4_bpatsubst(m4_dquote(m4_defn(acx_ltconfig)),[_LT_TAG_COMPILER
+],
+       [\&  _LT_TAGDECL([with_nagfor], [acx_is_nagfor], [0], [Is the compiler the NAG Fortran compiler?])])),
+       [_LT_CONFIG],
+       [AC_MSG_CHECKING([whether this is the NAG Fortran compiler])
+    $CC -V 2>&1 | grep '^NAG Fortran Compiler Release' >/dev/null 2>&1
+    _lt_result=$?
+    AS_IF([test $_lt_result -eq 0],[_lt_result=yes],[_lt_result=no])
+    AC_MSG_RESULT([$_lt_result])
+    _LT_TAGVAR([acx_is_nagfor], $][1)=$_lt_result
+    \&]))])dnl
    LT_INIT([$1])
+   m4_popdef([_LT_LANG_F77_CONFIG])dnl
+   m4_popdef([_LT_LANG_FC_CONFIG])dnl
+   m4_popdef([_LT_LANG_C_CONFIG])dnl
    m4_if(m4_cmp(m4_version_compare(LT_PACKAGE_VERSION,[2.4.6]),1),-1,
      [m4_popdef([_LT_COMPILER_PIC])m4_popdef([_LT_LINKER_SHLIBS])dnl
       m4_popdef([_LT_SYS_HIDDEN_LIBDEPS])])dnl
    dnl _KPSE_USE_LIBTOOL ensures libtool is also used for configure-time tests,
    dnl which deduces dependent libraries automatically
    _KPSE_USE_LIBTOOL
+dnl substitute -shared-intel if present
+   AS_FOR([acx_flag_var],[acx_flag_var_],[CFLAGS CXXFLAGS FCFLAGS F77FLAGS LDFLAGS FCLDFLAGS],
+     [AS_IF([eval test x\$\{acx_flag_var+set\} = xset],
+        [eval acx_temp="\" \$$acx_flag_var_ \""
+         AS_CASE([$acx_temp],[*\ -shared-intel\ *|*\ -static-intel\ *],
+           [acx_temp=`echo "$acx_temp" | sed -e 's/ \(-\(shared\|static\)-intel\)\b/ -Xcompiler \1 -XCClinker \1/'`])
+dnl take care of ifort/icc/icpc two-part options
+         eval acx_flag_var=\"`echo "$acx_temp" | sed -e 's/ -\(align\|allow\|assume\|ccdefault\|check\|convert\|debug\|debug-parameters\|diag-type\|diag-enable\|diag-disable\|double-size\|dynamic-linker\|dyncom\|export-dir\|extend-source\|fp-model\|fpscomp\|gen-interfaces\|heap-arrays\|imacros\|integer-size\|iprefix\|iquote\|iwithprefixbefore\|module\|names\|opt-report\|opt-streaming-stores\|pch-dir\|pch-use\|prof-dir\|prof-file\|real-size\|reentrancy\|stand\|tcollect-filter\|tune\|warn\|watch\) \(@<:@^-@:>@@<:@^ @:>@*\)\b/ -Xcompiler -\1 -Xcompiler \2/g' -e 's/^ //;s/ $//'`\"])])
+   AC_PROVIDE_IFELSE([AC_PROG_CC],
+     [AS_IF([test -n "$CC" -a X"$CC" != Xno],
+        [AC_LANG_PUSH([C])
+         _KPSE_CHECK_LIBTOOL
+         AC_LANG_POP([C])])])
    AC_PROVIDE_IFELSE([AC_PROG_FC],
      [AS_IF([test -n "$FC" -a X"$FC" != Xno],
         [AC_LANG_PUSH([Fortran])
          _ACX_LT_FORT_FLAGS_MANGLE
+         _KPSE_CHECK_LIBTOOL
          AC_LANG_POP([Fortran])])])
    AC_PROVIDE_IFELSE([AC_PROG_F77],
      [AS_IF([test -n "$F77" -a X"$F77" != Xno],
         [AC_LANG_PUSH([Fortran 77])
          _ACX_LT_FORT_FLAGS_MANGLE
+         _KPSE_CHECK_LIBTOOL
          AC_LANG_POP([Fortran 77])])])
-dnl substitute -shared-intel if present
-   AS_FOR([acx_flag_var],[acx_flag_var_],[CFLAGS CXXFLAGS FCFLAGS F77FLAGS LDFLAGS FCLDFLAGS],
-     [AS_IF([eval test x\$\{acx_flag_var+set\} = xset],
-        [eval acx_temp="\" \$$acx_flag_var_ \""
-         AS_CASE([$acx_temp],[*\ -shared-intel\ *|*\ -static-intel\ *],
-           [acx_temp=`echo "$acx_temp" | sed -e 's/ \(-\(shared\|static\)-intel\) / -Xcompiler \1 -XCClinker \1 /'`])
-dnl take care of ifort/icc/icpc two-part options
-         eval acx_flag_var=\"`echo "$acx_temp" | sed -r -e 's/ -(align|allow|assume|ccdefault|check|convert|debug|debug-parameters|diag-type|diag-enable|diag-disable|double-size|dynamic-linker|dyncom|export-dir|extend-source|fp-model|fpscomp|gen-interfaces|heap-arrays|imacros|integer-size|iprefix|iquote|iwithprefixbefore|module|names|opt-report|opt-streaming-stores|pch-dir|pch-use|prof-dir|prof-file|real-size|reentrancy|stand|tcollect-filter|tune|warn|watch) (@<:@^-@:>@@<:@^ @:>@*) / -Xcompiler -\1 -Xcompiler \2 /g' -e 's/^ //;s/ $//'`\"])])])dnl
+   AC_PROVIDE_IFELSE([AC_PROG_CXX],
+     [AS_IF([test -n "$CXX" -a X"$CXX" != Xno],
+        [AC_LANG_PUSH([C++])
+         _KPSE_CHECK_LIBTOOL
+         AC_LANG_POP([C++])])])])dnl
 dnl
 dnl Local Variables:
 dnl mode: autoconf
diff --git a/m4/kpse_libtool.m4 b/m4/kpse_libtool.m4
index da8b100b13474191d4dc61ea109878bbfea8288d..0cc3feaf6a0e874db4150ab94a0eda8440fa5d7d 100644
--- a/m4/kpse_libtool.m4
+++ b/m4/kpse_libtool.m4
@@ -41,6 +41,10 @@ AC_PROVIDE_IFELSE([AC_PROG_F77],
 ac_compile="$ac_pwd/libtool --mode=compile --tag=F77 $ac_compile"
 ])])[]dnl
 AC_LANG(_AC_LANG)[]dnl
+dnl After compiling/linking checks, now also $top_builddir/$objdir
+dnl needs to be cleaned.
+ac_clean_files_save="$ac_clean_files_save $ac_pwd/$objdir"
+ac_clean_files="$ac_clean_files $ac_pwd/$objdir"
 ]) # _KPSE_USE_LIBTOOL
 
 # _KPSE_CHECK_LIBTOOL([ACTION-IF-SUCCESS], [ACTION-IF-FAILURE = FAILURE])
diff --git a/patch/libtool.m4.arg_spaces.patch b/patch/libtool.m4.arg_spaces.patch
deleted file mode 100644
index fa8e0d811ded3a6bd4522e73ae6252f03e7c5ddd..0000000000000000000000000000000000000000
--- a/patch/libtool.m4.arg_spaces.patch
+++ /dev/null
@@ -1,17 +0,0 @@
---- a/m4/libtool.m4
-+++ b/m4/libtool.m4
-@@ -7538,10 +7538,11 @@ if AC_TRY_EVAL(ac_compile); then
-     case $prev$p in
- 
-     -L* | -R* | -l*)
--       # Some compilers place space between "-{L,R}" and the path.
-+       # Some compilers place space between "-{L,R,l}" and the path (value).
-        # Remove the space.
--       if test x-L = "$p" ||
--          test x-R = "$p"; then
-+       if test x-L = x"$p" ||
-+          test x-R = x"$p" ||
-+          test x-l = x"$p"; then
- 	 prev=$p
- 	 continue
-        fi
diff --git a/patch/libtool.m4.nag_wrapper.patch b/patch/libtool.m4.nag_wrapper.patch
deleted file mode 100644
index fcbb0753984fcab944728ca5e81bf3e85afdee7c..0000000000000000000000000000000000000000
--- a/patch/libtool.m4.nag_wrapper.patch
+++ /dev/null
@@ -1,27 +0,0 @@
---- a/config/ltmain.sh
-+++ b/config/ltmain.sh
-@@ -8881,7 +8881,8 @@ func_mode_link ()
- 	  xlcverstring="$wl-compatibility_version $wl$minor_current $wl-current_version $wl$minor_current.$revision"
- 	  verstring="-compatibility_version $minor_current -current_version $minor_current.$revision"
-           # On Darwin other compilers
--          case $CC in
-+          func_cc_basename $CC
-+          case $func_cc_basename_result in
-               nagfor*)
-                   verstring="$wl-compatibility_version $wl$minor_current $wl-current_version $wl$minor_current.$revision"
-                   ;;
---- a/m4/libtool.m4
-+++ b/m4/libtool.m4
-@@ -118,6 +118,12 @@ func_cc_basename ()
-       esac
-     done
-     func_cc_basename_result=`$ECHO "$cc_temp" | $SED "s%.*/%%; s%^$host_alias-%%"`
-+
-+    # Set result to 'nagfor-wrapper' when NAG compiler is called via a wrapper (e.g. mpif90).
-+    case $func_cc_basename_result in
-+      nagfor*) ;;
-+      *) $cc_temp -V 2>&1 | $GREP '^NAG Fortran Compiler Release' >/dev/null 2>&1 && func_cc_basename_result='nagfor-wrapper' ;;
-+    esac
- }
- ])# _LT_PREPARE_CC_BASENAME
-