AC_INIT
AC_PREREQ([2.68])
AC_CONFIG_HEADERS([h/gclincl.h])

VERSION=`cat majvers`.`cat minvers`
AC_SUBST(VERSION)


#
# Host information 
#


AC_CANONICAL_HOST
canonical=$host
my_host_kernel=`echo $host_os | awk '{j=split($1,A,"-");print A[[1]]}'`
my_host_system=`echo $host_os | awk '{j=split($1,A,"-");if (j>=2) print A[[2]]}'`
AC_DEFINE_UNQUOTED(HOST_CPU,"`echo $host_cpu | awk '{print toupper($0)}'`",[Host cpu])
AC_DEFINE_UNQUOTED(HOST_KERNEL,"`echo $my_host_kernel | awk '{print toupper($0)}'`",[Host kernel])
if test "$my_host_system" != "" ; then 
    AC_DEFINE_UNQUOTED(HOST_SYSTEM,"`echo $my_host_system | awk '{print toupper($0)}'`",[Host system])
fi
## host=CPU-COMPANY-SYSTEM
AC_MSG_RESULT(host=$host)

use=unknown
case $canonical in
    sh4*linux*) use=sh4-linux;;
    *x86_64*linux*) use=amd64-linux;;
    *x86_64*kfreebsd*)	use=amd64-kfreebsd;;
    *86*linux*) use=386-linux;;
    *riscv64*linux*) use=riscv64-linux;;
    *86*kfreebsd*) use=386-kfreebsd;;
    *86*gnu*)	use=386-gnu;;
    m68k*linux*) use=m68k-linux;;
    alpha*linux*) use=alpha-linux;;
    mips*linux*) use=mips-linux;;
    mipsel*linux*) use=mipsel-linux;;
    sparc*linux*) use=sparc-linux;;
    aarch64*linux*) use=aarch64-linux;;
    arm*linux*hf) use=armhf-linux;;
    arm*linux*) use=arm-linux;;
    s390*linux*) use=s390-linux;;
    ia64*linux*) use=ia64-linux;;
    hppa*linux*) use=hppa-linux;;
    powerpc*linux*) use=powerpc-linux;;
    powerpc-*-darwin*) use=powerpc-macosx;;
    *86*darwin*) use=386-macosx;;
    i*mingw*|i*msys*) use=mingw;;
    *cygwin*)
        if $CC -v 2>&1 | fgrep ming > /dev/null ;
        then use=mingw
        else use=gnuwin95
        fi;;
    *openbsd*) use=FreeBSD;;
    sparc-sun-solaris*) use=solaris;;
    i?86-pc-solaris*) use=solaris-i386;;
esac

AC_ARG_ENABLE([machine],[  --enable-machine=XXXX will force the use of one of the definitions in h/XXXX.defs],
	      [echo enable_machine=$enableval ; use=$enableval])

AC_MSG_RESULT([use=$use])	

def_dlopen="no"
def_statsysbfd="no"
def_custreloc="yes"
def_oldgmp="no"
def_pic="no";
def_static="no";
def_debug="no";
case $use in 
    *kfreebsd)
	ln -snf linux.defs h/$use.defs;;
    *gnu)
	ln -snf linux.defs h/$use.defs;;
    *linux)
	ln -snf linux.defs h/$use.defs;
	case $use in
	    ia64*)
		def_dlopen="yes" ; def_custreloc="no" ;;
	    hppa*)
		def_pic="yes" ;;
	esac;;
esac

AC_ARG_ENABLE([widecons],[  --enable-widecons will use a three word cons with simplified typing],
	      [if test "$enableval" = "yes" ; then AC_DEFINE([WIDE_CONS],[1],[three word cons]) fi])

AC_ARG_ENABLE([safecdr],[  --enable-safecdr will protect cdr from immfix and speed up type processing],
	      [if test "$enableval" = "yes" ; then
		   AC_DEFINE([USE_SAFE_CDR],[1],[protect cdr from immfix and speed up type processing])
		   AC_ARG_ENABLE([safecdrdbg],[  --enable-safecdrdbg will debug safecdr code],
				 [if test "$enableval" = "yes" ; then AC_DEFINE([DEBUG_SAFE_CDR],[1],[debug safecdr code]) fi])
	       fi])

AC_ARG_ENABLE([prelink],[  --enable-prelink will insist that the produced images may be prelinked],
	      [if test "$enable_prelink" = "yes" ; then PRELINK_CHECK=t; fi])
AC_SUBST(PRELINK_CHECK)

AC_ARG_ENABLE([vssize],[  --enable-vssize=XXXX will compile in a value stack of size XXX],
	      [AC_DEFINE_UNQUOTED(VSSIZE,$enableval,[value stack size])])
AC_ARG_ENABLE([bdssize],[  --enable-bdssize=XXXX will compile in a binding stack of size XXX],
	      [AC_DEFINE_UNQUOTED(BDSSIZE,$enableval,[binding stack size])])
AC_ARG_ENABLE([ihssize],[  --enable-ihssize=XXXX will compile in a invocation history stack of size XXX],
	      [AC_DEFINE_UNQUOTED(IHSSIZE,$enableval,[invocation history stack size])])
AC_ARG_ENABLE([frssize],[  --enable-frssize=XXXX will compile in a frame stack of size XXX],
	      [AC_DEFINE_UNQUOTED(FRSSIZE,$enableval,[frame stack size])])

AC_ARG_ENABLE([infodir],[  --enable-infodir=XXXX will force the use of a INFO_DIR=XXXXX as place to look for info],
	      [INFO_DIR=$enableval],[INFO_DIR=$prefix/share/info])
INFO_DIR=`eval echo $INFO_DIR/`

AC_ARG_ENABLE([emacsdir],[  --enable-emacsdir=XXXX will manually specify the location for elisp files],
	      [EMACS_SITE_LISP=$enableval],[EMACS_SITE_LISP=$prefix/share/emacs/site-lisp])
EMACS_SITE_LISP=`eval echo $EMACS_SITE_LISP/`

AC_ARG_ENABLE([xgcl],[  --enable-xgcl=yes will compile in support for XGCL],,[enable_xgcl=yes])

AC_ARG_ENABLE([dlopen],[  --enable-dlopen uses dlopen for loading objects, which can then not be retained  in saved images],
	      ,[enable_dlopen=$def_dlopen])
AC_ARG_ENABLE([statsysbfd],[  --enable-statsysbfd uses a static system bfd library for loading and relocating object files],
	      ,[enable_statsysbfd=$def_statsysbfd])
AC_ARG_ENABLE([dynsysbfd],[  --enable-dynsysbfd uses a dynamic shared system bfd library for loading and relocating object files],
	      ,[enable_dynsysbfd=no])
AC_ARG_ENABLE([custreloc],[  --enable-custreloc uses custom gcl code if available for loading  and relocationing object files],
	      ,[enable_custreloc=$def_custreloc])

AC_ARG_ENABLE([debug],[  --enable-debug builds gcl with -g in CFLAGS to enable running under gdb],
	      ,[enable_debug=$def_debug])
AC_ARG_ENABLE([static],[  --enable-static will link your GCL against static as opposed to shared system libraries],
	      ,[enable_static=$def_static])
AC_ARG_ENABLE([pic],[  --enable-pic builds gcl with -fPIC in CFLAGS],,[enable_pic=$def_pic])

load_opt=0
if test "$enable_dlopen" = "yes" ; then
    load_opt=1
fi
if test "$enable_statsysbfd" = "yes" ; then
    case $load_opt in
	0) load_opt=1;;
	1) load_opt=2;;
    esac
fi
if test "$enable_dynsysbfd" = "yes" ; then
    case $load_opt in
	0) load_opt=1;;
	1) load_opt=2;;
	2) load_opt=3;;
    esac
fi
if test "$enable_custreloc" = "yes" ; then
    case $load_opt in
	0) load_opt=1;;
	1) load_opt=2;;
	2) load_opt=3;;
	3) load_opt=4;;
	4) load_opt=5;;
    esac
fi

if test "$load_opt" != "1" ; then
    echo "Exactly one loader option must be chosen: dlopen=$enable_dlopen statsysbfd=$enable_statsysbfd dynsysbfd=$enable_dynsysbfd custreloc=$enable_custreloc"
    AC_MSG_ERROR([loader option failure])
fi


#
# System programs
#

# We set the default CFLAGS below, and don't want the autoconf default
# CM 20040106
if test "$CFLAGS" = "" ; then
    CFLAGS=" "
fi
if test "$LDFLAGS" = "" ; then
    LDFLAGS=" "
fi

AC_PROG_CC
AC_PROG_CPP
AC_SUBST(CC)

add_arg_to_cflags() {
    
    AC_MSG_CHECKING([for CFLAG $1])
    CFLAGS_ORI=$CFLAGS
    CFLAGS="$CFLAGS -Werror $1 `echo $1|sed 's,-Wno-,-W,1'`"
    AC_RUN_IFELSE(
	[AC_LANG_PROGRAM([[]],[[]])],
	[CFLAGS="$CFLAGS_ORI $1";AC_MSG_RESULT([yes]);return 0],
	[AC_MSG_RESULT([no])],
	[AC_MSG_RESULT([no])])
    CFLAGS=$CFLAGS_ORI
    return 1
    
}

assert_arg_to_cflags() {
    if ! add_arg_to_cflags $1 ; then AC_MSG_RESULT([cannot add $1 to CFLAGS]); exit 1 ; fi
    return 0
}

add_args_to_cflags() {
    
    while test "$#" -ge 1 ; do
  	add_arg_to_cflags $1
	shift
    done
}

add_arg_to_ldflags() {
    
    AC_MSG_CHECKING([for LDFLAG $1])
    LDFLAGS_ORI=$LDFLAGS
    LDFLAGS="$LDFLAGS -Werror $1"
    AC_RUN_IFELSE(
	[AC_LANG_PROGRAM([[]],[[]])],
	[LDFLAGS="$LDFLAGS_ORI $1";AC_MSG_RESULT([yes]);return 0],
	[AC_MSG_RESULT([no])],
	[AC_MSG_RESULT([no])])
    LDFLAGS=$LDFLAGS_ORI
    return 1
    
}

assert_arg_to_ldflags() {
    if ! add_arg_to_ldflags $1 ; then AC_MSG_RESULT([cannot add $1 to LDFLAGS]); exit 1 ; fi
    return 0
}

add_args_to_ldflags() {
    
    while test "$#" -ge 1 ; do
  	add_arg_to_ldflags $1
	shift
    done
}

remove_arg_from_ldflags() {
    
    NEW_LDFLAGS=""	  
    for i in $LDFLAGS; do
	if ! test "$i" = "$1" ; then
      	    NEW_LDFLAGS="$NEW_LDFLAGS $i"
	else
            AC_MSG_RESULT([removing $1 from LDFLAGS])
	fi
    done
    LDFLAGS=$NEW_LDFLAGS
    
    return 0
    
}

add_args_to_cflags  -fsigned-char -pipe -fcommon \
                    -fno-builtin-malloc -fno-builtin-free \
                    -fno-PIE -fno-pie -fno-PIC -fno-pic \
	            -Wall \
	            -Wno-builtin-requires-header -Wno-empty-body -Wno-self-assign \
	            -Wno-unused-but-set-variable

add_args_to_ldflags -no-pie # -Wl,-z,lazy

