AC_REVISION("$Id: configure.in,v 1.207 1998/06/17 14:01:20 grubba Exp $")
AC_INIT(interpret.c)
AC_CONFIG_HEADER(machine.h)

# We want an absolute path to the source-dir.
case "$srcdir" in
    /*)
    ;;
    *)
	oldsrcdir="$srcdir"
	srcdir="`cd \"$srcdir\";pwd`"
	AC_MSG_WARN([ Converted $oldsrcdir to $srcdir,
If this does not work, please use an absolute path to the configure script. ])
    ;;
esac

if test "x${CFLAGS-}" = x ; then
  cflags_is_set=no
else
  cflags_is_set=yes
fi

if test "x${CPPFLAGS-}" = x ; then
  cppflags_is_set=no
else
  cppflags_is_set=yes
fi

if test "x${LDFLAGS-}" = x ; then
  ldflags_is_set=no
else
  ldflags_is_set=yes
fi

for ac_site_file in $CONFIG_SITE; do
  if test -r "$ac_site_file"; then
    AC_MSG_WARN([You are using a site file to initialize configure, please
note that this can lead to problems if anything in
$ac_site_file is not correct. If Pike does not compile, or
does not run, please try this to compile Pike again:
rm ./config.cache ; CONFIG_SITE=x ./configure ; make])
  fi
done

if test ! -z "$pike_cv_prog_CC" -a ! -z "$CC" -a "$CC" != "$pike_cv_prog_CC"
then
  AC_MSG_WARN([CC is different than last time Pike was compiled,
It is probably best to delete ./config.cache before proceeding])
fi

pike_cv_prog_CC="$CC"

AC_PROG_CC
AC_ISC_POSIX
AC_MINIX

#############################################################################

LIBDIR=`(cd $srcdir/../lib ; pwd)`
BINDIR=`(cd $srcdir/../bin ; pwd)`
BUILDDIR=`pwd`
AC_SUBST(LIBDIR)
AC_SUBST(BINDIR)
AC_SUBST(BUILDDIR)

case $CC in
 *smartlink*) ;;
 *)
   CC="$BINDIR/smartlink $CC"
   ac_cv_prog_CC="$CC"
 ;;
esac

REALCC="`echo $CC| sed -e 's/.*smartlink //'`"
AC_SUBST(REALCC)

SMARTLINK="`echo $CC| sed -e 's/smartlink.*/smartlink/'`"
AC_SUBST(SMARTLINK)
export REALCC SMARTLINK



#############################################################################



AC_ARG_WITH(dynamic_modules,   [  --without-dynamic-modules        link modules statically],[],[with_dynamic_modules=yes])
AC_ARG_WITH(include-path,[  --with-include-path    A list of paths to search for include files.])
AC_ARG_WITH(lib-path,    [  --with-lib-path        A list of paths to search for libraries.])
AC_ARG_WITH(gdbm,        [  --without-gdbm         no GNU database manager support ])
AC_ARG_WITH(gmp,         [  --without-gmp          no Support bignums])
AC_ARG_WITH(readline,    [  --without-readline     no support for command line editing])
AC_ARG_WITH(debug,       [  --without-debug        disable run debugging],[],[with_debug=])
AC_ARG_WITH(rtldebug,    [  --without-rtldebug     disable run time self tests],[],[with_rtldebug=])
AC_ARG_WITH(cdebug,      [  --without-cdebug       disable -g],[],[with_cdebug=])
AC_ARG_WITH(threads,     [  --without-threads      no threads support],[],[with_threads=yes])
AC_ARG_WITH(zlib,        [  --without-zlib         no gz compression support],[],[with_zlib=yes])
AC_ARG_WITH(ssleay,      [  --without-ssleay       no support for the secure socket protocol],[],[with_ssleay=yes])
AC_ARG_WITH(mysql,       [  --without-mysql        no support for the Mysql database],[],[with_mysql=yes])
AC_ARG_WITH(dmalloc,     [  --with-dmalloc         enable memory-leak tests],[AC_DEFINE(DEBUG_MALLOC,10)],[])
AC_ARG_WITH(profiling,   [  --with-profiling       add code used to profile pike code ],[AC_DEFINE(PROFILING)],[])
AC_ARG_WITH(poll,        [  --with-poll            use poll instead of select],[AC_DEFINE(HAVE_AND_USE_POLL)],[])
AC_ARG_WITH(max-fd,      [  --with-max-fd=X        set how many filedescriptors can be used at once],[pike_cv_max_open_fd=$withval],[])
AC_ARG_WITH(oob,	 [  --with-oob             enable out-of-band data handling],[AC_DEFINE(WITH_OOB)],[])
AC_ARG_WITH(thread-trace,[  --with-trace-threads   enable individual tracing of threads],[AC_DEFINE(TRACE_THREADS)],[])
AC_ARG_WITH(compiler-trace,[  --with-compiler-trace  enable tracing of the compiler],[AC_DEFINE(YYDEBUG)],[])

#
# Allow --with(out)-debug to toggle both cdebug and rtldebug, but
# let --with(out)-{c,rtl}debug take precedence.
#
if test "x$with_cdebug" = x ; then
  with_cdebug="$with_debug"
fi

if test "x$with_rtldebug" = x ; then
  with_rtldebug="$with_debug"
fi


#
# Defaults for cdebug and rtldebug here:
#

if test "x$with_cdebug" = x ; then
  with_cdebug=yes
fi

if test "x$with_rtldebug" = x ; then
  with_rtldebug=yes
fi

if test "x$with_rtldebug" = xyes ; then
  AC_DEFINE(DEBUG)
fi

#############################################################################
AC_AIX
AC_MINIX

# We need some special hacks when running slowaris
AC_PATH_PROG(uname_prog,uname,no)
AC_MSG_CHECKING(operating system)
AC_CACHE_VAL(pike_cv_sys_os,
[
if test "$uname_prog" != "no"; then
  pike_cv_sys_os="`uname`"

  case "$pike_cv_sys_os" in
    SunOS)
      case "`uname -r`" in
        5.*) pike_cv_sys_os="Solaris";
      esac
    ;;
  esac
else
  pike_cv_sys_os="Not Solaris"
fi
])
AC_MSG_RESULT($pike_cv_sys_os)

LD='$(CC)'

case "$pike_cv_sys_os" in
  SCO*)
     case "$CFLAGS" in
       *-belf*) ;;
       *)
	  echo "Adding -belf option."
          CFLAGS="$CFLAGS -belf"
       ;;
     esac
     case "$LDFLAGS" in
       *-belf*) ;;
       *)
	  echo "Adding -belf option."
          LDFLAGS="$LDFLAGS -belf"
       ;;
     esac
  ;;
  SunOS*)
    # gcc on SunOS 4 forgets to define this:
    echo "SunOS. Adding -D__USE_FIXED_PROTOTYPES__"
    CFLAGS="$CFLAGS -D__USE_FIXED_PROTOTYPES__"
  ;;
  BSD/OS*)
    LD="$BINDIR/smartlink shlicc"
  ;;
esac

AC_SUBST(LD)

#############################################################################


# option, cache_name, variable
AC_DEFUN(AC_SYS_COMPILER_FLAG,
[
  AC_MSG_CHECKING($1)
  OLD_CFLAGS="[$]CFLAGS"
  AC_CACHE_VAL(pike_cv_option_$2,
  [
    CFLAGS="[$]OLD_CFLAGS $1"
    AC_TRY_RUN([int main(){exit(0);}],pike_cv_option_$2=yes,pike_cv_option_$2=no,pike_cv_option_$2=no)
  ])
  
  CFLAGS="[$]OLD_CFLAGS"
  
  if test x"[$]pike_cv_option_$2" = "xyes" ; then
    $3="[$]$3 $1"
    AC_MSG_RESULT(yes)
    $5
  else
    AC_MSG_RESULT(no)
    $4
  fi
])

# arch, option, cache_name, variable
AC_DEFUN(AC_SYS_CPU_COMPILER_FLAG,
[
 if test "`uname -m 2>/dev/null`" = "$1" ; then
    AC_SYS_COMPILER_FLAG($2,$3,$4)
 fi
])

# os, option, cache_name, variable
AC_DEFUN(AC_SYS_OS_COMPILER_FLAG,
[
 if test "x$pike_cv_sys_os" = "x$1" ; then
    AC_SYS_COMPILER_FLAG($2,$3,$4)
 fi
])

WARN=""
OPTIMIZE=""

if test $cflags_is_set = no; then
 if test "x${GCC-}" = xyes ; then
  WARN="-W -Wall -Wpointer-arith -Wno-unused"

  if test "x$with_cdebug" = "xno" ; then
    AC_SYS_COMPILER_FLAG(-fomit-frame-pointer,fomit_frame_pointer,OPTIMIZE)
    CFLAGS=`echo " $CFLAGS " | sed -e 's@ -g @ @g'`
  else
    :
  fi  

### Optimize for different SUN machines. If there is no working 'uname'
### no extra optimization will be done, but nothing should break either.
  case "`uname -m 2>/dev/null`" in
   sun4c)
    AC_SYS_COMPILER_FLAG(-msparclite,sparclite,OPTIMIZE) 
   ;;
   sun4m)
    AC_SYS_COMPILER_FLAG(-mv8,microsparc,OPTIMIZE)
   ;;
   sun4d)
    AC_SYS_COMPILER_FLAG(-msupersparc,supersparc,OPTIMIZE)
   ;;
   sun4u)
    AC_SYS_COMPILER_FLAG(-msupersparc,ultrasparc,OPTIMIZE)
   ;;
   i586)
    AC_SYS_COMPILER_FLAG(-mpentium,pentium,OPTIMIZE)
   ;;
   i686)
    AC_SYS_COMPILER_FLAG(-mpentiumpro,pentiumpro,OPTIMIZE,[
       AC_SYS_COMPILER_FLAG(-mpentium,pentium,OPTIMIZE)
    ])
   ;;
  esac
 else

