dnl Some reminders:
dnl	"dnl" is a "delete to newline" command for m4 - the remainder of the
dnl		line it's on, including the newline, will be discarded
dnl	"#" comments will usually be copied through
dnl	"[]" are quoting characters; use changequote around code that really
dnl		needs to use them, but try not to invoke autoconf macros
dnl		within such code
dnl
dnl When you change this file, re-run "autoreconf" -- that'll automatically
dnl re-run both autoconf and autoheader.
dnl
dnl
dnl To do:
dnl	CLOCKS_PER_SEC	(default 100, Ultrix differs)
dnl	CLK_TCK		(default 100, Ultrix differs)
dnl	clockid_t	(not on all systems)
dnl	timer_t		(not on all systems)
dnl
dnl
dnl	Now, the real stuff.
dnl
dnl This must be first.
AC_INIT(config.flags.in)
dnl
dnl This file does require autoconf 2.0 or better...
dnl
AC_PREREQ(2.0)dnl
dnl
dnl Hook for the "--enable-dce-compat" option, when Chris actually
dnl implements it.
dnl
AC_ARG_ENABLE(dce-compat,
    --enable-dce-compat	  DCE compatibility,
[dnl "true" action
  AC_MSG_ERROR([Actually, DCE compatibility doesn't work yet...])
],
[dnl "false" action
  true
])

dnl What C compiler?
AC_PROG_CC
AC_PROG_CXX
dnl Apparently autoconf doesn't test to see if the C compiler it locates
dnl actually works... oops!
AC_MSG_CHECKING(compiler availability and simple error detection)
AC_TRY_COMPILE(,[ exit(0); ],[
  dnl true
  AC_TRY_COMPILE(,[ syntax errors galore! ],
    dnl true
    AC_MSG_ERROR(Can't detect syntax errors!  Is CC set right?),
    dnl false
    true)],
  dnl false
  AC_MSG_ERROR(Can't compile test program!  Is CC set right?))

AC_TRY_COMPILE(,[
  typedef int Integer;
  extern int i;
  extern Integer i;
], , AC_MSG_ERROR(Can't redeclare variables with this compiler??))
AC_TRY_COMPILE(,[
  typedef long Long;
  extern int i;
  extern Long i;
], AC_MSG_ERROR(Compiler doesn't detect conflicting declarations.))
AC_MSG_RESULT(ok)

if test $ac_cv_prog_gcc = yes ; then
  CFLAGS="$CFLAGS -Werror"
fi

AC_PROG_CPP

AC_PROG_RANLIB

dnl Default value of prefix should be /usr/local/pthreads, not /usr/local.
AC_PREFIX_DEFAULT(/usr/local/pthreads)

dnl Determine $host, by guessing if necessary.
AC_CANONICAL_SYSTEM

syscall=NONE
sysincludes=NONE

changequote(,)dnl
case $host in
    i[45]86-*)
	host=`echo $host | sed 's/^i[45]86/i386/'`
	;;
esac
changequote([,])dnl

case $host in
    alpha-*-osf*)
	name=alpha-osf1
	sysincludes=alpha-osf1
	makefile_frag=alpha-osf1.mk
	if test $ac_cv_prog_gcc = no ; then
	  CFLAGS="$CFLAGS -std"
	fi
	;;
    hppa1.1-*-hpux*)
	name=hppa-hpux-9.03
	sysincludes=hpux-9.03
	config_flags="HPPA_HPUX_903=1"
	makefile_frag=hpux-9.03.mk
	;;
    sparc-*-sunos4.1.3*)
	name=sparc-sunos-4.1.3
	sysincludes=sunos-4.1.3
	makefile_frag=sun4os4.mk
	syscall=sparc-sunos4
	;;
    sparc-*-solaris2*)
	name=sparc-sunos-5.3
	sysincludes=sunos-5.3
	makefile_frag=sun4os5.mk
	syscall=sparc-sunos-5.3
	# Should really come up with a test for this...
	AC_DEFINE(LD_LINKS_STATIC_DATA)
	;;
    i386-*-linux*)
	name=i386-linux-1.0
	sysincludes=linux-1.0
	;;
    i386-*-bsdi)
	name=i386-bsdi-1.1
	sysincludes=bsdi-1.1
	;;
    i386-*-netbsd0.9*)
	name=i386-netbsd-0.9
	sysincludes=netbsd-0.9
	;;
    i386-*-netbsd1.0*)
	name=i386-netbsd-1.0
	sysincludes=netbsd-1.0
	makefile_frag=netbsd1.0.mk
	syscall=i386-netbsd1.0
	;;
    m68*-*-netbsd*)
	name=m68000-netbsd
	;;
    i386-*-freebsd2.*)
        name=i386-freebsd-2.0
        sysincludes=freebsd-2.0
        syscall=i386-freebsd2.0
        makefile_frag=netbsd1.0.mk
        ;;
    romp-*-bsd*)
	name=romp-bsd
	;;
    mips-*-ultrix*)
	name=r2000-ultrix-4.2
	sysincludes=ultrix-4.2
	syscall=r2000-ultrix-4.2
	makefile_frag=r2000-ultrix-4.2.mk
	;;
    *)
	AC_MSG_ERROR(System type $host not recognized or not supported.
See $srcdir/configure.in for supported systems.)
	exit 1
	;;
