dnl   configuration script for Guile
dnl   Process this file with autoconf to produce configure.
dnl
dnl  Copyright (C) 1998 Free Software Foundation, Inc.
dnl
dnl  This file is part of GUILE
dnl
dnl  GUILE 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
dnl  option) any later version.
dnl
dnl  GUILE 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 GNU
dnl  General Public License for more details.
dnl
dnl  You should have received a copy of the GNU General Public License
dnl  along with GUILE; see the file COPYING.  If not, write to the
dnl  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
dnl  Boston, MA 02111-1307, USA.

AC_INIT(Makefile.in)
. $srcdir/GUILE-VERSION
AM_INIT_AUTOMAKE($PACKAGE, $VERSION, no-define)
AM_MAINTAINER_MODE
AM_CONFIG_HEADER(libguile/scmconfig.h)

#include qthreads stuff
#--------------------------------------------------------------------
#
# User options
#
#--------------------------------------------------------------------

AC_ARG_ENABLE(dynamic-linking,
  [  --enable-dynamic-linking  Include support for dynamic linking],,
  enable_dynamic_linking=yes)

AC_ARG_ENABLE(guile-debug,
[  --enable-guile-debug    Include internal debugging functions])
if test "$enableval" = y || test "$enableval" = yes; then
  AC_DEFINE(GUILE_DEBUG)
fi

dnl The --disable-debug used to control these two.  But now they are
dnl a required part of the distribution.
AC_DEFINE(DEBUG_EXTENSIONS)
AC_DEFINE(READER_EXTENSIONS)

#--------------------------------------------------------------------

AC_PROG_CC
AC_PROG_CPP
AM_PROG_LIBTOOL

AC_AIX
AC_ISC_POSIX
AC_MINIX

AC_C_CONST
AC_C_INLINE

AC_HEADER_STDC
AC_HEADER_DIRENT
AC_HEADER_TIME
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(libc.h limits.h malloc.h memory.h string.h regex.h rxposix.h rx/rxposix.h sys/ioctl.h sys/select.h sys/time.h sys/timeb.h sys/times.h sys/types.h sys/utime.h time.h unistd.h utime.h)
GUILE_HEADER_LIBC_WITH_UNISTD

AC_TYPE_GETGROUPS
AC_TYPE_SIGNAL
AC_TYPE_MODE_T

AC_CHECK_LIB(m, main)
AC_CHECK_FUNCS(gethostbyname)
if test $ac_cv_func_gethostbyname = no; then
    AC_CHECK_LIB(nsl, gethostbyname)
fi
AC_CHECK_FUNCS(connect)
if test $ac_cv_func_connect = no; then
    AC_CHECK_LIB(socket, connect)
fi
AC_CHECK_LIB(termcap, tgoto)
AC_CHECK_LIB(readline, readline)
AC_CHECK_FUNCS(rl_clear_signals rl_cleanup_after_signal)