### Non-gcc thingies. This should probably be enhanced...

  # Solaris
  AC_SYS_OS_COMPILER_FLAG(Solaris,-fast,fast,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(Solaris,-xdepend,xdepend,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(Solaris,-xs,xs,CFLAGS)
  # Causes __STDC__ to be 1 which causes several headerfiles to behave
  # differently.
  # AC_SYS_OS_COMPILER_FLAG(Solaris,-Xc,Xc,WARN)

  # HP-UX
  AC_SYS_OS_COMPILER_FLAG(HP-UX,+Odataprefetch,plus_Odataprefetch,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(HP-UX,+Ofailsafe,plus_Ofailsafe,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(HP-UX,+O3,plus_O3,OPTIMIZE)
  # Ignore warnings 530 and 414 if you turn this on.
  # AC_SYS_OS_COMPILER_FLAG(HP-UX,+w1,plus_w1,WARN)

  # OSF/1
  AC_SYS_OS_COMPILER_FLAG(OSF1,-fast,fast,OPTIMIZE)
  # The need for -ieee is not detected properly by the FPE tests below.
  # AC_SYS_OS_COMPILER_FLAG(OSF1,-ieee,ieee,CFLAGS)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-readonly_strings,readonly_strings,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-assume trusted_short_alignment,
    assume_trusted_short_alignment,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-assume aligned_objects,
    assume_aligned_objects,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-no_misalign,no_misalign,OPTIMIZE)

  if test "x$with_cdebug" = "xyes" ; then
    # FIXME:
    # Maybe should remove the -g flag if this succeeds.
    AC_SYS_OS_COMPILER_FLAG(OSF1,-g3,g3,OPTIMIZE)
    AC_SYS_OS_COMPILER_FLAG(OSF1,-trapuv,trapuv,WARN)
  fi
  AC_SYS_OS_COMPILER_FLAG(OSF1,-host,host,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-std1,std1,WARN)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-warnprotos,warnprotos,WARN)

  # IRIX
  # sopt disabled since it takes too long time on interpret.c (>45min on an O�)
  # AC_SYS_OS_COMPILER_FLAG(IRIX,-sopt,sopt,OPTIMIZE)

  # -n32 moved to smartlink so it will affekt cc as well as linker
  # this way the user can override it by setting LDFLAGS and/or CFLAGS
  # AC_SYS_OS_COMPILER_FLAG(IRIX,-n32,n32,CFLAGS)
  AC_SYS_OS_COMPILER_FLAG(IRIX,-fullwarn,fullwarn,WARN)
  if test x$with_cdebug = xyes ; then
    AC_SYS_OS_COMPILER_FLAG(IRIX,-g3,g3,OPTIMIZE)
  fi
  if test x$with_rtldebug = xyes ; then
    AC_SYS_OS_COMPILER_FLAG(IRIX,-trapuw,trapuw,WARN)
  fi

dnl
dnl This does not work with Digital UNIX cc/ld (cc thinks it is ok, ld does not)
dnl  
dnl AC_SYS_COMPILER_FLAG(-native,native,OPTIMIZE)
dnl

dnl ### Ok. This is for apcc...
case "$CC" in
  *apcc)
    AC_SYS_COMPILER_FLAG(-XO=5,apcc_opt,OPTIMIZE)
    AC_SYS_COMPILER_FLAG(-Xcallmod=2,apcc_opt_2,OPTIMIZE)
  ;;
esac

# Old cc for solaris.
#  AC_SYS_CPU_COMPILER_FLAG(sun4m,-Xpipe=micro,microsparc,OPTIMIZE)
#  AC_SYS_CPU_COMPILER_FLAG(sun4d,-Xpipe=super,supersparc,OPTIMIZE)
#  AC_SYS_CPU_COMPILER_FLAG(sun4u,-Xpipe=super,ultrasparc,OPTIMIZE)

  case "${CFLAGS-} $OPTIMIZE" in
     *-O*) ;;
     *+O*) ;;
     *-fast*) ;;
     *)
      AC_SYS_COMPILER_FLAG(-O2,O2,OPTIMIZE)
      if test "x$pike_cv_option_O2" != "xyes"; then
        AC_SYS_COMPILER_FLAG(-O,O,OPTIMIZE)
      fi
     ;;
  esac
 fi
fi



if test "$GCC" = "yes"; then
  if test $cflags_is_set = no; then
    AC_SYS_COMPILER_FLAG(-pipe, pipe, CFLAGS)
  fi
else
#
# Special hack for HP-UX stupid cc
#
  if test "$pike_cv_sys_os" = "HP-UX"; then
    AC_TRY_COMPILE([int foo(int bar);],[],[],
    [
      OLD_CC="${CC-cc}"
      OLD_CFLAGS="$CFLAGS"
      if test "$CC" = "$BINDIR/smartlink cc" -a -x /usr/bin/cc ; then
        CC="$BINDIR/smartlink /usr/bin/cc"
      fi
      CFLAGS="-Ae +Olibcalls $CFLAGS"
      AC_TRY_COMPILE([int foo(int bar);],[],[],[
        CC="$OLD_CC"
	CFLAGS="$OLD_CFLAGS"
      ])
    ])
  fi
fi

if test "$pike_cv_sys_os" = "Solaris" ; then
  AC_DEFINE(SOLARIS)
fi

if test ! -d modules/. ; then
  mkdir modules
fi

#############################################################################

## Search for some popular places where libraries may be hidden.

#############################################################################

if test x$pike_cv_sys_os != xWindows_NT ; then

echo Searching for include-file directories...
if test $cppflags_is_set = no -a x${C_INCLUDE_PATH-} = x ; then
  for d in `echo ${with_include_path} | sed 's/:/ /g'` \
    `echo $prefix | sed "s@^NONE@$ac_default_prefix@g"`/include \
    /usr/local/include /sw/local/include /usr/gnu/include /opt/gnu/include \
    /sw/gnu/include /sw/include /usr/freeware/include \
    `echo /usr/X11*/include | sort -r`
  do
    AC_MSG_CHECKING($d)
    if test -d "$d/." ; then
      AC_MSG_RESULT(added)
      CPPFLAGS="${CPPFLAGS} -I$d"
    else
      AC_MSG_RESULT(no)
    fi
  done
else
  :
fi

CPPFLAGS="${CPPFLAGS} -I$srcdir"

echo Searching for library directories...
if test $ldflags_is_set = no ; then
  for d in `echo ${with_lib_path} | sed 's/:/ /g'` \
    `echo $exec_prefix | sed "s@^NONE@$prefix/lib@g" | sed "s@^NONE@$ac_default_prefix/lib@g"` \
    /usr/local/lib /sw/local/lib /sw/lib \
    /usr/gnu/lib /opt/gnu/lib /sw/gnu/lib /usr/freeware/lib \
    `echo /usr/X11*/lib | sort -r`
  do
    AC_MSG_CHECKING($d)
    if test -d "$d/." ; then
      AC_MSG_RESULT(added)
      LDFLAGS="${LDFLAGS} -R$d -L$d"
    else
      AC_MSG_RESULT(no)
    fi
  done
else
  :
fi

echo Searching for binary directories...
for d in `echo $exec_prefix | sed "s@^NONE@$prefix@g" | sed "s@^NONE@$ac_default_prefix@g"` \
    "" /usr /usr/ccs /usr/local /sw/local /usr/gnu /opt/gnu /sw/gnu \
    /sw /usr/freeware
do
  AC_MSG_CHECKING($d/bin)
  if test -d "$d/bin/." ; then
    AC_MSG_RESULT(added)
    EXTRA_PATH="${EXTRA_PATH}:$d/bin"
  else
    AC_MSG_RESULT(no)
  fi
done
export EXTRA_PATH

else
  :   # We are running NT
fi

#############################################################################

## At this point we have at last initialized the compiler!

#############################################################################

if test ! -f confdefs.h ; then
  AC_MSG_WARN([Warning, lost confdefs.h, testing will be flawed!!
You might need to use bash instead of /bin/sh to get correct results.
To do this, please type the following at an sh prompt:
	\$ rm ./config.cache
	\$ CONFIG_SHELL=/path/to/bash
	\$ export CONFIG_SHELL
	\$ $CONFIG_SHELL ./configure
])
fi

#############################################################################

AC_DEFINE(POSIX_SOURCE)

AC_MSG_CHECKING(ansi prototype capability)
AC_CACHE_VAL(pike_cv_sys_ansi_prototypes,
[
  AC_TRY_COMPILE([int foo(int bar);],[],
  [pike_cv_sys_ansi_prototypes=yes],
  [pike_cv_sys_ansi_prototypes=no])
])

if test "$pike_cv_sys_ansi_prototypes" = "yes"; then
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no, giving up...)
  exit 1
fi


#############################################################################

AC_PROG_INSTALL
AC_PROG_CPP
AC_PROG_RANLIB
AC_SET_MAKE

AC_PATH_PROG(AR, ar, ar, $PATH:$EXTRA_PATH)
export AR
AC_SUBST(AR)

#############################################################################

# installbsd seems to be broken on some versions of AIX
#
# /grubba 1997-08-16

case "$ac_cv_path_install" in
  *installbsd*)
    AC_MSG_CHECKING(if $ac_cv_path_install works)
    AC_CACHE_VAL(pike_cv_install_works,
[
    if $ac_cv_path_install "$srcdir/configure.in" ./conftest >/dev/null 2>&1
    then
      if cmp "$srcdir/configure.in" ./conftest >/dev/null 2>&1; then
        pike_cv_install_works=yes;
      else
        pike_cv_install_works=no;
      fi
    else
      pike_cv_install_works=no;
    fi
    if test -f ./conftest; then
      rm -f ./conftest
    fi
])
    AC_MSG_RESULT($pike_cv_install_works)
    if test "$pike_cv_install_works" = "no"; then
      echo "Warning: Falling back to $ac_install_sh"
      INSTALL="$ac_install_sh"
    fi
  ;;
esac

#############################################################################