AC_MSG_CHECKING([for inline semantics])
AC_COMPILE_IFELSE(
    [AC_LANG_SOURCE([[
			inline int foo(int i) {return i;}

			int
			bar(int i) {return foo(i);}
		     ]])],
    [if `nm conftest.o |grep foo |awk '{if (NF==3) exit(-1)}'` ; then
    	AC_MSG_RESULT([new])
     else
	AC_COMPILE_IFELSE(
	    [AC_LANG_SOURCE([[
	    			extern inline int foo(int i) {return i;}

				int
				bar(int i) {return foo(i);}
			    ]])],
	    [if `nm conftest.o |grep foo |awk '{if (NF==3) exit(-1)}'` ; then
	    	AC_MSG_RESULT([old])
		AC_DEFINE([OLD_INLINE],[1],[extern inline semantics])
	     else
	       	AC_MSG_ERROR([need working inline semantics])
	     fi],
    	    [AC_MSG_ERROR([need to probe inline semantics])])
     fi],
     [AC_MSG_ERROR([need to probe inline semantics])])

AC_MSG_CHECKING([for clang])
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM([[
			 #ifdef __clang__
			 #define RET 0
			 #else
			 #define RET 1
			 #endif
		     ]],
		     [[
			 return RET;
		     ]])],
    [AC_MSG_RESULT([yes])
     clang="yes"
     remove_arg_from_ldflags -pie
     AC_DEFINE([CLANG],[1],[running clang compiler])],	
    [AC_MSG_RESULT([no])])

case $use in
    *mingw*)
	assert_arg_to_cflags -fno-zero-initialized-in-bss
	assert_arg_to_cflags -mms-bitfields
	for i in makedefc windows/gcl.iss windows/sysdir.bat windows/install.lsp h/gclincl.h;  do
	    cat $i.in | sed 's,[^\r]\n$,\r\n,g' >tmp && mv tmp $i.in;
	done
	OLD_LDFLAGS=$LDFLAGS
	assert_arg_to_ldflags -pg
	GPL_FLAG="-pg"
	LDFLAGS=$OLD_LDFLAGS;;
    *gnuwin*)
	assert_arg_to_cflags -fno-zero-initialized-in-bss
	assert_arg_to_cflags -mms-bitfields
        assert_arg_to_ldflags -Wl,--stack,8000000
	OLD_LDFLAGS=$LDFLAGS
	assert_arg_to_ldflags -pg
	GPL_FLAG="-pg"
	LDFLAGS=$OLD_LDFLAGS;;
    386-macosx)
#	assert_arg_to_cflags -Wno-error=implicit-function-declaration
	add_arg_to_cflags -Wno-incomplete-setjmp-declaration
	assert_arg_to_ldflags -Wl,-no_pie
	if test "$build_cpu" = "x86_64" ; then
	    assert_arg_to_cflags -m64
	    assert_arg_to_ldflags -m64
	    assert_arg_to_ldflags -Wl,-headerpad,72
        else
	    assert_arg_to_cflags -m32
	    assert_arg_to_ldflags -m32
	    assert_arg_to_ldflags -Wl,-headerpad,56
        fi;;
    FreeBSD) assert_arg_to_ldflags -Z;;
esac

if test "$enable_static" = "yes" ; then
    assert_arg_to_ldflags -static
    assert_arg_to_ldflags -Wl,-zmuldefs
    AC_DEFINE(STATIC_LINKING,1,[staticly linked images])
fi

TO3FLAGS=""
TO2FLAGS=""

case "$use" in
    *mingw*)
  	TFPFLAG="";;
    m68k*)#FIXME gcc 4.x bug workaround
	TFPFLAG="";;
    *)
	TFPFLAG="-fomit-frame-pointer";;
esac

AC_CHECK_PROGS(AWK,[gawk nawk awk])
GCL_CC_ARGS=`echo $CC | ${AWK} '{$1="";print}'`
GCL_CC="`basename $CC` $GCL_CC_ARGS"
if echo $GCL_CC |grep gcc |grep -q win; then
    GCL_CC=gcc
fi
AC_SUBST(GCL_CC)

AC_ARG_ENABLE([gprof],[  --enable-gprof builds gcl with -pg in CFLAGS to enable profiling with gprof],
	      [if test "$enableval" = "yes" ; then
		   AC_MSG_CHECKING([working gprof])
		   case $use in
		       powerpc*) if test "$host_cpu" = "powerpc64le" ; then enableval="no"; fi;;
		       s390*) enableval="no";;#mcount smashes float args in make_shortfloat 20180313
		       sh4*)  enableval="no";;
		       m68k*)  enableval="no";;
		       ia64*) enableval="no";;
		       hppa*) enableval="no";;
#		       arm*)  if echo $canonical |grep -q hf$; then enableval="no"; fi;;#FIXME CALL and JUMP24 veneers needed for thumb 20190201 #FIXME mcount compiled as a 24/22 bit reloc even with -mlong-calls, marginally accessible
		       aarch64*) enableval="no";;#unreproducible buildd bug 20170824
		       *gnu)  enableval="no";;
		   esac		     
                   GP_FLAG=""
                   if test "$enableval" != "yes" ; then
		       AC_MSG_RESULT([disabled])
		   else
		       AC_MSG_RESULT([ok])
		       OLD_CFLAGS=$CFLAGS  # Do not run configure tests with -pg
                       assert_arg_to_cflags -pg
                       GP_FLAG="-pg"
                       CFLAGS=$OLD_CFLAGS
       		       TFPFLAG=""
		       AC_DEFINE(GCL_GPROF,1,[use gprof profiling])
		   fi
	       fi])

if test "$enable_debug" = "yes" ; then
    assert_arg_to_cflags -g
    # for subconfigurations
    CFLAGS="$CFLAGS -g"
else
    TO3FLAGS="-O3 $TFPFLAG"
    TO2FLAGS="-O"
fi

# gcc on ppc cannot compile our new_init.c with full opts --CM
TONIFLAGS=""
case $use in 
    powerpc*macosx) assert_arg_to_cflags -mlongcall;;
    *linux)
	case $use in
	    alpha*)
		assert_arg_to_cflags -mieee
#		if test "$enable_debug" != "yes" ; then TO3FLAGS="-O0" ; fi #FIXME needed asof gcc 4.8.1
		;;
	    aarch64*)
		TLIBS="$TLIBS -lgcc_s";;
	    hppa*)
		assert_arg_to_cflags -mlong-calls
		TLIBS="$TLIBS -lgcc_s" # workaround hppa __moddi3 local func symbols with default linker flags
		if test "$enable_debug" != "yes" ; then TO3FLAGS="-O2" ; TFPFLAG=""; fi #FIXME needed asof gcc 4.8.1
		;;
	    mips*)
		case $canonical in
		    mips64*linux*)
#			assert_arg_to_cflags -mxgot
			assert_arg_to_ldflags -Wl,-z,now;;
		esac
		;;
	    ia64*)
		if test "$enable_debug" != "yes" ; then TO3FLAGS="-O" ; fi #FIXME needed asof gcc 4.8.1
		;;
	    arm*)
		AC_DEFINE([SET_STACK_POINTER],["mov %%sp,%0\n\t"],[asm string to set the stack pointer])
		AC_MSG_CHECKING([how to set stack pointer])
		AC_MSG_RESULT([done])
		assert_arg_to_cflags -fdollars-in-identifiers
		assert_arg_to_cflags -g #?
		;;
	    powerpc*) 
		assert_arg_to_cflags -mlongcall
		if test "$host_cpu" != "powerpc64le" ; then assert_arg_to_cflags -mno-pltseq; fi
		;;
	esac;;
esac
if test "$enable_pic" = "yes" ; then
    assert_arg_to_cflags -fPIC
fi


FDEBUG=`echo $CFLAGS | tr ' ' '\012' |grep "^\-g$"|tr '\012' ' '`
#CFLAGS=`echo $CFLAGS | tr ' ' '\012' |grep -v "^\-g$"`
FOMITF=`echo $CFLAGS | tr ' ' '\012' |grep "^\-fomit-frame-pointer$"|tr '\012' ' '`
CFLAGS=`echo $CFLAGS | tr ' ' '\012' |grep -v "^\-fomit-frame-pointer$"|tr '\012' ' '`
FOOPT3=`echo $CFLAGS | tr ' ' '\012' |grep "^\-O3$"|tr '\012' ' '`
CFLAGS=`echo $CFLAGS | tr ' ' '\012' |grep -v "^\-O3$"|tr '\012' ' '`
FOOPT2=`echo $CFLAGS | tr ' ' '\012' |grep "^\-O2$"|tr '\012' ' '`
CFLAGS=`echo $CFLAGS | tr ' ' '\012' |grep -v "^\-O2$"|tr '\012' ' '`
FOOPT1=`echo $CFLAGS | tr ' ' '\012' |grep "^\-O1$"|tr '\012' ' '`
TMPF=`echo $CFLAGS | tr ' ' '\012' |grep "^\-O$"|tr '\012' ' '`
FOOPT1="$FOOPT1$TMPF"
CFLAGS=`echo $CFLAGS | tr ' ' '\012' |grep -v "^\-O1$"|grep -v "^\-O$"|tr '\012' ' '`
FOOPT0=`echo $CFLAGS | tr ' ' '\012' |grep "^\-O0$"|tr '\012' ' '`
CFLAGS=`echo $CFLAGS | tr ' ' '\012' |grep -v "^\-O0$"|tr '\012' ' '`

if test "$FOOPT0" != "" ; then
    TO3FLAGS=`echo $TO3FLAGS | sed 's,\-O[[123 ]],-O0 ,g' | sed 's,\-O$,-O0 ,g'`
    TO2FLAGS=`echo $TO2FLAGS | sed 's,\-O[[123 ]],-O0 ,g' | sed 's,\-O$,-O0 ,g'`
else 
    if test "$FOOPT1" != "" ; then
	TO3FLAGS=`echo $TO3FLAGS | sed 's,\-O[[2-3]],-O1,g'`
	TO2FLAGS=`echo $TO2FLAGS | sed 's,\-O[[2-3]],-O1,g'`
    else 
	if test "$FOOPT2" != "" ; then
	    TO3FLAGS=`echo "$TO3FLAGS" | sed 's,\-O3,-O2,g'`
	    TO2FLAGS=`echo "$TO2FLAGS" | sed 's,\-O3,-O2,g'`
	fi
    fi
fi

if test "$FDEBUG" != "" ; then
    TO3FLAGS=`echo $TO3FLAGS | sed 's,\-fomit-frame-pointer,,g'`
    TO2FLAGS=`echo $TO2FLAGS | sed 's,\-fomit-frame-pointer,,g'`
fi

if test "$FOMITF" != "" ; then
    TO3FLAGS="$TO3FLAGS $FOMITF"
fi

# Step 1: set the variable "system" to hold the name and version number
# for the system.  This can usually be done via the "uname" command, but
# there are a few systems, like Next, where this doesn't work.

AC_CHECK_PROGS(MAKEINFO,makeinfo,"false")
AC_SUBST(MAKEINFO)

AC_MSG_CHECKING([system version (for dynamic loading)])
if machine=`uname -m` ; then true; else machine=unknown ; fi

if test -f /usr/lib/NextStep/software_version; then
    system=NEXTSTEP-`${AWK} '/3/,/3/' /usr/lib/NextStep/software_version`
else
    system=`uname -s`-`uname -r`
    if test "$?" -ne 0 ; then
	AC_MSG_RESULT([unknown (cannot find uname command)])
	system=unknown
    else
	# Special check for weird MP-RAS system (uname returns weird
	# results, and the version is kept in special file).
	
	if test -r /etc/.relid -a "X`uname -n`" = "X`uname -s`" ; then
	    system="MP-RAS-`${AWK} '{print $3}' '/etc/.relid'`"
	fi
	if test "`uname -s`" = "AIX" ; then
	    system=AIX-`uname -v`.`uname -r`
	fi
	AC_MSG_RESULT($system)
    fi
