dnl Process this with autoconf to create configure
AC_INIT(java/lang/System.java)

dnl Can't be done in LIBGCJ_CONFIGURE because that confuses automake. 
AC_CONFIG_AUX_DIR(..)

AC_CANONICAL_SYSTEM

dnl We use these options to decide which functions to include.
AC_ARG_WITH(target-subdir,
[  --with-target-subdir=SUBDIR      Configuring in a subdirectory])
AC_ARG_WITH(cross-host,
[  --with-cross-host=HOST           Configuring with a cross compiler])

LIBGCJ_CONFIGURE(.)

AM_CONFIG_HEADER(include/config.h)

AM_PROG_LIBTOOL

if test -z "$with_target_subdir" || test "$with_target_subdir" = "."; then
   COMPPATH=.
else
   COMPPATH=..
fi
AC_SUBST(COMPPATH)

dnl The -no-testsuite modules omit the test subdir.
AM_CONDITIONAL(TESTSUBDIR, test -d $srcdir/testsuite)

dnl See whether the user prefers size or speed for Character.
dnl The default is size.
AC_ARG_ENABLE(fast-character,
[  --enable-fast-character          Prefer speed over size for Character],
# Nothing
, AC_DEFINE(COMPACT_CHARACTER))

dnl See if the user has requested runtime debugging.
AC_ARG_ENABLE(libgcj-debug,
[  --enable-libgcj-debug           Enable runtime debugging code],
  if test "$enable_libgcj_debug" = yes; then
     AC_DEFINE(DEBUG)
  fi)

dnl If the target is an eCos system, use the appropriate eCos
dnl I/O routines.
dnl FIXME: this should not be a local option but a global target
dnl system; at present there is no eCos target.
TARGET_ECOS="no"
AC_ARG_WITH(ecos,
[  --with-ecos      Enable runtime eCos target support.],
TARGET_ECOS="$with_ecos"
)

case "$TARGET_ECOS" in
   no)
      FILE_DESCRIPTOR=natFileDescriptorPosix.cc
      PROCESS=Posix
      ;;
   *)
      FILE_DESCRIPTOR=natFileDescriptorEcos.cc
      PROCESS=Ecos
      AC_DEFINE(ECOS)
      ;;
esac

AC_EGREP_HEADER(__uint32_t, sys/types.h, AC_DEFINE(HAVE_INT32_DEFINED))
AC_EGREP_HEADER(__uint32_t, sys/config.h, AC_DEFINE(HAVE_INT32_DEFINED))

dnl These may not be defined in a non-ANS conformant embedded system.
dnl FIXME: Should these case a runtime exception in that case?
AC_EGREP_HEADER(mktime, time.h, AC_DEFINE(HAVE_MKTIME))
AC_EGREP_HEADER(localtime, time.h, AC_DEFINE(HAVE_LOCALTIME))

dnl Create the subdirectory for natFileDescriptor.cc, or the attempt
dnl to create the link will fail.
test -d java || mkdir java
test -d java/io || mkdir java/io
AC_LINK_FILES(java/io/$FILE_DESCRIPTOR, java/io/natFileDescriptor.cc)

dnl Likewise for ConcreteProcess.java and natConcreteProcess.cc.
test -d java/lang || mkdir java/lang
AC_LINK_FILES(java/lang/${PROCESS}Process.java, java/lang/ConcreteProcess.java)
AC_LINK_FILES(java/lang/nat${PROCESS}Process.cc, java/lang/natConcreteProcess.cc)

SYSTEMSPEC=
AC_SUBST(SYSTEMSPEC)

AC_ARG_WITH(system-zlib,
[  --with-system-zlib               Use installed libz])
ZLIBSPEC=
AC_SUBST(ZLIBSPEC)

dnl FIXME: this should be _libs on some hosts.
libsubdir=.libs

dnl Allow the GC to be disabled.  Can be useful when debugging.
AC_MSG_CHECKING([for garbage collector to use])
AC_ARG_ENABLE(java-gc,
changequote(<<,>>)dnl
<<  --enable-java-gc=TYPE    choose garbage collector [boehm]>>,
changequote([,])
  GC=$enableval,
  GC=boehm)