AC_MSG_CHECKING(for yacc clone that handles %pure_parser)
AC_CACHE_VAL(pike_cv_prog_working_yacc,
[
for YACC in byacc "bison -y" yacc "None"; do
  set dummy $YACC; ac_word=$2
  has_it=no
  IFS="${IFS= 	}"; ac_save_ifs="$IFS"; IFS="${IFS}:"
  for ac_dir in $PATH; do
    test -z "$ac_dir" && ac_dir=.
    if test -f $ac_dir/$ac_word; then
      has_it="yes"
      break
    fi
  done
  IFS="$ac_save_ifs"
  
  if test "$has_it" = "yes"; then
cat >conftest.y<<\EOF
%pure_parser
%{
void parse();
void yyerror(char *);
%}
%%
all: foo bar
foo: 'f' 'o' 'o' { parse(); } ;
bar: 'b' 'a' 'r' ;
%%
int context;

int yylex(YYSTYPE *yylval)
{
  switch(context++)
  {
    case 0: return 'f';
    case 1: return 'o';
    case 2: return 'o';
    case 3: return 'b';
    case 4: return 'a';
    case 5: return 'r';
    default: return 0;
  }
}

int depth=0;
void parse()
{
  int tmp;
  if(depth++>5) return;
  tmp=context;
  context=0;
  yyparse();
  context=tmp;
}
int errors=0;
void yyerror(char *fel) { errors++; }
int main()
{
  parse();
  exit(errors);
}
EOF
    if $YACC -d conftest.y 1>&5 2>&5; then
      AC_TRY_RUN([
#include "y.tab.c"
],pure_parser_works=yes,pure_parser_works=no,pure_parser_works=no)
      if test "$pure_parser_works" = "yes"; then
        break
      fi
    fi
  fi
done
pike_cv_prog_working_yacc="$YACC"
])

YACC="$pike_cv_prog_working_yacc"

if test "x$YACC" = "xNone" ; then
  AC_MSG_RESULT('Get bison if you want to do "make export"')
  YACC="bison -y"
else
  AC_MSG_RESULT($YACC)
fi

#############################################################################

## At this point we have found all binaries we are going to use.

#############################################################################

AC_C_CONST

# The above doesn't find IBM C's buggy const.

if test "$ac_cv_c_const" = "yes"; then
  AC_MSG_CHECKING(if const really does work)
  AC_CACHE_VAL(pike_cv_c_really_const,[
    AC_TRY_COMPILE([
const int foo(int bar) { return bar; }
    ], [
      return bar(0);
    ], [
       pike_cv_c_really_const=yes
    ], [
       pike_cv_c_really_const=no
    ])
  ])

  AC_MSG_RESULT($pike_cv_c_really_const)
  if test "x$pike_cv_c_really_const" = xno ; then
    AC_DEFINE(const, [])
  else
    :
  fi
fi

#############################################################################

AC_HEADER_TIME
AC_HEADER_STDC
AC_CHECK_HEADERS(sys/rusage.h time.h sys/time.h sys/types.h unistd.h stdlib.h \
memory.h values.h string.h strings.h fcntl.h sys/filio.h sys/sockio.h crypt.h \
locale.h sys/resource.h sys/select.h sys/mman.h setjmp.h limits.h pthread.h \
thread.h dlfcn.h dld.h sys/times.h sched.h sys/procfs.h sys/param.h winsock.h \
sys/ioct.h sys/socket.h malloc.h netinet/in.h sys/wait.h winbase.h grp.h \
pwd.h passwd.h group.h winsock2.h signal.h sys/file.h poll.h sys/poll.h \
socket.h ieeefp.h fp_class.h)

AC_CHECK_SIZEOF(char *,4)
AC_CHECK_SIZEOF(long,4)
AC_CHECK_SIZEOF(int,4)
AC_CHECK_SIZEOF(short,2)
AC_CHECK_SIZEOF(float,4)
AC_CHECK_SIZEOF(double,8)

AC_TYPE_SIZE_T
AC_TYPE_PID_T
AC_TYPE_UID_T
AC_TYPE_SIGNAL
AC_CHECK_TYPE(time_t,INT32)

dnl AC_CHECK_LIB(PW, alloca)

AC_CHECK_LIB(m, floor)

AC_CHECK_LIB(socket, socket)

if test x$pike_cv_sys_os = xWindows_NT ; then
  LIBS="-lkernel32  $LIBS"
fi

AC_CHECK_LIB(nsl, gethostbyname)

AC_CHECK_LIB(dl, dlopen)
AC_CHECK_LIB(dld, dld_link)


if test "${pike_cv_sys_os}" = "Linux"; then
  if test "${ac_cv_lib_m_floor}" = "no"; then
    AC_MSG_WARN(I will compensate for this by adding -lc -lm)
    LIBS="${LIBS} -lc -lm"
  fi

  if test "$ac_cv_header_sched_h" = "yes"; then
    AC_MSG_CHECKING(if sched.h has a typo)
    AC_CACHE_VAL(pike_cv_broken_sched_h, [
      AC_TRY_COMPILE([
#include <sched.h>
      ],[ ],[ pike_cv_broken_shed_h=no ], [
        OLDCPPFLAGS="$CPPFLAGS"
	CPPFLAGS="$CPPFLAGS -D_P=__P"
	AC_TRY_COMPILE([
#include <sched.h>
        ],[ ],[ pike_cv_broken_sched_h=yes ],[
          pike_cv_broken_sched_h="very broken"
        ])
	CPPFLAGS="$OLDCPPFLAGS"
      ])
    ])
    AC_MSG_RESULT($pike_cv_broken_sched_h)
    if test "$pike_cv_broken_sched_h" = "yes"; then
      AC_MSG_WARN(Applying workaround)
      CPPFLAGS="$CPPFLAGS -D_P=__P"
    fi
  fi
fi

########################################################################

if test x$with_threads = xyes ; then
  OLDLIBS="$LIBS"

if test "x$pike_cv_sys_os" = "xWindows_NT" ; then
  # NT has threads...

  AC_DEFINE(_REENTRANT)
  AC_DEFINE(_THREAD_SAFE)
  AC_DEFINE(_UNIX_THREADS)
  AC_DEFINE(NT_THREADS)

