dnl Process this file with autoconf to produce a configure script.

#Copyright (C) 1995,1996,1997,1998,2000,2003 Free Software Foundation, Inc.

#This file is part of Gforth.

#Gforth is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.#See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.


dnl We use some automake macros here,
dnl but don't use automake for creating Makefile.in
AC_INIT([gforth],[0.6.2],[https://savannah.gnu.org/bugs/?func=addbug&group=gforth])
AC_PREREQ(2.54)
#snapshots have numbers major.minor.release-YYYYMMDD
#note that lexicographic ordering must be heeded.
#I.e., 0.4.1-YYYYMMDD must not exist before 0.4.1!
UPDATED="August 25, 2003"
AC_SUBST(UPDATED)
AC_CONFIG_HEADERS(engine/config.h)

#suppress the "-g -O2" default
test "$CFLAGS" || CFLAGS=-O2

#default setting that may be changed later:
test "$no_dynamic_default" || no_dynamic_default=0

AC_ARG_ENABLE(force-reg,
[  --enable-force-reg	  Use explicit register declarations if they appear in
			  the machine.h file. This can cause a good speedup,
			  but also incorrect code with some gcc versions on
			  some processors (default disabled).],
AC_DEFINE(FORCE_REG,,[Define if you want to use explicit register declarations for better performance or for more convenient CODE words (does not work with all GCC versions on all machines)]))

AC_ARG_WITH(debug,
[  --with-debug		  specifies option -g to compile with debug info
  --without-debug	  omits the -g switch and creates smaller images on
			  machines where "strip" has problems with gcc style
			  debugging informations.],
if test "$withval" = "yes"; then DEBUGFLAG=-g; fi)

dnl PEEPHOLE="yes"
dnl AC_ARG_WITH(peephole,
dnl [  --with-peephole	Enable peephole optimization (default)
dnl   --without-peephole	disables peephole optimization. Creates smaller,
dnl 			but slower images.],
dnl PEEPHOLE="$withval")

#variables mentioned in INSTALL
AC_ARG_VAR(CC, [The C compiler (must support GNU C 2.x); gcc-2.95 recommended.])
AC_ARG_VAR(FORTHSIZES, [Gforth command line options for the default stack and dictionary sizes (see INSTALL).])
AC_ARG_VAR(ac_cv_sizeof_char_p, [sizeof(char *)])
AC_ARG_VAR(ac_cv_sizeof_char, [sizeof(char)])
AC_ARG_VAR(ac_cv_sizeof_short, [sizeof(short)])
AC_ARG_VAR(ac_cv_sizeof_int, [sizeof(int)])
AC_ARG_VAR(ac_cv_sizeof_long, [sizeof(long)])
AC_ARG_VAR(ac_cv_sizeof_long_long, [sizeof(long long)])
AC_ARG_VAR(ac_cv_sizeof_intptr_t, [sizeof(intptr_t)])
AC_ARG_VAR(ac_cv_sizeof_int128_t, [sizeof(int128_t)])
AC_ARG_VAR(ac_cv_c_bigendian, [Is the target big-endian ("yes" or "no")?])
AC_ARG_VAR(no_dynamic_default, [run gforth with --dynamic (0) or --no-dynamic (1) by default])

#set up feature test macros, so the tests get them right:
# turn on all POSIX, SUSv3, and GNU features if available
AC_GNU_SOURCE
dnl AC_DEFINE_UNQUOTED([_GNU_SOURCE],1,[feature test macro])

dnl Don't define _POSIX_SOURCE etc. because some OSs (in particular
dnl MacOSX) disable some features then (MacOSX checks for _POSIX_SOURCE,
dnl but not for _XOPEN_SOURCE)
dnl AC_DEFINE_UNQUOTED([_POSIX_SOURCE],1,[feature test macro])
dnl AC_DEFINE_UNQUOTED([_POSIX_C_SOURCE],199506L,[feature test macro])
dnl AC_DEFINE_UNQUOTED([_XOPEN_SOURCE],600,[feature test macro])
# turn on large file support with 64-bit off_t where available
AC_SYS_LARGEFILE
dnl AC_DEFINE_UNQUOTED([_LARGEFILE_SOURCE],1,[feature test macro])
dnl AC_DEFINE_UNQUOTED([_FILE_OFFSET_BITS],64,[feature test macro])

#currently we force direct threading this way.  Eventually we should
#setup in the arch and engine files right

AC_PROG_CC

test "$GCC" = "yes" || AC_MSG_ERROR(Gforth uses GNU C extensions and requires GCC 2.0 or higher)

AC_SUBST(CC)
AC_SUBST(DEBUGFLAG)

AC_MSG_CHECKING(make type)
make_type=`make -v 2>&1|grep 'ake version'|sed 's/ version .*//'`

GNUMAKE='#'
test "$make_type" = "GNU Make" && GNUMAKE=''

AC_MSG_RESULT($make_type)

AC_SUBST(GNUMAKE)

dnl gcc-3.2 seems to work fine now
dnl AC_MSG_CHECKING(gcc version)
dnl gcc_version=`$CC -v 2>&1|grep 'gcc version'|sed 's/.*gcc version //'`
dnl AC_MSG_RESULT($gcc_version)
dnl if expr "$gcc_version" \> 3.0.4 >/dev/null && expr "$gcc_version" \< 3.2.1 >/dev/null
dnl then
dnl    no_dynamic_default=1
dnl    AC_MSG_WARN(Disabling dynamic native code generation by default (speed penalty factor ~2))
dnl fi

AC_MSG_CHECKING([whether the linker accepts -export-dynamic])
OLDLDFLAGS=$LDFLAGS
LDFLAGS="$LDFLAGS -export-dynamic"
dnl AC_TRY_LINK gives false positive on rs6000-ibm-aix4.2.1.0
dnl AC_TRY_LINK(,,ac_export_dynamic=yes,ac_export_dynamic=no)
AC_TRY_RUN(main(){exit(0);},ac_export_dynamic=yes,ac_export_dynamic=no,ac_export_dynamic=no)
test $ac_export_dynamic = yes|| LDFLAGS=$OLDLDFLAGS
AC_MSG_RESULT($ac_export_dynamic)

#terminology is a bit unusual here: The host is the system on which
#gforth will run; the system on which configure will run is the `build'
AC_CANONICAL_HOST
case "$host_cpu" in
	hppa*)
		machine=hppa
		$srcdir/mkinstalldirs arch/hppa
		AC_LIBOBJ(../arch/hppa/cache)
		#LDFLAGS="$LDFLAGS -Xlinker -N"
		LIBS="$LIBS -L/lib/pa1.1/"
		#executing dynamically generated code produced segfaults on
		#the only HPPA machine that could waste code space, so
		#disable it by default
		no_dynamic_default=1
		AC_MSG_WARN(Disabling dynamic native code generation by default (speed penalty factor ~2))
		;;
	sparc*)
		machine=sparc
		;;
	i386)
		machine=386
		CFLAGS="$CFLAGS -fomit-frame-pointer -fforce-addr"
		;;
	i486)
		machine=386
		CFLAGS="$CFLAGS -fomit-frame-pointer -fforce-addr"
		;;
	i*86)
		machine=386
		CFLAGS="$CFLAGS -fomit-frame-pointer -fforce-addr"
		CFLAGS_1="$CFLAGS"
		CFLAGS="$CFLAGS -march=pentium"
		AC_TRY_COMPILE(,,,CFLAGS="$CFLAGS_1")
		;;