esac

dnl Drop in $config_flags for @config_flags@ in output file.
dnl Since we're using system-specific makefile fragments for other purposes
dnl anyways, this should probably go away eventually.
AC_SUBST(config_flags)


AC_CHECK_HEADERS(sys/termio.h termios.h termio.h)

dnl Don't use AC_CHECK_HEADERS for this one, we want to define a special
dnl symbol name starting with pthread_ if it's found.
AC_CHECK_HEADER(va_list.h, AC_DEFINE(pthread_have_va_list_h))

dnl Generate a list of system calls that we could generate.  We later use
dnl a syscall template .S file to produce a .o file for each syscall.  This
dnl is a bit better for porting and development purposes than having every
dnl syscall listed for every target system; this way we only need the
dnl template, and special hanlding for only those syscalls that can't be
dnl handled by the templates.
AC_CHECK_HEADERS(syscall.h sys/syscall.h)

PTHREADS_CHECK_SYSCALLS(open write read creat close fcntl lseek dup2 dup pipe
	fchmod fchown execve fstat link unlink chdir chown chmod stat rename
	select getdtablesize ioctl ftruncate
	dnl - signals
		sigsuspend sigaction sigpause sigprocmask
	dnl - directory reading
		getdents readdir getdirentries
	dnl - variants of `wait' syscalls
		wait4 wait3 waitpid waitsys
	dnl - BSD socket calls
		socket bind connect accept listen getsockopt setsockopt
	dnl - SYSV stream calls
		poll putmsg getmsg
	dnl - Linux version uses one syscall
		socketcall

	dnl - Process info
		pgrpsys

	exit
	readv writev send sendto sendmsg recv recvfrom recvmsg
	getpeername getsockname
	shutdown
	getpgrp fork)

AC_SUBST(available_syscalls)
AC_SUBST(missing_syscalls)

dnl Disabled for now -- generates warnings from autoconf.
dnl ## Check some type sizes.
dnl AC_CHECK_SIZEOF(int)
dnl AC_CHECK_SIZEOF(long)
dnl AC_CHECK_SIZEOF(char *)

## Determine some typedef values from the system header files.
dnl
dnl Autoconf provides these automatically.  They provide reasonable
dnl fallbacks if the standard header files don't define the typedef names.
dnl
dnl AC_TYPE_MODE_T
AC_TYPE_OFF_T
dnl AC_TYPE_PID_T
AC_TYPE_SIZE_T
dnl AC_TYPE_UID_T
dnl
dnl Default these types if their definitions can't be found.
dnl
AC_CHECK_TYPE(ssize_t,int)
AC_CHECK_TYPE(time_t,long)
dnl
AC_CHECK_HEADERS(sys/time.h)
AC_HEADER_TIME

dnl
dnl  Usage: PTHREADS_FIND_TYPE(system-typedef-name, new-macro-name,
dnl				list-of-includes-in-square-brackets,
dnl				comma-separated-list-of-types-to-try)
dnl
dnl  PTHREADS_FIND_INTEGRAL_TYPE automatically provides a set of integral
dnl  types, and does not permit specification of additional types.
dnl
dnl  The specified types must all be able to work as prefixes -- i.e., no
dnl  direct specification of array or function types.  If you need such
dnl  types, add typedefs for them to include/pthread/xtypes.h, and include
dnl  that in the set of header files.  For simple struct types, you can
dnl  try including the definition directly here, but it had better not
dnl  contain any commas or square brackets.
dnl
dnl  If necessary, you can include other preprocessing commands and such
dnl  in the `includes' portion.
dnl
dnl  Note:  For now, each of these needs a corresponding entry
dnl  in acconfig.h.
dnl
dnl size_t
dnl
PTHREADS_FIND_INTEGRAL_TYPE(size_t, pthread_size_t, [
#include <stddef.h>
#include <sys/types.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
])
dnl
dnl ssize_t
dnl
PTHREADS_FIND_INTEGRAL_TYPE(ssize_t, pthread_ssize_t, [
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
])
dnl
dnl clock_t ---FIXME I don't know if this is the right set of header files. KR
dnl
PTHREADS_FIND_INTEGRAL_TYPE(clock_t, pthread_clock_t, [
#include <stddef.h>
#if TIME_WITH_SYS_TIME
# include <time.h>
# include <sys/time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
])
dnl
dnl time_t
dnl
PTHREADS_FIND_INTEGRAL_TYPE(time_t, pthread_time_t, [
#include <stddef.h>
#if TIME_WITH_SYS_TIME
# include <time.h>
# include <sys/time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
])
dnl
dnl fpos_t
dnl
dnl If fpos_t isn't defined in stdio.h, define it to be off_t.  It
dnl looks like AC_CHECK_TYPE won't work for this, because it doesn't
dnl include stdio.h.
dnl
AC_MSG_CHECKING(for fpos_t in stdio.h)
AC_CACHE_VAL(pthreads_cv_fpos_t_in_stdio,
  AC_TRY_COMPILE([#include <stdio.h>], [fpos_t position;],
	pthreads_cv_fpos_t_in_stdio=yes, pthreads_cv_fpos_t_in_stdio=no))
AC_MSG_RESULT($pthreads_cv_fpos_t_in_stdio)
if test $pthreads_cv_fpos_t_in_stdio = yes ; then
  PTHREADS_FIND_INTEGRAL_TYPE(fpos_t, pthread_fpos_t, [
#include <stdio.h>
#include <sys/types.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
])
else
  AC_DEFINE(fpos_t,off_t)
  AC_DEFINE(pthread_fpos_t,pthread_off_t)
fi
dnl
dnl off_t
dnl
PTHREADS_FIND_INTEGRAL_TYPE(off_t, pthread_off_t, [
#include <stdio.h>
#include <sys/types.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
])
dnl
dnl va_list -- This one could be tricky.  Fortunately, on some systems,
dnl we can just include the gcc stdarg.h header file if we can't determine
dnl the type here.  Won't work for non-gcc configurations though.
dnl
PTHREADS_FIND_TYPE(va_list, pthread_va_list, [#include <stdarg.h>],
	char *, char **, void *, void **, int *, long *)
dnl
dnl

arpa_headers="#include <sys/types.h>
#include <arpa/nameser.h>"

dnl
dnl Type for network (IP) addresses, 32 bits.
dnl For now, I'm assuming that the return type from _getlong is it.
dnl
AC_MSG_CHECKING(IP address type)
AC_CACHE_VAL(pthread_cv_type_ipaddr,
  AC_TRY_COMPILE($arpa_headers,[ &_getlong; ],[
    for type in "unsigned long" "unsigned int" ; do
      AC_TRY_COMPILE($arpa_headers, extern $type _getlong ();,[
        pthread_cv_type_ipaddr="$type"
        break
      ])
    done
  ],[ AC_MSG_WARN(Can't compile _getlong reference.) ])
  if test "$pthread_cv_type_ipaddr" = "" ; then
	AC_MSG_WARN(Can't determine _getlong return type.)
	AC_MSG_WARN(Defaulting to unsigned long.)
	pthread_cv_type_ipaddr="unsigned long"
  fi
)
AC_MSG_RESULT($pthread_cv_type_ipaddr)
AC_DEFINE_UNQUOTED(pthread_ipaddr_type,$pthread_cv_type_ipaddr)

dnl
dnl Type for network (IP) ports, 16 bits.
dnl For now, I'm assuming that the return type from _getshort is it.
dnl
AC_MSG_CHECKING(IP port type)
AC_CACHE_VAL(pthread_cv_type_ipport,
  AC_TRY_COMPILE($arpa_headers,[ &_getshort; ],[
    for type in "unsigned short" "unsigned int" ; do
      AC_TRY_COMPILE($arpa_headers, extern $type _getshort ();,[
        pthread_cv_type_ipport="$type"
        break
      ])
    done
  ],[ AC_MSG_WARN(Can't compile _getshort reference.) ])
  if test "$pthread_cv_type_ipport" = "" ; then
	AC_MSG_WARN(Can't determine _getshort return type.)
	AC_MSG_WARN(Defaulting to unsigned short.) 
	pthread_cv_type_ipport="unsigned short"
  fi
)
AC_MSG_RESULT($pthread_cv_type_ipport)
AC_DEFINE_UNQUOTED(pthread_ipport_type,$pthread_cv_type_ipport)

dnl
dnl Guess where terminal devices are stored.  This is for use in the
dnl ttyname() implementation provided here.
dnl
AC_MSG_CHECKING(pathname for terminal devices directory)
if test -d /devices/pseudo ; then
  pty_path=/devices/pseudo/
else
  pty_path=/dev/
fi
AC_DEFINE_UNQUOTED(_PATH_PTY,"$pty_path")
AC_MSG_RESULT($pty_path)

dnl
dnl Look for timezone info, for use in ctime.
dnl
AC_MSG_CHECKING(directory name for time zone info)
tzdir=NONE
for f in /usr/lib/zoneinfo /usr/share/zoneinfo /usr/share/lib/zoneinfo /etc/zoneinfo /usr/local/lib/zoneinfo
do
  if test -d $f ; then
    tzdir=$f
    break
  fi
done
case $tzdir in
NONE)	AC_MSG_ERROR(Can't find zoneinfo directory.) ;;
esac
AC_DEFINE_UNQUOTED(_PATH_TZDIR,"$tzdir")
AC_MSG_RESULT($tzdir)

AC_MSG_CHECKING(filename for local time zone)
tzfile=NONE
for f in $tzdir/localtime /etc/localtime
do
  if test -f $f ; then
    tzfile=$f
    break
  fi
done
case $tzfile in
NONE)	# Should this default to tzdir/localtime?
	AC_MSG_WARN(Can't find local time zone file.) 
	AC_MSG_WARN(Defaulting local time zone file to $tzdir/localtime.) 
	tzfile=$tzdir/localtime
	;;
esac
AC_DEFINE_UNQUOTED(_PATH_TZFILE,"$tzfile")
AC_MSG_RESULT($tzfile)

dnl
dnl Network stuff.  At the moment, I don't think there are any other
dnl values we should expect.  If we find a system that does store these
dnl files elsewhere -- or doesn't have them all -- then run some tests.
dnl
AC_DEFINE(_PATH_RESCONF,	"/etc/resolv.conf")
AC_DEFINE(_PATH_HOSTS,		"/etc/hosts")
AC_DEFINE(_PATH_NETWORKS,	"/etc/networks")
AC_DEFINE(_PATH_PROTOCOLS,	"/etc/protocols")
AC_DEFINE(_PATH_SERVICES,	"/etc/services")

dnl
dnl Other stuff
dnl
AC_DEFINE(_PATH_BSHELL,		"/bin/sh")

dnl If the system provides vfork, autoconf scripts will detect it.
dnl So we should hide it.
AC_CHECK_FUNCS(vfork)

srctop=`cd ${srcdir}/..;pwd`
AC_SUBST(srctop)

# Now we deal with symlinks &c.

test -d include || mkdir include || \
	AC_MSG_ERROR(Can't create include directory.)

test -d include/pthread || mkdir include/pthread || \
	AC_MSG_ERROR(Can't create include/pthread directory.)

test -d tests || mkdir tests || \
	AC_MSG_ERROR(Can't create tests directory.)

links="include/pthread/machdep.h include/pthread/posix.h \
	machdep.c syscall.S"
targets="../machdep/engine-$name.h ../machdep/posix-$sysincludes.h \
	../machdep/engine-$name.c ../machdep/syscall-$name.S"

# Both these targets are optional.  (Autoconf-generated configure scripts
# will require the existence of link targets, so check before adding them
# to the list.)
if test x$sysincludes != xNONE ; then
  links="$links include/sys"
  targets="$targets ../machdep/$sysincludes"
fi

if test x$syscall = xNONE ; then
  syscall=$name
fi
syscall_file=../machdep/syscall-template-$syscall.S
if test -r $srcdir/$syscall_file ; then
  links="$links syscall-template.S"
  targets="$targets $syscall_file"
  HAVE_SYSCALL_TEMPLATE=yes
else
  # This really isn't a fatal problem.  In fact, it's expected, initially,
  # for some targets.  This is just to persuade people to fix the targets
  # they deal with to provide some sort of template.
  #
  # Eventually this file probably will be required...
  AC_MSG_WARN(No syscall template file syscall-template-$syscall.S found.)
  HAVE_SYSCALL_TEMPLATE=no
fi
AC_SUBST(HAVE_SYSCALL_TEMPLATE)

dnl Now tell it to make the links.
dnl (The links are actually made by config.status.)
AC_LINK_FILES($targets, $links)

if test x$makefile_frag != x ; then
  makefile_frag=${srcdir}/$makefile_frag
else
  makefile_frag=/dev/null
fi
dnl Drop in file indicated by $makefile_frag on *the line after* the
dnl magic @makefile_frag@ sequence.
AC_SUBST_FILE(makefile_frag)


dnl Generate output files...
AC_CONFIG_HEADER(config.h)

dnl This must be last.
AC_OUTPUT(config.flags GNUmakefile Makefile \
	tests/Makefile:../tests/Makefile.in)
