dnl @(#)configure.in	11.33 (Sleepycat) 11/11/99
dnl Process this file with autoconf to produce a configure script.

AC_INIT(../db/db.c)
AC_CONFIG_HEADER(db_config.h:config.hin)

dnl Configure setup.
AC_PROG_INSTALL()
AC_CANONICAL_HOST
AC_ARG_PROGRAM()

dnl We cannot build in the top-level directory.
AC_MSG_CHECKING(if building in the top-level directory)
[ test -d db_archive ] && AC_MSG_ERROR([
Berkeley DB cannot be built in the top-level distribution directory.])
AC_MSG_RESULT(no)

dnl Substitution variables.
AC_SUBST(ADDITIONAL_LANG)
AC_SUBST(ADDITIONAL_LIBS)
AC_SUBST(ADDITIONAL_OBJS)
AC_SUBST(ADDITIONAL_PROGS)
AC_SUBST(CFLAGS)
AC_SUBST(CPPFLAGS)
AC_SUBST(CXX)
AC_SUBST(CXXFLAGS)
AC_SUBST(DEFAULT_INSTALL)
AC_SUBST(DEFAULT_LIB)
AC_SUBST(INSTALLER)
AC_SUBST(INSTALL_LIBS)
AC_SUBST(JAVAC)
AC_SUBST(JAVACFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(LIBDB_ARGS)
AC_SUBST(LIBS)
AC_SUBST(LIBTOOL)
AC_SUBST(LIBTSO_LIBS)
AC_SUBST(MAKEFILE_CC)
AC_SUBST(MAKEFILE_CCLINK)
AC_SUBST(MAKEFILE_CXX)
AC_SUBST(POSTLINK)
AC_SUBST(SOFLAGS)
AC_SUBST(SOLINK)
AC_SUBST(SOSUFFIX)

dnl $o is set to ".o" or ".lo", and is the file suffix used in the
dnl Makefile instead of .o
AC_SUBST(o)
o=.o
INSTALLER="\$(cp)"
DEFAULT_LIB="\$(libdb)"
DEFAULT_INSTALL="install_static"

dnl Set the version.
AM_VERSION_SET

dnl Set the default installation location.
AC_PREFIX_DEFAULT(/usr/local/BerkeleyDB.@DB_VERSION_MAJOR@.@DB_VERSION_MINOR@)

dnl Process all options before using them.  This is necessary because there
dnl are dependencies among them.
AM_OPTIONS_SET

dnl This is where we handle stuff that autoconf can't handle: compiler,
dnl preprocessor and load flags, libraries that the standard tests don't
dnl look for.  The default optimization is -O.
dnl
dnl There are additional libraries we need for some compiler/architecture
dnl combinations.
dnl
dnl Some architectures require DB to be compiled with special flags and/or
dnl libraries for threaded applications
dnl
dnl The makefile CC may be different than the CC used in config testing,
dnl because the makefile CC may be set to use $(LIBTOOL).
dnl
dnl XXX
dnl Don't override anything if it's already set from the environment.
optimize_def="-O"
case "$host_os" in
aix4.*)	   optimize_def="-O2"
	   CC=${CC-"xlc_r"}
	   CPPFLAGS="-D_THREAD_SAFE $CPPFLAGS"
	   LIBTSO_LIBS="\$(LIBS)";;
bsdi3*)	   CC=${CC-"shlicc2"}
	   optimize_def="-O2"
	   LIBS="-lipc $LIBS";;
bsdi*)	   optimize_def="-O2";;
freebsd*)  optimize_def="-O2"
	   CPPFLAGS="-D_THREAD_SAFE -pthread $CPPFLAGS";;
hpux*)	   CPPFLAGS="-D_REENTRANT $CPPFLAGS";;
irix*)	   optimize_def="-O2"
	   CPPFLAGS="-D_SGI_MP_SOURCE $CPPFLAGS";;
linux*)	   optimize_def="-O2"
	   CPPFLAGS="-D_REENTRANT $CPPFLAGS";;
mpeix*)	   CPPFLAGS="-D_POSIX_SOURCE -D_SOCKET_SOURCE $CPPFLAGS"
	   LIBS="-lsocket -lsvipc $LIBS";;
osf*)	   CPPFLAGS="-D_REENTRANT $CPPFLAGS";;
sco3.2v4*) CC=${CC-"cc -belf"}
	   LIBS="-lsocket -lnsl_s $LIBS";;
sco*)	   CC=${CC-"cc -belf"}
	   LIBS="-lsocket -lnsl $LIBS";;
solaris*)  CPPFLAGS="-D_REENTRANT $CPPFLAGS";;
esac

CFLAGS=${CFLAGS-$optimize_def}
CXXFLAGS=${CXXFLAGS-"$CFLAGS"}