#generic should work for IA64
#	ia64*)
#		machine=ia64
#		;;
        m68k)
		machine=m68k
		CFLAGS="$CFLAGS -fomit-frame-pointer"
		if test "$host_os" = "nextstep3"
		then
			AC_LIBOBJ(termios)
		fi
		;;
	mips*)
		machine=mips
		#dynamic native code has the following problems on MIPS:
		#
		#1) J/JAL seems relocatable, but is are only
		#relocatable within a 256MB-segment.  While we try to
		#get the linker to arrange this, there is no guarantee
		#that this will succeed (and if the user uses a lot of
		#memory, it is likely to fail).
		#
		#2) The way we generate dynamic native code may
		#violate MIPS architectural restrictions (in
		#particular, the delay slots of LW, MFLO, etc.)
		#
		#Therefore we disable dynamic native code by default:
		no_dynamic_default=1
		AC_MSG_WARN(Disabling dynamic native code generation by default (speed penalty factor ~2))
		;;
	alpha*)
		machine=alpha
		#full IEEE FP support for more uniformity across platforms:
		CFLAGS="$CFLAGS -mieee"
		;;
	power*|rs6000)
		machine=power
		$srcdir/mkinstalldirs arch/power
		AC_CHECK_FUNC(_sync_cache_range,[true],[AC_LIBOBJ(../arch/power/_sync_cache_range)])
		#long long is broken on (at least) gcc-2.95.* for PPC
		test x$ac_cv_sizeof_long_long = x && ac_cv_sizeof_long_long=0
		;;
	*)
		AC_MSG_WARN([Using a generic machine description])
		AC_MSG_WARN([Assuming C floats and doubles are IEEE floats and doubles (for SF@ DF@ SF! DF!)])
		AC_MSG_WARN([FLUSH-ICACHE will do nothing, so END-CODE may not work properly!])
		machine=generic
		#I-cache flushing would be needed for dynamic code generation
		no_dynamic_default=1
		AC_MSG_WARN(Disabling dynamic native code generation by default (speed penalty factor ~2))