AC_CACHE_CHECK([for rl_getc_function pointer in readline],
               ac_cv_var_rl_getc_function,
               [AC_TRY_LINK([
#include <stdio.h>
#include <readline/readline.h>],
                            [rl_getc_function;],
                            [ac_cv_var_rl_getc_function=yes],
                            [ac_cv_var_rl_getc_function=no])])
if test "$ac_cv_var_rl_getc_function" = "yes"; then
  AC_DEFINE(HAVE_RL_GETC_FUNCTION)
fi

if test $ac_cv_lib_readline_readline = yes -a $ac_cv_var_rl_getc_function = no; then
  echo 'Warning: libreadline is too old on your system.  Need >= 2.1.'
fi

# Checks for dynamic linking

if test "$enable_dynamic_linking" = "yes"; then

AC_CHECK_LIB(dl,dlopen)
if test "$ac_cv_lib_dl_dlopen" = "yes"; then
  AC_CHECK_FUNCS(dlopen)
  AC_DEFINE(DYNAMIC_LINKING)
else
AC_CHECK_LIB(dld,dld_link)
if test "$ac_cv_lib_dld_dld_link" = "yes"; then
  AC_DEFINE(DYNAMIC_LINKING)
else
AC_CHECK_FUNCS(shl_load)
if test "$ac_cv_func_shl_load" = "yes"; then
  AC_DEFINE(DYNAMIC_LINKING)
else
AC_CHECK_FUNCS(dlopen)
if test "$ac_cv_func_dlopen" = "yes"; then
  AC_DEFINE(DYNAMIC_LINKING)
fi
fi
fi
fi

fi

GUILE_DLSYM_USCORE

AC_CHECK_FUNCS(ctermid ftime getcwd geteuid gethostent gettimeofday lstat mkdir mknod nice readlink rename rmdir select setegid seteuid setlocale setpgid setsid sigaction strftime strptime symlink sync tcgetpgrp tcsetpgrp times uname waitpid bzero strdup system usleep)

### Some systems don't declare some functions.  On such systems, we
### need to at least provide our own K&R-style declarations.

### GUILE_FUNC_DECLARED(function, headerfile)

### Check for a declaration of FUNCTION in HEADERFILE; if it is
### not there, #define MISSING_FUNCTION_DECL.
AC_DEFUN(GUILE_FUNC_DECLARED, [
  AC_CACHE_CHECK(for $1 declaration, guile_cv_func_$1_declared,
    AC_EGREP_HEADER($1, $2,
		    guile_cv_func_$1_declared=yes, 
		    guile_cv_func_$1_declared=no))
  if test [x$guile_cv_func_]$1[_declared] = xno; then
    AC_DEFINE([MISSING_]translit($1, [a-z], [A-Z])[_DECL])
  fi
])

GUILE_FUNC_DECLARED(strptime, time.h)
GUILE_FUNC_DECLARED(bzero, string.h)
GUILE_FUNC_DECLARED(sleep, unistd.h)
GUILE_FUNC_DECLARED(usleep, unistd.h)

### On some systems usleep has no return value.  If it does have one,
### we'd like to return it; otherwise, we'll fake it.
AC_CACHE_CHECK([return type of usleep], guile_cv_func_usleep_return_type,
  [AC_EGREP_HEADER(changequote(<, >)<void[ 	]+usleep>changequote([, ]),
                   /usr/include/unistd.h,
		   [guile_cv_func_usleep_return_type=void],
		   [guile_cv_func_usleep_return_type=int])])
case "$guile_cv_func_usleep_return_type" in
  "void" )
    AC_DEFINE(USLEEP_RETURNS_VOID)
  ;;
esac

dnl <GNU-WIN32 hacks>

AC_CHECK_HEADER(sys/un.h, have_sys_un_h=1)
if test -n "$have_sys_un_h" ; then
AC_DEFINE(HAVE_UNIX_DOMAIN_SOCKETS)
fi

AC_CHECK_FUNCS(socketpair getgroups setpwent pause tzset)

dnl I don't know what this prefixing of cygwin32_ is for.
dnl scmconfig.h wasn't updated with the test results.
dnl so use AC_CHECK_FUNCS for now.

dnl how about:
dnl save confdefs.h
dnl if test $ac_cv_cigwin = yes; then
dnl   modify confdefs.h
dnl fi
dnl AC_CHECK_FUNCS...
dnl restore confdefs.h

dnl cp confdefs.h confdefs.h.bak
dnl for func in sethostent endhostent getnetent setnetent endnetent getprotoent endprotoent getservent endservent getnetbyaddr getnetbyname inet_lnaof inet_makeaddr inet_netof ; do
dnl cp confdefs.h.bak confdefs.h
dnl cat >> confdefs.h << EOF 
dnl #ifdef __CYGWIN32__
dnl #define $func cygwin32_$func
dnl #endif
dnl EOF
dnl AC_CHECK_FUNC($func)
dnl done
dnl cp confdefs.h.bak confdefs.h

AC_CHECK_FUNCS(sethostent endhostent getnetent setnetent endnetent getprotoent endprotoent getservent endservent getnetbyaddr getnetbyname inet_lnaof inet_makeaddr inet_netof)