fi

case $use in
    *macosx)
        AC_CHECK_HEADERS(malloc/malloc.h,,[AC_MSG_ERROR([need malloc.h on macosx])])
	AC_CHECK_MEMBER([struct _malloc_zone_t.memalign],
 			AC_DEFINE(HAVE_MALLOC_ZONE_MEMALIGN,1,[memalign element present]), [], 
			[
			    #include <malloc/malloc.h>
			])
	AC_SUBST(HAVE_MALLOC_ZONE_MEMALIGN)
	;;
esac

AC_CHECK_HEADERS(
    [setjmp.h],
    [AC_MSG_CHECKING([sizeof jmp_buf])
     AC_RUN_IFELSE(
	 [AC_LANG_PROGRAM(
		 [[
		     #include <stdio.h>
		     #include <setjmp.h>
		 ]],
		 [[
		     FILE *fp=fopen("conftest1","w");
		     fprintf(fp,"%lu\n",sizeof(jmp_buf));
		     fclose(fp);
		 ]])],
	 [sizeof_jmp_buf=`cat conftest1`
	  AC_MSG_RESULT($sizeof_jmp_buf)
	  AC_DEFINE_UNQUOTED(SIZEOF_JMP_BUF,$sizeof_jmp_buf,[sizeof jmp_buf])],
	 [AC_MSG_RESULT([no])])])

# sysconf
AC_CHECK_HEADERS(
    [unistd.h],
    [AC_CHECK_LIB(
	    [c],[sysconf],
	    [AC_MSG_CHECKING([_SC_CLK_TCK])
	     hz=0
	     AC_RUN_IFELSE(
		 [AC_LANG_PROGRAM(
			 [[
			     #include <unistd.h>
			     #include <stdio.h>
			 ]],
			 [[
			     FILE *fp=fopen("conftest1","w");
			     fprintf(fp,"%lu\n",sysconf(_SC_CLK_TCK));
			     fclose(fp);
			 ]],
			 [hz=`cat conftest1`
			  AC_DEFINE_UNQUOTED(HZ,$hz,[time system constant])])])
	     AC_MSG_RESULT($hz)])])


rm -f makedefsafter

AC_ARG_ENABLE([dynsysgmp],
	      [  --enable-dynsysgmp will link against the system libgmp3 overriding certain functions with patched versions from the local source])

if test "$enable_dynsysgmp" != "no" ; then
    AC_CHECK_HEADERS(
	[gmp.h],
	[AC_CHECK_LIB(
		[gmp],[__gmpz_init],
		[AC_MSG_CHECKING([for external gmp version])
		 AC_RUN_IFELSE(
		     [AC_LANG_PROGRAM(
			     [[
				 #include <gmp.h>
			     ]],
			     [[
				 #if __GNU_MP_VERSION > 3
				 return 0;
				 #else
				 return -1;
				 #endif
			     ]])],
		     [AC_MSG_RESULT([good])
		      TLIBS="$TLIBS -lgmp"
		      echo "#include \"gmp.h\"" >foo.c
		      echo "int main() {return 0;}" >>foo.c
		      MP_INCLUDE=`cpp foo.c | $AWK '/(\/|\\\\)gmp.h/ {if (!i) print $3;i=1}' | tr -d '"'`
		      rm -f foo.c])])])
    
    if test "$MP_INCLUDE" = "" ; then
	AC_MSG_RESULT([Cannot use dynamic gmp lib])
    fi
    
fi


if test "$MP_INCLUDE" = "" ; then			
    
    GMPDIR=gmp4
    AC_MSG_CHECKING([doing configure in gmp directory])
    echo
    echo "#"
    echo "#"
    echo "# -------------------"
    echo "# Subconfigure of GMP"
    echo "#"
    echo "#"
    
    if test "$use_common_binary" = "yes"; then
	cd $GMPDIR && ./configure --build=$host && cd ..
    else
	cd $GMPDIR && ./configure --host=$host --build=$build && cd ..
    fi
    #MY_SUBDIRS="$MY_SUBDIRS $GMPDIR" 
    
    echo "#"
    echo "#"
    echo "#"
    echo "# Subconfigure of GMP done"
    echo "# ------------------------"
    echo "#"
    
    if test "$MP_INCLUDE" = "" ; then
	cp $GMPDIR/gmp.h h/gmp.h
	MP_INCLUDE=h/gmp.h
	MPFILES=gmp_all
    fi
fi

AC_MSG_CHECKING([for leading underscore in object symbols])
cat>foo.c <<EOFF
#include <math.h>
#include <stdio.h>
int main() {FILE *f;double d=0.0;getc(f);d=cos(d);return 0;}
EOFF
$CC -c foo.c -o foo.o
if nm foo.o |grep " U " | grep "_cos" >/dev/null || nm foo.o |grep " U " | grep " _getc" >/dev/null ; then
    LEADING_UNDERSCORE=1
    AC_DEFINE(LEADING_UNDERSCORE,1,[symbol name mangling convention])
    AC_MSG_RESULT("yes")
else
    LEADING_UNDERSCORE=""
    AC_MSG_RESULT("no")
fi


AC_MSG_CHECKING("for GNU ld option -Map")
touch map
$CC -o foo [ -Wl,-Map ] map foo.o >/dev/null 2>&1
if test `cat map | wc -l` != "0" ; then
    AC_MSG_RESULT("yes")
    AC_DEFINE(HAVE_GNU_LD,1,[gnu linker present])
    GNU_LD=1
else
    AC_MSG_RESULT("no")
    GNU_LD=
fi
rm -f foo.c foo.o foo map

AC_MSG_CHECKING([for size of gmp limbs])
AC_RUN_IFELSE([AC_LANG_PROGRAM([[
				   #include <stdio.h>
				   #include "$MP_INCLUDE"
			       ]],
			       [[
				   FILE *fp=fopen("conftest1","w");
				   fprintf(fp,"%u",sizeof(mp_limb_t));
				   fclose(fp);
			       ]])],[mpsize=`cat conftest1`],[AC_MSG_ERROR([Cannot determine mpsize])])
AC_DEFINE_UNQUOTED(MP_LIMB_BYTES,$mpsize,[sizeof mp_limb in gmp library])
AC_MSG_RESULT($mpsize) 

AC_MSG_CHECKING([_SHORT_LIMB])
AC_RUN_IFELSE([AC_LANG_PROGRAM([[
				   #include <stdio.h>
				   #include "$MP_INCLUDE"
			       ]],
			       [[
				   #ifdef _SHORT_LIMB
				   return 0;
				   #else
				   return 1;
				   #endif
			       ]])],[AC_DEFINE(__SHORT_LIMB,1,[short gmp3 limbs]) AC_MSG_RESULT(yes)],[AC_MSG_RESULT(no)])

AC_MSG_CHECKING([_LONG_LONG_LIMB])
AC_RUN_IFELSE([AC_LANG_PROGRAM([[
				   #include <stdio.h>
				   #include "$MP_INCLUDE"
			       ]],
			       [[
				   #ifdef _LONG_LONG_LIMB
				   return 0;
				   #else
				   return 1;
				   #endif
			       ]])],[AC_DEFINE(__LONG_LONG_LIMB,1,[long gmp3 limbs]) AC_MSG_RESULT(yes)],[AC_MSG_RESULT(no)])

GMP=1
AC_DEFINE(GMP,1,[using gmp])
AC_SUBST(GMP)
AC_SUBST(GMPDIR)
echo > makedefsafter
echo "MPFILES=$MPFILES" >> makedefsafter
echo "PATCHED_SYMBOLS=$PATCHED_SYMBOLS" >> makedefsafter
echo >> makedefsafter



#
# X windows
# 

if test "$enable_xgcl" = "yes" ; then 
    
    AC_PATH_X
    
    AC_CHECK_LIB(X11,main,
		 [X_LIBS="$X_LIBS -lX11" AC_DEFINE(HAVE_XGCL,1,[using xgcl])],
		 [AC_MSG_RESULT([missing x libraries -- cannot compile xgcl])])
    
fi


AC_SUBST(X_LIBS)
AC_SUBST(X_CFLAGS)

#
# Dynamic loading
#

if test "$enable_dlopen" = "yes" ; then
    
    AC_CHECK_LIB([dl],[dlopen],,AC_MSG_ERROR([Cannot find dlopen]))
    
    TLIBS="$TLIBS -ldl -rdynamic"
    assert_arg_to_cflags -fPIC
    AC_DEFINE(USE_DLOPEN,1,[link compiled objects via libdl])

fi

if test "$enable_statsysbfd" = "yes" || test "$enable_dynsysbfd" = "yes" ; then
    AC_CHECK_HEADERS(
	[bfd.h],
	AC_CHECK_LIB(
	    [bfd],[bfd_init],
	    #
	    # Old binutils appear to need CONST defined to const
	    #
	    AC_MSG_CHECKING([need to define CONST for bfd])
	    AC_RUN_IFELSE(
		[AC_LANG_PROGRAM(
			[[
			    #define IN_GCC
			    #include <bfd.h>
			]],
			[[
			    symbol_info t;
			]])],
		AC_MSG_RESULT([no]),
		AC_RUN_IFELSE(
		    [AC_LANG_PROGRAM(
			    [[
				#define CONST const
				#define IN_GCC
				#include <bfd.h>
			    ]],
			    [[
				symbol_info t;
			    ]])],
		    AC_MSG_RESULT([yes]) 
		    AC_DEFINE(NEED_CONST,1,[binutils requires CONST definition]),
		    AC_MSG_ERROR([cannot use bfd]),
		    AC_MSG_ERROR([cannot use bfd])),
		AC_MSG_ERROR([cannot use bfd]))
	    ,,-liberty))
    
    AC_DEFINE(HAVE_LIBBFD,1,[use libbfd])
    
    #
    # BFD boolean syntax
    #
    
    AC_MSG_CHECKING(for useable bfd_boolean)
    AC_RUN_IFELSE(
	[AC_LANG_PROGRAM(
		[[
		    #define IN_GCC
		    #include <bfd.h>
		    bfd_boolean foo() {return FALSE;}
		]],
		[[]])],
	[AC_MSG_RESULT(yes) 
	 AC_DEFINE(HAVE_BFD_BOOLEAN,1,[bfd_boolean defined])],
	[AC_MSG_RESULT(no)])
    
    #
    # bfd_link_info.output_bfd minimal configure change check
    #
    
    AC_CHECK_MEMBER([struct bfd_link_info.output_bfd],
 		    AC_DEFINE(HAVE_OUTPUT_BFD,1,[output_bfd element present]), [], 
		    [[
			#include <bfd.h>
			#include <bfdlink.h>
		    ]])
    AC_SUBST(HAVE_OUTPUT_BFD)
    
    #
    # FIXME: Need to workaround mingw before this point -- CM
    #
    if test "$enable_statsysbfd" = "yes" && ! $CC -v 2>&1 | fgrep ming > /dev/null ; then
	echo 'int main() {bfd_init();bfd_openr("/dev/null",0);return 0;}' >foo.c
	MP=`$CC [ -Wl,-M ] -static -o foo foo.c -lbfd -liberty -ldl 2>&1 | grep -v : | tr '()' '\012\012' | $AWK '{print $NF}' | sort | uniq`
	rm -f foo.c foo
	if echo $MP | tr ' ' '\012' | grep libbfd.a >/dev/null; then
	    LIBBFD="`echo $MP | tr ' ' '\012' | grep libbfd.a | $AWK '{i=split($1,A,\"/\");for (j=1;j<=i;j++) if (j>1 && A[[j]]==\"..\") {j--;i-=2;for (k=j;k<=i;k++) A[[k]]=A[[k+2]];j--;}} END {for (j=1;j<=i;j++) printf(\"%s%s\",A[[j]],j!=i ? \"/\" : \"\")}'`"
	else
	    AC_MSG_ERROR([cannot locate external libbfd.a])
	fi   
	if echo $MP | tr ' ' '\012' | grep libiberty.a >/dev/null ; then
	    LIBIBERTY="`echo $MP | tr ' ' '\012' | grep libiberty.a | $AWK '{i=split($1,A,\"/\");for (j=1;j<=i;j++) if (j>1 && A[[j]]==\"..\") {j--;i-=2;for (k=j;k<=i;k++) A[[k]]=A[[k+2]];j--;}} END {for (j=1;j<=i;j++) printf(\"%s%s\",A[[j]],j!=i ? \"/\" : \"\")}'`"
	else
	    AC_MSG_ERROR([cannot locate external libiberty.a])
	fi   
	BUILD_BFD=copy_bfd
	AC_CHECK_LIB(z,inflate,
		     [TLIBS="$TLIBS -lz"],
		     AC_MSG_ERROR([Need zlib for bfd linking]),[])
	AC_CHECK_LIB(dl,dlsym,
		     [TLIBS="$TLIBS -ldl"],
		     AC_MSG_ERROR([Need libdl for bfd linking]),[])
	AC_SUBST(BUILD_BFD)
	AC_SUBST(LIBBFD)
	AC_SUBST(LIBIBERTY)
	
    else
	TLIBS="$TLIBS -lbfd -liberty -ldl"
    fi
