dnl AUTOCONF configuration for Berkeley-DB
dnl Copyright (C) 2003-2012 Sam Steingold <sds@gnu.org>
dnl Copyright (C) 2024 Bruno Haible <bruno@clisp.org>
dnl GNU GPL2+

AC_PREREQ([2.64])
AC_INIT(Berkeley-DB, 1.0, clisp-list)
AC_CONFIG_SRCDIR(dbi.lisp)
AC_CONFIG_HEADERS(config.h)

RSE_BOLD
BOLD_MSG([Berkeley-DB (Common)])
CL_MODULE_COMMON_CHECKS()

dnl Search for libdb and define LIBDB, LTLIBDB and INCDB.
AC_LIB_LINKFLAGS([db])
AC_SYS_LARGEFILE    dnl ensure 64 bit size_t for AC_CHECK_SIZEOF below

BOLD_MSG([Berkeley-DB (Headers)])
AC_CHECK_HEADERS(db.h)
if test "$ac_cv_header_db_h" = "no"; then
  AC_MSG_ERROR([cannot find Berkeley-DB headers])
fi

BOLD_MSG([Berkeley-DB (Functions)])
AC_LIB_APPENDTOVAR([LIBS], [$LIBDB])
AC_SEARCH_LIBS(db_env_create, db)
if test "$ac_cv_search_db_env_create" = "no"; then
  AC_MSG_ERROR([cannot find Berkeley-DB library])
fi

AC_CHECK_MEMBERS([DB_ENV.get_home, DB_ENV.set_msgcall],,,[#include <db.h>])
if test "$ac_cv_member_DB_ENV_get_home" = "no"; then
  AC_MSG_ERROR([Berkeley-DB version 4.2 or better is required])
fi
AC_CHECK_SIZEOF(db_recno_t,,[#include <stdio.h>
#include <db.h>])

dnl See db-5.3.28/docs/upgrading/upgrade_4_3_stat.html.
AC_CACHE_CHECK([whether DB->stat() accepts TXNid],ac_cv_db_stat_accept_txn,[
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <db.h>],
[[DB *db; db_create(&db,NULL,0); db->stat(db,NULL,NULL,0);]])],
ac_cv_db_stat_accept_txn=yes,ac_cv_db_stat_accept_txn=no)])
if test "$ac_cv_db_stat_accept_txn" = "yes"; then
AC_DEFINE(HAVE_DB_STAT_ACCEPT_TXN,1,[Define to 1 if DB->stat() accepts TXNid])
fi

dnl See db-5.3.28/docs/upgrading/upgrade_4_3_err.html.
AC_CACHE_CHECK([whether DB_ENV->set_errcall() accepts DBE],
ac_cv_dbe_set_errcall_accept_dbe,[
CFLAGS_save="$CFLAGS"
CFLAGS="$CFLAGS -Werror"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <db.h>
void my_callback (const DB_ENV* dbe, const char *errpfx, const char *msg) {}],
[[DB_ENV *dbe; db_env_create(&dbe,0); dbe->set_errcall(dbe,&my_callback);]])],
ac_cv_dbe_set_errcall_accept_dbe=yes,ac_cv_dbe_set_errcall_accept_dbe=no)
CFLAGS=$CFLAGS_save])
if test "$ac_cv_dbe_set_errcall_accept_dbe" = "yes"; then
AC_DEFINE(HAVE_DBE_SET_ERRCALL_ACCEPT_DBE,1,
[Define to 1 if DB_ENV->set_errcall() accepts DBE])
fi

dnl unannounced!
AC_CACHE_CHECK([whether DB->get_transactional() accepts just 1 argument],
ac_cv_db_get_transactional_1arg,[
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <db.h>],
[[DB *db; db_create(&db,NULL,0); db->get_transactional(db);]])],
ac_cv_db_get_transactional_1arg=yes,ac_cv_db_get_transactional_1arg=no)])
if test "$ac_cv_db_get_transactional_1arg" = "yes"; then
AC_DEFINE(HAVE_DB_GET_TRANSACTIONAL_1ARG,1,
[Define to 1 if DB->get_transactional() accepts just 1 argument])
fi

dnl See db-5.3.28/docs/upgrading/upgrade_4_4_lockstat.html.
AC_CHECK_MEMBERS([DB_LOCK_STAT.st_nconflicts, DB_LOCK_STAT.st_lock_wait,
DB_LOCK_STAT.st_nnowaits, DB_LOCK_STAT.st_lock_nowait],,,[#include <db.h>])
dnl See db-5.3.28/docs/upgrading/upgrade_4_4_mutex.html.
AC_CHECK_MEMBERS([DB_ENV.mutex_set_tas_spins, DB_ENV.mutex_get_tas_spins,
DB_ENV.mutex_set_align, DB_ENV.mutex_get_align],,,[#include <db.h>])
dnl http://www.oracle.com/technology/products/berkeley-db/db/update/4.4.20/if.4.4.20.html
dnl *DIRTY* -> DB_READ_UNCOMMITTED; DEGREE_2 -> DB_READ_COMMITTED
AC_CHECK_DECLS([DB_LOCK_READ_UNCOMMITTED],,,[#include <db.h>])

dnl See db-5.3.28/docs/upgrading/upgrade_4_7_log.html
dnl and db-5.3.28/docs/upgrading/upgrade_4_7_interdir.html.
AC_CHECK_MEMBERS([DB_ENV.log_set_config, DB_ENV.get_intermediate_dir_mode],
,,[#include <db.h>])

dnl undocumented - upgrade to 4.8
AC_CHECK_MEMBERS([DB_TXN_ACTIVE.gid, DB_TXN_ACTIVE.status],,,[#include <db.h>])

dnl undocumented
AC_CHECK_FUNCS(db_full_version)
AC_CHECK_MEMBERS([DB.compact],,,[#include <db.h>])

BOLD_MSG([Berkeley-DB (Output)])
AC_CONFIG_FILES(Makefile link.sh)
AC_OUTPUT

BOLD_MSG([Berkeley-DB (Done)])
