dnl Process this file with autoconf to produce a configure script.
AC_INIT(kaffe)

dnl Setup version number.
KVER=0.5

dnl Find the machine type and setup the relevant links
AC_CANONICAL_HOST
changequote(<<, >>)dnl
case "$host_cpu-$host_os" in

dnl =======================================================================
dnl = Need a line here for each architecture we support.  This enables the
dnl = configuration system to select the right files and fixup the bits
dnl = it cant work out for itself.
dnl =======================================================================

i[345]86-freebsd2*)
	host_cpu=i386
	host_os=freebsd2
	PIC=-fPIC
	LIBSHARE=-Bshareable
	LIBEXT=.so
	;;

dnl -----------------------------------------------------------------------
dnl This is an example configuration which doen't use shared libraries.
dnl
dnl i[345]86-freebsd2*)
dnl	host_cpu=i386
dnl	host_os=freebsd2
dnl	KAFFEVMFLAGS=-DNO_SHARED_LIBRARIES
dnl	NOSHAREDLIBS="-L../lib/native -L../lib/net -lkaffe_native -lkaffe_net"
dnl	;;
dnl
dnl -----------------------------------------------------------------------

i[345]86-linuxoldld)
	AC_MSG_ERROR(This version of Linux is not supported)
	;;

i[345]86-linux*)
	host_cpu=i386
	host_os=linux
	PIC=-fPIC
	LIBSHARE=-shared
	LIBEXT=.so
	;;

i[345]86-netbsd*)
	host_cpu=i386
	host_os=netbsd1
	PIC=-fpic
	LIBSHARE=-Bshareable
	LIBEXT=.so
	;;

i[345]86-bsdi*)
	host_cpu=i386
	version=`echo $host_os | sed -e s/bsdi//`
	host_os=bsdi
	LIBSHARE=-r
	CC=shlicc2
	LDTAIL="-lc_s.$version -lm_s.$version"
	;;

i[345]86-solaris2*)
	host_cpu=i386
	host_os=solaris2
	PIC=-fPIC
	LIBSHARE=-G
	LIBEXT=.so
	;;

i[345]86-sysv*)
	host_cpu=i386
	host_os=univel_svr4
	PIC=-fPIC
	LIBSHARE=-G
	LIBEXT=.so
	LDTAIL="-L/usr/ucblib -lucb -L/usr/local/lib/gcc-lib/i386-univel-sysv4.2MP/2.6.3 -Bstatic -lm -Bdynamic -lc -lgcc"
	;;

i[345]86-sco3.2v5*)
	host_cpu=i386
	host_os=sco3.2v5
	PIC=-fPIC
	LIBSHARE=-G
	LIBEXT=.so
	;;

dnl -----------------------------------------------------------------------
dnl Current use static kaffe version until I have time to fix NeXT config.
dnl problems.
dnl
dnl i[345]86-nextstep3)
dnl	host_cpu=i386 
dnl	host_os=nextstep3
dnl	LIBSHARE=-r
dnl	KAFFEFLAGS="-seglinkedit -all_load"
dnl	;;
dnl
dnl -----------------------------------------------------------------------

i[345]86-nextstep3)
	host_cpu=i386
	host_os=nextstep3
	KAFFEVMFLAGS=-DNO_SHARED_LIBRARIES
	NOSHAREDLIBS="-L../lib/native -L../lib/net -lkaffe_native -lkaffe_net"
	;;

dnl -----------------------------------------------------------------------
dnl This is the theoretical configurations for win32 Microsoft & Borland.
dnl

i[345]86-win32bc)
	host_cpu=i386
	host_os=win32/bc
	OBJEXT=.obj
	LIBEXT=.dll
	VPATHOPT=".c.path"
	VPATHSEP=";"
	;;

i[345]86-win32msvc)
	host_cpu=i386
	host_os=win32/mscv
	OBJEXT=.obj
	LIBEXT=.dll
	VPATHOPT="__VPATH_IGNORED"
	;;
dnl 
dnl -----------------------------------------------------------------------

sparc-netbsd*)
	host_cpu=sparc
	host_os=netbsd1
	PIC=-fpic
	LIBSHARE=-Bshareable
	LIBEXT=.so
	;;

sparc-sunos4*)
	host_cpu=sparc
	host_os=sunos
	PIC=-fPIC
	LIBSHARE=-Bdynamic
	LIBEXT=.so
	;;

