Subject: Re: bug#75667: Emacs 30.0.93 build failure if -lz but not <zlib.h>
From: Paul Eggert <[email-masked]>
To: Eli Zaretskii <[email-masked]>
Cc: [email-masked]
Date: Sun, 19 Jan 2025 16:33:52 -0800
List-Id: <debbugs-submit.debbugs.gnu.org>

On 2025-01-19 01:24, Eli Zaretskii wrote:

> Where is zlib.h on that platform?

It isn't installed, as far as I can see. I expected it's in some 
optional package that is absent on that build platform.

Although cfarm119 does have a file /opt/freeware/include/zlib.h, that 
corresponds to /opt/freeware/lib/libz.a, not to what -lz picks up which 
is /usr/lib/libz.a (a symlink to /usr/opt/rpm/lib/libz.a). I wouldn't 
recommend mixing up versions like that.

> It's too late to make serious changes in the configury on the release
> branch, but maybe we should simply disable HAVE_ZLIB_H on AIX?  Then
> we could find a proper solution on master.

Not sure that would be a good idea as this isn't an AIX-specific 
problem. Anybody can mess up .h and -l in this way, on any OS.

On the same platform I discovered a similar problem with -lsqlite3. I 
fixed both problems on master by installing the attached, and I'm 
marking the bug as done.

For the release branch I suppose we can just say something like "just 
use --without-zlib if you have problems".

As I'm sure you know, this library configury is waaay too complex and 
idiosyncratic. Why don't libz and libsqlite3 use pkg-config, for 
example? But I'm inclined to let the relevant library experts deal with 
that whenever they have time.
From a94988c01d45e4e494789d8715eed7ff7b383341 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sun, 19 Jan 2025 11:29:59 -0800
Subject: [PATCH 1/2] Port better to AIX 7.3 with -lz but not zlib.h

* configure.ac (HAVE_ZLIB, LIBZ):
Define only if zlib.h can be included (Bug#75667).
---
 configure.ac | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/configure.ac b/configure.ac
index debdaadfc7b..1d8c281ee21 100644
--- a/configure.ac
+++ b/configure.ac
@@ -5036,22 +5036,29 @@ AC_DEFUN
 
 HAVE_ZLIB=no
 LIBZ=
-if test "${with_zlib}" != "no"; then
-  OLIBS=$LIBS
-  AC_SEARCH_LIBS([inflateEnd], [z], [HAVE_ZLIB=yes])
-  LIBS=$OLIBS
-  case $ac_cv_search_inflateEnd in
-    -*) LIBZ=$ac_cv_search_inflateEnd ;;
-  esac
-fi
-if test "${HAVE_ZLIB}" = "yes"; then
-  AC_DEFINE([HAVE_ZLIB], [1],
-    [Define to 1 if you have the zlib library (-lz).])
-  ### mingw32 doesn't use -lz, since it loads the library dynamically.
-  if test "${opsys}" = "mingw32"; then
-     LIBZ=
-  fi
-fi
+AS_IF([test "${with_zlib}" != "no"],
+  [AC_CACHE_CHECK([for library containing inflateEnd],
+     [emacs_cv_libz],
+     [AC_LANG_CONFTEST([AC_LANG_PROGRAM([[#include <zlib.h>
+					  z_stream stream;]],
+					[[return inflateEnd (&stream);]])])
+      OLIBS=$LIBS
+      for emacs_cv_libz in '' -lz no; do
+	test "$emacs_cv_libz" = no && break
+	LIBS="$emacs_cv_libz $OLIBS"
+	AC_LINK_IFELSE([], [break])
+      done
+      LIBS=$OLIBS])
+
+   AS_CASE([$emacs_cv_libz],
+     [-* | ''],
+       [# mingw32 doesn't use -lz, since it loads the library dynamically.
+	AS_CASE([$opsys],
+	  [mingw32], [LIBZ=],
+	  [LIBZ=$emacs_cv_libz])
+	HAVE_ZLIB=yes
+	AC_DEFINE([HAVE_ZLIB], [1],
+	  [Define to 1 if you have the zlib library (-lz).])])])
 AC_SUBST([LIBZ])
 
 ### Dynamic library support
-- 
2.45.2

From dcccb9256262f8e992b75602dd4e861371b97361 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sun, 19 Jan 2025 16:23:06 -0800
Subject: [PATCH 2/2] Port better to AIX 7.3 with -lsqlite3 but not sqlite3.h

* configure.ac (HAVE_SQLITE): Yes only if sqlite3.h can be
included (Bug#75667).
---
 configure.ac | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 1d8c281ee21..ba39074c83a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3716,9 +3716,18 @@ AC_DEFUN
        CFLAGS="$SAVE_CFLAGS"
      fi
    else
-     AC_CHECK_LIB([sqlite3], [sqlite3_open_v2],
-       [HAVE_SQLITE3=yes],
-       [HAVE_SQLITE3=no])
+     AC_CACHE_CHECK([for sqlite3],
+       [emacs_cv_have_sqlite3],
+       [OLIBS=$LIBS
+	LIBS="-lsqlite3 $LIBS"
+	AC_LINK_IFELSE(
+	  [AC_LANG_PROGRAM([[#include <sqlite3.h>
+			     sqlite3 *handle;]],
+			   [[return sqlite3_open_v2 ("", &handle, 0, "");]])],
+	  [emacs_cv_have_sqlite3=yes],
+	  [emacs_cv_have_sqlite3=no])
+	LIBS=$OLIBS])
+     HAVE_SQLITE3=$emacs_cv_have_sqlite3
      if test "$HAVE_SQLITE3" = "yes"; then
        SQLITE3_LIBS=-lsqlite3
        LIBS="$SQLITE3_LIBS $LIBS"
-- 
2.45.2