GCLIBS=
GCINCS=
GCDEPS=
GCOBJS=
GCSPEC=
case "$GC" in
 boehm)
    AC_MSG_RESULT(boehm)
    GCDEPS='$(top_builddir)/../boehm-gc/libgcjgc.la'
    # We include the path to the boehm-gc build directory.
    # See Makefile.am to understand why.
    GCLIBS="$GCDEPS -L\$(here)/../boehm-gc/$libsubdir"
    GCINCS='-I$(top_srcdir)/../boehm-gc -I$(top_builddir)/../boehm-gc'
    GCSPEC='-lgcjgc'
    dnl We also want to pick up some cpp flags required when including
    dnl boehm-config.h.  Yuck.
    GCINCS="$GCINCS `cat ../boehm-gc/boehm-cflags`"
    GCOBJS=boehm.lo
    GCHDR=boehm-gc.h
    dnl The POSIX thread support needs to know this.
    AC_DEFINE(HAVE_BOEHM_GC)
    ;;
 no)
    AC_MSG_RESULT(none)
    GCOBJS=nogc.lo
    GCHDR=no-gc.h
    ;;
 *)
    AC_MSG_ERROR(unrecognized collector \"$GC\")
    ;;
esac
AC_SUBST(GCLIBS)
AC_SUBST(GCINCS)
AC_SUBST(GCDEPS)
AC_SUBST(GCOBJS)
AC_SUBST(GCSPEC)
AC_LINK_FILES(include/$GCHDR, include/java-gc.h)


dnl Note that this code is kept in sync with similar code in gcc/configure.in.
dnl In particular both packages must make the same decision about which
dnl thread package to use.
AC_MSG_CHECKING([for threads package to use])
AC_ARG_ENABLE(threads, [  --enable-threads=TYPE    choose threading package],
  THREADS=$enableval,
  dnl FIXME: figure out native threads to use here.
  THREADS=no)

if test "$THREADS" = yes; then
   case "$host" in
    *-*-vxworks*)
       THREADS=vxworks
       ;;
    *-*-linux*)
       # FIXME: this isn't correct in all cases.
       THREADS=posix
       ;;
    *-*-win*)
       THREADS=win32
       ;;
    *-*-irix*)
       # FIXME: for now, choose POSIX, because we implement that.
       # Later, choose irix threads.
       THREADS=posix
       ;;
    *-*-solaris*)
       # FIXME: for now, choose POSIX, because we implement that.
       # Later, choose solaris threads.
       THREADS=posix
       ;;
    *)
       # For now.
       THREADS=none
       ;;
   esac
fi

case "$THREADS" in
 no | none | single)
    THREADS=none
    ;;
 posix | pthreads)
    THREADS=posix
    case "$host" in
     *-*-linux*)
	AC_DEFINE(LINUX_THREADS)
	;;
    esac
    ;;
 qt)
    ;;
 decosf1 | irix | mach | os2 | solaris | win32 | dce | vxworks)
    AC_MSG_ERROR(thread package $THREADS not yet supported)
    ;;
 *)
    AC_MSG_ERROR($THREADS is an unknown thread package)
    ;;
esac
AC_MSG_RESULT($THREADS)

THREADLIBS=
THREADINCS=
THREADDEPS=
THREADOBJS=
THREADH=
THREADSPEC=
case "$THREADS" in
 posix)
    THREADLIBS=-lpthread
    THREADSPEC=-lpthread
    THREADOBJS=posix-threads.lo
    THREADH=posix-threads.h
    # MIT pthreads doesn't seem to have the mutexattr functions.
    # But for now we don't check for it.  We just assume you aren't
    # using MIT pthreads.
    AC_DEFINE(HAVE_PTHREAD_MUTEXATTR_INIT)
    ;;

 qt)
    THREADDEPS='$(top_builddir)/../qthreads/libgcjcoop.la'
    # We include the path to the qthreads build directory.
    # See Makefile.am to understand why.
    THREADLIBS="$THREADDEPS -L\$(here)/../qthreads/$libsubdir"
    THREADSPEC='-lgcjcoop'
    THREADOBJS=quick-threads.lo
    THREADINCS='-I$(top_srcdir)/../qthreads'
    THREADH=quick-threads.h
    ;;

 none)
    THREADOBJS=no-threads.lo
    THREADH=no-threads.h
    ;;