dnl If the user wants a debugging environment, add -g to the CFLAGS value.
dnl
dnl XXX
dnl Some compilers can't mix optimizing and debug flags.  The only way to
dnl handle this is to specify CFLAGS in the environment before configuring.
if test "$db_cv_debug" = yes; then
	AC_DEFINE(DEBUG)
	CFLAGS="$CFLAGS -g"
	CXXFLAGS="$CXXFLAGS -g"
fi

dnl The default compiler is cc (NOT gcc), the default CFLAGS is as specified
dnl above, NOT what is set by AC_PROG_CC, as it won't set optimization flags.
dnl We still call AC_PROG_CC so that we get the other side-effects.
AC_CHECK_PROG(CC, cc, cc)
AC_CHECK_PROG(CC, gcc, gcc)
AC_PROG_CC

dnl Because of dynamic library building, the ${CC} used for config tests
dnl may be different than the ${CC} we want to put in the Makefile.
dnl The latter is known as ${MAKEFILE_CC} in this script.
MAKEFILE_CC=${CC}
MAKEFILE_CCLINK="\$(CC)"
MAKEFILE_CXX="nocxx"

dnl Set some #defines based on configuration options.
if test "$db_cv_diagnostic" = yes; then
	AC_DEFINE(DIAGNOSTIC)
fi
if test "$db_cv_debug_rop" = yes; then
	AC_DEFINE(DEBUG_ROP)
fi
if test "$db_cv_debug_wop" = yes; then
	AC_DEFINE(DEBUG_WOP)
fi
if test "$db_cv_test" = yes; then
	AC_DEFINE(CONFIG_TEST)
fi

dnl See if we need the C++ compiler at all.  If so, we'd like to find one
dnl that interoperates with the C compiler we chose.  Since we prefered cc
dnl over gcc, we'll also prefer CC over g++/gcc.  If we're wrong, the user
dnl can set CC and CCC in their environment before running configure.
dnl
dnl XXX
dnl AC_PROG_CXX sets CXX, but it uses $CCC as its first choice.
if test "$db_cv_cxx" = "yes" -o "$db_cv_java" = "yes"; then
	if test "$GCC" != "yes"; then
		AC_CHECK_PROG(CCC, CC, CC)
	fi
	AC_PROG_CXX
	MAKEFILE_CXX=${CXX}
fi

dnl XXX
dnl Versions of GCC up to 2.8.0 required -fhandle-exceptions, but it is
dnl the default in versions 2.8.0 and after.
dnl
dnl $GXX may be set as a result of enabling C++ or Java.
if test "$GXX" = "yes"; then
	CXXFLAGS="-fhandle-exceptions $CXXFLAGS"
fi

dnl Export our compiler preferences for the libtool configuration.
export CC CCC
CCC=CXX

dnl Dynamic library and libtool configuration; optional, but required for
dnl Tcl or Java support.
LIBDB_ARGS="libdb.a"
LIBTOOL="nolibtool"
POSTLINK="@true"
SOSUFFIX="so"
if test "$db_cv_dynamic" = "yes"; then
	SAVE_CC="${MAKEFILE_CC}"
	SAVE_CXX="${MAKEFILE_CXX}"

	# Configure libtool.
	AC_MSG_CHECKING(libtool configuration)
	AC_MSG_RESULT([])
	${CONFIG_SHELL-/bin/sh} $srcdir/ltconfig \
		--no-verify $srcdir/ltmain.sh \
		--output=./libtool $host_os \
		--disable-static \
	|| AC_MSG_ERROR([libtool configure failed])

	SOSUFFIX=`sed -e '/^soname_spec=/!d' -e 's/.*\.\([[a-zA-Z0-9_]]*\).*/\1/' ./libtool`
	DEFAULT_LIB="\$(libso_target)"
	DEFAULT_INSTALL="install_dynamic"
	LIBDB_ARGS="\$(libso_linkname)"
	LIBTOOL="\$(SHELL) ./libtool"

	MAKEFILE_CC="\$(LIBTOOL) --mode=compile ${SAVE_CC}"
	MAKEFILE_CXX="\$(LIBTOOL) --mode=compile ${SAVE_CXX}"
	MAKEFILE_CCLINK="\$(LIBTOOL) --mode=link ${SAVE_CC}"

	INSTALLER="\$(LIBTOOL) --mode=install cp"
	POSTLINK="\$(LIBTOOL) --mode=execute true"
	SOLINK="\$(LIBTOOL) --mode=link ${SAVE_CC} -avoid-version"
	SOFLAGS="-rpath \$(libdir)"
	o=".lo"
fi

