[PATCH] PKG_CHECK_MODULES: Check if $pkg_cv_[]$1[]_LIBS works

H.J. Lu
2020-07-27T19:15:34Z

[-- Attachment #1: Type: text/plain, Size: 1414 bytes --]

On Mon, Jul 27, 2020 at 9:11 AM Aaron Merey <amerey@redhat.com> wrote:
>
> On Mon, Jul 27, 2020 at 11:32 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > On Sat, Jul 25, 2020 at 9:01 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> > > This caused:
> > >
> > > https://sourceware.org/bugzilla/show_bug.cgi?id=26301
> > >
> >
> > It is quite normal to have debuginfod headers without libdebuginfod on
> > multilib OSes. Restore AC_CHECK_LIB to check if libdebuginfod exists.
> > And always define HAVE_LIBDEBUGINFOD to 0 or 1 for
> >
> > binutils/dwarf.c:#if HAVE_LIBDEBUGINFOD
> > binutils/dwarf.c:#if HAVE_LIBDEBUGINFOD
> > binutils/dwarf.c:#if HAVE_LIBDEBUGINFOD
> > binutils/dwarf.h:#if HAVE_LIBDEBUGINFOD
> > binutils/objdump.c:#if HAVE_LIBDEBUGINFOD
> > binutils/objdump.c:#endif /* HAVE_LIBDEBUGINFOD */
> > binutils/readelf.c:#if HAVE_LIBDEBUGINFOD
> > binutils/readelf.c:#endif /* HAVE_LIBDEBUGINFOD */
> > gdb/top.c:#if HAVE_LIBDEBUGINFOD
> >
> > OK for master?
>
> Thanks for spotting this. Normally PKG_CHECH_MODULES would correctly
> detect whether the .so and header are installed and build accordingly,
> but when cross compiling the AC_CHECK_LIB may be needed.

I am not cross compiling. I am simply using "gcc -m32". The problem
is PKG_CHECK_MODULES which doesn't check if $pkg_cv_[]$1[]_LIBS
actually works. Here is the updated patch to fix PKG_CHECK_MODULES.
Any comments or objections?

--
H.J.

[-- Attachment #2: 0001-PKG_CHECK_MODULES-Check-if-pkg_cv_-1-_LIBS-works.patch --]
[-- Type: text/x-patch, Size: 8330 bytes --]

From 42d49b1444ad6c8475672f6a6a16810d9e7c15ef Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 27 Jul 2020 08:24:15 -0700
Subject: [PATCH] PKG_CHECK_MODULES: Check if $pkg_cv_[]$1[]_LIBS works

It is quite normal to have headers without library on multilib OSes.
Add AC_TRY_LINK to PKG_CHECK_MODULES to check if $pkg_cv_[]$1[]_LIBS
works. And always define HAVE_LIBDEBUGINFOD to 0 or 1 for

binutils/dwarf.c:#if HAVE_LIBDEBUGINFOD
binutils/dwarf.c:#if HAVE_LIBDEBUGINFOD
binutils/dwarf.c:#if HAVE_LIBDEBUGINFOD
binutils/dwarf.h:#if HAVE_LIBDEBUGINFOD
binutils/objdump.c:#if HAVE_LIBDEBUGINFOD
binutils/objdump.c:#endif /* HAVE_LIBDEBUGINFOD */
binutils/readelf.c:#if HAVE_LIBDEBUGINFOD
binutils/readelf.c:#endif /* HAVE_LIBDEBUGINFOD */
gdb/top.c:#if HAVE_LIBDEBUGINFOD

config/

PR binutils/26301
* debuginfod.m4 (AC_DEBUGINFOD): Always define HAVE_LIBDEBUGINFOD
to 0 or 1.
* pkg.m4 (PKG_CHECK_MODULES): Add AC_TRY_LINK to check if
$pkg_cv_[]$1[]_LIBS works.

binutils/

PR binutils/26301
* configure: Regenerated.

gdb/

PR binutils/26301
* configure: Regenerated.
---
binutils/configure | 38 +++++++++++++++++++++++++++++++++-----
config/debuginfod.m4 | 9 +++++++--
config/pkg.m4 | 6 ++++++
gdb/configure | 38 +++++++++++++++++++++++++++++++++-----
4 files changed, 79 insertions(+), 12 deletions(-)

diff --git a/binutils/configure b/binutils/configure
index c9fc5108e0..3f9ac88990 100755
--- a/binutils/configure
+++ b/binutils/configure
@@ -12398,6 +12398,7 @@ $as_echo_n "checking whether to use debuginfod... " >&6; }
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_debuginfod" >&5
$as_echo "$with_debuginfod" >&6; }