esac
AC_LINK_FILES(include/$THREADH, include/java-threads.h)
AC_SUBST(THREADLIBS)
AC_SUBST(THREADINCS)
AC_SUBST(THREADDEPS)
AC_SUBST(THREADOBJS)
AC_SUBST(THREADSPEC)

AM_CONDITIONAL(USING_GCC, test "$GCC" = yes)

CANADIAN=no
NULL_TARGET=no

# Find eh-common.h and support headers.  If we're in the tree with
# gcc, then look there.  Otherwise look in compat-include.  If all else
# fails, just hope the user has set things up somehow.
if test -r $srcdir/../gcc/eh-common.h; then
   EH_COMMON_INCLUDE='-I$(top_srcdir)/../gcc -I$(top_srcdir)/../include'
else
   if test -d $srcdir/../compat-include; then
      EH_COMMON_INCLUDE='-I$(top_srcdir)/../compat-include'
   else
      EH_COMMON_INCLUDE=
   fi
fi

if test -n "${with_cross_host}"; then
   # We are being configured with a cross compiler.  AC_REPLACE_FUNCS
   # may not work correctly, because the compiler may not be able to
   # link executables.

   # We assume newlib.  This lets us hard-code the functions we know
   # we'll have.
   AC_DEFINE(HAVE_MEMMOVE)
   AC_DEFINE(HAVE_MEMCPY)
   AC_DEFINE(HAVE_STRERROR)
   AC_DEFINE(HAVE_CTIME_R)
   AC_DEFINE(HAVE_GMTIME_R)
   AC_DEFINE(HAVE_LOCALTIME_R)
   dnl This is only for POSIX threads.
   AC_DEFINE(HAVE_PTHREAD_MUTEXATTR_INIT)
   dnl We also assume we are using gcc, which provides alloca.
   AC_DEFINE(HAVE_ALLOCA)

   ZLIBSPEC=-lgcj

   # If Canadian cross, then don't pick up tools from the build
   # directory.
   if test "$build" != "$with_cross_host"; then
      CANADIAN=yes
      EH_COMMON_INCLUDE=
      GCJ="${target_alias}/gcj"
   else
      GCJ=
   fi