else

  AC_MSG_CHECKING(unix threads)
  case "x$pike_cv_sys_os" in
    *)
      LIBS="${LIBS} -lthread"
    ;;
  esac

  AC_CACHE_VAL(pike_cv_unix_threads, [
    AC_TRY_RUN([
#define _REENTRANT
#define _THREAD_SAFE
#define _MIT_POSIX_THREADS 1
#include <thread.h>

void *foo(void *bar) { thr_exit(0); return 0; }
mutex_t tmp;
thread_t gazonk;

int main()
{
  void *sune;
  mutex_lock(& tmp);
  thr_create(0,0,foo,0,0,&gazonk);
  thr_join(gazonk,0,&sune);
  exit(0);
}
    ],pike_cv_unix_threads=yes,pike_cv_unix_threads=no,pike_cv_unix_threads=no)
  ])

  if test x$pike_cv_unix_threads = xyes ; then
    AC_MSG_RESULT(yes)
    AC_DEFINE(_REENTRANT)
    AC_DEFINE(_THREAD_SAFE)
    AC_DEFINE(_UNIX_THREADS)
  else
    AC_MSG_RESULT(no)

    # Test for POSIX threads instead

    case "x$pike_cv_sys_os" in
      xOSF1)
        LIBS="${OLDLIBS} -lpthread -lmach -lexc -lc"
      ;;
 
      xBSD/OS*)
        LIBS="${OLDLIBS}"
      ;;

      xFreeBSD*)      
	# Threads on FreeBSD earlier than 1998-06-07 are broken.
        AC_MSG_CHECKING(if this version of FreeBSD may have working threads)
	AC_CACHE_VAL(pike_cv_freebsd_threads, [
	  AC_TRY_RUN([
#include <osreldate.h>

int main(int argc, char **argv)
{
  /* FreeBSD before 226001 and 300000 - 300002 have broken threads.
   * Some instances of 300003 may have broken threads too.
   */
  if (__FreeBSD_version < 226001)
    return(1);
  if (__FreeBSD_version < 300000)
    return(0);
  if (__FreeBSD_version < 300003)
    return(1);
  return(0);
}
          ], pike_cv_freebsd_threads=yes, pike_cv_freebsd_threads=no,
	  pike_cv_freebsd_threads=yes)
	])
	if test "x$pike_cv_freebsd_threads" = "xno"; then
	  AC_MSG_RESULT(no)
	else
	  AC_MSG_RESULT(yes)
          LIBS="${OLDLIBS} -pthread"
	fi
      ;;

      *)
        LIBS="${OLDLIBS} -lpthread"
      ;;
    esac

    AC_CHECK_FUNCS(pthread_init)
    AC_CHECK_FUNCS(pthread_mutexattr_init)

    AC_MSG_CHECKING(posix threads)

    AC_CACHE_VAL(pike_cv_posix_threads, [
      AC_TRY_RUN([
#define _REENTRANT
#define _THREAD_SAFE
#define _MIT_POSIX_THREADS 1

#include <pthread.h>

void *foo(void *bar) { pthread_exit(0); return 0; }
pthread_mutexattr_t attr;
pthread_mutex_t tmp;
pthread_t gazonk;

int main()
{
  void *sune;
#ifdef HAVE_PTHREAD_INIT
  pthread_init();
#endif /* HAVE_PTHREAD_INIT */
#ifdef HAVE_PTHREAD_MUTEXATTR_INIT
  pthread_mutexattr_init(&attr);
#else
  memset(&attr, 0, sizeof(attr));
#endif /* HAVE_PTHREAD_MUTEXATTR_INIT */
  pthread_mutex_init(&tmp, &attr);
  pthread_mutex_lock(&tmp);
  pthread_create(&gazonk,0,foo,0);
  pthread_join(gazonk,&sune);
  exit(0);
}
      ],pike_cv_posix_threads=yes,pike_cv_posix_threads=no,pike_cv_posix_threads=no)
    ])

    if test $pike_cv_posix_threads = yes ; then
      AC_MSG_RESULT(yes)
      AC_DEFINE(_REENTRANT)
      AC_DEFINE(_THREAD_SAFE)
      AC_DEFINE(_MIT_POSIX_THREADS,1)

      AC_MSG_CHECKING(for pthread_attr_setstacksize)
      AC_CACHE_VAL(pike_cv_have_pthread_attr_setstacksize,[
        AC_TRY_LINK([#include <pthread.h>
          void foo(pthread_attr_t*x) { pthread_attr_setstacksize(x, 1024*1024); }
        ],[],[
	  pike_cv_have_pthread_attr_setstacksize=yes
	],[
	  pike_cv_have_pthread_attr_setstacksize=no
	])
      ])
      AC_MSG_RESULT($pike_cv_have_pthread_attr_setstacksize)
      if test x$pike_cv_have_pthread_attr_setstacksize = xyes ; then
        AC_DEFINE(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
      else
         :
      fi

      AC_MSG_CHECKING(for pthread_yield)
      AC_CACHE_VAL(pike_cv_have_pthread_yield,[
        AC_TRY_LINK([#include <pthread.h>
          void foo(void) { pthread_yield(); }
        ],[],[
	  pike_cv_have_pthread_yield=yes
	],[
	  pike_cv_have_pthread_yield=no
	])
      ])
      AC_MSG_RESULT($pike_cv_have_pthread_yield)
      if test x$pike_cv_have_pthread_yield = xyes ; then
        AC_DEFINE(HAVE_PTHREAD_YIELD)
      else
         :
      fi

      AC_MSG_CHECKING(for pthread_cond_init)
      AC_CACHE_VAL(pike_cv_have_pthread_cond_init,[
        AC_TRY_LINK([#include <pthread.h>
          void foo(pthread_cond_t*x, pthread_condattr_t*y) { pthread_cond_init(x,y); }
        ],[],[
	  pike_cv_have_pthread_cond_init=yes
	],[
	  pike_cv_have_pthread_cond_init=no
	])
      ])
      AC_MSG_RESULT($pike_cv_have_pthread_cond_init)
      if test x$pike_cv_have_pthread_cond_init = xyes ; then
        AC_DEFINE(HAVE_PTHREAD_COND_INIT)
      else
         :
      fi

      AC_MSG_CHECKING(if pthread_condattr_default is defined)
      AC_CACHE_VAL(pike_cv_have_pthread_condattr_default,[
        AC_TRY_LINK([#include <pthread.h>
          void foo(pthread_cond_t *bar) { pthread_cond_init(bar, pthread_condattr_default); }
        ],[],[
	  pike_cv_have_pthread_condattr_default=yes
	],[
	  pike_cv_have_pthread_condattr_default=no
	])
      ])

      AC_MSG_RESULT($pike_cv_have_pthread_condattr_default)

      if test x$pike_cv_have_pthread_condattr_default = xyes ; then
        AC_DEFINE(HAVE_PTHREAD_CONDATTR_DEFAULT)
      else
        :
      fi
    else
      AC_MSG_RESULT(no)
      LIBS="$OLDLIBS"

      # Now we've sunk really low -- test for proprietary threads

      # SGI old-style shared group processes
      # AC_CHECK_FUNCS(sproc oserror)
      # 
      # AC_MSG_CHECKING(Fallback to sproc)
      # 
      # if test x$ac_cv_func_sproc$ac_cv_func_oserror = xyesyes; then
      #   AC_MSG_RESULT(yes)
      #   AC_DEFINE(_REENTRANT)
      #   AC_DEFINE(_THREAD_SAFE)
      #   AC_DEFINE(_SGI_SPROC_THREADS)
      #   AC_DEFINE(_SGI_MP_SOURCE)
      # else
      #   AC_MSG_RESULT(no)
      #   :
      # fi
    fi
  fi
fi  # NT
else
  # Without threads
  :
fi

########################################################################

AC_FUNC_MEMCMP

if test "${OLD_LIBOBJS}" = "${LIBOBJS}" ; then
  AC_DEFINE(HAVE_MEMCMP)
fi

LIBOBJS="${OLD_LIBOBJS}"

AC_FUNC_MMAP

AC_CHECK_FUNCS( \
 _crypt \
 poll \
 bcopy \
 bzero \
 clock \
 crypt \
 fchmod \
 gethrtime \
 gethrvtime \
 getenv \
 getrlimit \
 getrusage \
 gettimeofday \
 index \
 localtime \
 memchr \
 memcpy \
 memset \
 mktime \
 perror \
 rindex \
 setlocale \
 setrlimit \
 sigaction \
 sigblock \
 sigprocmask \
 sigvec \
 strcasecmp \
 strchr \
 strcspn \
 strerror \
 strrchr \
 strtod \
 strtok \
 strtol \
 time \
 times \
 vfprintf \
 vsprintf \
 vsnprintf \
 wait3 \
 wait4 \
 waitpid \
 munmap \
 dld_link \
 dld_get_func \
 pipe \
 strdup \
 kill \
 alarm \
 fork \
 fork1 \
 flock \
 lockf \
 setuid getuid seteuid geteuid \
 setgid getgid setegid getegid \
 getpwent getpwnam getpwuid \
 getgrent getgrnam \
 initgroups setgroups \
 socketpair \
 fpclass \
 fp_class_d \
 isinf \
 isnan \
 iszero \
 finite \
 signbit \
 nice \
)

if test x$pike_cv_sys_os = xWindows_NT ; then
 AC_DEFINE(HAVE_LOADLIBRARY)
 AC_DEFINE(HAVE_FREELIBRARY)
 AC_DEFINE(HAVE_GETPROCADDRESS)
 AC_DEFINE(DL_EXPORT, _dlspec(export))
else
 AC_DEFINE(DL_EXPORT, [])
fi

if test $ac_cv_func_crypt$ac_cv_func__crypt = nono ; then
  AC_CHECK_LIB(crypt,crypt,[LIBS="${LIBS} -lcrypt" ; AC_DEFINE(HAVE_CRYPT)],
[
  if test -f "$srcdir/crypt.c"; then
    AC_MSG_RESULT(Using crypt.c for BSD style crypt())
    EXTRA_OBJS="${EXTRA_OBJS} crypt.o"
    AC_DEFINE(HAVE_CRYPT)
  else
    :
  fi
])

fi


#############################################################################

# FreeBSD 3.0 has broken F_SETFD when running with threads.

AC_MSG_CHECKING(whether F_SETFD exists and works)

AC_CACHE_VAL(pike_cv_broken_f_setfd, [
  AC_TRY_RUN([
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#include <sys/stat.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif /* HAVE_FCNTL_H */
#include <stdio.h>
#ifndef FD_CLOEXEC
#define FD_CLOEXEC 1
#endif /* FD_CLOEXEC */
int main(int argc, char **argv)
{
  int fd = 0;
  if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
    exit(1);
  }
  if (fcntl(fd, F_GETFD) != FD_CLOEXEC) {
    exit(1);
  }
  exit(0);
}
], pike_cv_broken_f_setfd=no, pike_cv_broken_f_setfd=yes,
   pike_cv_broken_f_setfd=no)
])

if test "x$pike_cv_broken_f_setfd" = "xyes"; then
  AC_MSG_RESULT(no)
  AC_DEFINE(HAVE_BROKEN_F_SETFD)
else
  AC_MSG_RESULT(yes)
fi

#############################################################################

AC_STRUCT_TM

#############################################################################

AC_MSG_CHECKING(extern int timezone)

AC_CACHE_VAL(pike_cv_has_external_timezone,[
AC_TRY_LINK([
#include <time.h>
],[
  int _tz;
  _tz=timezone;
  timezone=_tz;
],pike_cv_has_external_timezone=yes,pike_cv_has_external_timezone=no)
])

if test "$pike_cv_has_external_timezone" = "yes"; then
  AC_DEFINE(HAVE_EXTERNAL_TIMEZONE)
fi

AC_MSG_RESULT($pike_cv_has_external_timezone)

#############################################################################

# No test for this yet...
AC_DEFINE(HAVE_STRUCT_TIMEVAL)

#############################################################################

if test "x$ac_cv_func_strtol" = "xyes"; then
  AC_MSG_CHECKING(if strtol works for large hexadecimal constants)
  AC_CACHE_VAL(pike_cv_func_strtol_works, [
    AC_TRY_RUN([
#include <stdlib.h>
int main(int argc, char **argv)
{
  /* On some OS's strtol() rounds this to 0x7fffffff. */
  return(strtol("0xffffffff", NULL, 0) != 0xffffffff);
}
    ], pike_cv_func_strtol_works=yes,
       pike_cv_func_strtol_works=no,
       pike_cv_func_strtol_works=no)
  ])
  AC_MSG_RESULT($pike_cv_func_strtol_works)
  if test "x$pike_cv_func_strtol_works" = "xyes"; then
    AC_DEFINE(HAVE_WORKING_STRTOL)
  else :; fi
else :; fi

#############################################################################

AC_MSG_CHECKING(if gettimeofday takes two arguments)
AC_CACHE_VAL(pike_cv_func_gettimeofday_has_two_args,
[
AC_TRY_RUN([
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  if HAVE_TIME_H
#   include <time.h>
#  endif
# endif
#endif

#ifndef HAVE_STRUCT_TIMEVAL
struct timeval
{
  long tv_sec;
  long tv_usec;
};
#endif

int main() {
  struct timeval tv;
  gettimeofday(&tv,(void *)0);
  exit(0);
}
],
pike_cv_func_gettimeofday_has_two_args=yes,
pike_cv_func_gettimeofday_has_two_args=no,
pike_cv_func_gettimeofday_has_two_args=no)])

if test $pike_cv_func_gettimeofday_has_two_args = yes; then
  AC_DEFINE(GETTIMEOFDAY_TAKES_TWO_ARGS)
fi

AC_MSG_RESULT($pike_cv_func_gettimeofday_has_two_args)

AC_MSG_CHECKING(if struct tm has tm_gmtoff)

AC_CACHE_VAL(pike_cv_struct_tm_has_gmtoff,[
AC_TRY_COMPILE([
#ifdef TM_IN_SYS_TIME
#include <sys/time.h>
#endif
#include <time.h>
#else
],[
  int _tz;
  struct tm foo;
  _tz=foo->tm_gmtoff;
],pike_cv_struct_tm_has_gmtoff=yes,pike_cv_struct_tm_has_gmtoff=no)
])