dnl Optional C++ API.
if test "$db_cv_cxx" = "yes"; then
	if test "$db_cv_dynamic" = "yes"; then
		ADDITIONAL_LIBS="$ADDITIONAL_LIBS \$(libxso_target)"
		DEFAULT_INSTALL="${DEFAULT_INSTALL} install_dynamic_cxx"
	else
		ADDITIONAL_LIBS="$ADDITIONAL_LIBS \$(libcxx)"
		DEFAULT_INSTALL="${DEFAULT_INSTALL} install_static_cxx"
	fi
fi

dnl Optional Java API.
if test "$db_cv_java" = "yes"; then
	if test "$db_cv_dynamic" != "yes"; then
		AC_MSG_ERROR([--enable-java requires --enable-dynamic])
	fi

	AC_CHECK_PROG(JAVAC, javac, javac, nojavac)
	if test "$JAVAC" = "nojavac"; then
		AC_MSG_ERROR([no javac compiler in PATH])
	fi
	AC_PATH_PROG(JAVACABS, javac, nojavac)
	ADDITIONAL_LIBS="$ADDITIONAL_LIBS \$(libjso_target)"
	ADDITIONAL_LANG="$ADDITIONAL_LANG java"
	DEFAULT_INSTALL="${DEFAULT_INSTALL} install_java"
	# Want to do: JTOPDIR=`echo "$JTOPDIR" | sed -e 's:/[^/]*$::'`
	# but [] are removed.
	#
	JENDDIR=`echo "$JAVACABS" | sed -e 's:.*/::'`
	JTOPDIR=`echo "$JAVACABS" | sed -e "s:/$JENDDIR::"`
	if test -f "$JTOPDIR/include/jni.h"; then
		CPPFLAGS="$CPPFLAGSS -I$JTOPDIR/include"
	else
		JENDDIR=`echo "$JTOPDIR" | sed -e 's:.*/::'`
		JTOPDIR=`echo "$JTOPDIR" | sed -e "s:/$JENDDIR::"`
		if test -f "$JTOPDIR/include/jni.h"; then
			CPPFLAGS="$CPPFLAGS -I$JTOPDIR/include"
		else
			AC_MSG_ERROR([cannot find java include files])
		fi
	fi
	if test -d "$JTOPDIR/include/genunix"; then
		CPPFLAGS="$CPPFLAGS -I$JTOPDIR/include -I$JTOPDIR/include/genunix"
	fi
else
	JAVAC=nojavac
fi

dnl Optional TCL API.
if test "$db_cv_tcl" = "yes"; then
	if test "$db_cv_dynamic" != "yes"; then
		AC_MSG_ERROR([--enable-tcl requires --enable-dynamic])
	fi

	AM_TCL_LOAD

	ADDITIONAL_LIBS="$ADDITIONAL_LIBS \$(libtso_target)"
	DEFAULT_INSTALL="${DEFAULT_INSTALL} install_tcl"
fi

dnl Optional DB 1.85 compatibility API.
if test "$db_cv_compat185" = "yes"; then
	ADDITIONAL_OBJS="db185${o} $ADDITIONAL_OBJS"
fi

dnl Optional utilities.
if test "$db_cv_dump185" = "yes"; then
	ADDITIONAL_PROGS="db_dump185 $ADDITIONAL_PROGS"
fi

dnl Checks for typedefs, structures, and system/compiler characteristics.
AC_C_BIGENDIAN
AC_C_CONST
AC_HEADER_STAT
AC_HEADER_TIME
AC_STRUCT_ST_BLKSIZE
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T

dnl Define any short-hand types we're missing.
AM_SHORTHAND_TYPES

dnl Checks for header files.
AC_HEADER_DIRENT
AC_CHECK_HEADERS(sys/select.h)
AC_CHECK_HEADERS(sys/time.h)

dnl Check for mutexes.  We do this here because it changes $LIBS.
AM_DEFINE_MUTEXES

dnl Checks for system functions for which we have replacements.
dnl
dnl XXX
dnl The only portable getcwd call is getcwd(char *, size_t), where the
dnl buffer is non-NULL -- Solaris can't handle a NULL buffer, and they
dnl deleted getwd().
AC_REPLACE_FUNCS(getcwd getopt memcmp memcpy memmove)
AC_REPLACE_FUNCS(raise snprintf strerror vsnprintf)

dnl XXX
dnl Nasty hack.  AC_REPLACE_FUNCS added entries of the form xxx.o to the
dnl LIBOBJS variable.  They have to be xxx.lo if we are building shared
dnl libraries.  Use sed, configure already requires it.
tmp="`echo \"$LIBOBJS\" | sed \"s/\.o/${o}/g\"`"
LIBOBJS="$tmp"

dnl Check for system functions we optionally use.
AC_CHECK_FUNCS(getuid pread pstat_getdynamic sysconf)
AC_CHECK_FUNCS(sched_yield yield)

