dnl  Process this file with autoconf to produce a configure script.
dnl  Copyright (C) 1998 John Harper <john@dcs.warwick.ac.uk>
dnl  $Id: configure.in,v 1.119 2000/09/28 14:57:52 john Exp $
dnl
dnl  This file is part of librep.
dnl
dnl  librep is free software; you can redistribute it and/or modify it
dnl  under the terms of the GNU General Public License as published by
dnl  the Free Software Foundation; either version 2, or (at your option)
dnl  any later version.
dnl
dnl  librep is distributed in the hope that it will be useful, but
dnl  WITHOUT ANY WARRANTY; without even the implied warranty of
dnl  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl  GNU General Public License for more details.
dnl
dnl  You should have received a copy of the GNU General Public License
dnl  along with librep; see the file COPYING.  If not, write to
dnl  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

AC_REVISION($Revision: 1.119 $)

AC_INIT(src/rep_subrs.h)
AC_CONFIG_HEADER(config.h src/rep_config.h)

dnl Release versioning info
version="0.13.1"

dnl libtool versioning info: `CURRENT:REVISION:AGE'. CURRENT is the
dnl current interface id, REVISION is the version number of this
dnl implementation, AGE defines the first interface id also supported
dnl (i.e. all interfaces between CURRENT-AGE and CURRENT are supported)
libcurrent=10
librevision=1
libage=1
libversion="$libcurrent:$librevision:$libage"

makefile_template="Makefile.in:Makedefs.in"
output_files="src/Makefile:Makedefs.in:src/Makefile.in\
 lisp/Makefile:Makedefs.in:lisp/Makefile.in\
 Makefile:Makedefs.in:Makefile.in\
 man/Makefile:Makedefs.in:man/Makefile.in\
 intl/Makefile librep.spec"

dnl Find the system type
AC_CANONICAL_HOST

dnl Remove trailing slash in $prefix if necessary
case "${prefix}" in
  */)
    prefix=`echo ${prefix} | sed -e 's/^\(.*\)\/$/\1/'`
    ;;
esac

repdir='${datadir}/rep'
replispdir='${repdir}/${version}/lisp'
repexecdir='${libexecdir}/rep/${version}/${host_type}'
repcommonexecdir='${libexecdir}/rep/${host_type}'
repdocfile='${repexecdir}/DOC'

EXTRA_LIBOBJS=""
AC_SUBST(EXTRA_LIBOBJS)

dnl Checks for programs.
AC_PROG_CC
AC_ISC_POSIX
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_GCC_TRADITIONAL

AC_DISABLE_STATIC
AM_PROG_LIBTOOL

dnl Abort if shared libraries aren't enabled
if test "${enable_shared}" != "yes"; then
  AC_MSG_ERROR([Need shared libraries enabled])
fi

dnl Checks for libraries.
AC_CHECK_FUNC(gethostent, , AC_CHECK_LIB(nsl, gethostent))
AC_CHECK_FUNC(setsockopt, , AC_CHECK_LIB(socket, setsockopt))
AC_CHECK_FUNC(dlopen, , AC_CHECK_LIB(dl, dlopen))

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_HEADER_TIME
AC_CHECK_HEADERS(fcntl.h sys/ioctl.h sys/time.h sys/utsname.h unistd.h siginfo.h memory.h stropts.h termios.h string.h limits.h argz.h locale.h nl_types.h malloc.h sys/param.h)

dnl Check for GNU MP library and header files
AC_ARG_WITH(gmp,
 [  --without-gmp		  Don't use GMP for bignum/rational numbers],
 [], [with_gmp=maybe])

GMP_LIBS="-lm"
if test "$with_gmp" != "no"; then
  AC_ARG_WITH(gmp-prefix, [  --with-gmp-prefix=DIR   path to GMP],
   [if test "$withval" != "no"; then
      CPPFLAGS="${CPPFLAGS} -I$withval/include"
      LDFLAGS="${LDFLAGS} -L$withval/lib"
    fi],
   dnl debian brain-damage
   [if test -d /usr/include/gmp2; then
      CPPFLAGS="${CPPFLAGS} -I/usr/include/gmp2"
    fi])

  found_gmp=no
  AC_CHECK_HEADER(gmp.h,
    [AC_CHECK_LIB(gmp, mpz_init,
       [GMP_LIBS="-lgmp -lm"; found_gmp=yes],
       [AC_CHECK_LIB(gmp, __gmpz_init,
          [GMP_LIBS="-lgmp -lm"; found_gmp=yes])])])

  if test "$found_gmp" = "yes"; then
    AC_DEFINE(HAVE_GMP)
  elif test "$with_gmp" != "no"; then
    AC_MSG_ERROR([Can't find GMP (--without-gmp for cut-down non-GMP build)])
  fi
fi
AC_SUBST(GMP_LIBS)

dnl Check for GNU DBM library and header files
AC_ARG_WITH(gdbm-prefix,
[  --with-gdbm-prefix=DIR  path to GDBM],[
  if test "$withval" != "no"; then
    CPPFLAGS="${CPPFLAGS} -I$withval/include"
    LDFLAGS="${LDFLAGS} -L$withval/lib"
  fi
])
AC_CHECK_HEADER(gdbm.h,
    AC_CHECK_LIB(gdbm, gdbm_open, GDBM_LIBS="-lgdbm",
	AC_MSG_ERROR(Cannot find GDBM library)),
    AC_MSG_ERROR(Cannot find GDBM header))
AC_SUBST(GDBM_LIBS)

dnl Check for Doug Lea's malloc in libc, otherwise compile dlmalloc.c
doug_lea_malloc=yes
AC_CHECK_FUNC(malloc_get_state, , doug_lea_malloc=no)
AC_CHECK_FUNC(malloc_set_state, , doug_lea_malloc=no)
if test "$doug_lea_malloc" = "no"; then
  case ${host} in
    *-dec-osf*)
      dnl dlmalloc is broken on Tru64
      AC_DEFINE(LIBC_MALLOC)
      ;;
    *)
      EXTRA_LIBOBJS="${EXTRA_LIBOBJS} dlmalloc.o"
      AC_DEFINE(DOUG_LEA_MALLOC)
      ;;
  esac
else
  AC_DEFINE(DOUG_LEA_MALLOC)
  AC_DEFINE(LIBC_MALLOC)
fi

dnl Check for readline
AC_ARG_WITH(readline,
 [  --with-readline         support fancy command input editing
  --without-readline      Don't use readline], [], [with_readline=maybe])

if test "$with_readline" != "no"; then
  AC_ARG_WITH(readline-prefix,
  [  --with-readline-prefix=DIR path to readline],
  [ if test "$withval" != "no" -a "$withval" != "yes"; then
    # Save in case test with directory specified fails
    _cppflags=${CPPFLAGS}
    _ldflags=${LDFLAGS}

    CPPFLAGS="${CPPFLAGS} -I$withval/include"
    LDFLAGS="${LDFLAGS} -L$withval/lib"
  fi ])

  dnl check for terminal library
  dnl this is a very cool solution from octave's configure.in
  unset tcap
  for termlib in ncurses curses termcap terminfo termlib; do
    AC_CHECK_LIB(${termlib}, tputs, [tcap="$tcap -l$termlib"])
    case "$tcap" in
      *-l${termlib}*)
        break
      ;;
    esac
  done

  AC_CHECK_HEADER(readline/readline.h,
    AC_CHECK_LIB(readline, readline,[
      READLINE_LIBS="-lreadline $tcap"
      AC_DEFINE(HAVE_LIBREADLINE)], , $tcap))

  if test -z "$READLINE_LIBS"; then
    if test "$with_readline_prefix" = "yes"; then
      AC_MSG_ERROR([Can't find readline libraries])
    else
      CPPFLAGS=${_cppflags}
      LDFLAGS=${_ldflags}
    fi
  fi
fi
AC_SUBST(READLINE_LIBS)

dnl Check for dynamic loading
AC_CHECK_HEADER(dlfcn.h, [AC_DEFINE(HAVE_DLFCN_H)])
AC_CHECK_HEADER(dl.h, [AC_DEFINE(HAVE_DL_H)])
dl_ok=no
AC_CHECK_FUNC(dlopen, [AC_DEFINE(HAVE_DLOPEN) dl_ok=dl],
    AC_CHECK_LIB(dl, dlopen, [AC_DEFINE(HAVE_DLOPEN) dl_ok=dl]))
AC_CHECK_FUNC(shl_load, [AC_DEFINE(HAVE_SHL_LOAD) dl_ok=shl],
    AC_CHECK_LIB(dld, shl_load, [AC_DEFINE(HAVE_DLOPEN) dl_ok=shl]))

if test $dl_ok = dl; then
  AC_MSG_CHECKING([for preceding underscore in symbols])
  dnl copied from glib configure.in
  AC_TRY_RUN([#ifdef HAVE_DLFCN_H
	      # include <dlfcn.h>
	      #endif
              int glib_underscore_test (void) { return 42; }
              int main() {
                void *f1 = (void*)0, *f2 = (void*)0, *handle;
                handle = dlopen ((void*)0, 0);
                if (handle) {
                  f1 = dlsym (handle, "glib_underscore_test");
                  f2 = dlsym (handle, "_glib_underscore_test");
                } return (!f2 || f1);
              }],
              [AC_DEFINE(DLSYM_NEED_USCORE)
	       AC_MSG_RESULT(yes)],
	      [AC_MSG_RESULT(no)])

  dnl RTLD_GLOBAL is broken for Tru64 UNIX V5.0 and V5.0A, such that
  dnl   xh = dlopen("libx.so", RTLD_GLOBAL|RTLD_LAZY); /* provides func */
  dnl   yh = dlopen("liby.so", RTLD_GLOBAL|RTLD_LAZY); /* provides func */
  dnl   yf = dlsym(yh, "func"); /* Error: returns func from libx.so */
  dnl It's hard to test for this apart from the version string, so
  dnl that's what we use.
  case ${host} in
    *-dec-osf5*)
      AC_MSG_CHECKING([for broken RTLD_GLOBAL on Tru64])
      if /usr/sbin/sizer -v | grep -E -q -e 'UNIX [[TVX]]5\.0A?(-[[[:digit:]]]+)? '; then
	  AC_DEFINE(BROKEN_RTLD_GLOBAL)
	  AC_MSG_RESULT(yes)
      else
	  AC_MSG_RESULT(no)
      fi
      ;;
  esac
fi

if test $dl_ok != no; then
  AC_DEFINE(HAVE_DYNAMIC_LOADING)
else
  AC_MSG_ERROR([can't figure out how to do dynamic loading])
fi

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_TYPE_SIGNAL

dnl Checks for library functions.
AC_FUNC_ALLOCA
AC_FUNC_MMAP
AC_FUNC_MEMCMP
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(getcwd gethostname select socket strcspn strerror strstr stpcpy strtol psignal strsignal snprintf grantpt lrand48 getpagesize setitimer dladdr dlerror munmap putenv setenv setlocale strchr strcasecmp strncasecmp strdup __argz_count __argz_stringify __argz_next siginterrupt gettimeofday strtoll strtoq)
AC_REPLACE_FUNCS(realpath)

dnl Custom tests

dnl How do we get dependency lines in the Makefile?
if test "x${GCC}" = "xyes"; then
  MAKEDEP='$(CC) -MM'
else
  case ${host} in
    *-dec-osf*)
      dnl works on Tru64
      MAKEDEP='$(CC) -M'
      ;;
    *-sun-solaris*)
      dnl works on Solaris
      MAKEDEP='/usr/lib/cpp -M'
      ;;
    *)
      dnl disable dependences?
      MAKEDEP='true'
      ;;
  esac
fi
AC_SUBST(MAKEDEP)

dnl If using GCC and it doesn't look as though the cflags have been
dnl set explicitly, add some warning options.
if test "x${GCC}" = "xyes" -a "x$CFLAGS" = "x-g -O2"; then
  CFLAGS="${CFLAGS} -Wall -Wpointer-arith -Wmissing-prototypes"
  case ${host} in
    *-sun-solaris*)
      dnl Turn off implicit-int warnings since the X11 includes
      dnl on Solaris generate a lot of these
      CFLAGS="${CFLAGS} -Wno-implicit-int"
      ;;
  esac
fi

AC_ARG_WITH(extra-cflags,
  [  --with-extra-cflags=FLAGS Extra flags to pass to C compiler],
  CFLAGS="${CFLAGS} $with_extra_cflags")

dnl Does <unistd.h> declare char **environ?
AC_CACHE_CHECK([whether unistd.h declares environ], jade_cv_decl_environ,
 AC_TRY_COMPILE([#include <unistd.h>], [char **foo = environ;],
  [jade_cv_decl_environ=yes],
  [jade_cv_decl_environ=no]))
if test ${jade_cv_decl_environ} = no; then
  AC_DEFINE(ENVIRON_UNDECLARED)
fi

dnl Are ptys available, and if so, how?
AC_MSG_CHECKING([whether /dev/ptmx exists])
if test -r /dev/ptmx; then
  AC_MSG_RESULT([yes])
  AC_DEFINE(HAVE_PTYS)
  AC_DEFINE(HAVE_DEV_PTMX)
else
  AC_MSG_RESULT([no])
fi

dnl Look for old-style /dev/ptyXN devices
AC_CACHE_CHECK([whether /dev/ptyXN devices exist], jade_cv_sys_dev_pty,
 jade_cv_sys_dev_pty=no
 for c in p q r s t u v w x y z a b c d e f g h i j k l m n o; do
   if test -r "/dev/pty${c}0"; then
     jade_cv_sys_dev_pty=${c}
     break
   fi
 done)
if test ${jade_cv_sys_dev_pty} != no; then
  AC_DEFINE(HAVE_PTYS)
  AC_DEFINE_UNQUOTED(FIRST_PTY_LETTER, '${jade_cv_sys_dev_pty}')
fi

dnl Try to find the aclocal directory for installation
aclocaldir=none
AC_ARG_WITH(aclocaldir,
  [  --with-aclocaldir=DIR   Directory in which to install autoconf macros],
  aclocaldir=$withval)
AC_MSG_CHECKING([for aclocal directory])
if test "$aclocaldir" = none; then
  aclocaldir="`aclocal --print-ac-dir 2>/dev/null`"
  if test "x${aclocaldir}" = "x"; then
    aclocaldir='${datadir}/aclocal'
  fi
fi
AC_MSG_RESULT([${aclocaldir}])
AC_SUBST(aclocaldir)

AC_ARG_ENABLE(continuations,
 [  --disable-continuations Don't include support for continuations
			   or multi-threading],
 [if test "$enableval" != "no"; then AC_DEFINE(WITH_CONTINUATIONS) fi],
 [AC_DEFINE(WITH_CONTINUATIONS)])

AC_ARG_ENABLE(dballoc,
 [  --enable-dballoc	  Trace all memory allocations],
 [if test "$enableval" != "no"; then AC_DEFINE(DEBUG_SYS_ALLOC) fi])

AC_ARG_ENABLE(dbsyms,
 [  --disable-dbsyms	  When writing debug output, don't translate
			   addresses to symbol names],
 [if test "$enableval" != "no"; then AC_DEFINE(DB_RESOLVE_SYMBOLS) fi],
 [AC_DEFINE(DB_RESOLVE_SYMBOLS)])

AC_ARG_ENABLE(gprof,
 [  --enable-gprof	  Build for gprof (needs --enable-static)],
 [CFLAGS="${CFLAGS} -pg"; LDFLAGS="${LDFLAGS} -pg"])

dnl Assumption for now
HAVE_UNIX=1
AC_DEFINE(rep_HAVE_UNIX, 1)

dnl We need to find an integer type that's big enough to store any pointer
AC_MSG_CHECKING([for data type to store Lisp values])
AC_ARG_WITH(value-type,
 [  --with-value-type=TYPE  Implicitly signed integer type, at least as wide
			   as a pointer. [TYPE=<probed>] (see README)], [],
 [with_value_type="undef"])
if test "${with_value_type}" = "undef"; then
  AC_TRY_RUN([main () { exit (!(sizeof (int) >= sizeof (void *)));}],
	     [with_value_type=int])
fi
if test "${with_value_type}" = "undef"; then
  AC_TRY_RUN([main () { exit (!(sizeof (long int) >= sizeof (void *)));}],
	     [with_value_type="long int"])
fi
if test "${with_value_type}" = "undef"; then
  AC_TRY_RUN([main () { exit (!(sizeof (long long int) >= sizeof (void *)));}],
	     [with_value_type="long long int"])
fi
if test "${with_value_type}" = "undef"; then
  AC_MSG_ERROR([can't find Lisp value type; set --with-value-type (see README)])
fi
AC_MSG_RESULT([${with_value_type}])
AC_DEFINE_UNQUOTED(rep_PTR_SIZED_INT, ${with_value_type})

dnl Find the size (in bits) of the chosen value type
AC_MSG_CHECKING([for size of Lisp value type])
AC_ARG_WITH(value-sizeof,
 [  --with-value-sizeof=N   Size (in bytes) of value type. [N=<probed>]], [],
 [with_value_sizeof="undef"])
if test "${with_value_sizeof}" = "undef"; then
  dnl the following fragment is inspired by AC_CHECK_SIZEOF
  AC_TRY_RUN([#include <stdio.h>
	      main () {
		FILE *f = fopen ("conftestval", "w");
		if (!f) exit (1);
		fprintf (f, "%d\n", sizeof (${with_value_type}));
		exit (0);
	      }],
	      [ with_value_sizeof=`cat conftestval`; rm -f conftestval])
fi
AC_MSG_RESULT([${with_value_sizeof} bytes])
AC_DEFINE_UNQUOTED(rep_PTR_SIZED_INT_SIZEOF, ${with_value_sizeof})

dnl What's the suffix to get an integer constant of the above size?
AC_MSG_CHECKING([for value type constant suffix])
case "${with_value_type}" in
  "long long int"|"long long")
    rep_value_suffix=LL
    ;;
  *)
    dnl We should be okay to default to longs..
    rep_value_suffix=L
    ;;
esac
AC_MSG_RESULT([${rep_value_suffix}])
AC_DEFINE_UNQUOTED(rep_PTR_SIZED_INT_SUFFIX, ${rep_value_suffix})

dnl What's the printf integer conversion for this type?
AC_MSG_CHECKING([for value type printf conversion])
case "${with_value_type}" in
  "long long int"|"long long")
    dnl other options would be L or q!?
    rep_value_conv=ll
    ;;
  long|"long int")
    rep_value_conv=l
    ;;
  *)
    rep_value_conv=
    ;;
esac
AC_MSG_RESULT([${rep_value_conv}])
AC_DEFINE_UNQUOTED(rep_PTR_SIZED_INT_CONV, "${rep_value_conv}")

dnl Check for size of long
AC_CHECK_SIZEOF(long)

dnl Check if `long long int' is available, also check it's size, if not
dnl available, then set it's size to the same as long
AC_MSG_CHECKING([for long long int])
AC_TRY_COMPILE([],[long long int foo = 42;],
	       [AC_MSG_RESULT(yes)
		AC_DEFINE(rep_HAVE_LONG_LONG,1)
		AC_CHECK_SIZEOF(long long)],
	       [AC_MSG_RESULT(no)
		AC_DEFINE(SIZEOF_LONG_LONG,SIZEOF_LONG)])