if test "$pike_cv_struct_tm_has_gmtoff" = "yes"; then
  AC_DEFINE(STRUCT_TM_HAS_GMTOFF)
fi

AC_MSG_RESULT($pike_cv_struct_tm_has_gmtoff)

AC_MSG_CHECKING(for USHRT_MAX definition)

AC_CACHE_VAL(pike_cv_ushrt_max_is_defined,[
AC_EGREP_CPP(yes,[
#include <limits.h>
#ifdef USHRT_MAX
yes
#endif
],pike_cv_ushrt_max_is_defined=yes,pike_cv_ushrt_max_is_defined=no)
])

if test "$pike_cv_ushrt_max_is_defined" = "no" ; then
  AC_DEFINE(USHRT_MAX,65535)
fi

AC_MSG_RESULT($pike_cv_ushrt_max_is_defined)

define(MY_CHECK_HEADERS,
[
 AC_MSG_CHECKING(for $1 declaration)
 AC_CACHE_VAL(pike_cv_decl_$1,
 [
   pike_cv_decl_$1=nonexistant
   for a in $2
   do
     AC_EGREP_HEADER($1,$a,[ pike_cv_decl_$1=existant; break ])
   done
 ])
 AC_MSG_RESULT($pike_cv_decl_$1)
 if test "$pike_cv_decl_$1" = nonexistant; then
   AC_DEFINE(translit($1,[a-z],[A-Z])[]_DECL_MISSING)
 fi
])

MY_CHECK_HEADERS(strchr,string.h unistd.h stdlib.h)
MY_CHECK_HEADERS(malloc,memory.h unistd.h stdlib.h)
MY_CHECK_HEADERS(getpeername,sys/socket.h sys/socketvar.h sys/socketio.h winsock.h winsock2.h)
MY_CHECK_HEADERS(popen,stdio.h unistd.h)
MY_CHECK_HEADERS(getenv,unistd.h stdlib.h)
MY_CHECK_HEADERS(gethostname,unistd.h netdb.h winsock.h winsock2.h)

AC_ALLOCA

AC_C_CONST
AC_C_INLINE

AC_REPLACE_FUNCS(ualarm)

case "$LIBOBJS" in
   *ualarm.o*) ;;
  *)
    AC_DEFINE(HAVE_UALARM)
    AC_MSG_CHECKING(no of arguments to ualarm)
    AC_CACHE_VAL(pike_cv_ualarm_takes_two_args,
    [
      AC_TRY_COMPILE([
      #include <unistd.h>
      ],[
      ualarm(0,0);
      ],pike_cv_ualarm_takes_two_args=yes,pike_cv_ualarm_takes_two_args=no)

    ])
    if test "$pike_cv_ualarm_takes_two_args" = yes; then
       AC_MSG_RESULT(2)
       AC_DEFINE(UALARM_TAKES_TWO_ARGS)
    else
       AC_MSG_RESULT(1)
    fi
  ;;
esac

OLD_LIBOBJS="${LIBOBJS}"

########################################################################
dnl AC_MSG_CHECKING(stack direction)
dnl AC_CACHE_VAL(pike_cv_hardware_stack_direction,
dnl [
dnl AC_TRY_RUN([
dnl static int find_stack_direction ()
dnl {
dnl   static char	*addr = NULL;
dnl   char dummy;
dnl   if (addr == NULL)
dnl   {
dnl       addr = &dummy;
dnl       return find_stack_direction();
dnl   }
dnl   else
dnl   {
dnl     if (&dummy > addr) return 1; else return -1;
dnl   }
dnl }
dnl 
dnl int main() { exit( find_stack_direction() < 0); }
dnl ],pike_cv_hardware_stack_direction=up,pike_cv_hardware_stack_direction=down)
dnl ])
dnl 
dnl AC_MSG_RESULT($pike_cv_hardware_stack_direction)
dnl if test x$pike_cv_hardware_stack_direction = up ; then
dnl   AC_DEFINE(STACK_DIRECTION, 1)
dnl else
dnl   AC_DEFINE(STACK_DIRECTION, -1)
dnl fi

########################################################################

AC_MSG_CHECKING(byteorder)
AC_CACHE_VAL(pike_cv_hardware_byteorder,
[
AC_TRY_RUN([
#include <stdio.h>

int main()
{
#if SIZEOF_INT == 4
#define T int
#else
#define T long
#endif
  FILE *f;
  T a;
  int e;
  for(e=0;e<sizeof(T);e++)
    ((char *)&a)[e]=(e+1) % 10;

  f=fopen("conftest.out.2","w");
  for(e=0;e<sizeof(T);e++)
  {
    fprintf(f,"%d",(int)(a & 255));
    a>>=8;
  }
  fprintf(f,"\n");
  fclose(f);
  
  return 0;
}],
pike_cv_hardware_byteorder=`cat conftest.out.2`
,
pike_cv_hardware_byteorder=0
,
pike_cv_hardware_byteorder=0
)])

AC_MSG_RESULT($pike_cv_hardware_byteorder)
AC_DEFINE_UNQUOTED(BYTEORDER,$pike_cv_hardware_byteorder)

dnl#####################################################################

define(MY_CHECK_FUNCTION,[
AC_MSG_CHECKING(for working $1)
AC_CACHE_VAL(pike_cv_func_$1,[
AC_TRY_RUN([$2],pike_cv_func_$1=yes,pike_cv_func_$1=no,pike_cv_func_$1=no)
])
AC_MSG_RESULT([$]pike_cv_func_$1)
if test [$]pike_cv_func_$1 = yes; then
AC_DEFINE(translit(HAVE_$1,[a-z],[A-Z]))
fi])

########################################################################

MY_CHECK_FUNCTION(dlopen,
[
#include <dlfcn.h>
int main()
{
  dlopen(0, 0);
  exit(0);
}])


MY_CHECK_FUNCTION(ldexp,
[
#include <math.h>
int main()
{
  double x;
  int p;

  for(x=0.0;x<2.0;x+=1.0/64.0)
    for(p=-6;p<7;p++)
      if(ldexp(x,p) != x*pow(2.0, (double)p))
	exit(1);
    exit(0);
}])

########################################################################

MY_CHECK_FUNCTION(frexp,
[
#include <math.h>
int main()
{
  double x,y;
  int p,e;

  x=frexp(1.0, &e);
  if(x * pow(2.0,(double)e) != 1.0) exit(3)

  p=-7;
  for(x=3.0/4.0*pow(2.0,(double)p);x<50000.0;x*=2.0,p++)
  {
    if(frexp(x, &e)!=3.0/4.0) exit(1);
    if(e!=p) exit(2);
  }
  exit(0);
}])

#############################################################################
MY_CHECK_FUNCTION(GetSystemTimeAsFileTime,
[
#include <windows.h>
#include <winbase.h>

int main()
{
  FILETIME f;
  GetSystemTimeAsFileTime(&f);
  exit(0);
}
])
#############################################################################
MY_CHECK_FUNCTION(gethostname,
[
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_WINSOCK_H
#include <winsock.h>
#endif

int main(int argc, char **argv)
{
  char buffer[10000];
  char *x=gethostname(buffer,sizeof(buffer));
  exit(0);
}
])
#############################################################################
MY_CHECK_FUNCTION(strcoll,
[
#include <string.h>
#include <locale.h>
int main()
{
  setlocale(LC_CTYPE, "ISO-8859-1");
  exit(strcoll("abc", "def") >= 0 ||
       strcoll("ABC", "DEF") >= 0 ||
       strcoll("ABC", "ABCD") >= 0 ||
       strcoll("ABCD", "ABC") <= 0 ||
       strcoll("*^#", "*^#") != 0 ||
       strcoll("123", "456") >= 0);
}
])

########################################################################

MY_CHECK_FUNCTION(memmem,
[
#include <string.h>
char *a="foo bar gazonk";
char *b="foo";
char *c="bar";
char *d="gazonk";
int main()
{
  if(memmem(b,strlen(b),a,strlen(a))!=a ||
     memmem(c,strlen(c),a,strlen(a))!=(a+4) ||
     memmem(d,strlen(d),a,strlen(a))!=(a+8) ||
     memmem(d,0,a,strlen(a))!=a ||
     memmem(d,strlen(d)+1,a,strlen(a))!=0)
    exit(1);
  exit(0);
}
])

########################################################################

MY_CHECK_FUNCTION(memmove,
[
#include <string.h>
char buf[100];
int main()
{
  strcpy(buf,"foo bar gazonk elefantsnabel.");
  if(strcmp(buf,"foo bar gazonk elefantsnabel.")) exit(1);
  memmove(buf,buf+1,7);
  if(strcmp(buf,"oo bar  gazonk elefantsnabel.")) exit(1);
  memmove(buf+1,buf+1,9);
  if(strcmp(buf,"oo bar  gazonk elefantsnabel.")) exit(1);
  memmove(buf+1,buf,11);
  if(strcmp(buf,"ooo bar  gaznk elefantsnabel.")) exit(1);
  exit(0);
}
])

########################################################################

MY_CHECK_FUNCTION(isspace,
[
#include <ctype.h>
int main()
{
  if(isspace(' ')) exit(0);
  exit(1);
}
])

#############################################################################

AC_MSG_CHECKING(if we can declare environ)
AC_CACHE_VAL(pike_cv_declare_environ,[
AC_TRY_COMPILE([
#include <stdlib.h>
],[
  extern char **environ;
  exit(0);
],pike_cv_declare_environ=yes,pike_cv_declare_environ=no,pike_cv_declare_environ=yes)
])

AC_MSG_RESULT($pike_cv_declare_environ)
if test "x$pike_cv_declare_environ" = xyes ; then
  AC_DEFINE(DECLARE_ENVIRON)
fi

#############################################################################

AC_MSG_CHECKING([for union wait (old BSD convention)])
AC_CACHE_VAL(pike_cv_union_wait, [ AC_TRY_COMPILE([
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif /* HAVE_SYS_WAIT_H */
], [
  union wait status;
  exit(0);
], pike_cv_union_wait=yes, pike_cv_union_wait=no)
])