dnl </GNU-WIN32 hacks>

AC_CACHE_CHECK([for restartable system calls], scm_cv_restarts,
	if test $ac_cv_func_sigaction = yes; then
		[AC_TRY_COMPILE([#include <signal.h>],
				[int a = SA_RESTART],
				scm_cv_restarts=yes,
				scm_cv_restarts=no)]
	else
		scm_cv_restarts=no
	fi)
if test $scm_cv_restarts = yes; then
	AC_DEFINE(HAVE_RESTARTS)
fi

if test "$ac_cv_header_regex_h" = yes ||
   test "$ac_cv_header_rxposix_h" = yes ||
   test "$ac_cv_header_rx_rxposix_h" = yes; then
  GUILE_NAMED_CHECK_FUNC(regcomp, norx, [LIBOBJS="regex-posix.o $LIBOBJS"],
  [AC_CHECK_LIB(rx, main)
   GUILE_NAMED_CHECK_FUNC(regcomp, rx, [LIBOBJS="regex-posix.o $LIBOBJS"])]
  )
  dnl The following should not be necessary, but for some reason
  dnl autoheader misses it if we don't include it!
  if test "$ac_cv_func_regcomp_norx" = yes ||
     test "$ac_cv_func_regcomp_rx" = yes; then
    AC_DEFINE(HAVE_REGCOMP)
  fi
fi

AC_REPLACE_FUNCS(inet_aton putenv strerror)

# When testing for the presence of alloca, we need to add alloca.o
# explicitly to LIBOBJS to make sure that it is translated to
# `alloca.lo' for libtool later on.  This can and should be done more cleanly.
AC_FUNC_ALLOCA
if test "$ALLOCA" = "alloca.o"; then LIBOBJS="alloca.o $LIBOBJS"; fi

AC_STRUCT_ST_RDEV
AC_STRUCT_ST_BLKSIZE

# We could use AC_STRUCT_ST_BLOCKS here, but that adds fileblocks.o to
# LIBOBJS, which we don't need.  This seems more direct. 
AC_CACHE_CHECK([for st_blocks in struct stat], ac_cv_struct_st_blocks,
[AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/stat.h>], [struct stat s; s.st_blocks;],
ac_cv_struct_st_blocks=yes, ac_cv_struct_st_blocks=no)])
if test $ac_cv_struct_st_blocks = yes; then
  AC_DEFINE(HAVE_ST_BLOCKS)
fi

AC_STRUCT_TIMEZONE
GUILE_STRUCT_UTIMBUF

#--------------------------------------------------------------------
#
# Which way does the stack grow?
#
#--------------------------------------------------------------------

AC_TRY_RUN(aux (l) unsigned long l;
	     { int x; exit (l >= ((unsigned long)&x)); }
	   main () { int q; aux((unsigned long)&q); }, 
	   AC_DEFINE(SCM_STACK_GROWS_UP),,AC_MSG_WARN(Guessing that stack grows down -- see scmconfig.h.in))

AC_CACHE_CHECK([whether floats fit in longs], guile_cv_type_float_fits_long,
    [AC_TRY_RUN([main () { exit (sizeof(float) > sizeof(long)); }],
		[guile_cv_type_float_fits_long=yes],
		[guile_cv_type_float_fits_long=no],
		[guile_cv_type_float_fits_long=guess-yes])])
case $guile_cv_type_float_fits_long in
  "yes" )
    AC_DEFINE(SCM_SINGLES)
  ;;
  "guess-yes" )
    AC_DEFINE(SCM_SINGLES)
    AC_MSG_WARN([guessing that sizeof(long) == sizeof(float)])
    AC_MSG_WARN([see SCM_SINGLES in scmconfig.h.in])
  ;;
esac