else
   # Some POSIX thread systems don't have pthread_mutexattr_settype.
   # E.g., Solaris.
   AC_CHECK_FUNCS(strerror ioctl select open fsync sleep)
   AC_CHECK_FUNCS(ctime_r ctime, break)
   AC_CHECK_FUNCS(gmtime_r localtime_r readdir_r getpwuid_r)
   AC_CHECK_FUNCS(access stat mkdir rename rmdir unlink realpath)
   AC_CHECK_FUNCS(inet_aton inet_addr, break)
   AC_CHECK_FUNCS(inet_pton uname)

   AC_CHECK_FUNCS(gethostbyname_r, [
     AC_DEFINE(HAVE_GETHOSTBYNAME_R)
     # There are two different kinds of gethostbyname_r.
     # We look for the one that returns `int'.
     # Hopefully this check is robust enough.
     AC_EGREP_HEADER(int.*gethostbyname_r, netdb.h, [
       AC_DEFINE(GETHOSTBYNAME_R_RETURNS_INT)])])

   AC_CHECK_FUNCS(gethostbyaddr_r, [
     AC_DEFINE(HAVE_GETHOSTBYADDR_R)
     # There are two different kinds of gethostbyaddr_r.
     # We look for the one that returns `int'.
     # Hopefully this check is robust enough.
     AC_EGREP_HEADER(int.*gethostbyaddr_r, netdb.h, [
       AC_DEFINE(GETHOSTBYADDR_R_RETURNS_INT)])])

   AC_CHECK_FUNCS(gethostname, [
     AC_DEFINE(HAVE_GETHOSTNAME)
     AC_EGREP_HEADER(gethostname, unistd.h, [
       AC_DEFINE(HAVE_GETHOSTNAME_DECL)])])

   # Look for these functions in the thread library.
   save_LIBS="$LIBS"
   LIBS="$LIBS $THREADLIBS"
   AC_CHECK_FUNCS(pthread_mutexattr_settype pthread_mutexattr_setkind_np sched_yield)
   # Look for sched_yield.  Sometimes it is in the posix4 library.
   AC_CHECK_FUNCS(sched_yield, , [
     AC_CHECK_LIB(posix4, sched_yield, [
       AC_DEFINE(HAVE_SCHED_YIELD)
       THREADLIBS="$THREADLIBS -lposix4"])])
   LIBS="$save_LIBS"

   # We require a way to get the time.
   time_found=no
   AC_CHECK_FUNCS(gettimeofday time ftime, time_found=yes)
   if test "$time_found" = no; then
      AC_MSG_ERROR([no function found to get the time])
   fi

   # We require memmove.
   memmove_found=no
   AC_CHECK_FUNCS(memmove, memmove_found=yes)
   if test "$memmove_found" = no; then
      AC_MSG_ERROR([memmove is required])
   fi

   # We require memcpy.
   memcpy_found=no
   AC_CHECK_FUNCS(memcpy, memcpy_found=yes)
   if test "$memcpy_found" = no; then
      AC_MSG_ERROR([memcpy is required])
   fi

   # Some library-finding code we stole from Tcl.
   #--------------------------------------------------------------------
   #	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_CACHE_CHECK([for socket libraries], gcj_cv_lib_sockets,
    [gcj_cv_lib_sockets=
     gcj_checkBoth=0
     unset ac_cv_func_connect
     AC_CHECK_FUNC(connect, gcj_checkSocket=0, gcj_checkSocket=1)
     if test "$gcj_checkSocket" = 1; then
	 unset ac_cv_func_connect
	 AC_CHECK_LIB(socket, main, gcj_cv_lib_sockets="-lsocket",
		      gcj_checkBoth=1)
     fi
     if test "$gcj_checkBoth" = 1; then
	 gcj_oldLibs=$LIBS
	 LIBS="$LIBS -lsocket -lnsl"
	 unset ac_cv_func_accept
	 AC_CHECK_FUNC(accept,
		       [gcj_checkNsl=0
			gcj_cv_lib_sockets="-lsocket -lnsl"])
	 unset ac_cv_func_accept
	 LIBS=$gcj_oldLibs
     fi
     unset ac_cv_func_gethostbyname
     gcj_oldLibs=$LIBS
     LIBS="$LIBS $gcj_cv_lib_sockets"
     AC_CHECK_FUNC(gethostbyname, ,
		   [AC_CHECK_LIB(nsl, main,
				 [gcj_cv_lib_sockets="$gcj_cv_lib_sockets -lnsl"])])
     unset ac_cv_func_gethostbyname
     LIBS=$gcj_oldLIBS
   ])
   SYSTEMSPEC="$SYSTEMSPEC $gcj_cv_lib_sockets"

   if test "$with_system_zlib" = yes; then
      AC_CHECK_LIB(z, deflate, ZLIBSPEC=-lz, ZLIBSPEC=-lzgcj)
   else
      ZLIBSPEC=-lzgcj
   fi

   # On Solaris, and maybe other architectures, the Boehm collector
   # requires -ldl.
   if test "$GC" = boehm; then
      AC_CHECK_LIB(dl, main, SYSTEMSPEC="$SYSTEMSPEC -ldl")
   fi

   if test -d "$libgcj_basedir/../gcc/java"; then
      GCJ=
   else
      CANADIAN=yes
      NULL_TARGET=yes
      GCJ=gcj
   fi
fi

ZLIBS=
ZDEPS=
ZINCS=
if test "x$ZLIBSPEC" = "x-lzgcj"; then
   # We include the path to the zlib build directory.
   # See Makefile.am to understand why.
   ZDEPS='$(top_builddir)/../zlib/libzgcj.la'
   ZLIBS="$ZDEPS -L\$(here)/../zlib/$libsubdir"
   ZINCS='-I$(top_srcdir)/../zlib'
else
   ZLIBS="$ZLIBSPEC"
fi
AC_SUBST(ZLIBS)
AC_SUBST(ZDEPS)
AC_SUBST(ZINCS)

AM_CONDITIONAL(CANADIAN, test "$CANADIAN" = yes)
AM_CONDITIONAL(NULL_TARGET, test "$NULL_TARGET" = yes)
AM_CONDITIONAL(NATIVE, test "$CANADIAN" = no || test "$NULL_TARGET" = yes)
AM_CONDITIONAL(USE_LIBDIR, test -z "$with_cross_host")
AC_SUBST(EH_COMMON_INCLUDE)

