bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org
Cc: Paul Eggert <eggert@cs.ucla.edu>
Subject: libgmp: link to the correct shared library
Date: Sun, 12 Jul 2020 23:48:47 +0200	[thread overview]
Message-ID: <8597850.UMjU5CPAqZ@omega> (raw)
In-Reply-To: <1798517.4igLZ8d9xF@omega>

> 	(main): Verify that gmp.h and libgmp versions match.

This test fails when I build this gnulib module in my usual way:
  ./configure --prefix=/PREFIX64 \
              CPPFLAGS="-I/PREFIX64/include -Wall" \
              LDFLAGS="-L/PREFIX64/lib"

It reports:
  gmp header version (6.2.0) does not match gmp library version (6.1.0).

The reason is that I have installed a GMP 6.2.0 in --prefix=/PREFIX64,
but since no -rpath options are passed to the linker when creating the
test program, the test program is linked against the GMP 6.1.2 found
in /usr:

$ ldd test-libgmp
        linux-vdso.so.1 =>  (0x00007ffd529f4000)
        libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007f13fd097000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f13fcccd000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f13fd317000)

We don't want to prevent users from installing their own copies of system
libraries. This is
  - one of the principal user freedoms,
  - needed for hackers to use the newest versions of system libraries.

And, as we all know, LD_LIBRARY_PATH is not the real solution.

This patch fixes the issue.


2020-07-12  Bruno Haible  <bruno@clisp.org>

	libgmp: Link to the correct shared library.
	* m4/libgmp.m4 (gl_LIBGMP): Invoke AC_LIB_HAVE_LINKFLAGS.
	* modules/libgmp (Depends-on): Add havelib.
	(Link): Mention $(LIBGMP) and $(LTLIBGMP).
	* modules/libgmp-tests (Makefile.am): Link test-libgmp with $(LIBGMP).

diff --git a/m4/libgmp.m4 b/m4/libgmp.m4
index b569bb7..7a8742e 100644
--- a/m4/libgmp.m4
+++ b/m4/libgmp.m4
@@ -1,44 +1,60 @@
+# libgmp.m4 serial 2
 # Configure the GMP library or a replacement.
-
 dnl Copyright 2020 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
+dnl gl_LIBGMP
+dnl Searches for an installed libgmp.
+dnl If found, it sets and AC_SUBSTs HAVE_LIBGMP=yes and the LIBGMP and LTLIBGMP
+dnl variables, and augments the CPPFLAGS variable, and #defines HAVE_LIBGMP
+dnl and HAVE_GMP to 1.
+dnl Otherwise, it sets and AC_SUBSTs HAVE_LIBGMP=no and LIBGMP and LTLIBGMP to
+dnl empty.
+
 AC_DEFUN([gl_LIBGMP],
 [
   AC_ARG_WITH([libgmp],
     [AS_HELP_STRING([--without-libgmp],
        [do not use the GNU Multiple Precision (GMP) library;
         this is the default on systems lacking libgmp.])])
-
-  AC_CHECK_HEADERS_ONCE([gmp.h])
-  GMP_H=gmp.h
-  LIB_GMP=
-
-  case $with_libgmp in
-    no) ;;
-    yes) GMP_H= LIB_GMP=-lgmp;;
-    *) if test "$ac_cv_header_gmp_h" = yes; then
-         gl_saved_LIBS=$LIBS
-         AC_SEARCH_LIBS([__gmpz_roinit_n], [gmp])
-         LIBS=$gl_saved_LIBS
-         case $ac_cv_search___gmpz_roinit_n in
-           'none needed')
-             GMP_H=;;
-           -*)
-             GMP_H= LIB_GMP=$ac_cv_search___gmpz_roinit_n;;
-         esac
-       fi;;
+  case "$with_libgmp" in
+    no)
+      HAVE_LIBGMP=no
+      LIBGMP=
+      LTLIBGMP=
+      ;;
+    *)
+      AC_LIB_HAVE_LINKFLAGS([gmp], [],
+        [#include <gmp.h>],
+        [static const mp_limb_t x[2] = { 0x73, 0x55 };
+         mpz_t tmp;
+         mpz_roinit_n (tmp, x, 2);
+        ],
+        [no])
+      if test $HAVE_LIBGMP = no; then
+        case "$with_libgmp" in
+          yes)
+            AC_MSG_ERROR([GMP not found, although --with-libgmp was specified. Try specifying --with-libgmp-prefix=DIR.])
+            ;;
+        esac
+      fi
+      ;;
   esac
-
-  if test -z "$GMP_H"; then
-    AC_DEFINE([HAVE_GMP], 1,
+  if test $HAVE_LIBGMP = yes; then
+    GMP_H=
+    dnl This is redundant, as HAVE_LIBGMP is also defined to 1.
+    AC_DEFINE([HAVE_GMP], [1],
       [Define to 1 if you have the GMP library instead of just the
        mini-gmp replacement.])
+  else
+    GMP_H=gmp.h
   fi
-
-  AC_SUBST([LIB_GMP])
   AC_SUBST([GMP_H])
   AM_CONDITIONAL([GL_GENERATE_GMP_H], [test -n "$GMP_H"])
+
+  dnl For backward compatibility.
+  LIB_GMP="$LIBGMP"
+  AC_SUBST([LIB_GMP])
 ])
diff --git a/modules/libgmp b/modules/libgmp
index b686717..70f20cf 100644
--- a/modules/libgmp
+++ b/modules/libgmp
@@ -8,6 +8,7 @@ lib/mini-gmp.h
 m4/libgmp.m4
 
 Depends-on:
+havelib
 
 configure.ac:
 gl_LIBGMP
@@ -33,7 +34,7 @@ Include:
 <gmp.h>
 
 Link:
-$(LIB_GMP)
+$(LTLIBGMP) when linking with libtool, $(LIBGMP) otherwise
 
 License:
 LGPLv3+ or GPLv2+
diff --git a/modules/libgmp-tests b/modules/libgmp-tests
index c01b876..94faf1c 100644
--- a/modules/libgmp-tests
+++ b/modules/libgmp-tests
@@ -10,4 +10,4 @@ configure.ac:
 Makefile.am:
 TESTS += test-libgmp
 check_PROGRAMS += test-libgmp
-test_libgmp_LDADD = $(LDADD) @LIB_GMP@
+test_libgmp_LDADD = $(LDADD) @LIBGMP@



  reply	other threads:[~2020-07-12 21:49 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-07  2:03 [PATCH] libgmp: new module Paul Eggert
2020-07-12 20:30 ` Bruno Haible
2020-07-12 20:57 ` libgmp tests: Add some safety checks Bruno Haible
2020-07-12 21:48   ` Bruno Haible [this message]
2020-07-23 22:13     ` libgmp: link to the correct shared library Paul Eggert
2020-07-23 22:30       ` Bruno Haible
2020-07-23 23:23         ` Paul Eggert
2020-07-24  0:00           ` Bruno Haible
2020-07-26 17:23             ` Paul Eggert
2020-07-26 17:54             ` Paul Eggert
2020-07-12 21:57 ` libgmp: Avoid warning when --without-libgmp is used Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8597850.UMjU5CPAqZ@omega \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).