+have_libdebuginfod=0
if test "x$with_debuginfod" != xno; then

pkg_failed=no
@@ -12439,6 +12440,28 @@ fi
pkg_failed=untried
fi

+pkg_save_LDFLAGS="$LDFLAGS"
+LDFLAGS="$LDFLAGS $pkg_cv_DEBUGINFOD_LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+int
+main ()
+{
+return 0;
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+ pkg_failed=no
+else
+ pkg_failed=yes
+fi
+rm -f core conftest.err conftest.$ac_objext \
+ conftest$ac_exeext conftest.$ac_ext
+LDFLAGS=$pkg_save_LDFLAGS
+

if test $pkg_failed = yes; then
@@ -12458,7 +12481,8 @@ fi
# Put the nasty error message in config.log where it belongs
echo "$DEBUGINFOD_PKG_ERRORS" >&5

- if test "x$with_debuginfod" = xyes; then
+ have_libdebuginfod=0
+ if test "x$with_debuginfod" = xyes; then
as_fn_error $? "\"--with-debuginfod was given, but libdebuginfod is missing or unusable.\"" "$LINENO" 5
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: libdebuginfod is missing or unusable; some features may be unavailable." >&5
@@ -12467,7 +12491,8 @@ $as_echo "$as_me: WARNING: libdebuginfod is missing or unusable; some features m
elif test $pkg_failed = untried; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
- if test "x$with_debuginfod" = xyes; then
+ have_libdebuginfod=0
+ if test "x$with_debuginfod" = xyes; then
as_fn_error $? "\"--with-debuginfod was given, but libdebuginfod is missing or unusable.\"" "$LINENO" 5
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: libdebuginfod is missing or unusable; some features may be unavailable." >&5
@@ -12478,15 +12503,18 @@ else
DEBUGINFOD_LIBS=$pkg_cv_DEBUGINFOD_LIBS
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; }
-
-$as_echo "#define HAVE_LIBDEBUGINFOD 1" >>confdefs.h
-
+ have_libdebuginfod=1
fi
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: debuginfod support disabled; some features may be unavailable." >&5
$as_echo "$as_me: WARNING: debuginfod support disabled; some features may be unavailable." >&2;}
fi

+cat >>confdefs.h <<_ACEOF
+#define HAVE_LIBDEBUGINFOD $have_libdebuginfod
+_ACEOF
+
+