dnl Check for the sigfillset macro/function.  If it exists, we assume we can
dnl use it and sigprocmask to block signals when creating temporary files.
dnl
dnl !!!
dnl Don't replace this test with AC_CHECK_FUNCS, it fails on Solaris because
dnl the C library has the sigfillset symbol, but common compiler flags like
dnl -Xc can make the sigset_t type unavailable.
AC_CACHE_CHECK([for sigfillset], db_cv_have_sigfillset, [dnl
AC_TRY_LINK([#include <signal.h>], [sigset_t set; sigfillset(&set);],
	[db_cv_have_sigfillset=yes], [db_cv_have_sigfillset=no])])
if test "$db_cv_have_sigfillset" = yes; then
	AC_DEFINE(HAVE_SIGFILLSET)
fi

dnl Check for fcntl(2) to deny child process access to file descriptors.
AC_CACHE_CHECK([for fcntl/F_SETFD], db_cv_fcntl_f_setfd, [dnl
AC_TRY_RUN([
#include <sys/types.h>
#include <fcntl.h>
main(){exit(fcntl(1, F_SETFD, 1) == -1);}],
	[db_cv_fcntl_f_setfd=yes], [db_cv_fcntl_f_setfd=no])])
if test "$db_cv_fcntl_f_setfd" = yes; then
	AC_DEFINE(HAVE_FCNTL_F_SETFD)
fi

dnl A/UX has a broken getopt(3).
case "$host_os" in
aux*)	ADDITIONAL_OBJS="getopt${o} $ADDITIONAL_OBJS";;
esac

dnl Checks for system functions for which we don't have replacements.

dnl We require qsort(3) and select(2).
AC_CHECK_FUNCS(qsort, , AC_MSG_ERROR([No qsort library function.]))
AC_CHECK_FUNCS(select, , AC_MSG_ERROR([No select library function.]))

dnl Some versions of sprintf return a pointer to the first argument instead
dnl of a character count.  We assume that the return value of snprintf and
dnl vsprintf etc. will be the same as sprintf, and check the easy one.
AC_CACHE_CHECK([for int type sprintf return value], db_cv_sprintf_count, [dnl
AC_TRY_RUN([main(){char buf[20]; exit(sprintf(buf, "XXX") != 3);}],
	[db_cv_sprintf_count=yes], [db_cv_sprintf_count=no])])
if test "$db_cv_sprintf_count" = no; then
	AC_DEFINE(SPRINTF_RET_CHARPNT)
fi

dnl Vendors are doing 64-bit lseek in different ways.
dnl AIX, HP/UX and Solaris all use _FILE_OFFSET_BITS to specify a "big-file"
dnl environment.
if test "$db_cv_bigfile" = no; then
	case "$host_os" in
	bsdi*|aix*|hpux*|solaris*)	AC_DEFINE(HAVE_FILE_OFFSET_BITS);;
	esac
fi

dnl Figure out how to create shared regions.
dnl
dnl First, we look for mmap.
dnl
dnl	BSD/OS has mlock(2), but it doesn't work.
dnl
dnl	Nextstep (version 3.3) apparently supports mmap(2) (the mmap symbol
dnl	is defined in the C library) but does not support munmap(2).  Don't
dnl	try to use mmap if we can't find munmap.
dnl
dnl	Ultrix has mmap(2), but it doesn't work.
mmap_ok=no
case "$host_os" in
ultrix*)
	AC_MSG_WARN([mmap(2) interface ignored on Ultrix.]);;
bsdi*)
	AC_MSG_WARN([mlock(2) interface ignored on BSD/OS.])
	mmap_ok=yes
	AC_CHECK_FUNCS(mmap munmap, , mmap_ok=no);;
*)
	mmap_ok=yes
	AC_CHECK_FUNCS(mlock munlock)
	AC_CHECK_FUNCS(mmap munmap, , mmap_ok=no);;
esac

dnl Second, we look for shmget.
dnl
dnl	SunOS has the shmget(2) interfaces, but there appears to be a missing
dnl	#include <debug/debug.h> file, so we ignore them.
shmget_ok=no
case "$host_os" in
sunos*)
	AC_MSG_WARN([shmget(2) interface ignored on SunOS.]);;
*)
	shmget_ok=yes
	AC_CHECK_FUNCS(shmget, , shmget_ok=no);;
esac

dnl We require either mmap/munmap(2) or shmget(2).
if test "$mmap_ok" = no -a "$shmget_ok" = no; then
	AC_MSG_ERROR([Neither mmap/munmap(2) or shmget(2) library functions.])
fi

dnl Check for programs used in building and installation.
AM_PROGRAMS_SET

AC_OUTPUT(Makefile include.tcl:../test/include.tcl \
    db.h:../include/db.src db_int.h:../include/db_int.src \
    db_185.h:../include/db_185.h)
