Subject: bug#7621: add a way to pass link dependencies

From: Bruno Haible <[email-masked]>
Date: Mon, 13 Feb 2023 13:13:06 +0100

To answer my own question:

In 1) I wrote:
> Typical values of LTLIBICONV are
>
>     -liconv
>     -rpath /usr/local/lib -L/usr/local/lib -liconv
>     /usr/local/lib/libiconv.a
>     -rpath /usr/local/lib /usr/local/lib/libiconv.so
>     /usr/local/lib/libiconv.la

But that is incorrect. /usr/local/lib/libiconv.a and /usr/local/lib/libiconv.la
cannot be among the results, because the Gnulib 'havelib' macros would put
"-L/usr/local/lib -liconv" into the LTLIBICONV variable instead.
See lib-link.m4:
LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-L$found_dir -l$name"

So, the value of LTLIBICONV consists of libtool options *only*. No file names.

I confirmed this, through some tests:
When libiconv is installed with --disable-shared, then (regardless
whether libiconv.la is deleted after installation):
  LTLIBICONV = -LPREFIX/lib -liconv
  LIBICONV = PREFIX/lib/libiconv.a

Then I asked:
> Now in which Automake provided variable could I stuff these linker options
> or libtool options?

The answer, as given by Ralf Wildenhues in
https://lists.gnu.org/archive/html/bug-gnulib/2006-09/msg00146.html :

  "this case is only a problem when Libtool is not used:
   libtool will reorder the command line so that `-l' flags appear after
   all encountered objects."

That is, we need to distinguish the case when libtool is used from the case
where libtool is not used.

* When libtool is used, we need to use $LTLIBICONV, not $LIBICONV. As documented
  in https://www.gnu.org/software/automake/manual/html_node/Libtool-Flags.html,
  these go into the library_LDFLAGS variable.
  In the libtool invocation, these *_LDFLAGS occur before the *.lo and *.la files,
  but this is not a problem since, as Ralf wrote,
    "libtool will reorder the command line so that `-l' flags appear after
     all encountered objects."

* When libtool is not used, i.e. the target is library.a, we just ignore both
  $LIBICONV and $LTLIBICONV. The documentation is at
  https://www.gnu.org/software/automake/manual/html_node/A-Library.html.

  When linking a program that uses library.a, $LIBICONV or $LTLIBICONV will
  need to be used, depending on whether this linking happens without or with
  libtool.

There is no need to unify these two cases (creating a .la library vs.
creating a .a library). It's just two different cases, two different Automake
rules.