Subject: Re: bug#9728: Incorrect -L flags order for HP-UX

From: Peter O'Gorman <[email-masked]>
Date: Thu, 13 Oct 2011 09:06:22 -0500

On 10/13/2011 05:32 AM, Vincent Lefevre wrote:
> On 2011-10-12 21:52:36 -0500, Peter O'Gorman wrote:
>> If the flags were added by the user, they could set LIBS=-L/opt/gnu/lib
>> instead on some platfroms, but that won't work on HP-UX either because its
>> linker is rather strict that -L flags for paths that contain the library be
>> ordered before the -l flag for that library.
>>
>> Anyway, the problem can be avoided by not putting the -L and -l flags for
>> gmp in LDFLAGS and LIBS, instead put both in e.g. GMP_LIBS,
>> (GMP_LIBS="-L/opt/gnu/lib -lgmp"), then add $(GMP_LIBS) to LDADD.
>
> This doesn't work with autoconf's AC_CHECK_LIB and other tests.
> And using LDFLAGS for autoconf and LDADD in the Makefile would
> be quite hard to maintain due to the risk of inconsistencies.
>
> autoconf and libtool should agree on the way to add -L flags.
>

Hi Vincent,

Well, if this is a bug at all, it is an automake/autoconf one, libtool 
does not set the order of flags in Makefile.in, but I don't believe that 
it is a bug.

LDFLAGS, CFLAGS, etc are for the user to set. It should be possible for 
the user to build a package doing ./configure ..options..; make 
LDFLAGS=-fancyflag CPPFLAGS=-I/want/coffee;

http://www.gnu.org/prep/standards/standards.html#Command-Variables

The usual way to get specific flags set to allow this is to save 
LDFLAGS, CPPFLAGS etc. before doing an autoconf test, do the test, then 
restore the original flags.

Something like the attached patch should work for mpfr.

Peter
--- ./tests/Makefile.am.orig	2011-10-13 12:38:25.706848813 +0000
+++ ./tests/Makefile.am	2011-10-13 12:49:01.617145045 +0000
@@ -42,8 +42,8 @@
 
 EXTRA_DIST = tgeneric.c tgeneric_ui.c mpf_compat.h inp_str.data tmul.dat
 
-LDADD = libfrtests.la $(MPFR_LIBM) $(top_builddir)/src/libmpfr.la
-INCLUDES = -I$(top_srcdir)/src -I$(top_builddir)/src
+LDADD = libfrtests.la $(MPFR_LIBM) $(top_builddir)/src/libmpfr.la $(GMP_LIBS)
+AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_builddir)/src $(GMP_CPPFLAGS)
 
 # LOADLIBES (documented in the "GNU make" manual and equivalent to LDLIBS)
 # enables to compile a program foo.c in the test directory by simply doing
@@ -54,7 +54,7 @@
 # Moreover, dependencies are not tracked. Thus you may want to run
 # "make tversion" (for instance) just before, to make sure that every
 # dependency has been rebuilt.
