dnl Process this file with autoconf to produce a configure script.
dnl
dnl
define(S48_MISALIGNED_DOUBLES, [dnl
	if test "$GCC" = "yes"; then
		AC_MSG_CHECKING([-munaligned-doubles])
		oldCFLAGS="$CFLAGS"
		CFLAGS="$CFLAGS -munaligned-doubles"
		AC_TRY_RUN([int main() { return 0;}],
			[AC_MSG_RESULT(yes)],
			[AC_MSG_RESULT(no)
				CFLAGS="$oldCFLAGS"],
			[AC_MSG_RESULT(no)
				CFLAGS="$oldCFLAGS"])
	fi
])dnl
dnl
dnl
dnl
define(S48_ENABLE_UNIVERSAL_BINARY, [dnl
AC_MSG_CHECKING([whether we are building a Universal Binary])
dnl
AC_ARG_ENABLE([universal-binary],
[AC_HELP_STRING([--enable-universal-binary],
		[Build MacOS X Universal Binary])],
	[dnl 
         CFLAGS="${CFLAGS} -arch i386 -arch ppc";
	 option_enable_universal_binary="yes";
	 AC_DEFINE([BUILD_UNIVERSAL_BINARY], 1,
		   [Define if we are building an OS X Universal Binary.])
	 AC_MSG_RESULT(yes)],
   	[dnl
	 option_enable_universal_binary="no";
	 AC_MSG_RESULT(no)])])