# Determine gcj version number.
if test "$GCJ" = ""; then
   if test -z "${with_multisubdir}"; then
      builddotdot=.
   else
      builddotdot=`echo ${with_multisubdir} | sed -e 's:[^/][^/]*:..:g'`
   fi
   dir="`cd ${builddotdot}/../../gcc && pwd`"
   GCJ="$dir/gcj -B$dir/"
fi
changequote(<<,>>)
gcjvers="`$GCJ -v 2>&1 | sed -n 's/^.*version \([^ ]*\).*$/\1/p'`"
changequote([,])
AC_DEFINE_UNQUOTED(GCJVERSION, "$gcjvers")

AC_SUBST(AM_RUNTESTFLAGS)

dnl We check for sys/filio.h because Solaris 2.5 defines FIONREAD there.
dnl On that system, sys/ioctl.h will not include sys/filio.h unless
dnl BSD_COMP is defined; just including sys/filio.h is simpler.
AC_CHECK_HEADERS(unistd.h sys/time.h sys/types.h fcntl.h sys/ioctl.h sys/filio.h sys/stat.h sys/select.h sys/socket.h netinet/in.h arpa/inet.h netdb.h pwd.h)
dnl We avoid AC_HEADER_DIRENT since we really only care about dirent.h
dnl for now.  If you change this, you also must update natFile.cc.
AC_CHECK_HEADERS(dirent.h)

AC_MSG_CHECKING([whether struct sockaddr_in6 is in netinet/in.h])
AC_TRY_COMPILE([#include <netinet/in.h>], [struct sockaddr_in6 addr6;],
  [AC_DEFINE(HAVE_INET6)
   AC_MSG_RESULT(yes)],
  [AC_MSG_RESULT(no)])

AC_MSG_CHECKING([for socklen_t in sys/socket.h])
AC_TRY_COMPILE([#include <sys/socket.h>], [socklen_t x = 5;],
  [AC_DEFINE(HAVE_SOCKLEN_T)
   AC_MSG_RESULT(yes)],
  [AC_MSG_RESULT(no)])

AC_MSG_CHECKING([for tm_gmtoff in struct tm])
AC_TRY_COMPILE([#include <time.h>], [struct tm tim; tim.tm_gmtoff = 0;],
  [AC_DEFINE(STRUCT_TM_HAS_GMTOFF)
   AC_MSG_RESULT(yes)],
  [AC_MSG_RESULT(no)
   AC_MSG_CHECKING([for global timezone variable])
   dnl FIXME: we don't want a link check here because that won't work
   dnl when cross-compiling.  So instead we make an assumption that
   dnl the header file will mention timezone if it exists.
   AC_TRY_COMPILE([#include <time.h>], [long z2 = timezone;],
     [AC_DEFINE(HAVE_TIMEZONE)
      AC_MSG_RESULT(yes)],
     [AC_MSG_RESULT(no)])])

AC_FUNC_ALLOCA

AC_CHECK_PROGS(PERL, perl, false)

case "${host}" in
  i?86-*-linux*)
    SIGNAL_HANDLER=include/i386-signal.h
	;;
  sparc-sun-solaris*)
    SIGNAL_HANDLER=include/sparc-signal.h
	;;
  *)
    SIGNAL_HANDLER=include/default-signal.h
	;;
esac

AC_LINK_FILES($SIGNAL_HANDLER, include/java-signal.h)

if test "${multilib}" = "yes"; then
  multilib_arg="--enable-multilib"
else
  multilib_arg=
fi

here=`pwd`
AC_SUBST(here)

AC_OUTPUT(Makefile libgcj.spec testsuite/Makefile,
[if test -n "$CONFIG_FILES"; then
  ac_file=Makefile . ${libgcj_basedir}/../config-ml.in
fi],
srcdir=${srcdir}
host=${host}
target=${target}
with_multisubdir=${with_multisubdir}
ac_configure_args="${multilib_arg} ${ac_configure_args}"
CONFIG_SHELL=${CONFIG_SHELL-/bin/sh}
libgcj_basedir=${libgcj_basedir}
CC="${CC}"
CXX="${CXX}"
)