esac
AC_SUBST(host)

dnl the following macro produces a warning with autoconf-2.1
AC_CHECK_SIZEOF(char *)
case "$ac_cv_sizeof_char_p" in
  2)
    wordsize=16
    ;;
  4)
    wordsize=32
    ;;
  8)
    wordsize=64
    ;;
esac

AC_CHECK_SIZEOF(char)
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(long long)
AC_CHECK_SIZEOF(intptr_t)
AC_CHECK_SIZEOF(int128_t)

AC_MSG_CHECKING([for a C type for cells])
ac_cv_int_type_cell=none
case "$ac_cv_sizeof_char_p" in
  $ac_cv_sizeof_int)
    ac_cv_int_type_cell=int
    ;;
  $ac_cv_sizeof_short)
    ac_cv_int_type_cell=short
    ;;
  $ac_cv_sizeof_char)
    ac_cv_int_type_cell=char
    ;;
  $ac_cv_sizeof_long)
    ac_cv_int_type_cell=long
    ;;
  $ac_cv_sizeof_long_long)
    ac_cv_int_type_cell="long long"
    ;;
  $ac_cv_sizeof_intptr_t)
    ac_cv_int_type_cell="intptr_t"
    ;;
  $ac_cv_sizeof_int128_t)
    ac_cv_int_type_cell="int128_t"
    ;;
esac
AC_MSG_RESULT($ac_cv_int_type_cell)
AC_DEFINE_UNQUOTED(CELL_TYPE,$ac_cv_int_type_cell,[an integer type that is as long as a pointer])

AC_MSG_CHECKING([for a C type for double-cells])
ac_cv_int_type_double_cell=none
case `expr 2 '*' "$ac_cv_sizeof_char_p"` in
  $ac_cv_sizeof_short)
    ac_cv_int_type_double_cell=short
    ;;
  $ac_cv_sizeof_int)
    ac_cv_int_type_double_cell=int
    ;;
  $ac_cv_sizeof_long)
    ac_cv_int_type_double_cell=long
    ;;
  $ac_cv_sizeof_long_long)
    ac_cv_int_type_double_cell="long long"
    ;;
  $ac_cv_sizeof_intptr_t)
    ac_cv_int_type_double_cell="intptr_t"
    ;;
  $ac_cv_sizeof_int128_t)
    ac_cv_int_type_double_cell="int128_t"
    ;;
esac
AC_MSG_RESULT($ac_cv_int_type_double_cell)
if test "$ac_cv_int_type_double_cell" = none; then
	AC_MSG_WARN([Emulating double-cell arithmetic. This may be slow.])
	AC_LIBOBJ(dblsub)
	AC_DEFINE(BUGGY_LONG_LONG,,[define this if there is no working DOUBLE_CELL_TYPE on your machine])
else
	AC_DEFINE_UNQUOTED(DOUBLE_CELL_TYPE,$ac_cv_int_type_double_cell,[an integer type that is twice as long as a pointer])
fi

AC_TYPE_OFF_T
AC_CHECK_SIZEOF(off_t)
test $ac_cv_sizeof_off_t -gt $ac_cv_sizeof_char_p
ac_small_off_t=$?
AC_DEFINE_UNQUOTED(SMALL_OFF_T,$ac_small_off_t,[1 if off_t fits in a Cell])

ENGINE_FLAGS=
AC_SUBST(ENGINE_FLAGS)

# Try if GCC understands -fno-gcse

AC_MSG_CHECKING([if $CC understands -fno-gcse])
CFLAGS_1="$CFLAGS"
CFLAGS="$CFLAGS -fno-gcse"
AC_TRY_COMPILE(,,ac_nogcse=yes;ENGINE_FLAGS="$ENGINE_FLAGS -fno-gcse",ac_nogcse=no)
CFLAGS="$CFLAGS_1"
AC_MSG_RESULT($ac_nogcse)

