AC_INIT(kaffe)

dnl Setup version number.
KVER=1.00

dnl Find the compiler early on in case we need to override.
AC_PROG_CC

dnl If gcc is the compiler, compile with -Wall for lots of warnings
if test "$GCC" = "yes"; then
	CFLAGS="$CFLAGS -Wall -Wstrict-prototypes"
fi

dnl Find the machine type and setup the relevant links
AC_CANONICAL_HOST

dnl =========================================================================
dnl By default we use dynamic libraries for everything
dnl -------------------------------------------------------------------------

dynamic_libraries=yes
vm_dynamic_library=yes

dnl -------------------------------------------------------------------------

dnl =========================================================================
dnl Force use of static libraries
dnl -------------------------------------------------------------------------

AC_ARG_WITH(staticlib,[  --with-staticlib        Force the use of static libraries rather than shared])
if test "$with_staticlib" = "yes" ; then
	dynamic_libraries=no
fi

changequote(<<, >>)dnl

dnl =========================================================================
dnl Look for the configuration information
dnl -------------------------------------------------------------------------

known=no
orig_host_cpu=$host_cpu
orig_host_os=$host_os
if test -f $srcdir/config/config.alias ; then
	. $srcdir/config/config.alias
fi
if test -f $srcdir/config/$host_cpu/$host_os/config.frag ; then
	known=yes
	. $srcdir/config/$host_cpu/$host_os/config.frag
fi
if test "$known" = "no" ; then
	AC_MSG_ERROR(Configuration $host_cpu-$host_os not supported)
fi

dnl =========================================================================

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

if test "$dynamic_libraries" = "no" ; then
	KAFFEVMFLAGS="-DNO_SHARED_LIBRARIES"
	LIBEXT=".a"
fi
if test "$OBJEXT" = "" ; then
	OBJEXT=".o"
fi
if test "$LIBEXT" = "" ; then
	LIBEXT=".a"
fi
if test "$SCREXT" = "" ; then
	SCREXT=""
fi
if test "$LD" = "" ; then
	LD=ld
fi
if test "$LINKLIB" = "" ; then
	if test "$LIBEXT" = ".a" ; then
		LINKLIB='$(AR) $(LIB) $(OBJECT)\
		$(RANLIB) $(LIB)'
	else
		LINKLIB='$(LD) $(LDFLAGS) -o $(LIB) $(OBJECT) $(LDTAIL)'
	fi
fi

if test "$VPATHSEP" = "" ; then
	VPATHSEP=":"
fi
if test "$VPATHOPT" = "" ; then
	VPATHOPT="VPATH"
fi

dnl What kind of system are we using?
case $host_os in
win32*)
	SYSTEM=win32 ;;
*)
	SYSTEM=unix ;;
esac

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

changequote([, ])dnl

dnl =========================================================================
dnl Set the base set of link files - there may be more added later
dnl -------------------------------------------------------------------------

link_from="config/$host_cpu/$host_os/md.h config/$host_cpu/$host_os/md.c"
link_to="config/md.h kaffe/kaffevm/md.c"

dnl =========================================================================

dnl =========================================================================
dnl Look for configuration specific Makefile fragement and include it
dnl -------------------------------------------------------------------------

if test -f $srcdir/config/$host_cpu/$host_os/Make.frag ; then
	cpu_os_frag=$srcdir/config/$host_cpu/$host_os/Make.frag
else
	cpu_os_frag=/dev/null
fi
if test -f $srcdir/config/$host_cpu/Make.frag ; then
	cpu_frag=$srcdir/config/$host_cpu/Make.frag
else
	cpu_frag=/dev/null
fi
AC_SUBST_FILE(cpu_os_frag)dnl
AC_SUBST_FILE(cpu_frag)dnl

dnl =========================================================================

dnl =========================================================================
dnl Allow selection of a specific execution engine
dnl -------------------------------------------------------------------------

AC_ARG_WITH(engine,[  --with-engine=ENGINE    Force use of a specific execution engine])

dnl Translate 'interpreter' to 'intrp'
if test "$with_engine" = "interpreter" ; then
	with_engine=intrp
fi

dnl Save the requested engine and set the default to 'jit'
want_engine=$with_engine
if test "$with_engine" = "" ; then
	with_engine="jit"
fi

dnl -------------------------------------------------------------------------
dnl Check for JIT support and add in the necessary configuration files
dnl -------------------------------------------------------------------------

