From 81237c97cf23efc2f2a364bf5bbf4efd27709733 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Sun, 5 Apr 2026 02:25:55 +1200
Subject: [PATCH 1/4] libarchive: Add configure and meson options.

A follow-up patch will make use of it.

(Proof-of-concept)
---
 configure                  | 140 +++++++++++++++++++++++++++++++++++++
 configure.ac               |  13 ++++
 meson.build                |  16 +++++
 meson_options.txt          |   3 +
 src/Makefile.global.in     |   4 ++
 src/include/pg_config.h.in |   3 +
 6 files changed, 179 insertions(+)

diff --git a/configure b/configure
index fe22bc71d0c..6141cdb0256 100755
--- a/configure
+++ b/configure
@@ -718,6 +718,9 @@ LIBCURL_CPPFLAGS
 LIBCURL_LIBS
 LIBCURL_CFLAGS
 with_libcurl
+LIBARCHIVE_LIBS
+LIBARCHIVE_CFLAGS
+with_libarchive
 with_uuid
 LIBURING_LIBS
 LIBURING_CFLAGS
@@ -877,6 +880,7 @@ with_libedit_preferred
 with_liburing
 with_uuid
 with_ossp_uuid
+with_libarchive
 with_libcurl
 with_libnuma
 with_libxml
@@ -911,6 +915,8 @@ ICU_CFLAGS
 ICU_LIBS
 LIBURING_CFLAGS
 LIBURING_LIBS
+LIBARCHIVE_CFLAGS
+LIBARCHIVE_LIBS
 LIBCURL_CFLAGS
 LIBCURL_LIBS
 LIBNUMA_CFLAGS
@@ -1596,6 +1602,7 @@ Optional Packages:
   --with-liburing         build with io_uring support, for asynchronous I/O
   --with-uuid=LIB         build contrib/uuid-ossp using LIB (bsd,e2fs,ossp)
   --with-ossp-uuid        obsolete spelling of --with-uuid=ossp
+  --with-libarchive       build with libarchive support
   --with-libcurl          build with libcurl support
   --with-libnuma          build with libnuma support
   --with-libxml           build with XML support
@@ -1635,6 +1642,10 @@ Some influential environment variables:
               C compiler flags for LIBURING, overriding pkg-config
   LIBURING_LIBS
               linker flags for LIBURING, overriding pkg-config
+  LIBARCHIVE_CFLAGS
+              C compiler flags for LIBARCHIVE, overriding pkg-config
+  LIBARCHIVE_LIBS
+              linker flags for LIBARCHIVE, overriding pkg-config
   LIBCURL_CFLAGS
               C compiler flags for LIBCURL, overriding pkg-config
   LIBCURL_LIBS
@@ -8912,6 +8923,135 @@ fi
 
 
 