-LOADLIBES=$(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $(top_builddir)/tests/.libs/libfrtests.a $(top_builddir)/src/.libs/libmpfr.a $(LIBS) $(MPFR_LIBM)
+LOADLIBES=$(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $(top_builddir)/tests/.libs/libfrtests.a $(top_builddir)/src/.libs/libmpfr.a $(LIBS) $(MPFR_LIBM) $(GMP_LIBS)
 
 check_LTLIBRARIES = libfrtests.la
 libfrtests_la_SOURCES = mpfr-test.h memory.c rnd_mode.c tests.c cmp_str.c random2.c
--- ./src/Makefile.am.orig	2011-10-13 12:38:25.747671563 +0000
+++ ./src/Makefile.am	2011-10-13 12:39:36.024692542 +0000
@@ -8,6 +8,7 @@
 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 # PARTICULAR PURPOSE.
 
+AM_CPPFLAGS = $(GMP_CPPFLAGS)
 
 EXTRA_DIST = round_raw_generic.c jyn_asympt.c x86/core2/mparam.h        \
 	x86/mparam.h x86_64/core2/mparam.h x86_64/pentium4/mparam.h  	\
@@ -57,7 +58,7 @@
 scale2.c set_z_exp.c ai.c gammaonethird.c ieee_floats.h			\
 grandom.c
 
-libmpfr_la_LIBADD = @LIBOBJS@
+libmpfr_la_LIBADD = @LIBOBJS@ $(GMP_LIBS)
 
 # Libtool -version-info CURRENT[:REVISION[:AGE]] for libmpfr.la
 #
--- ./tune/Makefile.am.orig	2011-10-13 12:38:25.768653665 +0000
+++ ./tune/Makefile.am	2011-10-13 12:49:41.404012941 +0000
@@ -8,19 +8,19 @@
 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 # PARTICULAR PURPOSE.
 
-
+AM_CPPFLAGS = $(GMP_CPPFLAGS)
 EXTRA_PROGRAMS = tuneup speed bidimensional_sample
 
 tuneup_SOURCES = tuneup.c
-tuneup_LDADD = -lspeed $(top_builddir)/src/libmpfr.la
+tuneup_LDADD = -lspeed $(top_builddir)/src/libmpfr.la $(GMP_LIBS)
 tuneup_LDFLAGS = -static
 
 speed_SOURCES = speed.c
-speed_LDADD = -lspeed $(top_builddir)/src/libmpfr.la
+speed_LDADD = -lspeed $(top_builddir)/src/libmpfr.la $(GMP_LIBS)
 speed_LDFLAGS = -static
 
 bidimensional_sample_SOURCES = bidimensional_sample.c
-bidimensional_sample_LDADD = -lspeed $(top_builddir)/src/libmpfr.la
+bidimensional_sample_LDADD = -lspeed $(top_builddir)/src/libmpfr.la $(GMP_LIBS)
 bidimensional_sample_LDFLAGS = -static
 
 INCLUDES = -I$(top_srcdir)/src -I$(top_builddir)/src
--- ./configure.ac.orig	2011-10-13 12:27:30.375020247 +0000
+++ ./configure.ac	2011-10-13 14:03:45.688749700 +0000
@@ -50,17 +50,17 @@
 unset gmp_lib_path GMP_CFLAGS GMP_CC
 AC_ARG_WITH(gmp_include,
    [  --with-gmp-include=DIR  GMP include directory ],
-   CPPFLAGS="$CPPFLAGS -I$withval")
+   [GMP_CPPFLAGS="-I$withval"])
 AC_ARG_WITH(gmp_lib,
    [  --with-gmp-lib=DIR      GMP lib directory ], [
-   LDFLAGS="$LDFLAGS -L$withval"
+   GMP_LDFLAGS="-L$withval"
    gmp_lib_path="$withval"
   ])
 AC_ARG_WITH(gmp,
    [  --with-gmp=DIR          GMP install directory ], [
    if test -z "$with_gmp_lib" && test -z "$with_gmp_include" ; then
-      CPPFLAGS="$CPPFLAGS -I$withval/include"
-      LDFLAGS="$LDFLAGS -L$withval/lib"
+      GMP_CPPFLAGS="-I$withval/include"
+      GMP_LDFLAGS="-L$withval/lib"
       gmp_lib_path="$withval/lib"
    else
       AC_MSG_FAILURE([Do not use --with-gmp and --with-gmp-include/--with-gmp-lib options simultaneously.])
@@ -71,8 +71,8 @@
    [  --with-gmp-build=DIR    GMP build directory (please read INSTALL file)],
    [
    if test -z "$gmp_lib_path" && test -z "$with_gmp_include" ; then
-      CPPFLAGS="$CPPFLAGS -I$withval -I$withval/tune"
-      LDFLAGS="$LDFLAGS -L$withval -L$withval/.libs -L$withval/tune"
+      GMP_CPPFLAGS="-I$withval -I$withval/tune"
+      GMP_LDFLAGS="-L$withval -L$withval/.libs -L$withval/tune"
       gmp_lib_path="$withval$PATH_SEPARATOR$withval/.libs$PATH_SEPARATOR$withval/tune"
       if test -r $withval/Makefile ; then
          GMP_CFLAGS=`$SED -n 's/^CFLAGS = //p' $withval/Makefile`
@@ -171,6 +171,10 @@
 dnl options); if the compiler changes due to __GMP_CC in gmp.h, one
 dnl would have incorrect settings.
 dnl FIXME: Move this in aclocal ?
+mpfr_save_LDFLAGS="$LDFLAGS"
+mpfr_save_CPPFLAGS="$CPPFLAGS"
+LDFLAGS="$GMP_LDFLAGS $LDFLAGS"
+CPPFLAGS="$GMP_CPPFLAGS $CPPFLAGS"
 if test -z "$GMP_CC$GMP_CFLAGS" ; then
    AC_MSG_CHECKING(for CC and CFLAGS in gmp.h)
    GMP_CC=__GMP_CC
@@ -448,11 +452,13 @@
 dnl is libgmp-3 (due to libtool versionning). The best solution
 dnl is to believe it works under MS-Windows.
 if test "$dont_link_with_gmp" = yes ; then
-LIBS="-lgmp $LIBS"
+GMP_LIBS="$GMP_LDFLAGS -lgmp"
 else
 
 dnl Check if we can link with GMP
-AC_CHECK_LIB(gmp, __gmpz_init, [LIBS="-lgmp $LIBS"],
+mpfr_save_LIBS="$LIBS"
+AC_CHECK_LIB(gmp, __gmpz_init, [GMP_LIBS="$GMP_LDFLAGS -lgmp"
+LIBS="-lgmp $LIBS"],
  [AC_MSG_ERROR(libgmp not found or uses a different ABI (including static vs shared).
 Please read the INSTALL file -- see "In case of problem".)])
 
@@ -511,7 +517,12 @@
 AC_CHECK_FUNCS([__gmpn_rootrem __gmpn_sbpi1_divappr_q])
 
 dnl End of tests which need to link with GMP.
+LIBS="$mpfr_save_LIBS"
 fi
+LDFLAGS="$mpfr_save_LDFLAGS"
+CPPFLAGS="$mpfr_save_CPPFLAGS"
+AC_SUBST([GMP_CPPFLAGS])
+AC_SUBST([GMP_LIBS])
 
 dnl Remove also many MACROS (AC_DEFINE) which are unused by MPFR
 dnl and polluate (and slow down because libtool has to parse them) the build.