fi

AC_ARG_ENABLE([xdr],[  --enable-xdr=yes will compile in support for XDR])

if test "$enable_xdr" != "no" ; then
    XDR_LIB=""
    AC_CHECK_FUNC([xdr_double],XDR_LIB=" ",
		  [AC_CHECK_LIB([tirpc],[xdr_double],[XDR_LIB=tirpc],
				[AC_CHECK_LIB([gssrpc],[xdr_double],[XDR_LIB=gssrpc],
					      [AC_CHECK_LIB([rpc],[xdr_double],[XDR_LIB=rpc],
							    [AC_CHECK_LIB([oncrpc],[xdr_double],[XDR_LIB=oncrpc])])])])])
    
    if test "$XDR_LIB" != ""; then
	AC_DEFINE(HAVE_XDR,1,[have xdr extensions])
	if test "$XDR_LIB" != " "; then
	    TLIBS="$TLIBS -l$XDR_LIB"
	    add_arg_to_cflags -I/usr/include/$XDR_LIB
	fi
    fi
fi


AC_MSG_CHECKING([__builtin_clzl])
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <stdio.h>
		#include <unistd.h>
	    ]],
	    [[
		unsigned long u;
		long j;
		if (__builtin_clzl(0)!=sizeof(long)*8)
      		   return -1;
		   for (u=1,j=sizeof(long)*8-1;j>=0;j--,u<<=1)
      		       if (__builtin_clzl(u)!=j)
  			  return -1;
	    ]])],
    [AC_MSG_RESULT([yes])
     AC_DEFINE(HAVE_CLZL,[1],[clzl instruction])],
    [AC_MSG_RESULT([no])])

AC_MSG_CHECKING([__builtin_ctzl])
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <stdio.h>
		#include <unistd.h>
	    ]],
	    [[
		unsigned long u;
		long j;
		if (__builtin_ctzl(0)!=sizeof(long)*8)
      		   return -1;
		   for (u=1,j=0;j<sizeof(long)*8-1;j++,u<<=1)
      		       if (__builtin_ctzl(u)!=j)
  			  return -1;
	    ]])],
    [AC_MSG_RESULT([yes])
     AC_DEFINE(HAVE_CTZL,[1],[ctzl instruction])],
    [AC_MSG_RESULT([no])])


case $use in
    sh4*) ;;     #FIXME, these exceptions needed as of gcc 4.7
    hppa*) ;;    #FIXME
    powerpc*) ;; #FIXME
    alpha*) ;;   #FIXME
    ia64*) ;;    #FIXME
    *) 
	AC_MSG_CHECKING(__builtin___clear_cache)
	AC_RUN_IFELSE(
	    [AC_LANG_PROGRAM(
		    [[]],
		    [[
			void *v,*ve;
			__builtin___clear_cache(v,ve);
		    ]])],
	    [AC_DEFINE(HAVE_BUILTIN_CLEAR_CACHE,1,[have __builtin__clear_cache instruction])
	     AC_MSG_RESULT(yes)],
	    AC_MSG_RESULT(no));;
esac

#AC_CONFIG_SUBDIRS($MY_SUBDIRS)

# Find where Data begins.  This is used by the storage allocation
# mechanism, in the PAGE macro.  This offset is subtracted from
# addresses, in calculating a page for an address in the heap.

AC_CHECK_SIZEOF(long,0)

####  Memory areas and alignment

AC_MSG_CHECKING(for byte order)
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[]],
	    [[
		/* Are we little or big endian?  Adapted from Harbison&Steele.  */
		union {long l;char c[sizeof(long)];} u;
		u.l = 1;
		return u.c[sizeof(long)-1] ? 1 : 0;
	    ]])],[
	AC_MSG_RESULT(little)],
    [AC_MSG_RESULT(big) 
     AC_DEFINE(WORDS_BIGENDIAN,1,[big endian byte order])])
AC_SUBST(WORDS_BIGENDIAN)


AC_MSG_CHECKING(for word order)
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[]],
	    [[
		/* Are we little or big endian?  Adapted from Harbison&Steele.  */
		union {double d;int l[sizeof(double)/sizeof(int)];} u;
		u.d = 1.0;
		return u.l[sizeof(double)/sizeof(int)-1] ? 0 : 1;
	    ]])],
    [AC_MSG_RESULT(little)],
    [AC_MSG_RESULT(big) 
     AC_DEFINE(DOUBLE_BIGENDIAN,1,[big endian word order])])
AC_SUBST(DOUBLE_BIGENDIAN)

# pagewidth
AC_MSG_CHECKING(for pagewidth)
case $use in
    mips*) min_pagewidth=14;;
    *) min_pagewidth=12;;
esac
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <stdio.h>
		#include <unistd.h>
		#ifdef __CYGWIN__
		#define getpagesize() 4096
		#endif
	    ]],
	    [[
		size_t i=getpagesize(),j; 
		FILE *fp=fopen("conftest1","w");
		for (j=0;i>>=1;j++);
		    j=j<$min_pagewidth ? $min_pagewidth : j;
		    fprintf(fp,"%u",j);
	    ]])],
    [PAGEWIDTH=`cat conftest1`],
    [PAGEWIDTH=0])
AC_MSG_RESULT($PAGEWIDTH)
AC_DEFINE_UNQUOTED(PAGEWIDTH,$PAGEWIDTH,[system pagewidth])
AC_SUBST(PAGEWIDTH)

AC_MSG_CHECKING([for required object alignment])
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <stdio.h>
		#include <stdlib.h>
		#define EXTER
		#define INLINE
		#include "$MP_INCLUDE"
		#include "./h/enum.h"
		#define OBJ_ALIGN
		#include "./h/type.h"
		#include "./h/lu.h"
		#include "./h/object.h"
	    ]],
	    [[
		unsigned long i;
   		FILE *fp=fopen("conftest1","w");
		for (i=1;i && NOT_OBJECT_ALIGNED(i); i<<=1);
		    if (!i) return -1;
		       fprintf(fp,"%lu",i);
		       fclose(fp);
		       return 0;
	    ]])],
    [obj_align=`cat conftest1`
     AC_MSG_RESULT($obj_align)
     AC_DEFINE_UNQUOTED(OBJ_ALIGNMENT,$obj_align,[needed object alignment bytes])],
    [AC_MSG_ERROR([Cannot find object alignent])])

AC_MSG_CHECKING([for C extension variable alignment])
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM([[]],
		     [[
			 char *v __attribute__ ((aligned ($obj_align)));
		     ]])],[obj_align="__attribute__ ((aligned ($obj_align)))"],[AC_MSG_ERROR([Need alignment attributes])])
AC_MSG_RESULT($obj_align)
AC_DEFINE_UNQUOTED(OBJ_ALIGN,$obj_align,[can use C extension for object alignment])

AC_MSG_CHECKING([for C extension noreturn function attribute])
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM([[]],
		     [[
			 extern int v() __attribute__ ((noreturn));
		     ]])],
    [no_return="__attribute__ ((noreturn))"],[no_return=])
AC_MSG_RESULT($no_return)
AC_DEFINE_UNQUOTED(NO_RETURN,$no_return,[can use C extension for functions that do not return])

AC_MSG_CHECKING([sizeof struct contblock])

AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <stdio.h>
		#include <stdlib.h>
		#define EXTER
		#define INLINE
		#include "$MP_INCLUDE"
		#include "h/enum.h"
		#include "h/type.h"
		#include "h/lu.h"
		#include "h/object.h"
	    ]],
	    [[
		FILE *f=fopen("conftest1","w");
		fprintf(f,"%u",sizeof(struct contblock));
		fclose(f);
	    ]])],
    [sizeof_contblock=`cat conftest1`],
    [AC_MSG_ERROR([Cannot find sizeof struct contblock])],
    [AC_MSG_ERROR([Cannot find sizeof struct contblock])])

AC_MSG_RESULT($sizeof_contblock)
AC_DEFINE_UNQUOTED(SIZEOF_CONTBLOCK,$sizeof_contblock,[sizeof linked list for contiguous pages])

AC_MSG_CHECKING([for sbrk])
HAVE_SBRK=""
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <unistd.h>
		#include <stdio.h>
	    ]],
	    [[
		FILE *f;
		if (!(f=fopen("conftest1","w")))
		   return -1;
		   fprintf(f,"%p",sbrk(0));
	    ]])],
    [HAVE_SBRK=1;AC_MSG_RESULT([yes])],
    AC_MSG_RESULT([no: WARNING you must be able to emulate sbrk: as on mingw or macosx]),
    AC_MSG_RESULT([no: WARNING you must be able to emulate sbrk: as on mingw or macosx]))

if test "$use" = "386-macosx" ; then
    AC_MSG_RESULT([emulating sbrk for mac]);
    HAVE_SBRK=0
fi