# Try if GCC understands -fno-strict-aliasing
AC_MSG_CHECKING([if $CC understands -fno-strict-aliasing])
CFLAGS_1="$CFLAGS"
CFLAGS="$CFLAGS -fno-strict-aliasing"
AC_TRY_COMPILE(,,ac_nostrictaliasing=yes;ENGINE_FLAGS="$ENGINE_FLAGS -fno-strict-aliasing",ac_nostrictaliasing=no)
CFLAGS="$CFLAGS_1"
AC_MSG_RESULT($ac_nostrictaliasing)

# Try if GCC understands -fno-crossjumping
AC_MSG_CHECKING([if $CC understands -fno-crossjumping])
CFLAGS_1="$CFLAGS"
CFLAGS="$CFLAGS -fno-crossjumping"
AC_TRY_COMPILE(,,ac_nocrossjumping=yes;ENGINE_FLAGS="$ENGINE_FLAGS -fno-crossjumping",ac_nocrossjumping=no)
CFLAGS="$CFLAGS_1"
AC_MSG_RESULT($ac_nocrossjumping)

# Try if GCC understands __attribute__((unused))
AC_MSG_CHECKING([how to suppress 'unused variable' warnings])
AC_TRY_COMPILE(,[int __attribute__((unused)) foo;], MAYBE_UNUSED='__attribute__((unused))',)
AC_DEFINE_UNQUOTED(MAYBE_UNUSED,$MAYBE_UNUSED,[attribute for possibly unused variables])
AC_MSG_RESULT($MAYBE_UNUSED)

AC_CHECK_PROG(asm_fs,asm.fs,arch/$machine/asm.fs,,$srcdir/arch/$machine)
AC_SUBST(asm_fs)

AC_CHECK_PROG(disasm_fs,disasm.fs,arch/$machine/disasm.fs,,$srcdir/arch/$machine)
AC_SUBST(disasm_fs)

AC_PATH_PROG(INSTALL_INFO,install-info,[echo '>>>>Please make info dir entry:'],$PATH:/sbin:/usr/sbin:/usr/local/sbin)

case "$host_os" in
	*win32|cygwin)
		DIRSEP="\\\\"
		PATHSEP=";"
		;;
	*darwin*)
		#Darwin uses some funny preprocessor by default; eliminate it:
		AC_MSG_NOTICE([using -no-cpp-precomp on Darwin])
		CFLAGS="$CFLAGS -no-cpp-precomp"
		DIRSEP="/"
		PATHSEP=":"
		;;
	*)
		DIRSEP="/"
		PATHSEP=":"
		;;
esac
AC_SUBST(DIRSEP)
AC_DEFINE_UNQUOTED(DIRSEP,'$DIRSEP',[a directory separator character])
AC_SUBST(PATHSEP)
AC_DEFINE_UNQUOTED(PATHSEP,'$PATHSEP',[a path separator character])


dnl Now a little support for DOS/DJGCC
AC_SUBST(GFORTH_EXE)
GFORTH_EXE=""
AC_SUBST(GFORTHFAST_EXE)
GFORTHFAST_EXE=""
AC_SUBST(GFORTHITC_EXE)
GFORTHITC_EXE=""
AC_SUBST(GFORTHDITC_EXE)
GFORTHDITC_EXE=""

AC_SUBST(FORTHSIZES)

dnl if test "$PEEPHOLE" = "yes"
dnl then
dnl    PEEPHOLEFLAG="true"
dnl    AC_DEFINE(HAS_PEEPHOLE,,[Define if you want to use peephole optimization])
dnl else
dnl    PEEPHOLEFLAG="false"
dnl fi
PEEPHOLEFLAG="true"
AC_SUBST(PEEPHOLEFLAG)

dnl copy commands for systems that don't have links
AC_SUBST(LINK_KERNL)
LINK_KERNL=""

#if test $host_os=dos
#then
#  echo Configuring for DOS!!!
#  MAKE_EXE="coff2exe gforth"
#  LINK_KERNL='$(CP) kernl32l.fi kernel.fi'
#fi

dnl the following macro produces a warning with autoconf-2.1
AC_C_BIGENDIAN
AC_SUBST(KERNEL)
dnl ac_cv_c_bigendian is an undocumented variable of autoconf-2.1
if test $ac_cv_c_bigendian = yes; then
  bytesex=b
  KERNEL="kernl16b.fi kernl16l.fi kernl32b.fi kernl32l.fi kernl64b.fi kernl64l.fi"