+#
+# libarchive
+#
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to build with libarchive support" >&5
+$as_echo_n "checking whether to build with libarchive support... " >&6; }
+
+
+
+# Check whether --with-libarchive was given.
+if test "${with_libarchive+set}" = set; then :
+  withval=$with_libarchive;
+  case $withval in
+    yes)
+
+$as_echo "#define USE_LIBARCHIVE 1" >>confdefs.h
+
+      ;;
+    no)
+      :
+      ;;
+    *)
+      as_fn_error $? "no argument expected for --with-libarchive option" "$LINENO" 5
+      ;;
+  esac
+
+else
+  with_libarchive=no
+
+fi
+
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_libarchive" >&5
+$as_echo "$with_libarchive" >&6; }
+
+if test "$with_libarchive" = yes ; then
+
+pkg_failed=no
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for libarchive" >&5
+$as_echo_n "checking for libarchive... " >&6; }
+
+if test -n "$LIBARCHIVE_CFLAGS"; then
+    pkg_cv_LIBARCHIVE_CFLAGS="$LIBARCHIVE_CFLAGS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libarchive\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libarchive") 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_LIBARCHIVE_CFLAGS=`$PKG_CONFIG --cflags "libarchive" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+if test -n "$LIBARCHIVE_LIBS"; then
+    pkg_cv_LIBARCHIVE_LIBS="$LIBARCHIVE_LIBS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libarchive\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libarchive") 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_LIBARCHIVE_LIBS=`$PKG_CONFIG --libs "libarchive" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+
+
+
+if test $pkg_failed = yes; then
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+
+if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
+        _pkg_short_errors_supported=yes
+else
+        _pkg_short_errors_supported=no
+fi
+        if test $_pkg_short_errors_supported = yes; then
+	        LIBARCHIVE_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "libarchive" 2>&1`
+        else
+	        LIBARCHIVE_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "libarchive" 2>&1`
+        fi
+	# Put the nasty error message in config.log where it belongs
+	echo "$LIBARCHIVE_PKG_ERRORS" >&5
+
+	as_fn_error $? "Package requirements (libarchive) were not met:
+
+$LIBARCHIVE_PKG_ERRORS
+
+Consider adjusting the PKG_CONFIG_PATH environment variable if you
+installed software in a non-standard prefix.
+
+Alternatively, you may set the environment variables LIBARCHIVE_CFLAGS
+and LIBARCHIVE_LIBS to avoid the need to call pkg-config.
+See the pkg-config man page for more details." "$LINENO" 5
+elif test $pkg_failed = untried; then
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+	{ { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
+$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
+as_fn_error $? "The pkg-config script could not be found or is too old.  Make sure it
+is in your PATH or set the PKG_CONFIG environment variable to the full
+path to pkg-config.
+
+Alternatively, you may set the environment variables LIBARCHIVE_CFLAGS
+and LIBARCHIVE_LIBS to avoid the need to call pkg-config.
+See the pkg-config man page for more details.
+
+To get pkg-config, see <http://pkg-config.freedesktop.org/>.
+See \`config.log' for more details" "$LINENO" 5; }
+else
+	LIBARCHIVE_CFLAGS=$pkg_cv_LIBARCHIVE_CFLAGS
+	LIBARCHIVE_LIBS=$pkg_cv_LIBARCHIVE_LIBS
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+
+fi
+fi
+
+
 #
 # libcurl
 #
diff --git a/configure.ac b/configure.ac
index 6873b7546dd..ad75e2200c7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1059,6 +1059,19 @@ fi
 AC_SUBST(with_uuid)
 
 
+#
+# libarchive
+#
+AC_MSG_CHECKING([whether to build with libarchive support])
+PGAC_ARG_BOOL(with, libarchive, no, [build with libarchive support],
+              [AC_DEFINE([USE_LIBARCHIVE], 1, [Define to 1 to build with libarchive support. (--with-libarchive)])])
+AC_MSG_RESULT([$with_libarchive])
+AC_SUBST(with_libarchive)
+if test "$with_libarchive" = yes ; then
+  PKG_CHECK_MODULES(LIBARCHIVE, libarchive)
+fi
+
+
 #
 # libcurl
 #
diff --git a/meson.build b/meson.build
index 6bc74c2ba79..cce5fc40d05 100644
--- a/meson.build
+++ b/meson.build
@@ -923,6 +923,22 @@ endif
 
 
 ###############################################################
+# Library: libarchive
+###############################################################
+
+libarchive_opt = get_option('libarchive')
+if not libarchive_opt.disabled()
+  libarchive = dependency('libarchive', required: libarchive_opt)
+else
+  libarchive = not_found_dep
+endif
+if libarchive.found()
+  cdata.set('USE_LIBARCHIVE', 1)
+endif
+
+
+
+i###############################################################
 # Library: LLVM
 ###############################################################
 
diff --git a/meson_options.txt b/meson_options.txt
index 6a793f3e479..671ffe127ff 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -100,6 +100,9 @@ option('icu', type: 'feature', value: 'auto',
 option('ldap', type: 'feature', value: 'auto',
   description: 'LDAP support')
 
+option('libarchive', type : 'feature', value: 'auto',
+  description: 'libarchive support')
+
 option('libcurl', type : 'feature', value: 'auto',
   description: 'libcurl support')
 
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index a7699b026bb..060165bd27d 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -195,6 +195,7 @@ with_systemd	= @with_systemd@
 with_gssapi	= @with_gssapi@
 with_krb_srvnam	= @with_krb_srvnam@
 with_ldap	= @with_ldap@
+with_libarchive	= @with_libarchive@
 with_libcurl	= @with_libcurl@
 with_libnuma	= @with_libnuma@
 with_liburing	= @with_liburing@
@@ -224,6 +225,9 @@ krb_srvtab = @krb_srvtab@
 ICU_CFLAGS		= @ICU_CFLAGS@
 ICU_LIBS		= @ICU_LIBS@
 
+LIBARCHIVE_CFLAGS	= @LIBARCHIVE_CFLAGS@
+LIBARCHIVE_LIBS		= @LIBARCHIVE_LIBS@
+
 LIBNUMA_CFLAGS		= @LIBNUMA_CFLAGS@
 LIBNUMA_LIBS		= @LIBNUMA_LIBS@
 
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index d8d61918aff..62c2cc93902 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -695,6 +695,9 @@
 /* Define to 1 to build with LDAP support. (--with-ldap) */
 #undef USE_LDAP
 
+/* Define to 1 to build with libarchive support. (--with-libarchive) */
+#undef USE_LIBARCHIVE
+
 /* Define to 1 to build with libcurl support. (--with-libcurl) */
 #undef USE_LIBCURL
 
-- 
2.53.0