if test "$HAVE_SBRK" = "1" ; then
    
    AC_MSG_CHECKING([for ADDR_NO_RANDOMIZE constant])
    AC_RUN_IFELSE(
	[AC_LANG_PROGRAM(
		[[
		    #include <sys/personality.h>
 		    #include <stdio.h>
		]],
		[[
		    FILE *f;
		    if (!(f=fopen("conftest1","w"))) return -1;
		       fprintf(f,"%x",ADDR_NO_RANDOMIZE);
		]])],
	[ADDR_NO_RANDOMIZE=`cat conftest1`
	 AC_MSG_RESULT([yes $ADDR_NO_RANDOMIZE])],
	[ADDR_NO_RANDOMIZE=0
	 AC_MSG_RESULT([no assuming 0x40000])
	 AC_DEFINE_UNQUOTED(ADDR_NO_RANDOMIZE,0x40000,[punt guess for no randomize value])])
    
    AC_MSG_CHECKING([for ADDR_COMPAT_LAYOUT constant])
    AC_RUN_IFELSE(
	[AC_LANG_PROGRAM(
		[[
		    #include <sys/personality.h>
 		    #include <stdio.h>
		]],
		[[
		    FILE *f;
		    if (!(f=fopen("conftest1","w"))) return -1;
		       fprintf(f,"%x",ADDR_COMPAT_LAYOUT);
		]])],
	[ADDR_COMPAT_LAYOUT=`cat conftest1`
	 AC_MSG_RESULT([yes $ADDR_COMPAT_LAYOUT])],
	[ADDR_COMPAT_LAYOUT=0
	 AC_MSG_RESULT([no])]
	AC_DEFINE_UNQUOTED(ADDR_COMPAT_LAYOUT,0,[constant to reserve upper 3Gb for C stack]))
    
    AC_MSG_CHECKING([for ADDR_LIMIT_3GB constant])
    AC_RUN_IFELSE(
	[AC_LANG_PROGRAM(
		[[
		    #include <sys/personality.h>
 		    #include <stdio.h>
		]],
		[[
		    FILE *f;
		    if (!(f=fopen("conftest1","w"))) return -1;
		       fprintf(f,"%x",ADDR_LIMIT_3GB);
		]])],
	[ADDR_LIMIT_3GB=`cat conftest1`
	 AC_MSG_RESULT([yes $ADDR_LIMIT_3GB])],
	[ADDR_LIMIT_3GB=0
	 AC_MSG_RESULT([no])]
	AC_DEFINE_UNQUOTED(ADDR_LIMIT_3GB,0,[only 3Gb of address space]))
    
    AC_MSG_CHECKING([for personality(ADDR_NO_RANDOMIZE) support])
    AC_RUN_IFELSE(
	[AC_LANG_SOURCE(
		[[
		    #include <stdio.h>
		    #include <stdlib.h>
		    int main(int argc,char *argv[],char *envp[]) {
			#include "h/unrandomize.h"
			return 0;
		    }
		]])],
	[AC_MSG_RESULT(yes)
	 AC_DEFINE(CAN_UNRANDOMIZE_SBRK,1,[can prevent sbrk from returning random values])],
	[AC_MSG_RESULT(no)])

    AC_MSG_CHECKING([that sbrk is (now) non-random])
    SBRK=0
    AC_RUN_IFELSE(
	[AC_LANG_SOURCE(
		[[
		    #include <stdio.h>
		    #include <stdlib.h>
		    int main(int argc,char * argv[],char * envp[]) {
			FILE *f;
			#ifdef CAN_UNRANDOMIZE_SBRK
			#include "h/unrandomize.h"
			#endif
			if (!(f=fopen("conftest1","w")))
			   return -1;
			   fprintf(f,"%p",sbrk(0));
			   return 0;
		    }
		]])],[SBRK=`cat conftest1`])
    if test "$SBRK" = "0" ; then
	AC_MSG_ERROR([cannot trap sbrk])
    fi

    SBRK1=0
    AC_RUN_IFELSE(
	[AC_LANG_SOURCE(
		[[
		    #include <stdio.h>
		    #include <stdlib.h>
		    int main(int argc,char * argv[],char * envp[]) {
			FILE *f;
			#ifdef CAN_UNRANDOMIZE_SBRK
			#include "h/unrandomize.h"
			#endif
			if (!(f=fopen("conftest1","w"))) return -1;
			   fprintf(f,"%p",sbrk(0));
			   return 0;
		    }
		]])],[SBRK1=`cat conftest1`])
    if test "$SBRK1" = "0" ; then
	AC_MSG_ERROR([cannot trap sbrk])
    fi
    if test "$SBRK" = "$SBRK1" ; then
	AC_MSG_RESULT([yes])
    else
	AC_MSG_RESULT([no])
	echo "Cannot build with randomized sbrk. Your options:"
	echo " - upgrade to a kernel/libc that knows about personality(ADDR_NO_RANDOMIZE)"
	echo " - recompile your kernel with CONFIG_COMPAT_BRK (if it has that option)"
	echo " - run sysctl kernel.randomize_va_space=0 before using gcl"
	AC_MSG_ERROR([exiting])
    fi
fi

AC_MSG_CHECKING(CSTACK_DIRECTION)
AC_RUN_IFELSE(
    [AC_LANG_SOURCE(
	    [[
		#include <stdio.h>
		#include <stdlib.h>
		int main(int argc,char **argv,char **envp) {
		    FILE *fp = fopen("conftest1","w");
		    #ifdef CAN_UNRANDOMIZE_SBRK
		    #include "h/unrandomize.h"
		    #endif
		    fprintf(fp,"%d",(alloca(sizeof(void *))>alloca(sizeof(void *))) ? -1 : 1);
		    fclose(fp);
		    return 0;
		}]])],
    [cstack_direction=`cat conftest1`],[cstack_direction=0])
AC_DEFINE_UNQUOTED(CSTACK_DIRECTION,$cstack_direction,[whether C stack grows up or down])
AC_MSG_RESULT($cstack_direction)

AC_MSG_CHECKING([finding CSTACK_ALIGNMENT])
AC_RUN_IFELSE(
    [AC_LANG_SOURCE(
	    [[
		#include <stdio.h>
		#include <stdlib.h>
		int main(int argc,char **argv,char **envp) {
		    void *b,*c;
		    FILE *fp = fopen("conftest1","w");
		    long n;
		    #ifdef CAN_UNRANDOMIZE_SBRK
		    #include "h/unrandomize.h"
		    #endif
		    b=alloca(sizeof(b));
		    c=alloca(sizeof(c));
		    n=b>c ? b-c : c-b;
		    n=n>sizeof(c) ? n : 1;
		    fprintf(fp,"%ld",n);
		    fclose(fp);
		    return 0;
		}]])],
    [cstack_alignment=`cat conftest1`],[cstack_alignment=0])
AC_DEFINE_UNQUOTED(CSTACK_ALIGNMENT,$cstack_alignment,[C stack alignment])
AC_MSG_RESULT($cstack_alignment)

AC_ARG_ENABLE([cstackmax],[  --enable-cstackmax=xxxx will ensure that the cstack begins below xxxx or fail],
	      [if test "$enableval" != "" ; then AC_DEFINE_UNQUOTED([CSTACKMAX],$enableval,[cstack max]) fi])


AC_MSG_CHECKING(CSTACK_ADDRESS)
AC_RUN_IFELSE(
    [AC_LANG_SOURCE(
	    [[
		#include <stdio.h>
		#include <stdlib.h>
		int
		main(int argc,char **argv,char **envp) {
		    FILE *fp = fopen("conftest1","w");
		    unsigned long i,j;
		    
		    #ifdef CAN_UNRANDOMIZE_SBRK
		    #include "h/unrandomize.h"
		    #endif
		    j=1;
		    j<<=$PAGEWIDTH;
		    j<<=16;
		    i=(unsigned long)alloca(sizeof(void *));
		    if ($cstack_direction==1) i-=j;
		    j--;
		    i+=j;
		    i&=~j;
		    fprintf(fp,"0x%lx",i-1);
		    fclose(fp);
		    return 0;
		}]])],
    [cstack_address=`cat conftest1`],[cstack_address=0])
AC_DEFINE_UNQUOTED(CSTACK_ADDRESS,$cstack_address,[starting C stack address])
AC_MSG_RESULT($cstack_address)

AC_MSG_CHECKING([cstack bits])
AC_RUN_IFELSE(
    [AC_LANG_SOURCE(
	    [[
		#include <stdio.h>
		#include <stdlib.h>
		int
		main(int argc,char **argv,char **envp) {
		    void *v ;
		    FILE *fp = fopen("conftest1","w");
		    long i,j;
		    
		    #ifdef CAN_UNRANDOMIZE_SBRK
		    #include "h/unrandomize.h"
		    #endif
		    j=1;
		    j<<=$PAGEWIDTH;
		    j<<=16;
		    i=(long)&v;
		    if ($cstack_direction==1) i-=j;
		    j--;
		    i+=j;
		    i&=~j;
		    for (j=0;(i>>j)!=(i>>(sizeof(long)*8-1));j++);
		    fprintf(fp,"%ld",j);
		    fclose(fp);
		    return 0;
		}]])],
    [cstack_bits=`cat conftest1`],[cstack_bits=0])
AC_DEFINE_UNQUOTED(CSTACK_BITS,$cstack_bits,[log starting C stack address])
AC_MSG_RESULT($cstack_bits)

AC_MSG_CHECKING(NEG_CSTACK_ADDRESS)
AC_RUN_IFELSE(
    [AC_LANG_SOURCE(
	    [[
		#include <stdio.h>
		#include <stdlib.h>
		int
		main(int argc,char **argv,char **envp) {
		    #ifdef CAN_UNRANDOMIZE_SBRK
		    #include "h/unrandomize.h"
		    #endif
		    return (long)$cstack_address<0 ? 0 : -1;
		}]])],
    [AC_MSG_RESULT(yes)
     neg_cstack_address=1
     AC_DEFINE(NEG_CSTACK_ADDRESS,1,[C stack address is negative])],
    [AC_MSG_RESULT(no)
     neg_cstack_address=0])

AC_ARG_ENABLE([immfix],[  --enable-immfix will enable an immediate fixnum table above the C stack])

AC_ARG_ENABLE([fastimmfix],[  --enable-fastimmfix=XXXX will reject low immediate fixnums unless 2^XXX can be attained],,[enable_fastimmfix=64])


if test "$use" != "386-gnu" ; then #hurd can push .data below C stack, but sbrk(0) remains above, foiling unexec
    
    AC_MSG_CHECKING([finding default linker script])
    touch unixport/gcl.script
    echo "int main() {return 0;}" >foo.c
    $CC $LDFLAGS -Wl,--verbose foo.c -o foo 2>&1 | \
	$AWK '/==================================================/ {i=1-i;next} {if (i) print}' >gcl.script
    rm -rf foo.c foo
    
    if test "`cat gcl.script | wc -l`" != "0" ; then
	AC_MSG_RESULT(got it)
	AC_MSG_NOTICE([trying to adjust text start])
	cp gcl.script gcl.script.def
	
	n=-1;
	k=0;
	lim=`$AWK 'END {printf("%d\n",m*8-2)}' m=$ac_cv_sizeof_long`;
	max=0;
	min=$lim;
	while test $n -lt $lim ; do
            j=`$AWK 'END {for (i=j=0;j<=n;j++) i=i ? i*2 : 1;printf("%x\n",3*i)}' n=$n </dev/null`
            cat gcl.script.def | $AWK '/SIZEOF_HEADERS/ {gsub("0x[[0-9]]*","0x" j,$0);} {print}' j=$j >gcl.script
	    #        diff -u gcl.script.def gcl.script
            echo "int main() {return 0;}" >foo.c
            if ( $CC $LDFLAGS -Wl,-T gcl.script foo.c -o foo && ./foo ) >/dev/null 2>&1 ; then
		if test $n -lt $min ; then min=$n; fi; 
		if test $n -gt $max; then max=$n; fi; 
	    elif test $max -gt 0 ; then
		break;
            fi;
            n=`$AWK 'END {print n+1}' n=$n </dev/null`
	done	  
	
	AC_MSG_NOTICE([min log text start $min])
	AC_MSG_NOTICE([max log text start $max])
	
	if test $neg_cstack_address -eq 1 ; then #FIXME test this
      	    if test $cstack_bits -lt $max ; then
		max=$cstack_bits;
		AC_MSG_NOTICE([max log text start reduced to $max considering c stack address])
	    fi	
	fi
	
	j=-1;
	low_shft="";
	if test $min -le $max ; then
            if test $max -ge $enable_fastimmfix && test "$enable_immfix" != "no" ; then
	        j=`$AWK 'END {for (i=j=0;j<=n;j++) i=i ? i*2 : 1;printf("%x\n",3*i)}' n=$max </dev/null`
		low_shft=`$AWK 'END {print n+1}' n=$max </dev/null`;
		AC_MSG_NOTICE([raising log text to $j for a $max bit wide low immfix table])
            else					
	        j=`$AWK 'END {for (i=j=0;j<=n;j++) i=i ? i*2 : 1;printf("%x\n",3*i)}' n=$min </dev/null`
		AC_MSG_NOTICE([lowering log text to $j to maximize data area])
	    fi
	fi
	
	if test "$low_shft" != "" ; then
            AC_DEFINE_UNQUOTED(LOW_SHFT,$low_shft,[upper immediate fixnum bound])      	 
            AC_DEFINE_UNQUOTED(OBJNULL,(object)0x$j,[lowest address non-object])      	 
	else				   
            AC_DEFINE_UNQUOTED(OBJNULL,NULL,[lowest address non-object])      	 
	fi					     
	
	#      echo $j;
	AC_MSG_CHECKING([our linker script])
	if test "$j" -ne "-1" ; then
            cat gcl.script.def | $AWK '/SIZEOF_HEADERS/ {gsub("0x[[0-9]]*","0x" j,$0);} {print}' j=$j >gcl.script
	    AC_MSG_RESULT([done])
	    rm -f gcl.script.def
	    assert_arg_to_ldflags -Wl,-T,gcl.script
	    cp gcl.script unixport
	else
	    AC_MSG_RESULT([none found or not needed])
	    rm -f gcl.script gcl.script.def
	fi
	rm -rf foo.c foo
    else
        AC_DEFINE_UNQUOTED(OBJNULL,NULL,[lowest address non-object])
	AC_MSG_RESULT([not found])
    fi 
    