if test -f $srcdir/config/$host_cpu/$host_os/${with_engine}-md.h ; then
	link_from="$link_from config/$host_cpu/${with_engine}-$host_cpu.def config/$host_cpu/${with_engine}-icode.h config/$host_cpu/$host_os/${with_engine}-md.h"
	link_to="$link_to kaffe/kaffevm/jit.def kaffe/kaffevm/icode.h config/jit-md.h"
	jitsupport=yes
else
	with_engine="intrp"
fi
dnl Make sure we support jit mode if requested.
if test "$jitsupport" = "" && test "$want_engine" != "" && test "$want_engine" != "intrp" ; then
	AC_MSG_ERROR(Configuration $host_cpu-$host_os does not support $want_engine mode)
fi
AC_MSG_CHECKING(execution engine)
AC_MSG_RESULT($with_engine)

AC_SUBST_FILE(engine_frag)dnl
engine_frag=$srcdir/kaffe/kaffevm/$with_engine/Makefile.frag

dnl =========================================================================

AC_MSG_CHECKING(library type)
if test "$dynamic_libraries" = "no" ; then
	AC_MSG_RESULT(static)
else
	AC_MSG_RESULT(shared)
fi

dnl =========================================================================
dnl Do we include our Sun compatibility scripts?
dnl -------------------------------------------------------------------------