AC_MSG_CHECKING(for struct linger)
AC_CACHE_VAL(scm_cv_struct_linger,
	AC_TRY_COMPILE([
#include <sys/types.h>
#include <sys/socket.h>],
			[struct linger lgr;  lgr.l_linger = 100],
			scm_cv_struct_linger="yes",
			scm_cv_struct_linger="no"))
AC_MSG_RESULT($scm_cv_struct_linger)
if test $scm_cv_struct_linger = yes; then
	AC_DEFINE(HAVE_STRUCT_LINGER)
fi
	
#--------------------------------------------------------------------
#
# How can you violate a stdio abstraction by setting a stream's fd?
#
#--------------------------------------------------------------------

AC_MSG_CHECKING(how to set a stream file descriptor)
AC_CACHE_VAL(scm_cv_fd_setter,
	AC_TRY_COMPILE([#include <stdio.h>],
			[stdout->_file = 1],
			scm_cv_fd_setter="_file",
	AC_TRY_COMPILE([#include <stdio.h>],
			[stdout->__file = 1],
			scm_cv_fd_setter="__file",
	AC_TRY_COMPILE([#include <stdio.h>],
			[stdout->_fileno = 1],
			scm_cv_fd_setter="_fileno",
			scm_cv_fd_setter=""))))

if test "$scm_cv_fd_setter"; then
	AC_MSG_RESULT($scm_cv_fd_setter)
	AC_DEFINE_UNQUOTED(FD_SETTER, $scm_cv_fd_setter)
else
	AC_MSG_RESULT(we couldn't do it!)
fi

#--------------------------------------------------------------------
# How to find out whether a FILE structure contains buffered data.
# From Tk we have the following list:
#		_cnt:		Most UNIX systems
#		__cnt:		HPUX and SCO
#		_r:		BSD
#		readCount:	Sprite
#	Or, in GNU libc there are two fields, _gptr and _egptr, which
#	have to be compared.
#	These can also be known as _IO_read_ptr and _IO_read_end.
#--------------------------------------------------------------------

AC_MSG_CHECKING(how to get buffer char count from FILE structure)
AC_CACHE_VAL(scm_cv_struct_file_count,
	AC_TRY_COMPILE([#include <stdio.h>],
			[FILE *f = stdin; f->_cnt = 0],
			scm_cv_struct_file_count="_cnt",
	AC_TRY_COMPILE([#include <stdio.h>],
			[FILE *f = stdin; f->__cnt = 0],
			scm_cv_struct_file_count="__cnt",
	AC_TRY_COMPILE([#include <stdio.h>],
			[FILE *f = stdin; f->_r = 0],
			scm_cv_struct_file_count="_r",
	AC_TRY_COMPILE([#include <stdio.h>],
			[FILE *f = stdin; f->readCount = 0],
			scm_cv_struct_file_count="readCount",
	scm_cv_struct_file_count="")))))
if test "$scm_cv_struct_file_count"; then
	AC_MSG_RESULT($scm_cv_struct_file_count)
	AC_DEFINE_UNQUOTED(FILE_CNT_FIELD, $scm_cv_struct_file_count)
else
AC_CACHE_VAL(scm_cv_struct_file_gptr,
	AC_TRY_COMPILE([#include <stdio.h>],
			[FILE *f = stdin; f->_gptr = f->egptr;],
			scm_cv_struct_file_gptr=1,
	scm_cv_struct_file_gptr=""))
if test "$scm_cv_struct_gptr"; then
	AC_MSG_RESULT(gptr)
	AC_DEFINE_UNQUOTED(FILE_CNT_GPTR, $scm_cv_struct_file_gptr)
else
AC_CACHE_VAL(scm_cv_struct_file_readptr,
	AC_TRY_COMPILE([#include <stdio.h>],
			[FILE *f = stdin; f->_IO_read_ptr = f->_IO_read_end;],
			scm_cv_struct_file_readptr=1))
if test "$scm_cv_struct_file_readptr"; then
	AC_MSG_RESULT(read_ptr)
	AC_DEFINE_UNQUOTED(FILE_CNT_READPTR, $scm_cv_struct_file_readptr)
else
	AC_MSG_RESULT(we couldn't do it!)
fi
fi
fi

#--------------------------------------------------------------------
#
# Flags for thread support
#
#--------------------------------------------------------------------

### What thread package has the user asked for?
AC_ARG_WITH(threads, [  --with-threads          thread interface],
            , with_threads=no)

### Turn $with_threads into either the name of a threads package, like
### `qt', or `no', meaning that threads should not be supported.
AC_MSG_CHECKING(whether to support threads)
case "$with_threads" in
  "yes" | "qt" | "coop" | "")
    with_threads=qt
  ;;
  "no" )
  ;;
  * )
    AC_MSG_ERROR(invalid value for --with-threads: $with_threads)
  ;;
esac
AC_MSG_RESULT($with_threads)

## Make sure the threads package we've chosen is actually supported on
## the present platform.
case "${with_threads}" in
  "qt" )
    ## This configures the QuickThreads package, and sets or clears
    ## the THREAD_PACKAGE variable if qthreads don't configure
    ## correctly.
    QTHREADS_CONFIGURE
   ;;
esac

## If we're using threads, bring in some other parts of Guile which
## work with them.
if test "${THREAD_PACKAGE}" != "" ; then
  AC_DEFINE(USE_THREADS, 1)

  ## Include the Guile thread interface in the library...
  LIBOBJS="$LIBOBJS threads.o"

  ## ... and tell it which package to talk to.
  case "${THREAD_PACKAGE}" in
    "QT" )
      AC_DEFINE(USE_COOP_THREADS, 1)
    ;;
    * )
      AC_MSG_ERROR(invalid value for THREAD_PACKAGE: ${THREAD_PACKAGE})
    ;;
  esac

  ## Bring in scm_internal_select, if appropriate.
  if test $ac_cv_func_gettimeofday = yes &&
     test $ac_cv_func_select = yes; then
    LIBOBJS="$LIBOBJS iselect.o"
    AC_DEFINE(GUILE_ISELECT, 1)
  fi
fi

## If we're using GCC, ask for aggressive warnings.
case "$GCC" in
  yes )
    ## We had -Wstrict-prototypes in here for a bit, but Guile does too
    ## much stuff with generic function pointers for that to really be
    ## less than exasperating.
    CFLAGS="$CFLAGS -Wall -Wpointer-arith -Wmissing-prototypes" ;;
esac

AC_PROG_AWK

## If we're creating a shared library (using libtool!), then we'll
## need to generate a list of .lo files corresponding to the .o files
## given in LIBOBJS.  We'll call it LIBLOBJS.
LIBLOBJS="`echo ${LIBOBJS} | sed 's/\.o/.lo/g'`"

AC_SUBST(GUILE_MAJOR_VERSION)
AC_SUBST(GUILE_MINOR_VERSION)
AC_SUBST(GUILE_VERSION)

dnl Tell guile-config what flags guile users should link against.
GUILE_LIBS="$LDFLAGS $THREAD_LIBS_INSTALLED $LIBS"
AC_SUBST(GUILE_LIBS)

dnl timestamping the interpreter and scheme libraries:
dnl
dnl Help us notice when we're running one version of the Guile
dnl interpreter against a different version of the ice-9 Scheme code.
dnl This will definitely detect version skew due to differing
dnl snapshots and releases, but may not catch skew for the developers.
dnl Hopefully it will not detect skew when there is none; if that
dnl happens, the warnings will be useless, and we should remove this.
GUILE_STAMP="`date`"
AC_SUBST(GUILE_STAMP)

AC_SUBST(AWK)
AC_SUBST(LIBLOBJS)

AC_OUTPUT([Makefile libguile/Makefile libguile/guile-snarf libguile/versiondat.h ice-9/Makefile ice-9/version.scm qt/Makefile qt/qt.h qt/md/Makefile qt/time/Makefile guile-config/Makefile doc/Makefile], [chmod +x libguile/guile-snarf])

dnl Local Variables:
dnl comment-start: "dnl "
dnl comment-end: ""
dnl comment-start-skip: "\\bdnl\\b\\s *"
dnl End:
