dnl AUTOCONF configuration for Berkeley-DB
dnl Copyright (C) 2003-2006  Sam Steingold <sds@gnu.org>
dnl GNU GPL2

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

RSE_BOLD
BOLD_MSG([Berkeley-DB (Tools)])
AC_PROG_CC
AC_PROG_CPP

dnl Search for libdb and define LIBDB, LTLIBDB and INCDB.
AC_CONFIG_AUX_DIR(../../src/build-aux)
AC_LIB_LINKFLAGS([db])

BOLD_MSG([Berkeley-DB (Headers)])
AC_HEADER_STDC
AC_HEADER_TIME
AC_CHECK_HEADERS(sys/time.h time.h)
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_FUNCS(gettimeofday localtime strftime)
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>])

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

BOLD_MSG([Berkeley-DB (Done)])