dnl
dnl
dnl
dnl We might want AC_WORDS_BIGENDIAN in the future.
dnl We might want AC_CHAR_UNSIGNED in the future.
dnl
define(S48_POSIX_LIBC, [dnl
echo checking for RISC/OS POSIX library lossage
if test -f /usr/posix/usr/lib/libc.a; then
  LIBS="${LIBS} /usr/posix/usr/lib/libc.a"
fi
])dnl
dnl
dnl Run AC_PROG_CC, but don't accept it's changes to CFLAGS.
dnl For some insane reason, it sets CFLAGS to either `-O' or `-g -O' for gcc.
dnl I don't want the silly -g (because if you are using nlist, you can't strip
dnl the binary), I want -O2 for gcc and -O for other C compilers.
define(S48_PROG_CC, [dnl
	oldCFLAGS="$CFLAGS"
	AC_PROG_CC
	if test "z$oldCFLAGS" = z; then
		if test "z$GCC" = z; then
			CFLAGS='-O'
		else
			CFLAGS='-O2'
		fi
	fi
])dnl
dnl
dnl Linux/ELF systems need the -rdynamic flag when linking so that
dnl dlsym() can find symbols in the executable.
dnl Note, on some Sun's, you can link with -rdynamic but the resulting
dnl a.out always core dumps.
define(S48_RDYNAMIC, [dnl
	AC_MSG_CHECKING([link with -rdynamic])
	AC_TRY_COMPILE([],
[#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__ELF__)
	this must not compile
#endif],
		[AC_MSG_RESULT(no)],
		[AC_MSG_RESULT(yes)
			LDFLAGS="$LDFLAGS -rdynamic"])
])dnl
dnl
define(S48_IEEE_ENDIANNESS, [dnl
AC_MSG_CHECKING([IEEE floating-point endianness])
if test "$option_enable_universal_binary" = "yes" 
then 
  AC_MSG_RESULT([building Universal Binary; using compiler defined macros instead])
else
AC_TRY_RUN([#include <stdio.h>

typedef unsigned long word32_t;

typedef union { double d; word32_t word[2]; } double_overlay;

#define DOUBLE_WORD0(x) ((double_overlay*)&(x))->word[0]
#define DOUBLE_WORD1(x) ((double_overlay*)&(x))->word[1]


int
main(void)
{
  double n = 0.3;
	
  /* least significant byte first */
  if ((DOUBLE_WORD0(n) == 0x33333333) && (DOUBLE_WORD1(n) == 0x3fd33333))
    return 0;
  /* most significant byte first */
  else if ((DOUBLE_WORD1(n) == 0x33333333) && (DOUBLE_WORD0(n) == 0x3fd33333))
    return 1;
  else {
    fprintf(stderr, "WARNING: unknown IEEE format; assuming IEEE with least significant byte first\n");
    return 0;
  }
}], ieee_endianness="least first", ieee_endianness="most first", ieee_endianness="least first")
AC_MSG_RESULT([$ieee_endianness])
if test "$ieee_endianness" = "most first"; then
  AC_DEFINE([IEEE_MOST_FIRST], 1, [Define if IEEE doubles are stored with most-significant byte first.])
fi
fi
])dnl
dnl
define(S48_USCORE, [dnl
	AC_MSG_CHECKING([underscore before symbols])
	echo 'main() { return 0; } fnord() {}' >conftest.c
	if ${CC} ${CFLAGS} ${CPPFLAGS} ${LDFLAGS} conftest.c ${LIBS} &&
	nm a.out | grep _fnord >/dev/null; then
		AC_MSG_RESULT([yes])
		AC_DEFINE(USCORE)
	else
		AC_MSG_RESULT([no])
	fi
	rm -f conftest.c a.out
])dnl
dnl
define(S48_CREATE_BUILD_DIRS, [dnl
        mkdir -p c/unix
        mkdir -p c/posix
        mkdir -p c/fake
])dnl
dnl
AC_INIT(c/prescheme.h)
     AC_CONFIG_HEADER(c/sysdep.h)
     S48_ENABLE_UNIVERSAL_BINARY
     S48_CREATE_BUILD_DIRS
     S48_PROG_CC
     AC_ISC_POSIX
     dnl set the cross-compile flag before we try anything.
     AC_TRY_RUN([int main() { return 0;}], [], [], [true])
     AC_PROG_INSTALL
     AC_CHECK_LIB(m, main)
     AC_CHECK_LIB(dl, main)
     AC_CHECK_LIB(mld, main)
     AC_CHECK_LIB(nsl, main)
     AC_CHECK_LIB(gen, main)
     AC_CHECK_LIB(socket, main)
     AC_CHECK_LIB(sun, getpwnam)
     dnl Solaris 2.3 seems to need -lelf for nlist().  (tnx Bryan O'Sullivan)
     AC_CHECK_LIB(elf, main)
     S48_POSIX_LIBC
     AC_CONST
     AC_MSG_CHECKING(for socklen_t)
     AC_TRY_COMPILE([#include <sys/types.h>
                     #include <sys/socket.h>
                     socklen_t x;
                    ],[],
		    [AC_MSG_RESULT(yes)],
		    [AC_TRY_COMPILE([#include <sys/types.h>
                                     #include <sys/socket.h>
                                     int accept (int,
				                 struct sockaddr *,
						 size_t *);
                                    ],[],
		                    [AC_MSG_RESULT(size_t)
                                     AC_DEFINE(socklen_t,size_t)],
				    [AC_MSG_RESULT(int)
                                     AC_DEFINE(socklen_t,int)])])
     AC_RETSIGTYPE
     AC_CHECK_HEADERS(libgen.h sys/timeb.h posix/time.h)
     AC_CHECK_HEADERS(sys/select.h)
     AC_CHECK_HEADERS(sysexits.h)
     AC_CHECK_HEADERS(langinfo.h)
     AC_CHECK_FUNC(nl_langinfo, [], [AC_LIBOBJ([c/fake/langinfo])])
     AC_CHECK_FUNCS(gettimeofday ftime nlist select setitimer sigaction)
     AC_CHECK_FUNC(dlopen, [AC_DEFINE(HAVE_DLOPEN, 
				1, [Define to 1 if the interface to the dynamic linker exists])],
			   [AC_CHECK_FUNC(nlist, [AC_LIBOBJ([c/fake/libdl1])],
	       					 [AC_LIBOBJ([c/fake/libdl2])])])
     AC_CHECK_FUNCS(socket chroot)
     AC_CHECK_FUNC(strerror, AC_DEFINE(HAVE_STRERROR),
			      [AC_LIBOBJ([c/fake/strerror.o])])
dnl Some idiot renamed `environ' as `__environ' in some versions of Linux.
dnl POSIX says it's `environ'.
     AC_MSG_CHECKING([environ])
     AC_TRY_LINK([],
		 [extern char **environ;
		  if (environ)
		     return 0;
		  else
		     return 1;],
               dnl Have `environ'
		 [AC_DEFINE(ENVIRON_NAME,environ)
		  AC_MSG_RESULT([using environ])],
               dnl Do not have `environ'
	         [AC_TRY_LINK([],
		              [extern char **__environ;
			       if (__environ)
			         return 0;   
			       else
				 return 1;],
                            dnl Have `__environ'
		               [AC_DEFINE(ENVIRON_NAME,__environ)
	     		        AC_MSG_RESULT([using __environ])],
                           dnl Do not have `__environ'
                               [AC_MSG_WARN([no environ variable found])
                                AC_LIBOBJ([c/fake/environ.o])])])
     AC_MSG_CHECKING([n_name])
     AC_TRY_LINK([#include <nlist.h>],
		 [struct nlist name_list;
		  name_list.n_name = "foo";],
		 AC_DEFINE(NLIST_HAS_N_NAME)
		 AC_MSG_RESULT([yes]),
		 AC_MSG_RESULT([no]))
     AC_MSG_CHECKING([__NEXT__])
     AC_TRY_LINK(,[
#ifdef	__NeXT__
return 0;
#else
fail
#endif
],
		CC="$CC -posix"
		AC_DEFINE(HAVE_SIGACTION)
		AC_MSG_RESULT([yes]),
		AC_MSG_RESULT([no]))
     S48_USCORE
     S48_RDYNAMIC
     S48_MISALIGNED_DOUBLES
     S48_IEEE_ENDIANNESS
     AC_SUBST(CFLAGS)
     AC_SUBST(LDFLAGS)
AC_OUTPUT(Makefile)
