dnl AUTOCONF configuration for Berkeley-DB
dnl Copyright (C) 2003-2012 Sam Steingold <sds@gnu.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 <http://www.sleepycat.com/docs/ref/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.stat(&db,NULL,0,NULL);]])],
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 <http://www.sleepycat.com/docs/ref/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; 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.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 http://www.sleepycat.com/docs/ref/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 http://www.sleepycat.com/docs/ref/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 http://www.oracle.com/technology/documentation/berkeley-db/db/ref/upgrade.4.7/log.html
dnl http://www.oracle.com/technology/documentation/berkeley-db/db/ref/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)])