AC_MSG_RESULT($pike_cv_union_wait)
if test "x$pike_cv_union_wait" = "xyes" ; then
  AC_MSG_CHECKING([whether union wait is really needed])
  AC_CACHE_VAL(pike_cv_union_wait_needed, [ AC_TRY_COMPILE([
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif /* HAVE_SYS_WAIT_H */
], [
  int status;
  return WIFEXITED(status) || 0;
], pike_cv_union_wait_needed=no, pike_cv_union_wait_needed=yes)
])
  if test "x$pike_cv_union_wait_needed" = "xyes" ; then
    AC_DEFINE(HAVE_UNION_WAIT)
  else :; fi
else :; fi

#############################################################################

AC_MSG_CHECKING(how to extract an unsigned char)
AC_CACHE_VAL(pike_cv_method_extract_uchar,
[
AC_TRY_RUN([
int main()
{
  char i,*p;
  i=-10;
  p=&i;
  if(*(unsigned char *)(p)!= 0x100 - 10) exit(1);
  exit(0);
}
],pike_cv_method_extract_uchar=by_cast,
pike_cv_method_extract_uchar=not_by_cast,
pike_cv_method_extract_uchar=not_by_cast)])

if test "$pike_cv_method_extract_uchar" = by_cast; then
  AC_MSG_RESULT(by cast)
  AC_DEFINE(EXTRACT_UCHAR_BY_CAST)
else
  AC_MSG_RESULT(not by cast)
fi

########################################################################

AC_MSG_CHECKING(how to extract a signed char)
AC_CACHE_VAL(pike_cv_method_extract_char,
[
AC_TRY_RUN([
int main()
{
  char i,*p;
  i=-10;
  p=&i;
  if(*(signed char *)(p)!= -10) exit(1);
  exit(0);
}
],pike_cv_method_extract_char=by_cast,
pike_cv_method_extract_char=not_by_cast,
pike_cv_method_extract_char=not_by_cast)])

if test "$pike_cv_method_extract_char" = by_cast; then
  AC_MSG_RESULT(by cast)
  AC_DEFINE(EXTRACT_CHAR_BY_CAST)
else
  AC_MSG_RESULT(not by cast)
fi

#############################################################################

AC_MSG_CHECKING(if float conversion can cause SIGFPE)
AC_CACHE_VAL(pike_cv_sys_idiot_sigfpe,
[
 AC_TRY_RUN([
#include <math.h>

float foo_float = 0.0;

int main(int argc, char **argv)
{
  float x=2.0, y=-128.0;
  x = pow(2.0, -128.0);
  foo_float = x;
  if (x == 2.0) exit(1);
  x = pow(2.0, 1024.0);
  foo_float = x;
  if(x == 2.0) exit(1);
  exit(0);
}
],pike_cv_sys_idiot_sigfpe=no,pike_cv_sys_idiot_sigfpe=yes)
])

if test $pike_cv_sys_idiot_sigfpe = yes ; then
  AC_MSG_RESULT(yes)

  IEEE_FLAG=no
  if test "x${GCC-}" = xyes ; then
    AC_SYS_COMPILER_FLAG(-mieee, mieee, CFLAGS,[],IEEE_FLAG=yes)
  else
    AC_SYS_COMPILER_FLAG(-ieee, ieee, CFLAGS,[],IEEE_FLAG=yes)
  fi
  TEST_IGNORE_SIGFPE=yes

  if test $IEEE_FLAG = yes; then
    AC_MSG_CHECKING(if float conversion is still broken)
    AC_CACHE_VAL(pike_cv_sys_idiot_sigfpe_with_ieee,
[
 OLD_CFLAGS="$CFLAGS"
 CFLAGS="$CFLAGS $OPTIMIZE"
 AC_TRY_RUN([
#include <math.h>

float foo_float = 0.0;

int main(int argc, char **argv)
{
  float x=2.0, y=-128.0;
  x = pow(2.0, -128.0);
  foo_float = x;
  if (x == 2.0) exit(1);
  x = pow(2.0, 1024.0);
  foo_float = x;
  if(x == 2.0) exit(1);
  exit(0);
}
],pike_cv_sys_idiot_sigfpe_with_ieee=no,pike_cv_sys_idiot_sigfpe_with_ieee=yes)
  CFLAGS="$OLD_CFLAGS"
])
   
  if test $pike_cv_sys_idiot_sigfpe_with_ieee = yes ; then
    AC_MSG_RESULT(yes)
  else
    AC_MSG_RESULT(no)
    TEST_IGNORE_SIGFPE=no
  fi
fi # IEEE_FLAG

if test $TEST_IGNORE_SIGFPE = yes; then
  AC_MSG_CHECKING(if ignoring SIGFPE helps)
  AC_CACHE_VAL(pike_cv_sys_ignore_sigfpe,
[
 OLD_CFLAGS="$CFLAGS"
 CFLAGS="$CFLAGS $OPTIMIZE"
AC_TRY_RUN([
#include <signal.h>
#include <math.h>
int counter=0;
RETSIGTYPE func(void)
{
 if(counter++>10) exit(7);
 signal(SIGFPE,func);
}

float foo_float = 0.0;

int main(int argc, char **argv)
{
  float x=2.0, y=-128.0;
  signal(SIGFPE,func);
  x = pow(2.0, -128.0);
  foo_float = x;
  if (x == 2.0) exit(1);
  x = pow(2.0, 1024.0);
  foo_float = x;
  if(x == 2.0) exit(1);
  exit(0);
}
],pike_cv_sys_ignore_sigfpe=yes,pike_cv_sys_ignore_sigfpe=no)
  CFLAGS="$OLD_CFLAGS"
])

if test "$pike_cv_sys_ignore_sigfpe" = yes; then
 AC_MSG_RESULT(yes)
 AC_DEFINE(IGNORE_SIGFPE)
else
 AC_MSG_RESULT(no)
fi
fi # TEST_IGNORE_SIGFPE

else
 AC_MSG_RESULT(no)
fi # pike_cv_sys_idiot_sigfpe

#############################################################################

define(float_check, [[
#include <math.h>
#define TY $1
#define TEST_$1
#define TEST_$2
#define EXPECTED_SIZE_float 4
#define EXPECTED_SIZE_double 8
void check(TY n, char *pf, char *pd)
{
  int i;
  char *p, *p2 = (char*)&n;
#ifdef TEST_float
    p = pf;
#else
    p = pd;
#endif
  for(i=0; i<sizeof(TY); i++)
#ifdef TEST_BIG
    if(p[i] != p2[i])
      exit(1);
#else
    if(p[i] != p2[sizeof(TY)-i-1])
      exit(1);
#endif
}
int main()
{
  if(sizeof(TY)!=EXPECTED_SIZE_$1)
    exit(1);
  check((TY)0.0, "\000\000\000\000", "\000\000\000\000\000\000\000\000");
  check((TY)1.0, "\077\200\000\000", "\077\360\000\000\000\000\000\000");
  check((TY)-1.0, "\277\200\000\000", "\277\360\000\000\000\000\000\000");
  check((TY)2.0, "\100\000\000\000", "\100\000\000\000\000\000\000\000");
  check((TY)1.5, "\077\300\000\000", "\077\370\000\000\000\000\000\000");
  check((TY)0.032999999821186065673828125, "\075\007\053\002", "\077\240\345\140\100\000\000\000");
  exit(0);
}
]])

AC_MSG_CHECKING(for type of float)
AC_CACHE_VAL(pike_cv_float_type,
[AC_TRY_RUN(float_check(float,BIG),pike_cv_float_type=ieee_big,
  [AC_TRY_RUN(float_check(float,LITTLE),pike_cv_float_type=ieee_little,
    pike_cv_float_type=not_ieee)],pike_cv_float_type=not_ieee)])
if test "$pike_cv_float_type" = ieee_big; then
  AC_MSG_RESULT(IEEE big endian)
  AC_DEFINE(FLOAT_IS_IEEE_BIG)
else
  if test "$pike_cv_float_type" = ieee_little; then
    AC_MSG_RESULT(IEEE little endian)
    AC_DEFINE(FLOAT_IS_IEEE_LITTLE)
  else
    AC_MSG_RESULT(not IEEE)
  fi
fi

AC_MSG_CHECKING(for type of double)
AC_CACHE_VAL(pike_cv_double_type,
[AC_TRY_RUN(float_check(double,BIG),pike_cv_double_type=ieee_big,
  [AC_TRY_RUN(float_check(double,LITTLE),pike_cv_double_type=ieee_little,
    pike_cv_double_type=not_ieee)],pike_cv_double_type=not_ieee)])
if test "$pike_cv_double_type" = ieee_big; then
  AC_MSG_RESULT(IEEE big endian)
  AC_DEFINE(DOUBLE_IS_IEEE_BIG)
else
  if test "$pike_cv_double_type" = ieee_little; then
    AC_MSG_RESULT(IEEE little endian)
    AC_DEFINE(DOUBLE_IS_IEEE_LITTLE)
  else
    AC_MSG_RESULT(not IEEE)
  fi
fi

#############################################################################

AC_MSG_CHECKING(for volatile)
AC_CACHE_VAL(pike_cv_volatile,
[
 AC_TRY_COMPILE([],[ volatile int foo=1;], pike_cv_volatile=yes,pike_cv_volatile=no)
])

if test "$pike_cv_volatile" = yes; then
 AC_MSG_RESULT(yes)
 AC_DEFINE(VOLATILE,volatile)
else
 AC_MSG_RESULT(no)
 AC_DEFINE(VOLATILE,)
fi

########################################################################