else
    
    AC_DEFINE_UNQUOTED(OBJNULL,NULL,[lowest address non-object])      	 
    
fi

mem_top=0
mem_range=0
AC_MSG_CHECKING(mem top)
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <stdio.h>
	    ]],
	    [[
		unsigned long i,j,k,l;
		FILE *fp = fopen("conftest1","w");
		
		for (i=2,k=1;i;k=i,i<<=1);
		    l=$cstack_address;
		    l=$cstack_direction==1 ? (l<k ? k-1 : -1) : l;
		    for (i=j=k;j && i<l;j>>=1,i|=j);
			if (j<(k>>3)) i=0;
			   j=1;
			   j<<=$PAGEWIDTH;
			   j<<=4;
			   j--;
			   i+=j;
			   i&=~j;
			   fprintf(fp,"0x%lx",i);
			   fclose(fp);
			   return 0;
	    ]])],
    [mem_top=`cat conftest1`],[mem_top="0x0"])
AC_MSG_RESULT($mem_top)

if test "$mem_top" != "0x0" ; then
    AC_MSG_CHECKING(finding upper mem half range)
    AC_RUN_IFELSE(
	[AC_LANG_PROGRAM(
		[[
		    #include <stdio.h>
		]],
		[[
		    unsigned long j;
		    FILE *fp = fopen("conftest1","w");
		    
		    for (j=1;j && !(j& $mem_top);j<<=1);
			fprintf(fp,"0x%lx",j>>1);
			fclose(fp);
			return 0;
		]])],
	[mem_range=`cat conftest1`],[mem_range="0x0"])
    AC_MSG_RESULT($mem_range)
    if test "$mem_range" != "0x0" ; then 
	AC_DEFINE_UNQUOTED(MEM_TOP,$mem_top,[beginning address for immediate fixnum range])
        AC_DEFINE_UNQUOTED(MEM_RANGE,$mem_range,[size of immediate fixnum address space])
    fi
fi

if test "$enable_immfix" != "no" ; then
    if test "$mem_top" != "0x0" ; then
	if test "$mem_range" != "0x0" ; then 
	    AC_DEFINE_UNQUOTED(IM_FIX_BASE,$mem_top,[beginning address for immediate fixnum range])
            AC_DEFINE_UNQUOTED(IM_FIX_LIM,$mem_range,[size of immediate fixnum address space])
	fi
    fi
fi

AC_MSG_CHECKING([sizeof long long int])
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <stdio.h>
	    ]],
	    [[
		if (sizeof(long long int) == 2*sizeof(long)) return 0;
		   return 1;
	    ]])],
    [AC_DEFINE(HAVE_LONG_LONG,1,[long long is available]) AC_MSG_RESULT(yes)],
    [AC_MSG_RESULT(no)])

AC_SUBST(HAVE_LONG_LONG)

AC_CHECK_HEADERS([dirent.h],
		 AC_MSG_CHECKING([for d_type])
		 AC_RUN_IFELSE(
		     [AC_LANG_PROGRAM(
			     [[
				 #include <dirent.h>
				 #include <string.h>
			     ]],
			     [[
				 struct dirent *d;
				 DIR *r=opendir("./");
				 for (;(d=readdir(r)) && strcmp("configure",d->d_name););
				 return d && d->d_type==DT_REG ? 0 : -1;
			     ]])],
		     [AC_MSG_RESULT([yes]) AC_DEFINE(HAVE_D_TYPE,1,[have struct dirent d_type field])],
		     AC_MSG_RESULT([no]),AC_MSG_RESULT([no])))

# ansi lisp
SYSTEM=ansi_gcl
CLSTANDARD=ANSI
AC_ARG_ENABLE([ansi],[  --enable-ansi builds a large gcl aiming for ansi compliance],
	      [if test "$enable_ansi" = "no" ; then
		   SYSTEM=gcl
		   CLSTANDARD=CLtL1
	       else
	           AC_DEFINE([ANSI_COMMON_LISP],[1],[ANSI compliant image])
	       fi],
	       [AC_DEFINE([ANSI_COMMON_LISP],[1],[ANSI compliant image])])

FLISP="saved_$SYSTEM"
AC_SUBST(FLISP)
AC_SUBST(SYSTEM)
AC_SUBST(CLSTANDARD)

# Maximum number of pages



# Check if Posix compliant getcwd exists, if not we'll use getwd.
AC_CHECK_FUNCS(getcwd)
AC_CHECK_FUNCS(getwd)
AC_CHECK_FUNC(uname, , AC_DEFINE(NO_UNAME,1,[no uname call]))
AC_CHECK_FUNC(gettimeofday, , AC_DEFINE(NO_GETTOD))


AC_CHECK_HEADERS(sys/ioctl.h)

# OpenBSD has elf_abi.h instead of elf.h
AC_CHECK_HEADERS(elf.h elf_abi.h)

AC_CHECK_HEADERS(sys/sockio.h)


#--------------------------------------------------------------------
#	The code below deals with several issues related to gettimeofday:
#	1. Some systems don't provide a gettimeofday function at all
#	   (set NO_GETTOD if this is the case).
#	2. SGI systems don't use the BSD form of the gettimeofday function,
#	   but they have a BSDgettimeofday function that can be used instead.
#	3. See if gettimeofday is declared in the <sys/time.h> header file.
#	   if not, set the GETTOD_NOT_DECLARED flag so that tclPort.h can
#	   declare it.
#--------------------------------------------------------------------

AC_CHECK_FUNC([BSDgettimeofday],
              [AC_DEFINE(HAVE_BSDGETTIMEOFDAY,1,[have bsdgettimeofday])],
	      [AC_CHECK_FUNC([gettimeofday], ,
	                     [AC_DEFINE([NO_GETTOD],1,[no gettimeofday call])])])

AC_EGREP_HEADER([gettimeofday],
                [sys/time.h],
		[AC_MSG_CHECKING([for gettimeofday declaration])
		 AC_MSG_RESULT([present])],
		[AC_MSG_CHECKING([for gettimeofday declaration])
		 AC_MSG_RESULT([missing])
		 AC_DEFINE(GETTOD_NOT_DECLARED,1,[No gettimeofday call -- fixme])])


AC_CHECK_LIB(m,sin,LIBS="${LIBS} -lm",true)
AC_CHECK_LIB(mingwex,main,LIBS="${LIBS} -lmingwex",true)

AC_MSG_CHECKING([for buggy maximum sscanf length])
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <stdio.h>
	    ]],
	    [[
		char *s= "2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931277361782154249992295763514822082698951936680331825288693984964651058209392398294887933203625094431173012381970684161404";
		int n, m;
		double f;
		char *endptr;
		FILE *fp=fopen("conftest1","w");
		
		n=sscanf(s,"%lf%n",&f,&m);
		fprintf(fp,"%d",m);
		fclose(fp);
		return s[m];
	    ]])],
    [AC_MSG_RESULT([none])],
    [buggy_maximum_sscanf_length=`cat conftest1`
     AC_MSG_RESULT([$buggy_maximum_sscanf_length])
     AC_DEFINE_UNQUOTED(BUGGY_MAXIMUM_SSCANF_LENGTH,$buggy_maximum_sscanf_length,[sscanf terminates prematurely (Windows XP)])])


EXTRA_LOBJS=
AC_ARG_ENABLE([japi],[  --enable-japi=yes will compile in support for the JAPI graphical interface if present on your system],
	      [if test "$enable_japi" = "yes" ; then
		   AC_CHECK_HEADERS([japi.h],
				    [AC_DEFINE(HAVE_JAPI_H)
				     EXTRA_LOBJS="${EXTRA_LOBJS} gcl_japi.o"
				     LIBS="${LIBS} -ljapi -lwsock32"])
	       fi])

# Should really find a way to check for prototypes, but this 
# basically works for now.  CM
#
AC_CHECK_HEADERS(math.h,AC_DEFINE(HAVE_MATH_H,1,[have math.h]))
AC_CHECK_HEADERS(complex.h,AC_DEFINE(HAVE_COMPLEX_H,1,[have complex.h]))

#
# For DBL_MAX et. al. on (only) certain Linux arches, apparently CM
#
AC_CHECK_HEADERS(values.h,AC_DEFINE(HAVE_VALUES_H,1,[have values.h]))

#
# Sparc solaris keeps this in float.h, rework either/or with values.h later
#
AC_CHECK_HEADERS(float.h,AC_DEFINE(HAVE_FLOAT_H,1,[have float.h]))

#
# The second alternative is for solaris.  This needs to be
# a more comprehensive later, i.e. checking that the fpclass
# test makes sense.  CM
#
AC_MSG_CHECKING([for isnormal])
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#define _GNU_SOURCE
		#include <math.h>
	    ]],
	    [[
		float f;
		return isnormal(f) || !isnormal(f) ? 0 : 1;
	    ]])],
    [AC_DEFINE(HAVE_ISNORMAL,1,[Have isnormal function]) AC_MSG_RESULT(yes)],
    [AC_MSG_CHECKING([for fpclass of ieeefp.h])
     AC_RUN_IFELSE(
	 [AC_LANG_PROGRAM(
		 [[
		     #include <ieeefp.h>
		 ]],
		 [[
		     float f;
		     return fpclass(f)>=FP_NZERO || fpclass(f)<FP_NZERO ? 0 : 1;
		 ]])],[AC_DEFINE(HAVE_IEEEFP,1,[Have ieeefp fpclass function]) AC_MSG_RESULT(yes)],
	 [AC_MSG_RESULT(no)])])

AC_MSG_CHECKING([for isfinite])
AC_RUN_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#define _GNU_SOURCE
		#include <math.h>
	    ]],
	    [[
		float f;
		return isfinite(f) || !isfinite(f) ? 0 : 1;
	    ]])],[AC_DEFINE(HAVE_ISFINITE,1,[Have isfinite function]) AC_MSG_RESULT(yes)],
    [AC_MSG_CHECKING([for finite()])
     AC_RUN_IFELSE(
	 [AC_LANG_PROGRAM(
		 [[
		     #include <math.h>
		     #include <ieeefp.h>
		 ]],
		 [[
		     float f;
		     return finite(f) || !finite(f) ? 0 : 1;
		 ]])],
	 [AC_DEFINE(HAVE_FINITE,1,[Have finite function]) AC_MSG_RESULT(yes)],
   	 [AC_MSG_ERROR(no)])])