dnl This is the malloc granularity. It's unlikely that anything has
dnl less alignment than the default value of the number of bytes in
dnl the value type
AC_ARG_WITH(malloc-alignment,
 [  --with-malloc-alignment=BYTES Must be less than or equal to the alignment
			   returned by the malloc function. [BYTES=N]], [],
 [with_malloc_alignment=${with_value_sizeof}])
AC_DEFINE_UNQUOTED(rep_MALLOC_ALIGNMENT, ${with_malloc_alignment})

dnl Check for gcc crashes on alpha (seems to be fixed in new snapshots, but
dnl just to make it compile on all machines)
if test "x${GCC}" = "xyes"; then
  AC_MSG_CHECKING([for broken alpha gcc])
  AC_TRY_COMPILE([],[__builtin_return_address(1);],
		 [AC_MSG_RESULT(no)],
		 [AC_DEFINE(BROKEN_ALPHA_GCC,1)
		  AC_MSG_RESULT(yes)
		  AC_MSG_WARN([Backtrace support will not be compiled])])
fi

AC_MSG_CHECKING([for stack growth direction])
AC_ARG_WITH(stack-direction,
 [  --with-stack-direction=DIR Stack growth direction. -1 for downwards,
			   +1 for upwards.],
 [AC_MSG_RESULT(${with_stack_direction})],
 [with_stack_direction=unknown])