AC_MSG_CHECKING(for sig_atomic_t)
AC_CACHE_VAL(ac_cv_type_sig_atomic_t,
[
  AC_EGREP_CPP(sig_atomic_t, [
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#include <sys/types.h>
#if STDC_HEADERS
#include <stdlib.h>
#include <stddef.h>
#endif], ac_cv_type_sig_atomic_t=yes, ac_cv_type_sig_atomic_t=no)])
AC_MSG_RESULT($ac_cv_type_sig_atomic_t)
if test x"$ac_cv_type_sig_atomic_t" = xno; then
  AC_DEFINE(sig_atomic_t, int)
else
  :
fi

AC_MSG_CHECKING(if signal handlers reset automatically)
AC_CACHE_VAL(pike_cv_sys_signal_oneshot,
[
AC_TRY_RUN([
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#include <sys/types.h>
#if STDC_HEADERS
#include <stdlib.h>
#include <stddef.h>
#endif
VOLATILE sig_atomic_t sigrun=0;
RETSIGTYPE func(int sig) { sigrun=1; }

int main()
{
#define sig SIGSEGV

#ifdef HAVE_SIGACTION
  {
    struct sigaction action;
    action.sa_handler= func;
    sigfillset(&action.sa_mask);
#ifdef SA_INTERRUPT
    action.sa_flags=SA_INTERRUPT;
#endif
    sigaction(sig,&action,0);
  }
#else
#ifdef HAVE_SIGVEC
  {
    struct sigvec action;
    action.sv_handler= func;
    action.sv_mask=-1;
#ifdef SV_INTERRUPT
    action.sv_flags=SV_INTERRUPT;
#endif
    sigvec(sig,&action,0);
  }
#else
  signal(sig, func);
#endif
#endif

  kill(getpid(), sig);
  while(!sigrun) sleep(1);
  sigrun=0;
  kill(getpid(), sig);
  while(!sigrun) sleep(1);
  sigrun=0;
  exit(0);
}
],pike_cv_sys_signal_oneshot=no,pike_cv_sys_signal_oneshot=yes,pike_cv_sys_signal_oneshot=yes)])

if test "$pike_cv_sys_signal_oneshot" = yes; then
  AC_MSG_RESULT(yes)
  AC_DEFINE(SIGNAL_ONESHOT)
else
  AC_MSG_RESULT(no)
fi

########################################################################

AC_MSG_CHECKING(available file descriptors)
AC_CACHE_VAL(pike_cv_max_open_fd,
[
AC_TRY_RUN([
#include <stdio.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif

#ifndef MAX_FD
#define MAX_FD 256
#endif

int main()
{
  FILE *f;
  long limit;

#if !defined(RLIMIT_NOFILE) && defined(RLIMIT_OFILE)
#define RLIMIT_NOFILE RLIMIT_OFILE
#endif

#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
  struct rlimit lim;

  if(getrlimit(RLIMIT_NOFILE,&lim))
  {
    limit = MAX_FD;
  }else{
    if(lim.rlim_max == RLIM_INFINITY)
    {
      limit=16384; /* noone needs more */
    }else{
      limit=lim.rlim_max;
      if(limit > 16384) limit=16384;
    }
  }
#else
  limit = MAX_FD;
#endif

  f=fopen("conftest.out.2","w");
  fprintf(f,"%ld\n",(long)limit);
  fclose(f);

  return 0;
}
],
pike_cv_max_open_fd=`cat conftest.out.2`
,
pike_cv_max_open_fd=256
,
pike_cv_max_open_fd=256
)])
AC_MSG_RESULT($pike_cv_max_open_fd)
AC_DEFINE_UNQUOTED(MAX_OPEN_FILEDESCRIPTORS,$pike_cv_max_open_fd)

AC_MSG_CHECKING(full availability of struct rusage members)
AC_CACHE_VAL(pike_cv_func_getrusage_full,
[
AC_TRY_LINK([
#include <sys/time.h>
#ifdef HAVE_SYS_RUSAGE_H
#include <sys/rusage.h>
#endif
#include <sys/resource.h>
#ifndef RUSAGE_SELF
#define RUSAGE_SELF     0
#endif
],[
    struct rusage rus;
    long *v = (long *)main;
    getrusage(RUSAGE_SELF, &rus);
    *v++ = rus.ru_maxrss;
    *v++ = rus.ru_ixrss;
    *v++ = rus.ru_idrss;
    *v++ = rus.ru_isrss;
    *v++ = rus.ru_minflt;
    *v++ = rus.ru_majflt;
    *v++ = rus.ru_nswap;
    *v++ = rus.ru_inblock;
    *v++ = rus.ru_oublock;
    *v++ = rus.ru_msgsnd;
    *v++ = rus.ru_msgrcv;
    *v++ = rus.ru_nsignals;
    *v++ = rus.ru_nvcsw;
    *v++ = rus.ru_nivcsw;
],
  pike_cv_func_getrusage_full=yes
,
  pike_cv_func_getrusage_full=no
)])

if test "$pike_cv_func_getrusage_full" = yes; then
  AC_MSG_RESULT(all there)
else
  AC_MSG_RESULT(getrusage is restricted)
  AC_DEFINE(GETRUSAGE_RESTRICTED)
fi

########################################################################
# 
# IRIX also has procfs support, but it it different, a separate test
# should be made. /Hubbe
#

if test "$ac_cv_header_sys_procfs_h" = "yes"; then
  AC_MSG_CHECKING(getrusage() through procfs)
  AC_CACHE_VAL(pike_cv_getrusage_procfs, [
    AC_TRY_COMPILE([
#ifndef _LARGEFILE_SOURCE
#  define _FILE_OFFSET_BITS 64
#  define _LARGEFILE_SOURCE
#  define _LARGEFILE64_SOURCE 1
#endif
#include <sys/procfs.h>
#include <sys/fcntl.h>
int proc_fd;
long tmp;
char proc_name[20];
prusage_t  pru;
prstatus_t prs;
    ],[
      sprintf(proc_name, "/proc/%05d", getpid());
      proc_fd = open(proc_name, O_RDONLY);
      ioctl(proc_fd, PIOCUSAGE, &pru);
      ioctl(proc_fd, PIOCSTATUS, &prs);
      tmp=pru.pr_utime.tv_sec;
    ],
      pike_cv_getrusage_procfs=yes
    ,
      pike_cv_getrusage_procfs=no
    )
  ])

  if test "$pike_cv_getrusage_procfs" = yes; then
    AC_MSG_RESULT(yes)
    AC_DEFINE(GETRUSAGE_THROUGH_PROCFS)
  else
    AC_MSG_RESULT(no)
  fi
fi

#############################################################################

AC_MSG_CHECKING(for isgraph)
AC_CACHE_VAL(pike_cv_func_isgraph, [ AC_TRY_COMPILE([
#include <ctype.h>
], [
return(!isgraph('A'));
], pike_cv_func_isgraph=yes, pike_cv_func_isgraph=no)
])
AC_MSG_RESULT($pike_cv_func_isgraph)
if test "x$pike_cv_func_isgraph" = "xyes" ; then
  AC_DEFINE(HAVE_ISGRAPH)
else :; fi

#############################################################################

AC_MSG_CHECKING(for gcc function attributes)
AC_CACHE_VAL(pike_cv_gcc_attributes,
[
AC_TRY_RUN([
#include <stdarg.h>
void fatal(char *foo,...) __attribute__ ((noreturn,format (printf,1,2)));
int sqr(int x) __attribute__ ((const));

int sqr(int x) { return x*x; }

void fatal(char *foo,...)
{
  va_list args;
  va_start(foo,args);
  printf(foo,args);
  va_end(args);
  exit(2);
}
main() {  exit(0); }

], pike_cv_gcc_attributes=yes, pike_cv_gcc_attributes=no, pike_cv_gcc_attributes=no)])

AC_MSG_RESULT($pike_cv_gcc_attributes)
if test "$pike_cv_gcc_attributes" = yes; then
 AC_DEFINE(HAVE_FUNCTION_ATTRIBUTES)
fi

AC_MSG_CHECKING(how to concatenate two symbols)
AC_CACHE_VAL(pike_cv_cpp_symbol_concat, [
  AC_TRY_COMPILE([
int AABB=0;
#define CONCAT(X,Y)	X##Y
  ], [
    return CONCAT(AA,BB);
  ], pike_cv_cpp_symbol_concat=ANSI, [
    AC_TRY_COMPILE([
int AABB=0;
#define CONCAT(X,Y)	X/**/Y
    ], [
      return CONCAT(AA,BB);
    ], pike_cv_cpp_symbol_concat=KR, [
      pike_cv_cpp_symbol_concat=Unknown
    ])
  ])
])
AC_MSG_RESULT($pike_cv_cpp_symbol_concat)
case "$pike_cv_cpp_symbol_concat" in
  "ANSI")
    AC_DEFINE(HAVE_ANSI_CONCAT)
  ;;
  "KR")
    AC_DEFINE(HAVE_KR_CONCAT)
  ;;
esac

#############################################################################

AC_MSG_CHECKING(how to set things nonblocking)
AC_CACHE_VAL(pike_cv_sys_nonblock,
[
  # Kludge needed for AmigaOS
  if test "$pike_cv_sys_os" = "AmigaOS" ; then
    pike_cv_sys_nonblock=USE_FCNTL_O_NONBLOCK
  else
    AC_TRY_RUN([
#define TESTING
#define USE_FCNTL_FNDELAY
#include "$srcdir/fd_control.c"
], pike_cv_sys_nonblock=USE_FCNTL_FNDELAY,
    AC_TRY_RUN([
#define TESTING
#define USE_FCNTL_O_NDELAY
#include "$srcdir/fd_control.c"
], pike_cv_sys_nonblock=USE_FCNTL_O_NDELAY,
    AC_TRY_RUN([
#define TESTING
#define USE_FCNTL_O_NONBLOCK
#include "$srcdir/fd_control.c"
], pike_cv_sys_nonblock=USE_FCNTL_O_NONBLOCK,
    AC_TRY_RUN([
#define TESTING
#define USE_IOCTL_FIONBIO
#include "$srcdir/fd_control.c"
], pike_cv_sys_nonblock=USE_IOCTL_FIONBIO,
    AC_TRY_RUN([
#define TESTING
#define USE_IOCTLSOCKET_FIONBIO
#include "$srcdir/fd_control.c"
], pike_cv_sys_nonblock=USE_IOCTLSOCKET_FIONBIO,
unset pike_cv_sys_nonblock,
unset pike_cv_sys_nonblock),
unset pike_cv_sys_nonblock),
unset pike_cv_sys_nonblock),
unset pike_cv_sys_nonblock),
unset pike_cv_sys_nonblock)
  fi
])

if test "${pike_cv_sys_nonblock}" = ""; then
  AC_MSG_RESULT(none found)
else
 AC_MSG_RESULT($pike_cv_sys_nonblock)
 AC_DEFINE_UNQUOTED($pike_cv_sys_nonblock)
fi

rm -f core

##########################################################################

# Set info about shared libraries.
AC_SUBST(SO)
AC_SUBST(LDSHARED)
AC_SUBST(CCSHARED)
AC_SUBST(LINKFORSHARED)

# SO is the extension of shared libraries
# -- usually so, sl on HP-UX
AC_MSG_CHECKING(SO)
if test -z "$SO"
then
	case "$pike_cv_sys_os" in
	hp*|HP*)   SO=sl;;
	*)	   SO=so;;
	esac