sparc-solaris2*)
	host_cpu=sparc
	host_os=solaris2
	PIC=-fPIC
	LIBSHARE=-G
	LIBEXT=.so
	;;

dnl -----------------------------------------------------------------------
dnl Current use static kaffe version until I have time to fix NeXT config.
dnl problems.
dnl
dnl sparc-nextstep3)
dnl	host_cpu=sparc
dnl	host_os=nextstep3
dnl	LIBSHARE=-r
dnl	KAFFEFLAGS="-seglinkedit -all_load"
dnl	;;
dnl
dnl -----------------------------------------------------------------------

sparc-nextstep3)
	host_cpu=sparc
	host_os=nextstep3
	KAFFEVMFLAGS=-DNO_SHARED_LIBRARIES
	NOSHAREDLIBS="-L../lib/native -L../lib/net -lkaffe_native -lkaffe_net"
	;;

powerpc-linux*)
	host_cpu=powerpc
	host_os=linux
	CFLAGS=-fsigned-char
	LDTAIL=-r
	KAFFEVMFLAGS=-DNO_SHARED_LIBRARIES
	NOSHAREDLIBS="-L../lib/native -L../lib/net -lkaffe_native -lkaffe_net"
	;;

m68k-amigaos*)
	host_cpu=m68k
	host_os=amigaos
	CFLAGS="-g -O2 -fno-omit-frame-pointer"
	KAFFEVMFLAGS=-DNO_SHARED_LIBRARIES
	NOSHAREDLIBS="-L../lib/native -L../lib/net -lkaffe_native -lkaffe_net"
	VPATHSEP=" "
	;;

dnl =======================================================================
dnl = End of configurations
dnl =======================================================================

*)
	AC_MSG_ERROR(Configuration $host_cpu-$host_os not supported)
esac

dnl =======================================================================
dnl = Start of default configurations
dnl =======================================================================

if test "$OBJEXT" = "" ; then
	OBJEXT=".o"
fi
if test "$LIBEXT" = "" ; then
	LIBEXT=".a"
fi
if test "$VPATHSEP" = "" ; then
	VPATHSEP=":"
fi
if test "$VPATHOPT" = "" ; then
	VPATHOPT="VPATH"
fi

dnl =======================================================================
dnl = End of default configurations
dnl =======================================================================

changequote([, ])dnl

dnl Select either interpreter of just-in-time mode.
AC_ARG_ENABLE(jit,--enable-jit,TYPE=jit)
AC_ARG_ENABLE(interpreter,--enable-interpreter,TYPE=intrp)

if test -f $srcdir/config/$host_cpu/$host_os/jit-md.h ; then
	jitsrc="config/$host_cpu/jit-$host_cpu.def config/$host_cpu/jit-icode.h"
	jitdst="kaffevm/jit.def kaffevm/icode.h"
else
	jitsrc=""; jitdst=""
fi
dnl Make sure we support jit mode.
if test "$jitsrc" = "" && test "$enable_jit" = "yes"; then
	AC_MSG_ERROR(Configuration $host_cpu-$host_os does not support JIT mode)
fi
dnl If no mode specified, select jit if available otherwise use interpreter.
if test "$TYPE" = ""; then
	if test "$jitsrc" != "" ; then
		TYPE=jit
	else
		TYPE=intrp
	fi
fi
AC_MSG_CHECKING(machine modes)
if test $TYPE = jit ; then
	AC_MSG_RESULT(just-in-time)
else
	AC_MSG_RESULT(interpreting)
	jitsrc=""; jitdst=""
fi

AC_SUBST(KVER)
AC_SUBST(LIBSHARE)
AC_SUBST(PIC)
AC_SUBST(LDTAIL)
AC_SUBST(NOSHAREDLIBS)
AC_SUBST(LDFLAGS)
AC_SUBST(LIBEXT)
AC_SUBST(OBJEXT)
AC_SUBST(TYPE)
AC_SUBST(KAFFEFLAGS)
AC_SUBST(KAFFEVMFLAGS)
AC_SUBST(VPATHSEP)
AC_SUBST(VPATHOPT)
AC_DEFINE_UNQUOTED(LIBRARYSUFFIX, "$LIBEXT")
AC_DEFINE_UNQUOTED(KAFFEVERSION, "$KVER")