AC_ARG_WITH(suncompat,[  --without-suncompat     Don't include Sun compatibility scripts])
AC_MSG_CHECKING(sun compatibility mode)
if test "$with_suncompat" = "" ; then
	with_suncompat=yes
fi
if test "$with_suncompat" = "yes" ; then
	suncompatible="install-compat"
	AC_SUBST(suncompatible)
fi
AC_MSG_RESULT($with_suncompat)

dnl =========================================================================
dnl Use the new internal threading system "jthreads"
dnl -------------------------------------------------------------------------

AC_ARG_WITH(threads,[  --with-threads=SYSTEM   Define which threading system to use])
AC_MSG_CHECKING(thread system)
if test "$with_threads" = "" ; then
	with_threads=unix-jthreads
fi
if test ! -d $srcdir/kaffe/kaffevm/systems/${with_threads} ; then
	AC_MSG_ERROR(Configuration $host_cpu-$host_os does not support $with_threads threads)
fi
THREAD_SYSTEM=$with_threads
AC_MSG_RESULT($THREAD_SYSTEM)
AC_SUBST_FILE(threads_frag)dnl
threads_frag=$srcdir/kaffe/kaffevm/systems/$with_threads/Makefile.frag

dnl =========================================================================
dnl Look for trampoline code if in JIT mode
dnl -------------------------------------------------------------------------
if test "$with_engine" != "intrp" ; then
	AC_MSG_CHECKING(for trampolines)
	if test -f $srcdir/config/$host_cpu/trampolines.c ; then
		link_from="$link_from config/$host_cpu/trampolines.c"
		link_to="$link_to kaffe/kaffevm/trampolines.c"
		AC_MSG_RESULT(yes)
	else
		AC_MSG_ERROR(does not support trampolines - now mandatory)
	fi
fi

dnl =========================================================================
dnl Set install root directory which prefixes all install target dirs
dnl -------------------------------------------------------------------------

AC_ARG_WITH(installroot,[  --with-installroot=DIR  Install everything under DIR])
INSTROOT="$with_installroot"

dnl =========================================================================

dnl =========================================================================
dnl Setup for generating a depend make target
dnl -------------------------------------------------------------------------

AC_MSG_CHECKING(how to generate depend files)
if test "$DEPTOOL" = ""; then
	dnl This should just generate harmless
	dnl .d files
	DEPTOOL="true"
	INC_DEPEND="# automatic dependency generation not enabled in configure"

	dnl If DEPTOOL is undefined, we can use gcc,
	dnl otherwise, I'm at a loss, as the switch is
	dnl very compiler specific (if avail, at all.)
        if test "$ac_cv_prog_gcc" = "yes" ; then
		DEPTOOL="$CC -M"
		INC_DEPEND="include .depend"
	fi
fi
AC_MSG_RESULT($DEPTOOL)

dnl =========================================================================
dnl Make the necessary substitutions
dnl -------------------------------------------------------------------------

AC_SUBST(KVER)
AC_SUBST(LIBSHARE)
AC_SUBST(PIC)
AC_SUBST(PICVM)
AC_SUBST(JAVA_LIBRARIES)
AC_SUBST(NOSHAREDLIBS)
AC_SUBST(LDFLAGS)
AC_SUBST(KAFFELINK)
AC_SUBST(LIBEXT)
AC_SUBST(LIBVMEXT)
AC_SUBST(OBJEXT)
AC_SUBST(EXEEXT)
AC_SUBST(SCREXT)
AC_SUBST(with_engine)
AC_SUBST(host_cpu)
AC_SUBST(host_os)
AC_SUBST(KAFFEFLAGS)
AC_SUBST(KAFFEVMFLAGS)
AC_SUBST(VPATHSEP)
AC_SUBST(VPATHOPT)
AC_SUBST(LINKLIB)
AC_SUBST(LINKVMLIB)
AC_SUBST(LD)
AC_SUBST(THREAD_SYSTEM)
AC_SUBST(DEPTOOL)
AC_SUBST(INC_DEPEND)
AC_DEFINE_UNQUOTED(LIBRARYSUFFIX, "$LIBEXT")
AC_DEFINE_UNQUOTED(KAFFEVERSION, "$KVER")
if test "$program_prefix" = "NONE" ; then
	KPREFIX=""
else
	KPREFIX=$program_prefix
fi
AC_SUBST(KPREFIX)
AC_SUBST(INSTROOT)
KAFFE_ARCHOS="$host_cpu-$host_os"
AC_DEFINE_UNQUOTED(ARCHOS, "$KAFFE_ARCHOS")
AC_SUBST(KAFFE_ARCHOS)

dnl =========================================================================

dnl =========================================================================
dnl Make the links required
dnl -------------------------------------------------------------------------

AC_LINK_FILES($link_from, $link_to)

dnl =========================================================================

dnl =========================================================================
dnl Checks for programs.
dnl -------------------------------------------------------------------------

AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PROG_RANLIB
AC_CHECK_PROG(AR, ar, ar cr)
AC_CHECK_PROGS(MKDIR, mkdirs 'mkdir -p', mkdir -p)
AC_CHECK_PROG(DLLTOOL, dlltool, dlltool)
dnl We also need to install scripts
INSTALL_SCRIPT='${INSTALL} -m 755'
AC_SUBST(INSTALL_SCRIPT)

dnl If symlink is overridden then don't bother with the test.
if test "$LN_S" = "" ; then
	AC_PROG_LN_S
fi
dnl This shell macro caches uses of ln -s in the generated script and
dnl converts them to LN_S
ln() {
	if test "$1" = "-s" ; then
		$LN_S $2 $3
	else
		ln $1 $2
	fi
}

dnl If dlltool is found then we must generate and use the 'lib.exp' file.
dnl  If not found then just use true to avoid the command erroring.

if test "$DLLTOOL" != "" ; then
	LDTAIL='lib.exp '$LDTAIL
else
	DLLTOOL="true"
fi
AC_SUBST(LDTAIL)
dnl -------------------------------------------------------------------------

dnl =========================================================================
dnl Checks for types
dnl -------------------------------------------------------------------------

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 =========================================================================
dnl Checks for alignments
dnl -------------------------------------------------------------------------

AC_CACHE_CHECK(alignment of void*, ac_cv_alignmentof_voidp,
AC_TRY_RUN([#include <stdio.h>
main() { struct { char c; void *p; } t; FILE *f;
  if ((char*) &t.c != (char*) &t)  exit (1);
  f=fopen ("conftestdata", "w");
  if (! f)                         exit (1);
  fprintf (f, "%d", ((char*) &t.p)-((char*) &t));
  fclose(f); exit (0); }],
ac_cv_alignmentof_voidp=`cat conftestdata`, ac_cv_alignmentof_voidp=1,
ac_cv_alignmentof_voidp=1))
AC_DEFINE_UNQUOTED(ALIGNMENTOF_VOIDP,$ac_cv_alignmentof_voidp)

dnl -------------------------------------------------------------------------

AC_TYPE_SIGNAL
AC_TYPE_SIZE_T
AC_CHECK_TYPE(ssize_t,int)

dnl -------------------------------------------------------------------------

AC_DEFUN(AC_CHECK_LIBRARY,
AC_CHECK_LIB($1,$2,
  changequote(, )dnl
  $3="-l$1 $$3";
  LIBS="-l$1 $LIBS"
  ac_tr_lib=HAVE_LIB`echo $1 | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`
changequote([, ])dnl
  AC_DEFINE_UNQUOTED($ac_tr_lib)
))

dnl =========================================================================
dnl Checks for libraries
dnl -------------------------------------------------------------------------

dnl Checks for standard libraries

AC_CHECK_LIBRARY(c,printf,STD_LIBRARIES)
AC_CHECK_LIBRARY(mw,_mwvalidcheckl,STD_LIBRARIES)
AC_CHECK_LIBRARY(m,sin,STD_LIBRARIES)
AC_CHECK_LIBRARY(gmp,mpz_abs,STD_LIBRARIES)
AC_SUBST(STD_LIBRARIES)

dnl Checks for libraries for vm library.

if test "$dynamic_libraries" = "yes" ; then
	dnl Don't bother with dynamic loaders if we're using static linking
	AC_CHECK_LIBRARY(dl,dlopen,VM_LIBRARIES)
	if test "$ac_cv_lib_dl_dlopen" = "no" ; then
		AC_CHECK_LIBRARY(dl,_dlopen,VM_LIBRARIES)
		if test "$ac_cv_lib_dl__dlopen" = "no" ; then
			AC_CHECK_LIBRARY(dld,dld_init,VM_LIBRARIES)
		fi
	fi
fi
AC_SUBST(VM_LIBRARIES)

dnl Check libraries for native library.
dnl None required.

dnl Check for libraries for net library.
AC_CHECK_FUNC(gethostname)
if test $ac_cv_func_gethostname = no; then
      AC_CHECK_LIBRARY(nsl, gethostname, NET_LIBRARIES)
fi
AC_CHECK_FUNC(gethostbyname)
if test $ac_cv_func_gethostbyname = no; then
      AC_CHECK_LIBRARY(nsl, gethostbyname, NET_LIBRARIES)
fi
AC_CHECK_FUNC(connect)
if test $ac_cv_func_connect = no; then
  AC_CHECK_LIBRARY(socket, connect, NET_LIBRARIES)
fi
AC_SUBST(NET_LIBRARIES)

dnl Check for libraries for zip library.
AC_CHECK_LIBRARY(z,deflate,ZIP_LIBRARIES)
AC_SUBST(ZIP_LIBRARIES)

dnl Check for libraries for math library.
AC_CHECK_LIBRARY(gmp,mpz_add,MATH_LIBRARIES)
AC_SUBST(MATH_LIBRARIES)

dnl Checks for libraries for X related libraries.
AC_PATH_X
if test "$have_x" = "yes" ; then
	AC_DEFINE(HAVE_X)
	if test "${x_libraries}" != NONE && test -n "${x_libraries}"; then
		X_LIBRARIES=-L`echo ${x_libraries} | sed -e "s/:/ -L/g"`
	fi
	X_LIBRARIES="$X_LIBRARIES -lX11 -L/usr/local/lib"
	if test "${x_includes}" != NONE && test -n "${x_includes}"; then
		X_INCLUDES=-I`echo ${x_includes} | sed -e "s/:/ -I/g"`
	fi
fi
AC_SUBST(X_INCLUDES)
AC_SUBST(X_LIBRARIES)
AC_CHECK_LIBRARY(ipc,shmat,X_LIBRARIES)

OLIBS=$LIBS
LIBS="$LIBS $X_LIBRARIES"
AC_CHECK_LIBRARY(jpeg,jpeg_read_header,JPEG_LIBRARIES)
AC_CHECK_LIBRARY(png,png_create_info_struct,PNG_LIBRARIES)
AC_CHECK_LIBRARY(gif,DGifOpenFileHandle,GIF_LIBRARIES)
LIBS=$OLIBS
dnl If libpng is included, we also need libz - if we don't have it then
dnl we can't include png support.
if test "$PNG_LIBRARIES" != "" ; then
	if test "$ZIP_LIBRARIES" != "" ; then
		PNG_LIBRARIES="$PNG_LIBRARIES $ZIP_LIBRARIES"
	else
		PNG_LIBRARIES=""
	fi
fi
AC_SUBST(JPEG_LIBRARIES)
AC_SUBST(PNG_LIBRARIES)
AC_SUBST(GIF_LIBRARIES)
AWTLIB="$X_LIBRARIES $JPEG_LIBRARIES $PNG_LIBRARIES $GIF_LIBRARIES"
AC_SUBST(AWTLIB)

dnl -------------------------------------------------------------------------

dnl =========================================================================
dnl Checks for header files.
dnl -------------------------------------------------------------------------

CPPFLAGS="$CPPFLAGS -I/usr/local/include"
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h sys/time.h unistd.h sys/socket.h winnt.h wintypes.h wtypes.h winbase.h windows.h winsock.h dlfcn.h sys/ioctl.h sys/filio.h malloc.h alloca.h sys/param.h sys/utsname.h pwd.h asm/sigcontext.h sigcontext.h asm/signal.h signal.h mach-o/rld.h sys/types.h sys/stat.h string.h sys/select.h memory.h bsd/libc.h dl.h features.h sys/wait.h zlib.h netinet/tcp.h netinet/in_systm.h jpeglib.h gif.h png.h gmp.h)
AC_HEADER_DIRENT

dnl -------------------------------------------------------------------------

dnl =========================================================================
dnl Checks for typedefs, structures, and compiler characteristics.
dnl -------------------------------------------------------------------------

AC_HEADER_TIME
AC_STRUCT_TM
AC_STRUCT_TIMEZONE
AC_C_BIGENDIAN
AC_C_CHAR_UNSIGNED
AC_C_INLINE

dnl =========================================================================
dnl Check for struct sigcontext.
dnl -------------------------------------------------------------------------

AC_CACHE_CHECK([for struct sigcontext],
  ac_cv_struct_sigcontext,
[AC_TRY_COMPILE([#include <asm/signal.h>
#include <asm/sigcontext.h>], 
[struct sigcontext t;],
  ac_cv_struct_sigcontext=yes, ac_cv_struct_sigcontext=no)])
if test $ac_cv_struct_sigcontext = yes; then
  AC_DEFINE(HAVE_STRUCT_SIGCONTEXT)
fi

dnl Check for struct sigcontext_struct.
dnl Just to complicate things there are two different sets of assembly files
dnl we must check which are mutually incompatible.

AC_CACHE_CHECK([for struct sigcontext_struct (without asm/sigcontext.h)],
  ac_cv_struct_sigcontext_struct,
[AC_TRY_COMPILE([#include <asm/signal.h>],
[struct sigcontext_struct t;],
  ac_cv_struct_sigcontext_struct=yes, ac_cv_struct_sigcontext_struct=no)])

AC_CACHE_CHECK([for struct sigcontext_struct (with asm/sigcontext.h)],
  ac_cv_struct_sigcontext_struct_with,
[AC_TRY_COMPILE([#include <asm/signal.h>
#include <asm/sigcontext.h>], 
[struct sigcontext_struct t;],
  ac_cv_struct_sigcontext_struct_with=yes, ac_cv_struct_sigcontext_struct_with=no)])

if test $ac_cv_struct_sigcontext_struct = yes -o $ac_cv_struct_sigcontext_struct_with = yes; then
  AC_DEFINE(HAVE_STRUCT_SIGCONTEXT_STRUCT)
fi

dnl -------------------------------------------------------------------------

dnl =========================================================================
dnl Checks for library functions.
dnl -------------------------------------------------------------------------

AC_FUNC_STRFTIME
AC_FUNC_MMAP
AC_CHECK_FUNCS(select socket getsockname)
AC_CHECK_FUNCS(memcpy memmove)
AC_CHECK_FUNCS(mkdir)
AC_CHECK_FUNCS(getcwd getwd gettimeofday ftime time uname getuid)
AC_CHECK_FUNCS(remainder remainderf fmod fmodf drem rint floor ceil finite isinf isnan strtod strdup)
AC_CHECK_FUNCS(strerror hstrerror)
AC_CHECK_FUNCS(fcntl ioctl)
AC_CHECK_FUNCS(alarm setitimer)
AC_CHECK_FUNCS(sigprocmask sigsetmask sigemptyset sigaddset signal sigaction)
AC_CHECK_FUNCS(sbrk valloc memalign mallopt)
AC_CHECK_FUNCS(waitpid kill fork execve execvp)
AC_CHECK_FUNCS(rld_load shl_load LoadLibrary dld_link)
AC_CHECK_FUNCS(sync fsync)
AC_CHECK_FUNCS(atexit on_exit vsnprintf snprintf)

dnl Some systems must include dlfcn to get these right.
AC_CACHE_CHECK([for dlopen], ac_cv_func_dlopen,
[AC_TRY_LINK([#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#else
char dlopen();
#endif], [dlopen(0, 0)],
ac_cv_func_dlopen=yes, ac_cv_func_dlopen=no)
])
if test "$ac_cv_func_dlopen" = "yes" ; then
  AC_DEFINE(HAVE_DLOPEN)
fi

AC_CACHE_CHECK([for dlerror], ac_cv_func_dlerror,
[AC_TRY_LINK([#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#else
char dlerror();
#endif], [dlerror()],
  ac_cv_func_dlerror=yes, ac_cv_func_dlerror=no)
])
if test "$ac_cv_func_dlerror" = "yes" ; then
  AC_DEFINE(HAVE_DLERROR)
fi


if test "$host_cpu" = "alpha" ; then
  AC_CACHE_CHECK([for alpha support of amask instruction],
    ac_cv_alpha_asm_amask,
  [AC_TRY_COMPILE(, [ long r; __asm__("amask 1,%0" : "=r"(r)); return !r; ],
    ac_cv_alpha_asm_amask=yes, ac_cv_alpha_asm_amask=no)
  ])
  if test "$ac_cv_alpha_asm_amask" = "yes" ; then
    AC_DEFINE(HAVE_ALPHA_ASM_AMASK)
  fi
fi

dnl -------------------------------------------------------------------------

dnl =========================================================================
dnl Work out if select is defined
dnl -------------------------------------------------------------------------

AC_CACHE_CHECK([for declaration of select],
  ac_cv_declared_select,
[AC_TRY_COMPILE([
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
], [ void *foo = select; ],
  ac_cv_declared_select=yes; AC_DEFINE(HAVE_DECLARED_SELECT), ac_cv_declared_select=no)
])

dnl -------------------------------------------------------------------------

dnl =========================================================================
dnl Some dynamic library need an underscore on the loading name, some don't
dnl -------------------------------------------------------------------------

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_UNDERSCORE)
fi

dnl =========================================================================
dnl Some C compilers add '_' to C names.
dnl -------------------------------------------------------------------------
AC_CACHE_CHECK(for underscore in C assembly names, ac_cv_underscore_c_names,
AC_TRY_LINK([extern int foo_alias();asm("\n_foo_alias:");void foo() {};],
	[foo_alias();], 
ac_cv_underscore_c_names=yes, ac_cv_underscore_c_names=no))
if test "$ac_cv_underscore_c_names" = "yes"; then
  AC_DEFINE(HAVE_UNDERSCORED_C_NAMES)
fi

dnl -------------------------------------------------------------------------

changequote(<<, >>)dnl
subdirs="config include libraries kaffe test"
make_output="\
Makefile \
config/Makefile include/Makefile kaffe/Makefile test/Makefile \
libraries/Makefile \
kaffe/kaffevm/Makefile kaffe/kaffe/Makefile kaffe/kaffeh/Makefile \
kaffe/man/Makefile kaffe/scripts/Makefile \
"

dnl Default packages
make_output="$make_output \
libraries/javalib/Makefile \
libraries/clib/Makefile \
libraries/clib/native/Makefile \
libraries/clib/net/Makefile \
libraries/clib/zip/Makefile \
libraries/clib/math/Makefile \
libraries/clib/management/Makefile \
"
JAVA_LIBRARIES=" \
../../libraries/clib/native/libnative$LIBEXT \
../../libraries/clib/net/libnet$LIBEXT \
../../libraries/clib/zip/libzip$LIBEXT \
../../libraries/clib/math/libmath$LIBEXT \
../../libraries/clib/management/libmanagement$LIBEXT \
"

dnl If we have X then include AWT support.
if test "$have_x" = "yes" ; then
make_output="$make_output \
libraries/clib/awt/Makefile \
"
JAVA_LIBRARIES="$JAVA_LIBRARIES \
../../libraries/clib/awt/libawt$LIBEXT \
"
fi

changequote([, ])dnl

dnl If we have share libraries, cancel all the non-shared ones.
dnl  Otherwise, add on the machine again to help linking.
if test "$dynamic_libraries" = "yes" ; then
	NOSHAREDLIBS="-L../kaffevm -lkaffevm $VM_LIBRARIES $STD_LIBRARIES"
	KAFFE_LIBDEP=""
else
	NOSHAREDLIBS="../kaffevm/libkaffevm$LIBEXT $JAVA_LIBRARIES $STD_LIBRARIES $ZIP_LIBRARIES $NET_LIBRARIES $X_LIBRARIES $AWTLIB"
	KAFFE_LIBDEP="$KAFFE_LIBDEP ../kaffevm/libkaffevm$LIBEXT $JAVA_LIBRARIES"
	PIC=""
	PICVM=""
	LIBSHARE=""
fi

dnl If the vm is to be static but everything else isn't, set it up.
if test "$vm_dynamic_library" = "no" -a "$dynamic_libraries" = "yes"; then
	LINKVMLIB='ld -r -o $(LIB) $(OBJECT)'
	PICVM=""
	LIBVMEXT=.o
	NOSHAREDLIBS="../kaffevm/libkaffevm.o $VM_LIBRARIES $STD_LIBRARIES"
	KAFFE_LIBDEP="$KAFFE_LIBDEP ../kaffevm/libkaffevm.o"
else
	LINKVMLIB=$LINKLIB
	PICVM=$PIC
	LIBVMEXT=$LIBEXT
fi
dnl If the vm is static or all libraries are static then make sure we set
dnl the no-share-vmlibrary flag.
if test "$vm_dynamic_library" = "no" -o "$dynamic_libraries" = "no"; then
	KAFFEVMFLAGS="$KAFFEVMFLAGS -DNO_SHARED_VMLIBRARY"
fi

AC_SUBST(subdirs)
AC_SUBST(packages)
AC_SUBST(KAFFE_LIBDEP)

dnl =========================================================================
dnl Define directory to put native library support
dnl -------------------------------------------------------------------------

# We do this hear to because we need prefixes set.
test "x$prefix" = xNONE && prefix=$ac_default_prefix
test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'
nativedir=$libdir/kaffe
AC_SUBST(nativedir)

dnl A few extra files to process.
output="$make_output \
libraries/javalib/rebuildLib \
kaffe/scripts/install-jar \
kaffe/scripts/kaffe \
kaffe/scripts/javac \
kaffe/scripts/javakey \
kaffe/scripts/jar \
kaffe/scripts/javap \
kaffe/scripts/jdb \
kaffe/scripts/pizza \
kaffe/scripts/javadoc \
kaffe/scripts/pizzadoc \
kaffe/scripts/appletviewer \
kaffe/scripts/rmic \
kaffe/scripts/rmiregistry \
kaffe/scripts/serialver \
kaffe/scripts/native2ascii \
kaffe/scripts/compat/java \
kaffe/scripts/compat/javac \
kaffe/scripts/compat/appletviewer \
kaffe/scripts/compat/javadoc \
"

dnl =========================================================================
dnl Define default classpath (not needed), kaffehome and libraryhome.
dnl -------------------------------------------------------------------------

kaffehome=`eval echo $datadir`/kaffe
AC_DEFINE_UNQUOTED(DEFAULT_KAFFEHOME, "$kaffehome")

AC_CONFIG_HEADER(config/config.h include/jtypes.h)
AC_OUTPUT($output)

dnl =========================================================================
dnl Create empty .depend files to ensure that make works even 
dnl when the depend target is not executed.
dnl -------------------------------------------------------------------------

changequote(<<, >>)dnl
for makefile in $make_output
do
	makepath=`echo "$makefile" | sed 's,[^/]*$,,'`
	dependfile=$makepath.depend
	echo creating $dependfile
	touch $dependfile
done
changequote([, ])dnl

dnl =========================================================================
dnl Generate example environment
dnl -------------------------------------------------------------------------

libdir=`eval echo $libdir` ; libdir=`eval echo $libdir`
bindir=`eval echo $bindir` ; bindir=`eval echo $bindir`
datadir=`eval echo $datadir`; nativedir=`eval echo $nativedir`

cp /dev/null ENVIRONMENT
if test "$dynamic_libraries" = "yes" ; then
	echo LD_LIBRARY_PATH=$libdir:\$LD_LIBRARY_PATH >> ENVIRONMENT
	echo KAFFELIBRARYPATH=$nativedir:\$LD_LIBRARY_PATH >> ENVIRONMENT
	echo export LD_LIBRARY_PATH KAFFELIBRARYPATH >> ENVIRONMENT
fi
echo PATH=$bindir:\$PATH >> ENVIRONMENT
echo export PATH >> ENVIRONMENT

dnl -------------------------------------------------------------------------