fi
AC_MSG_RESULT($SO)
# LDSHARED is the ld *command* used to create shared library
# -- "ld" on SunOS 4.x.x, "ld -G" on SunOS 5.x, "ld -shared" on IRIX 5
AC_MSG_CHECKING(LDSHARED)
if test -z "$LDSHARED"
then
	case "$pike_cv_sys_os" in
	AIX*)
         if test "$GCC" = yes ; then
	   if test "`uname -r`" -ge 2 -a "`uname -v`" -ge 4 ; then
             LDSHARED="ld -G -bexpall"
	   else if test "`uname -v`" -gt 4 ; then
	       # Extra forward compatibility...
	       # AIX 5+
	       LDSHARED="ld -G -bexpall"
	     else
	       # AIX 4.1 and earlier don't have/have unreliable
	       # handling of dlopen et al.
	       #
	       # Even AIX 4.2.1 has buggy dlopen -- NEVER attempt
	       # to dlopen a file that hasn't been linked with -G.
	       pike_cv_sys_dynamic_loading=no
	       LDSHARED="ld"
	     fi
	   fi
         else
           # Dynamic linking turned off for AIX-cc
	   #
	   # In addition, the dynamic loading test seems to
	   # cause our RS/6000 to hang, which causes the
	   # watchdog-timer to PANIC.
	   pike_cv_sys_dynamic_loading=no
           LDSHARED="ld"
         fi
	;;
	IRIX*)
	  if test "$GCC" = yes ; then
	    # egcs defaults to building -n32 objects
	    LDSHARED="ld -shared -n32"
	  else
            LDSHARED="ld -shared"
	  fi
	;;
	SunOS*) LDSHARED="ld";;
	Solaris) LDSHARED="/usr/ccs/bin/ld -G";;
	hp*|HP*) LDSHARED="ld -b";;
	OSF*)
           LDSHARED="ld -expect_unresolved '*' -shared -msym -O3"
        ;;
	DYNIX/ptx*) LDSHARED="ld -G";;
	next) LDSHARED="$CC $CFLAGS -nostdlib -r";;
	Linux*) LDSHARED="$CC -shared";;
        GNU*) LDSHARED="ld -Bshareable";;
	FreeBSD*) LDSHARED="ld -Bshareable";;
	NetBSD) LDSHARED="ld -Bshareable";;
	OpenBSD*) LDSHARED="ld -Bshareable";;
	BSD/OS*) LDSHARED="shlicc -r";;
	SCO_SV*) LDSHARED="cc -G -KPIC -Wl,-Bexport";;
	UNIX_SV) LDSHARED="ld -G";;
        Windows_NT*) LDSHARED="$CC -shared";;
	*) LDSHARED="ld";;
	esac
fi
AC_MSG_RESULT($LDSHARED)

# CCSHARED are the C *flags* used to create objects to go into a shared
# library -- this is only needed for a few systems
AC_MSG_CHECKING(CCSHARED)
if test -z "$CCSHARED" ; then
  if test "$GCC" = yes ; then
    case $pike_cv_sys_os in
      BSD/OS*) CCSHARED="";;
      *) CCSHARED="-fpic";;
    esac
  else
    case $pike_cv_sys_os in
	hp*|HP*) CCSHARED="+z"; ;;
	FreeBSD*) CCSHARED="-fpic";;
	SCO_SV*) CCSHARED="-KPIC -dy -Bdynamic";;
	Solaris*) CCSHARED="-KPIC -Bdynamic";;
        Windows_NT*) CCSHARED="-shared" ;;
    esac
  fi
fi
AC_MSG_RESULT($CCSHARED)

AC_MSG_CHECKING(LINKFORSHARED)

AC_CACHE_VAL(pike_cv_sys_linkforshared,[
# LINKFORSHARED are the flags passed to the $(CC) command that links
# the pike executable -- this is only needed for a few systems
if test -z "$LINKFORSHARED"
then
  LINKFORSHARED=""
  if test "$GCC" = yes ; then
    case $pike_cv_sys_os in
	AIX*)	LINKFORSHARED="-Wl,-bexpall -Wl,-brtl";;
	hp*|HP*) LINKFORSHARED="-Wl,-E";;
	Linux*) LINKFORSHARED="-rdynamic";;
        GNU*) LINKFORSHARED="-rdynamic";;
	next) LINKFORSHARED="-u libsys_s";;
	SCO_SV*) LINKFORSHARED="-fpic -Wl,-Bexport";;
    esac
  else
    case $pike_cv_sys_os in
	AIX*)	LINKFORSHARED="-Wl,-bexpall -Wl,-brtl";;
	hp*|HP*) LINKFORSHARED="-Wl,-E";;
	Linux*) LINKFORSHARED="-rdynamic";;
        GNU*) LINKFORSHARED="-rdynamic";;
	next) LINKFORSHARED="-u libsys_s";;
	SCO_SV*) LINKFORSHARED="-Bdynamic -dy -Wl,-Bexport";;
    esac
  fi
fi


   OLD_CFLAGS="$CFLAGS"
   CFLAGS="$CFLAGS $LINKFORSHARED"
   AC_TRY_RUN([int main() { exit(0); }],
 pike_cv_sys_linkforshared="$LINKFORSHARED", pike_cv_sys_linkforshared='', pike_cv_sys_linkforshared='')
   CFLAGS="$OLD_CFLAGS"
])

LINKFORSHARED="$pike_cv_sys_linkforshared"

AC_MSG_RESULT($LINKFORSHARED)

#############################################################################

cat >conftest.c <<EOF
#include <stdio.h>
void testfunc(void) {
  fprintf(stderr,"Calling testfunc2\n");
  testfunc2();
  fprintf(stderr,"testfunc2 returned!\n");
  exit(1);
}
EOF

AC_MSG_CHECKING(if dynamic loading works)
AC_CACHE_VAL(pike_cv_sys_dynamic_loading,
[
  echo $CC -c $CFLAGS $CCSHARED conftest.c -o conftest.o 1>&5
  $CC -c $CFLAGS $CCSHARED conftest.c -o conftest.o 1>&5 2>&5

  echo $BINDIR/smartlink $LDSHARED $LDFLAGS conftest.o -o conftest.$SO 1>&5
  $BINDIR/smartlink $LDSHARED $LDFLAGS conftest.o -o conftest.$SO 1>&5 2>&5

  if test -f conftest.$SO ; then
    mv conftest.$SO myconftest.so

    OLD_CFLAGS="$CFLAGS"
    CFLAGS="$CFLAGS $LINKFORSHARED"
    OLD_CC="$CC"
    case "$LD" in
      *shlicc) CC="$BINDIR/smartlink shlicc";;
    esac
    AC_TRY_RUN([
#define TESTING
#include "$srcdir/dynamic_load.c"
void testfunc2(void) { exit(0); }
], pike_cv_sys_dynamic_loading=yes, pike_cv_sys_dynamic_loading=no, pike_cv_sys_dynamic_loading=no)
    CFLAGS="$OLD_CFLAGS"
    CC="$OLD_CC"
  else
    pike_cv_sys_dynamic_loading=no
  fi
  rm -f myconftest.so >/dev/null 2>/dev/null
])
AC_MSG_RESULT($pike_cv_sys_dynamic_loading)

if test x$pike_cv_sys_dynamic_loading = xno ; then
  with_dynamic_modules=no
fi


#############################################################################

echo
echo "Compiler summary:"
echo
echo "C Compiler:    $CC"
echo "Linker:        $LD"
echo "Shared linker: $LDSHARED"
echo
echo "Shared object extension: $SO"
echo
echo "CPPFLAGS:      $CPPFLAGS"
echo "CFLAGS:        $CFLAGS"
echo "CCSHARED:      $CCSHARED"
echo "LINKFORSHARED: $LINKFORSHARED"
echo "WARN:          $WARN"
echo "OPTIMIZE:      $OPTIMIZE"
echo "LDFLAGS:       $LDFLAGS"
echo "LIBS:          $LIBS"
echo

#############################################################################

export LDFLAGS CFLAGS CPPFLAGS LIBS

AC_CONFIG_SUBDIRS(modules)

my_libdir=`echo $prefix | sed "s@^NONE@$ac_default_prefix@g"`
default_master=$my_libdir/lib/pike/master.pike
AC_DEFINE_UNQUOTED(DEFAULT_MASTER,"$default_master")

AC_SUBST_FILE(dependencies)
dependencies=$srcdir/dependencies

AC_SUBST(YACC)
AC_SUBST(INSTALL)
AC_SUBST(WARN)
AC_SUBST(OPTIMIZE)
AC_SUBST(EXTRA_OBJS)
AC_SUBST(RANLIB)
AC_SUBST(DEBUGDEF)
AC_SUBST(ac_configure_args)

if test x$ac_cv_func_dlopen$ac_cv_func_dld_link = xnono ; then
  if test x$pike_cv_sys_os != xWindows_NT ; then
    with_dynamic_modules=no
  fi
fi

if test x$with_dynamic_modules = xyes ; then
  dmmsrc="modules/dynamic_module_makefile.in"
else
  dmmsrc="modules/static_module_makefile.in"
fi

AC_SUBST(dmmsrc)

AC_OUTPUT(Makefile modules/static_module_makefile modules/dynamic_module_makefile:$dmmsrc,[echo foo >stamp-h])