cat >>confdefs.h <<_ACEOF
diff --git a/config/debuginfod.m4 b/config/debuginfod.m4
index f9eb0988e3..aec05104be 100644
--- a/config/debuginfod.m4
+++ b/config/debuginfod.m4
@@ -16,10 +16,12 @@ AC_ARG_WITH([debuginfod],
AC_MSG_CHECKING([whether to use debuginfod])
AC_MSG_RESULT([$with_debuginfod])

+have_libdebuginfod=0
if test "x$with_debuginfod" != xno; then
PKG_CHECK_MODULES([DEBUGINFOD], [libdebuginfod >= 0.179],
- [AC_DEFINE([HAVE_LIBDEBUGINFOD], [1], [Define to 1 if debuginfod is enabled.])],
- [if test "x$with_debuginfod" = xyes; then
+ [have_libdebuginfod=1],
+ [have_libdebuginfod=0
+ if test "x$with_debuginfod" = xyes; then
AC_MSG_ERROR(["--with-debuginfod was given, but libdebuginfod is missing or unusable."])
else
AC_MSG_WARN([libdebuginfod is missing or unusable; some features may be unavailable.])
@@ -27,4 +29,7 @@ if test "x$with_debuginfod" != xno; then
else
AC_MSG_WARN([debuginfod support disabled; some features may be unavailable.])
fi
+AC_DEFINE_UNQUOTED([HAVE_LIBDEBUGINFOD],
+ [$have_libdebuginfod],
+ [Define to 1 if debuginfod is enabled.])
])
diff --git a/config/pkg.m4 b/config/pkg.m4
index 13a8890178..45587e97c8 100644
--- a/config/pkg.m4
+++ b/config/pkg.m4
@@ -147,6 +147,12 @@ AC_MSG_CHECKING([for $2])
_PKG_CONFIG([$1][_CFLAGS], [cflags], [$2])
_PKG_CONFIG([$1][_LIBS], [libs], [$2])

+dnl Check whether $pkg_cv_[]$1[]_LIBS works.
+pkg_save_LDFLAGS="$LDFLAGS"
+LDFLAGS="$LDFLAGS $pkg_cv_[]$1[]_LIBS"
+AC_TRY_LINK([],[return 0;], [pkg_failed=no], [pkg_failed=yes])
+LDFLAGS=$pkg_save_LDFLAGS
+
m4_define([_PKG_TEXT], [Alternatively, you may set the environment variables $1[]_CFLAGS
and $1[]_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.])
diff --git a/gdb/configure b/gdb/configure
index adcfa49c63..747605aaa2 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -6996,6 +6996,7 @@ $as_echo_n "checking whether to use debuginfod... " >&6; }
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_debuginfod" >&5
$as_echo "$with_debuginfod" >&6; }

+have_libdebuginfod=0
if test "x$with_debuginfod" != xno; then

pkg_failed=no
@@ -7037,6 +7038,28 @@ fi
pkg_failed=untried
fi

+pkg_save_LDFLAGS="$LDFLAGS"
+LDFLAGS="$LDFLAGS $pkg_cv_DEBUGINFOD_LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+int
+main ()
+{
+return 0;
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+ pkg_failed=no
+else
+ pkg_failed=yes
+fi
+rm -f core conftest.err conftest.$ac_objext \
+ conftest$ac_exeext conftest.$ac_ext
+LDFLAGS=$pkg_save_LDFLAGS
+

if test $pkg_failed = yes; then
@@ -7056,7 +7079,8 @@ fi
# Put the nasty error message in config.log where it belongs
echo "$DEBUGINFOD_PKG_ERRORS" >&5

- if test "x$with_debuginfod" = xyes; then
+ have_libdebuginfod=0
+ if test "x$with_debuginfod" = xyes; then
as_fn_error $? "\"--with-debuginfod was given, but libdebuginfod is missing or unusable.\"" "$LINENO" 5
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: libdebuginfod is missing or unusable; some features may be unavailable." >&5
@@ -7065,7 +7089,8 @@ $as_echo "$as_me: WARNING: libdebuginfod is missing or unusable; some features m
elif test $pkg_failed = untried; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
- if test "x$with_debuginfod" = xyes; then
+ have_libdebuginfod=0
+ if test "x$with_debuginfod" = xyes; then
as_fn_error $? "\"--with-debuginfod was given, but libdebuginfod is missing or unusable.\"" "$LINENO" 5
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: libdebuginfod is missing or unusable; some features may be unavailable." >&5
@@ -7076,15 +7101,18 @@ else
DEBUGINFOD_LIBS=$pkg_cv_DEBUGINFOD_LIBS
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; }
-
-$as_echo "#define HAVE_LIBDEBUGINFOD 1" >>confdefs.h
-
+ have_libdebuginfod=1
fi
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: debuginfod support disabled; some features may be unavailable." >&5
$as_echo "$as_me: WARNING: debuginfod support disabled; some features may be unavailable." >&2;}
fi

+cat >>confdefs.h <<_ACEOF
+#define HAVE_LIBDEBUGINFOD $have_libdebuginfod
+_ACEOF
+
+

# Libunwind support for ia64.

--
2.26.2