[PATCH 1/2] ltmain.in: ensure old libraries are not archived

2021-04-06 Thread Alexandre Janniaux
Previously, adding a static archive library, aka. old library, as a
dependency of a new libtool library compiled as a static archive (be it
a convenience library or an installed static library) resulted in the
dependency archive being added into the target archive as-is.

However, linkers are usually not able to use nested archive as-is. It
results in errors like these on Linux and Darwin at least:

/usr/bin/ld: ./.libs/libfoo.a: member ./.libs/libfoo.a(libbar.a) in
archive is not an object

or

/usr/bin/nm: libbar.a: file format not recognized

With libfoo.a being the static libtool target and libbar.a being the
nested/linked static library.

Libtool has a quite close behaviour with convenience libraries, where
the content (objects) of the convenience library are extracted and added
back into the new target, but that´s mostly a libtool creation.

The static dependencies should not be "linked" into other static
libraries as there should not be linking involved, and we don´t want to
potentially duplicate symbols like done for the convenience libraries.

Instead, keep them in the dependency_libs of the new libtool archive
created and forward them until we link a shared library or an
executable.
---
 build-aux/ltmain.in | 11 ---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/build-aux/ltmain.in b/build-aux/ltmain.in
index 96b37003..d172deae 100644
--- a/build-aux/ltmain.in
+++ b/build-aux/ltmain.in
@@ -5508,7 +5508,6 @@ func_mode_link ()
   *.$libext)
# An archive.
func_append deplibs " $arg"
-   func_append old_deplibs " $arg"
continue
;;
 
@@ -5867,6 +5866,8 @@ func_mode_link ()
  if test conv = "$pass"; then
deplibs="$deplib $deplibs"
continue
+ else
+   func_append old_deplibs " $deplib"
  fi
  case $linkmode in
  lib)
@@ -6817,7 +6818,6 @@ func_mode_link ()
   # Now set the variables for building old libraries.
   build_libtool_libs=no
   oldlibs=$output
-  func_append objs "$old_deplibs"
   ;;
 
 lib)
@@ -8697,7 +8697,11 @@ EOF
  build_libtool_libs=no
   ;;
*)
- oldobjs="$old_deplibs $non_pic_objects"
+ oldobjs="$non_pic_objects"
+ # This is not correct to add old_deplibs creating an archive
+ # so append them only when creating an executable or a shared
+ # library.
+ test yes != "$build_old_libs" && oldobjs="$oldobjs $old_deplibs"
  $preload && test -f "$symfileobj" \
&& func_append oldobjs " $symfileobj"
  addlibs=$old_convenience
@@ -8958,6 +8962,7 @@ EOF
done
dlprefiles=$newdlprefiles
  fi
+ func_append dependency_libs " $old_deplibs"
  $RM $output
  # place dlname in correct position for cygwin
  # In fact, it would be nice if we could use this code for all target
-- 
2.31.1




[PATCH 2/2] ltmain.in: defer warn dependency to static library

2021-04-06 Thread Alexandre Janniaux
When linking old library to any libtool target that is not an
executable, libtool warns that it might not be portable, in particular
because shared libraries need PIC from the static library to be working.

However, when adding an old library as dependency to a static libtool
library, there is no linkage, and thus no portability issue, so there's
no reason to warn. If the static libtool library is then linked to a
shared library, the dependency will be forwarded and the warning will
happen at this time.
---
 build-aux/ltmain.in | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/build-aux/ltmain.in b/build-aux/ltmain.in
index d172deae..ecd9a5a0 100644
--- a/build-aux/ltmain.in
+++ b/build-aux/ltmain.in
@@ -5891,9 +5891,11 @@ func_mode_link ()
;;
  esac
  if $valid_a_lib; then
-   echo
-   $ECHO "*** Warning: Linking the shared library $output against 
the"
-   $ECHO "*** static library $deplib is not portable!"
+   if test yes != "$build_old_libs"; then
+   echo
+   $ECHO "*** Warning: Linking the shared library $output 
against the"
+   $ECHO "*** static library $deplib is not portable!"
+   fi
deplibs="$deplib $deplibs"
  else
echo
-- 
2.31.1