Subject: bug#50098: Configuring --with-libsigsegv results in link error

From: Bruno Haible <[email-masked]>
Date: Sun, 22 Aug 2021 01:37:00 +0200

Paul Eggert wrote:
> > Using grep 3.7 (as well as master), I'm seeing undefined symbol errors after pointing the build to use a local installation of libsigsegv (version 2.13).
> 
> Thanks for reporting that. It's a bug in Gnulib's c-stack module, which 
> I fixed in Gnulib by installing the attached.

Thanks. It was my mistake from 2021-05-18; sorry.

This patch refactors the macros a bit, so that the test for libsigsegv is
only performed once, not twice.

--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2021-08-21  Bruno Haible  <[email-masked]>
+
+	c-stack: Test for libsigsegv once, not twice.
+	* m4/sigsegv.m4: New file, based on modules/sigsegv.
+	* modules/sigsegv (Files): Add it.
+	(configure.ac): Require gl_SIGSEGV. Test gl_sigsegv_uses_libsigsegv
+	instead of with_libsigsegv and gl_cv_lib_sigsegv.
+	* m4/c-stack.m4 (gl_C_STACK): Likewise.
+
 2021-08-21  Bruno Haible  <[email-masked]>
 
 	rawmemchr: Fix use in relocatable-prog-wrapper (regression 2021-08-20).
@@ -23,7 +32,7 @@
 
 2021-08-17  Paul Eggert  <[email-masked]>
 
-	c-stack: fix libsigsegv dependency
+	c-stack: fix libsigsegv dependency (regression 2021-05-18)
 	Problem reported for GNU grep by Evan Miller (Bug#50098).
 	* m4/c-stack.m4 (gl_C_STACK): Call gl_LIBSIGSEGV before
 	testing gl_cv_lib_sigsegv.
diff --git a/m4/c-stack.m4 b/m4/c-stack.m4
index f3de54fad..caf1d2de7 100644
--- a/m4/c-stack.m4
+++ b/m4/c-stack.m4
@@ -7,16 +7,14 @@
 
 # Written by Paul Eggert.
 
-# serial 24
+# serial 25
 
 AC_DEFUN([gl_C_STACK],
 [
   dnl 'c-stack' needs -lsigsegv if and only if the 'sigsegv' module needs it.
-  if test "$with_libsigsegv" = yes; then
-    gl_LIBSIGSEGV
-    if test "$gl_cv_lib_sigsegv" = yes; then
-      AC_SUBST([LIBCSTACK], [$LIBSIGSEGV])
-      AC_SUBST([LTLIBCSTACK], [$LTLIBSIGSEGV])
-    fi
+  AC_REQUIRE([gl_SIGSEGV])
+  if test $gl_sigsegv_uses_libsigsegv = yes; then
+    AC_SUBST([LIBCSTACK], [$LIBSIGSEGV])
+    AC_SUBST([LTLIBCSTACK], [$LTLIBSIGSEGV])
   fi
 ])
diff --git a/m4/sigsegv.m4 b/m4/sigsegv.m4
new file mode 100644
index 000000000..d5d1fc4b1
--- /dev/null
+++ b/m4/sigsegv.m4
@@ -0,0 +1,20 @@
+# sigsegv.m4 serial 1
+dnl Copyright (C) 2021 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 From Bruno Haible.
+
+AC_DEFUN_ONCE([gl_SIGSEGV],
+[
+  AC_ARG_WITH([libsigsegv],
+    [AS_HELP_STRING([--with-libsigsegv],
+       [use the GNU libsigsegv library, when present, instead of the gnulib module 'sigsegv'])])
+  if test "$with_libsigsegv" = yes; then
+    gl_LIBSIGSEGV
+    gl_sigsegv_uses_libsigsegv="$gl_cv_lib_sigsegv"
+  else
+    gl_sigsegv_uses_libsigsegv=no
+  fi
+])
diff --git a/modules/sigsegv b/modules/sigsegv
index 2ef84332d..fdd815c37 100644
--- a/modules/sigsegv
+++ b/modules/sigsegv
@@ -20,6 +20,7 @@ m4/mmap-anon.m4
 m4/sigaltstack.m4
 m4/stack-direction.m4
 m4/libsigsegv.m4
+m4/sigsegv.m4
 
 Depends-on:
 havelib
@@ -28,15 +29,11 @@ stdint
 getpagesize
 
 configure.ac:
-AC_ARG_WITH([libsigsegv],
-  [AS_HELP_STRING([--with-libsigsegv],
-     [use the GNU libsigsegv library, when present, instead of the gnulib module 'sigsegv'])])
-SIGSEGV_H=sigsegv.h
-if test "$with_libsigsegv" = yes; then
-  gl_LIBSIGSEGV
-  if test "$gl_cv_lib_sigsegv" = yes; then
-    SIGSEGV_H=
-  fi
+AC_REQUIRE([gl_SIGSEGV])
+if test $gl_sigsegv_uses_libsigsegv = yes; then
+  SIGSEGV_H=
+else
+  SIGSEGV_H=sigsegv.h
 fi
 AC_SUBST([SIGSEGV_H])
 AM_CONDITIONAL([GL_GENERATE_SIGSEGV_H], [test -n "$SIGSEGV_H"])