AC_LINK_FILES(config/$host_cpu/$host_os/md.h config/$host_cpu/$host_os/md.c $jitsrc, config/md.h kaffevm/md.c $jitdst)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_LN_S
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_CHECK_TOOL(RANLIB, ranlib, :)
AC_CHECK_TOOL(AR, ar)


dnl Checks for types
AC_CHECK_SIZEOF(short,0)
AC_CHECK_SIZEOF(int,0)
AC_CHECK_SIZEOF(long,0)
AC_CHECK_SIZEOF(long long,0)
AC_CHECK_SIZEOF(__int64,0)
AC_CHECK_SIZEOF(void*,0)

dnl Checks for libraries.
AC_CHECK_LIB(dl,dlopen)
AC_CHECK_LIB(dl,_dlopen)
AC_CHECK_LIB(dld,dld_init)
AC_CHECK_LIB(socket,socket)
AC_CHECK_LIB(nsl,dial)
AC_CHECK_LIB(m,finite)

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h sys/time.h unistd.h sys/socket.h winsock.h dlfcn.h windows.h winbase.h wtypes.h sys/ioctl.h sys/filio.h malloc.h sys/param.h sys/utsname.h pwd.h dir.h asm/sigcontext.h mach-o/rld.h)
AC_HEADER_DIRENT

dnl Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_TIME
AC_STRUCT_TM
AC_STRUCT_TIMEZONE
AC_C_BIGENDIAN

dnl Checks for library functions.
AC_TYPE_SIGNAL
AC_FUNC_STRFTIME
AC_CHECK_FUNCS(select socket getsockname)
AC_CHECK_FUNCS(memcpy memmove)
AC_CHECK_FUNCS(mkdir)
AC_CHECK_FUNCS(getcwd getwd gettimeofday uname getuid getpagesize)
AC_CHECK_FUNCS(remainder remainderf fmod rint floor finite isinf isnan strtod)
AC_CHECK_FUNCS(strerror hstrerror)
AC_CHECK_FUNCS(fcntl ioctl)

dnl Some dynamic library need an underscore on the loading name, some don't
AC_CACHE_CHECK(for underscore in dlsym, ac_cv_underscore,
echo "#define _LIBC_ \"`ls /lib/lib*c.so* /usr/lib/lib*c.so.* 2>/dev/null | head -1`\"" >> confdefs.h
AC_TRY_RUN(main()
{[void* lib = (void*)dlopen(_LIBC_);
  if (dlsym(lib, "_printf") != 0) { exit(0); } else { exit(1); } }],
ac_cv_underscore=yes, ac_cv_underscore=no, ac_cv_underscore=cross))
if test "$ac_cv_underscore" = "yes"; then
  AC_DEFINE(HAVE_DYN_UNDERSTORE)
fi

changequote(<<, >>)dnl
subdirs="kaffevm lib kaffe kaffeh man scripts config test"
make_output="Makefile kaffevm/Makefile kaffe/Makefile kaffeh/Makefile man/Makefile scripts/Makefile test/Makefile config/Makefile lib/Makefile lib/support/Makefile lib/native/Makefile lib/net/Makefile"
if test -d $srcdir/sun ; then
	subdirs="$subdirs sun"
	make_output="$make_output sun/Makefile"
fi
libsubdirs="native net support"
changequote([, ])dnl

AC_SUBST(subdirs)
AC_SUBST(libsubdirs)

AC_CONFIG_HEADER(config/config.h lib/support/jtypes.h)
AC_OUTPUT($make_output)

dnl Generate example environment
libdir=`eval echo $libdir` ; libdir=`eval echo $libdir`
bindir=`eval echo $bindir` ; bindir=`eval echo $bindir`
datadir=`eval echo $datadir`
echo CLASSPATH=.:$datadir/kaffe/classes.zip > ENVIRONMENT
echo KAFFEHOME=$datadir/kaffe >> ENVIRONMENT
if test "$KAFFEVMFLAGS" != "-DNO_SHARED_LIBRARIES" ; then
	echo LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:$libdir >> ENVIRONMENT
fi
echo PATH=\$PATH:$bindir >> ENVIRONMENT
echo export CLASSPATH KAFFEHOME LD_LIBRARY_PATH PATH >> ENVIRONMENT
