Subject: when linking a shared library on Linux, libtool 2.4.6 ignores libfoo.so arguments

From: Nicolas Boulenguez <[email-masked]>
Date: Tue, 5 Apr 2022 12:50:59 +0200

Hello.

When linking a shared library or a program against a (locally built, not
installed) foo library, it is recommended to prefer
  dir/libfoo.so
over
  -Ldir -lfoo
which only adds complexity and unwanted ambiguity (for example,
/usr/lib/libfoo.{a,so} may silently be selected if dir/libfoo.so is
unexpectedly missing).

Libtool recognizes such options when linking a program, but ignores
them when linking a shared library.

The attached reproducer
* builds with -Ldir -lfoo in order to ensure that the sources are
  correct
* demonstrates the link failure with a direct path
* builds with the attached patch applied and a direct path

Just in case it helps, the original bug report is here:
Bug-Debian: https://bugs.debian.org/960469

Thanks.
Description: allow dir/libfoo.so as argument when linking a shared library
 This already works for programs, and is actually recommended for
 uninstalled yet libraries instead of the more ambiguous -Ldir -lfoo.
Bug-Debian: https://bugs.debian.org/960469
Author: Nicolas Boulenguez <[email-masked]>

--- a/build-aux/ltmain.in
+++ b/build-aux/ltmain.in
@@ -5517,6 +5517,13 @@
 	continue
 	;;
 
+      *.so)
+	# An explicit path to a shared library.
+	func_append deplibs " $arg"
+	func_append old_deplibs " $arg"
+	continue
+	;;
+
       *.la)
 	# A libtool-controlled library.
 
@@ -5871,6 +5878,16 @@
 	  func_resolve_sysroot "$deplib"
 	  lib=$func_resolve_sysroot_result
 	  ;;
+	*.so)
+	  # FIXME: linkmode=prog copies .so arguments without this stanza. Duplicate code?
+	  if test lib = "$linkmode"; then
+	    deplibs="$deplib $deplibs"
+	    test lib = "$linkmode" && newdependency_libs="$deplib $newdependency_libs"
+	  elif test prog != "$linkmode"; then
+	    func_warning "shared library '$deplib' ignored for archive/object"
+	  fi
+	  continue
+	  ;;
 	*.$libext)
 	  if test conv = "$pass"; then
 	    deplibs="$deplib $deplibs"