if test "${with_stack_direction}" = unknown; then
  case ${host_cpu} in
    sparc|i?86)
      AC_MSG_RESULT([assuming downwards])
      with_stack_direction="-1"
      ;;
    dnl any other known stack directions..?
  esac
fi
if test "${with_stack_direction}" = unknown; then
  AC_TRY_RUN([ void inner (char *foo) { char bar; exit (!(foo >= &bar)); }
	       void main () { char foo; inner (&foo); } ],
   [AC_MSG_RESULT([downwards])
    with_stack_direction=-1],
   [AC_MSG_RESULT([upwards])
    with_stack_direction=+1])
fi
if test "${with_stack_direction}" != unknown; then
  AC_DEFINE_UNQUOTED(STACK_DIRECTION, ${with_stack_direction})
fi

dnl Nonstandard exported symbols
AC_SUBST(version)
AC_SUBST(libversion)
AC_SUBST(repdir)
AC_SUBST(repexecdir)
AC_SUBST(repcommonexecdir)
AC_SUBST(replispdir)
AC_SUBST(repdocfile)
AC_SUBST(HAVE_UNIX)
AC_SUBST(HAVE_X11)
AC_SUBST(LIBOBJS)

AC_DEFINE_UNQUOTED(rep_VERSION, "${version}")
AC_DEFINE_UNQUOTED(rep_INTERFACE, ${libcurrent})