#--------------------------------------------------------------------
#	Check for the existence of the -lsocket and -lnsl libraries.
#	The order here is important, so that they end up in the right
#	order in the command line generated by make.  Here are some
#	special considerations:
#	1. Use "connect" and "accept" to check for -lsocket, and
#	   "gethostbyname" to check for -lnsl.
#	2. Use each function name only once:  can't redo a check because
#	   autoconf caches the results of the last check and won't redo it.
#	3. Use -lnsl and -lsocket only if they supply procedures that
#	   aren't already present in the normal libraries.  This is because
#	   IRIX 5.2 has libraries, but they aren't needed and they're
#	   bogus:  they goof up name resolution if used.
#	4. On some SVR4 systems, can't use -lsocket without -lnsl too.
#	   To get around this problem, check for both libraries together
#	   if -lsocket doesn't work by itself.
#--------------------------------------------------------------------
AC_MSG_CHECKING([for sockets])
tcl_checkBoth=0
AC_CHECK_FUNC(connect, tcl_checkSocket=0, tcl_checkSocket=1)
if test "$tcl_checkSocket" = 1; then
    AC_CHECK_LIB(socket, main, TLIBS="$TLIBS -lsocket", tcl_checkBoth=1)
fi


if test "$tcl_checkBoth" = 1; then
    tk_oldLibs=$TLIBS
    TLIBS="$TLIBS -lsocket -lnsl"
    AC_CHECK_FUNC(accept, tcl_checkNsl=0, [TLIBS=$tk_oldLibs])
fi
AC_CHECK_FUNC(gethostbyname, , AC_CHECK_LIB(nsl, main, [TLIBS="$TLIBS -lnsl"]))

# readline
AC_ARG_ENABLE(readline,[  --enable-readline    enables command line completion via the readline library ])

if test "$use" = "mingw" ; then
    enable_readline=no
fi

if test "$enable_readline" != "no" ; then
    AC_CHECK_HEADERS([readline/readline.h],
			AC_CHECK_LIB([readline],[rl_initialize],
				[AC_DEFINE(USE_READLINE,1,[use readline library])
				 AC_CHECK_LIB([readline],[el_getc],AC_DEFINE(READLINE_IS_EDITLINE,1,[readline is editline]))
				 # These tests discover differences between readline 4.1 and 4.3
				 AC_CHECK_LIB([readline],[rl_completion_matches],
					[AC_DEFINE(HAVE_DECL_RL_COMPLETION_MATCHES,1,[have readline completion matches])
				 	 AC_DEFINE(HAVE_RL_COMPENTRY_FUNC_T,1,[have readline completion matches])])
			         AC_MSG_CHECKING([RL_COMPLETION_ENTRY_FUNCTION_TYPE_FUNCTION])
                                 AC_COMPILE_IFELSE(
                                        [AC_LANG_PROGRAM(
	                                       [[
                                                  #include <readline/readline.h>
						  extern Function *rl_completion_entry_function __attribute__((weak));
	    				       ]],
					       [[]])],
                                        [AC_DEFINE(RL_COMPLETION_ENTRY_FUNCTION_TYPE_FUNCTION,1,[rl_completion_entry_function returns type Function])
                                         AC_MSG_RESULT([yes])],
                                        [AC_MSG_RESULT([no])
                                         AC_MSG_CHECKING([RL_COMPLETION_ENTRY_FUNCTION_TYPE_RL_COMPENTRY_FUNC_T])
                                         AC_COMPILE_IFELSE(
                                                [AC_LANG_PROGRAM(
	                                               [[
		                                          #include <readline/readline.h>
							  extern rl_compentry_func_t *rl_completion_entry_function __attribute__((weak));
	                                               ]],
						       [[]])],
	                                        [AC_DEFINE(RL_COMPLETION_ENTRY_FUNCTION_TYPE_RL_COMPENTRY_FUNC_T,1,[rl_completion_entry_function returns type rl_compentry_func_t])
	                                         AC_MSG_RESULT([yes])],
                                                [AC_MSG_RESULT([no])
	                                         AC_MSG_ERROR([Unknown rl_completion_entry_function return type])])])

                                 AC_MSG_CHECKING([RL_READLINE_NAME_TYPE_CHAR])
                                 AC_COMPILE_IFELSE(
                                        [AC_LANG_PROGRAM(
	                                       [[
		                                  #include <readline/readline.h>
		                                  extern char *rl_readline_name __attribute__((weak));
	                                       ]],
	                                       [[]])],
                                        [AC_DEFINE(RL_READLINE_NAME_TYPE_CHAR,1,[rl_readline_name returns type char])
                                         AC_MSG_RESULT([yes])],
                                        [AC_MSG_RESULT([no])
                                         AC_MSG_CHECKING([RL_READLINE_NAME_TYPE_CONST_CHAR])
                                         AC_COMPILE_IFELSE(
                                                [AC_LANG_PROGRAM(
	                                               [[
		                                          #include <readline/readline.h>
		                                          extern const char *rl_readline_name __attribute__((weak));
	                                               ]],
	                                               [[]])],
                                                [AC_DEFINE(RL_READLINE_NAME_TYPE_CONST_CHAR,1,[rl_readline_name returns type const char])
                                                 AC_MSG_RESULT([yes])],
                                                [AC_MSG_RESULT([no])
						 AC_MSG_ERROR([Unknown rl_readline_name return type])])])
			 	 TLIBS="$TLIBS -lreadline" #some machines don't link this, e.g. Slackware
				 RL_OBJS=gcl_readline.o]),
				 [],[AC_INCLUDES_DEFAULT([#include <stdio.h>])])
fi

AC_SUBST(RL_OBJS)
AC_SUBST(RL_LIB)

# sockets

AC_MSG_CHECKING([For network code for nsocket.c])
AC_LINK_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <sys/time.h>
		#include <sys/types.h>
		#include <unistd.h>
		
		#include <errno.h>
		#include <fcntl.h>
		#include <stdio.h>
		
		/************* for the sockets ******************/ 
		#include <sys/socket.h>		/* struct sockaddr, SOCK_STREAM, ... */
		#ifndef NO_UNAME
		#   include <sys/utsname.h>	/* uname system call. */
		#endif
		#include <netinet/in.h>		/* struct in_addr, struct sockaddr_in */
		#include <arpa/inet.h>		/* inet_ntoa() */
		#include <netdb.h>		/* gethostbyname() */
	    ]],
	    [[
		connect(0,(struct sockaddr *)0,0);
		gethostbyname("jil");
		socket(AF_INET, SOCK_STREAM, 0);
	    ]])],
    [AC_DEFINE(HAVE_NSOCKET,1,[can use nsocket library])
     AC_MSG_RESULT([yes])],
    [AC_MSG_RESULT([no])])


AC_MSG_CHECKING([check for listen using fcntl])
AC_COMPILE_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <stdio.h>
		#include <fcntl.h>
	    ]],
	    [[
		FILE *fp=fopen("configure.in","r");
		int orig;
		orig = fcntl(fileno(fp), F_GETFL);
		if (! (orig & O_NONBLOCK )) return 0;
	    ]])],
    [AC_DEFINE(LISTEN_USE_FCNTL,1,[can use fcntl for listen function])
     AC_MSG_RESULT([yes])],
    [AC_MSG_RESULT([no])])


AC_CHECK_FUNC(profil, ,[AC_DEFINE(NO_PROFILE,1,[no profil system call])])
AC_SUBST(NO_PROFILE)
AC_CHECK_FUNC(setenv,[AC_DEFINE(HAVE_SETENV,1,[have setenv call])],no_setenv=1 )
AC_SUBST(HAVE_SETENV)
if test "$no_setenv" = "1" ; then
    AC_CHECK_FUNC(putenv,[AC_DEFINE(HAVE_PUTENV,1,[have putenv call])],)
    AC_SUBST(HAVE_PUTENV)
fi

AC_CHECK_FUNC(_cleanup, [AC_DEFINE(USE_CLEANUP,1,[have _cleanup function])],)
AC_SUBST(USE_CLEANUP)
gcl_ok=no

AC_MSG_CHECKING([FIONBIO vs. O_NONBLOCK for nonblocking I/O])

case $system in
    OSF*)
	AC_DEFINE(USE_FIONBIO,1,[use fionbio for non-blocking io])
	AC_MSG_RESULT(FIONBIO)
	;;
    SunOS-4*)
	AC_DEFINE(USE_FIONBIO,1,[use fionbio for non-blocking io])
	AC_MSG_RESULT(FIONBIO)
	;;
    ULTRIX-4.*)
	AC_DEFINE(USE_FIONBIO,1,[use fionbio for non-blocking io])
	AC_MSG_RESULT(FIONBIO)
	;;
    *)
	AC_MSG_RESULT(O_NONBLOCK)
	;;
esac


AC_MSG_CHECKING(check for SV_ONSTACK)
AC_COMPILE_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <signal.h>
		int joe=SV_ONSTACK;
	    ]],
	    [[]])],
    [AC_DEFINE(HAVE_SV_ONSTACK,1,[have sv_onstack])
     AC_SUBST(HAVE_SV_ONSTACK)
     AC_MSG_RESULT([yes])],
    [AC_MSG_RESULT([no])])

AC_MSG_CHECKING(check for SIGSYS)
AC_COMPILE_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <signal.h>
		int joe=SIGSYS;
	    ]],[[]])],
    [AC_DEFINE(HAVE_SIGSYS,1,[have SIGSYS signal])
     AC_SUBST(HAVE_SIGSYS)
     AC_MSG_RESULT([yes])],
    [AC_MSG_RESULT([no])])


AC_MSG_CHECKING(check for SIGEMT)
AC_COMPILE_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <signal.h>
		int joe=SIGEMT;
	    ]],[[]])],
    [AC_DEFINE(HAVE_SIGEMT,1,[have SIGEMT signal])
     AC_SUBST(HAVE_SIGEMT)
     AC_MSG_RESULT([yes])],
    [AC_MSG_RESULT([no])])

AC_CHECK_FUNCS(sigaltstack)
AC_CHECK_FUNCS(feenableexcept)

AC_CHECK_HEADERS(dis-asm.h,
		 MLIBS=$LIBS
		 AC_CHECK_LIB(opcodes,init_disassemble_info)
		 AC_CHECK_LIB(dl,dlopen,#opcodes changes too quickly to link directly 
			      AC_CHECK_FUNCS(print_insn_i386,LIBS="$MLIBS -ldl")))

#if test $use = "386-linux" ; then
AC_CHECK_HEADERS(asm/sigcontext.h)
AC_CHECK_HEADERS(asm/signal.h)
AC_MSG_CHECKING([for sigcontext...])
AC_COMPILE_IFELSE(
    [AC_LANG_PROGRAM(
	    [[
		#include <signal.h>
	    ]],
	    [[
		struct sigcontext foo;
	    ]])],
    [AC_DEFINE(SIGNAL_H_HAS_SIGCONTEXT,1,[have sigcontext of signal.h])
     AC_MSG_RESULT([sigcontext of signal.h])],
    [AC_MSG_RESULT([sigcontext NOT of signal.h])
     AC_MSG_CHECKING([for sigcontext...])
     AC_COMPILE_IFELSE(
	 [AC_LANG_PROGRAM(
		 [[
		     #include <signal.h>
		     #ifdef HAVE_ASM_SIGCONTEXT_H     
		     #include <asm/sigcontext.h>
		     #endif
		     #ifdef HAVE_ASM_SIGNAL_H          
		     #include <asm/signal.h>
		     #endif
		 ]],
		 [[
		     struct sigcontext foo;
		 ]])],
	 [AC_DEFINE(HAVE_SIGCONTEXT,1,[have sigcontext])
	  AC_MSG_RESULT(sigcontext asm files)],
	 [AC_MSG_RESULT([no sigcontext found])])])

AC_PATH_PROG(EMACS,emacs)


# check for where the emacs site lisp directory is.
rm -f conftest.el
cat >> conftest.el <<EOF
(let ((ans ".") (tem load-path) cur)
 (while (setq cur (car tem))
   (setq tem (cdr tem))
   (cond ((and (string-match "site-lisp/?$" cur) (file-directory-p cur))
          (setq ans cur)
           (if (string-match "-[0-9.]+$" cur) nil
               (setq tem nil)))))
  (message ans))
EOF

AC_MSG_CHECKING([emacs site lisp directory])
if [[ "$EMACS_SITE_LISP" = "unknown" ]] ; then
    if [[ "$EMACS" != "" ]] ; then
	EMACS_SITE_LISP=`$EMACS -q -batch --no-site-file -l conftest.el 2>&1 | sed -e /Loading/d | sed -e /load/d  |sed -e /Warning:/d`
    else
	EMACS_SITE_LISP=""
    fi
fi
AC_MSG_RESULT($EMACS_SITE_LISP)
AC_SUBST(EMACS_SITE_LISP)

# check for where the emacs site lisp default.el is
rm -f conftest.el
cat >> conftest.el <<EOF
(let ((ans "./default.el") file (tem load-path))
 (while tem
   (cond ((file-exists-p
           (setq file (expand-file-name "/default.el" (car tem))))
	  (setq tem nil) (setq ans file)))
   (setq tem (cdr tem)))
  (message ans))
EOF

AC_MSG_CHECKING([emacs default.el])
if [[ "$EMACS" != "" ]] ; then
    EMACS_DEFAULT_EL=`$EMACS -q -batch --no-site-file -l conftest.el 2>&1 | sed -e /Loading/d | sed -e /load/d |sed -e /Warning:/d`
else
    EMACS_DEFAULT_EL=""
fi
if  test -f "${EMACS_DEFAULT_EL}"  ; then true;else
    if test -d $EMACS_SITE_LISP ; then
	EMACS_DEFAULT_EL=${EMACS_SITE_LISP}/default.el
    fi
fi
AC_MSG_RESULT($EMACS_DEFAULT_EL)
AC_SUBST(EMACS_DEFAULT_EL)



# check for where the emacs site lisp info/dir is
rm -f conftest.el
cat >> conftest.el <<EOF
(let ((ans "") file (tem Info-default-directory-list))
 (while tem
   (cond ((file-exists-p (setq file (expand-file-name "dir" (car tem))))
          (setq ans (file-name-as-directory (car tem)))		
	  (setq tem nil) ))
   (setq tem (cdr tem)))
  (message ans))
EOF

AC_MSG_CHECKING([emacs info/dir])
if test "$use" = "mingw" ; then
    INFO_DIR=\$\(prefix\)/lib/gcl-$VERSION/info/
else
    if [[ "$EMACS" != "" ]] && [[ "$INFO_DIR" = "unknown" ]] ; then
	INFO_DIR=`$EMACS -q -batch --no-site-file -l conftest.el 2>&1 | sed -e /Loading/d | sed -e /load/d  |sed -e /Warning:/d`
    fi
fi

AC_MSG_RESULT($INFO_DIR)
AC_SUBST(INFO_DIR)

AC_ARG_ENABLE([tcltk],[  --enable-tcltk will try to build gcl-tk])
AC_ARG_ENABLE([tkconfig],
	      [  --enable-tkconfig=XXXX will force the use of a TK_CONFIG_PREFIX=XXXXX as place to look for tkConfig.sh and tclConfig.sh],
	      [TK_CONFIG_PREFIX=$enableval],[TK_CONFIG_PREFIX=unknown])
AC_ARG_ENABLE([tclconfig],
	      [  --enable-tclconfig=XXXX will force the use of a TCL_CONFIG_PREFIX=XXXXX as place to look for tclConfig.sh and tclConfig.sh],
	      [TCL_CONFIG_PREFIX=$enableval],[TCL_CONFIG_PREFIX=unknown])

if test "$enable_tcltk" != "no" ; then
    
    if test -d "${TCL_CONFIG_PREFIX}"  ; then true ; else
	
	AC_CHECK_PROG(TCLSH,tclsh,tclsh,${TCLSH})
	
	if test "${TCLSH}" = "" ; then true ; else
	    
	    rm -f conftest.tcl
	    cat >> conftest.tcl <<EOF
[
puts [set tcl_version]
]
EOF
	    
	    TCL_VERSION=`${TCLSH} < conftest.tcl`
	fi
	
	if test -x /usr/lib/tcl$TCL_VERSION/tclConfig.sh ; then
	    TCL_CONFIG_PREFIX=/usr/lib/tcl$TCL_VERSION
	fi
	
    fi
    
    if test -f ${TCL_CONFIG_PREFIX}/tclConfig.sh  ; then . ${TCL_CONFIG_PREFIX}/tclConfig.sh ; fi
    
    if test -d "${TK_CONFIG_PREFIX}"  ; then true ; else
	if test -f ${TCL_CONFIG_PREFIX}/tkConfig.sh ; then
	    TK_CONFIG_PREFIX=${TCL_CONFIG_PREFIX}
	else
	    if test -x `echo $TCL_CONFIG_PREFIX | sed 's,tcl,tk,g'`/tkConfig.sh ; then
		TK_CONFIG_PREFIX=`echo $TCL_CONFIG_PREFIX | sed 's,tcl,tk,g'`
	    fi      
	fi
    fi
    
    if test -f ${TK_CONFIG_PREFIX}/tkConfig.sh  ; then . ${TK_CONFIG_PREFIX}/tkConfig.sh ; fi
    
    if test -d ${TCL_CONFIG_PREFIX}/tcl${TCL_VERSION} ; then
	TCL_LIBRARY=${TCL_CONFIG_PREFIX}/tcl${TCL_VERSION}
    else
	if test -d ${TCL_CONFIG_PREFIX}/../tcl${TCL_VERSION} ; then
	    TCL_LIBRARY=${TCL_CONFIG_PREFIX}/../tcl${TCL_VERSION}
	fi
    fi
    
    if test -d ${TK_CONFIG_PREFIX}/tk${TK_VERSION} ; then
	TK_LIBRARY=${TK_CONFIG_PREFIX}/tk${TK_VERSION}
    else
	if test -d ${TK_CONFIG_PREFIX}/../tk${TK_VERSION} ; then
	    TK_LIBRARY=${TK_CONFIG_PREFIX}/../tk${TK_VERSION}
	fi
    fi
    
    if test -f ${TCL_CONFIG_PREFIX}/../include/tcl.h ; then
	TCL_INCLUDE=-I${TCL_CONFIG_PREFIX}/../include
    else
	if test -f /usr/include/tcl${TCL_VERSION}/tcl.h ; then
	    TCL_INCLUDE=-I/usr/include/tcl${TCL_VERSION}
	fi
    fi
    
    if test -f ${TK_CONFIG_PREFIX}/../include/tk.h ; then
	TK_INCLUDE=-I${TK_CONFIG_PREFIX}/../include
    else
	if test -f /usr/include/tcl${TCL_VERSION}/tk.h ; then
	    TK_INCLUDE=-I/usr/include/tcl${TCL_VERSION}
	fi	
    fi
    
    TCL_VERSION_DOT_FREE=`echo ${TCL_VERSION} | tr -d .`
    if test -f ${TK_CONFIG_PREFIX}/../bin/tcl${TCL_VERSION_DOT_FREE}.dll  ; then
	TCL_LIBS="-L${TK_CONFIG_PREFIX}/../bin -ltk${TCL_VERSION_DOT_FREE} -ltcl${TCL_VERSION_DOT_FREE}"
	TCL_STUB_LIBS="-L${TK_CONFIG_PREFIX}/lib -ltkstub${TCL_VERSION_DOT_FREE} -ltclstub${TCL_VERSION_DOT_FREE}"
    else
	AC_CHECK_LIB(lieee,main,have_ieee=1,have_ieee=0)
	if test "$have_ieee" = "0" ; then
	    TCL_LIBS=`echo ${TCL_LIBS} | sed -e "s:-lieee::g" `
	fi
	TCL_STUB_LIBS=""
    fi
    
fi

AC_SUBST(TK_CONFIG_PREFIX)

AC_SUBST(TK_LIBRARY)
AC_SUBST(TCL_LIBRARY)
AC_SUBST(TK_XINCLUDES)
AC_SUBST(TK_INCLUDE)
AC_SUBST(TCL_INCLUDE)
AC_SUBST(TK_LIB_SPEC)
AC_SUBST(TK_BUILD_LIB_SPEC)
AC_SUBST(TK_XLIBSW)
AC_SUBST(TK_XINCLUDES)
AC_SUBST(TCL_LIB_SPEC)
AC_SUBST(TCL_DL_LIBS)
AC_SUBST(TCL_LIBS)






AC_MSG_CHECKING([for tcl/tk])
if test -d "${TK_CONFIG_PREFIX}" ; then
    AC_MSG_RESULT([using TK_VERSION=${TK_VERSION} of ${TK_CONFIG_PREFIX}])
else
    AC_MSG_RESULT([not found])
fi

AC_ARG_ENABLE([notify],[  --enable-notify=no will disable the automatic notification of gcl maintainers of successful builds/problems],
	      [NOTIFY=$enable_notify
	       AC_SUBST(NOTIFY)])


# for sgbc the mprotect capabilities.

# the time handling for unixtime, add timezone
AC_CHECK_HEADERS(sys/mman.h,AC_CHECK_FUNCS(mprotect))
AC_CHECK_HEADERS(alloca.h)
AC_FUNC_ALLOCA


LDFLAGS="`echo $GPL_FLAG $LDFLAGS | sed 's,gcl.script,../unixport/gcl.script,g'`"
AC_SUBST(LDFLAGS)
LIBS="$X_PRE_LIBS $X_LIBS $X_EXTRA_LIBS $LIBS $TLIBS"
AC_SUBST(LIBS)
CFLAGS="$CFLAGS $GP_FLAG"
FINAL_CFLAGS="$CFLAGS $CPPFLAGS $X_CFLAGS"
AC_SUBST(FINAL_CFLAGS)
# Work around bug with gcc on ppc -- CM
NIFLAGS="$CFLAGS $CPPFLAGS $TONIFLAGS -I\$(GCLDIR)/o"
AC_SUBST(NIFLAGS)
CFLAGS="$CFLAGS $CPPFLAGS $TO3FLAGS -I\$(GCLDIR)/o"
AC_SUBST(CFLAGS)
O3FLAGS=$TO3FLAGS
AC_SUBST(O3FLAGS)
O2FLAGS=$TO2FLAGS
AC_SUBST(O2FLAGS)

AC_SUBST(EXTRA_LOBJS)
AC_SUBST(LEADING_UNDERSCORE)
AC_SUBST(GNU_LD)
if test -f h/$use.defs  ; then
    
    AC_SUBST(use)
    AC_CONFIG_FILES([makedefc windows/gcl.iss windows/sysdir.bat windows/install.lsp ])
AC_OUTPUT
    echo makedefc
    cat makedefc
    
    echo    add-defs1 $use
    CC=$CC ./add-defs1 $use
    
else
    echo "Unable to guess machine type"
    echo use configure --enable-machine=XXX,  for XXX such that h/XXX.defs exists, ie in h/*.defs
fi