else
  bytesex=l
  KERNEL="kernl16l.fi kernl16b.fi kernl32l.fi kernl32b.fi kernl64l.fi kernl64b.fi"
fi

#check how to do asm(".skip 16")
AC_MSG_CHECKING([if and how we can waste code space])
skipcode=no
case $ac_cv_sizeof_long in
   4) 
      tryskip=".skip 4\n.skip 4\n.skip 4\n.skip 4"
      ;;
   8) 
      tryskip=".skip 8\n.skip 8"
      ;;
esac

for i in "$tryskip" ".block 16" ".org .+16" ".=.+16" ".space 16"
do
	AC_TRY_RUN(
int foo(int);
main()
{
  exit(foo(0)!=16);
}
int foo(int x)
{
  if (x) {
  label1:
    asm("$i");
  label2:
  }
  return (&&label2)-(&&label1);
}
	,skipcode=$i; break
	,,)
done	
AC_MSG_RESULT($skipcode)
if test "$skipcode" = no
then 
    no_dynamic_default=1
    AC_DEFINE_UNQUOTED(SKIP16,((void)0),statement for skipping 16 bytes)
    AC_MSG_WARN(Disabling dynamic native code generation by default (speed penalty factor ~2))
else
    AC_DEFINE_UNQUOTED(SKIP16,asm("$skipcode"),statement for skipping 16 bytes)
fi

AC_DEFINE_UNQUOTED(NO_DYNAMIC_DEFAULT,$no_dynamic_default,default value for no_dynamic)

dnl Checks for programs.
AC_PROG_LN_S
AC_PROG_INSTALL

dnl Checks for library functions
dnl This check is just for making later checks link with libm.
dnl using sin here is no good idea since it is built-into gcc and typechecked
AC_CHECK_LIB(m,asin)
AC_CHECK_LIB(dl,dlopen)
dnl check for ffcall libraries
dnl unfortunately, these four calls are separated out into a library each.
AC_CHECK_LIB(avcall,__builtin_avcall)
AC_CHECK_LIB(callback,__vacall_r)
AC_CHECK_LIB(vacall,vacall)
AC_CHECK_LIB(trampoline,alloc_trampoline)
FFCALLFLAG="false"
OLDCALLFLAG="true"
test $ac_cv_lib_avcall___builtin_avcall = yes && FFCALLFLAG="true" && OLDCALLFLAG="false" && AC_DEFINE(HAS_FFCALL,,[define this if you want to use the ffcall libraries])
test $ac_cv_lib_avcall___builtin_avcall = no && AC_DEFINE(HAS_OLDCALL,,[define this if you want to use the old call libraries])
AC_SUBST(FFCALLFLAG)
AC_SUBST(OLDCALLFLAG)
if test "$host_os" != "nextstep3"
then
	AC_FUNC_MEMCMP
fi
AC_REPLACE_FUNCS(memmove strtoul pow10 strerror strsignal atanh)
AC_FUNC_FSEEKO
AC_CHECK_FUNCS(ftello dlopen sys_siglist getrusage)
AC_CHECK_TYPES(stack_t,,,[#include <signal.h>])
AC_DECL_SYS_SIGLIST
AC_CHECK_FUNC(getopt_long,[true],[AC_LIBOBJ([getopt]) AC_LIBOBJ([getopt1])])
AC_CHECK_FUNCS(expm1 log1p)
AC_REPLACE_FUNCS(rint ecvt)
dnl No check for select, because our replacement is no good under
dnl anything but DOS
AC_CHECK_HEADERS(sys/mman.h fnmatch.h)
AC_FUNC_FNMATCH
test $ac_cv_func_fnmatch_works = yes || AC_LIBOBJ(fnmatch)
AC_CHECK_FUNCS(mmap sysconf getpagesize)
AM_PATH_LISPDIR

kernel_fi=kernl${wordsize}${bytesex}.fi
AC_SUBST(kernel_fi)

#this breaks bindists
#dnl replace srource directory by absolute value
#if test $srcdir = "."; then srcdir=`pwd` 
#fi

AC_SUBST(machine)
AC_CONFIG_FILES([
Makefile
Makedist
gforthmi
vmgen
machpc.fs
engine/Makefile
doc/version.texi ])
AC_CONFIG_COMMANDS([stamp-h],[[echo timestamp > stamp-h
chmod +x gforthmi
chmod +x vmgen
test -d kernel||mkdir kernel]],[[]])
AC_OUTPUT