dnl Hacks for libintl
AM_LC_MESSAGES
VERSION="$version"
PACKAGE="librep"
DATADIRNAME="share"
MKINSTALLDIRS='${top_srcdir}/mkinstalldirs'
INTLOBJS="\$(GETTOBJS)"
GT_YES='#'
GT_NO=''
l=l
AC_SUBST(VERSION)
AC_SUBST(PACKAGE)
AC_SUBST(DATADIRNAME)
AC_SUBST(MKINSTALLDIRS)
AC_SUBST(INTLOBJS)
AC_SUBST(GT_YES)
AC_SUBST(GT_NO)
AC_SUBST(l)

dnl If it looks like GNU gettext is in libc, don't compile a local copy
USE_INCLUDED_LIBINTL=yes
AC_SUBST(USE_INCLUDED_LIBINTL)
AC_CHECK_FUNC(_nl_msg_cat_cntr,
 [AC_DEFINE(LIBC_GETTEXT)
  AC_CHECK_HEADERS(libintl.h)
  USE_INCLUDED_LIBINTL=no])

dnl Build all files
AC_OUTPUT(${output_files})

dnl If it doesn't look like GNU Make is being used, give a friendly warning
tem=`make --version -f /dev/null 2>&1 | grep GNU`
if test "x$tem" = "x"; then
  AC_MSG_WARN([You need to use GNU Make when compiling])
fi

dnl Local variables:
dnl major-mode: sh-mode
dnl End:
