dnl Process this file with autoconf to produce a configure script.

AC_INIT(pl-wam.c)

AC_CONFIG_HEADER(config.h)
AC_SUBST(PLBASE)
AC_SUBST(COFLAGS)
AC_SUBST(CWFLAGS)
AC_SUBST(CMFLAGS)
AC_SUBST(CIFLAGS)
AC_SUBST(AR)
AC_SUBST(MKDIR)
AC_SUBST(ETAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(ARCH)
AC_SUBST(PLSHARED)
AC_SUBST(PL)
AC_SUBST(RT)
AC_SUBST(PLVERSION)
AC_SUBST(VPATH)
AC_SUBST(COUTOPT)
AC_SUBST(INSTALLTARGET)
AC_SUBST(INSTALLRLMAN)
AC_SUBST(EXTRAOBJ)
AC_SUBST(BOOTFILE)

if test ! -z "$CIFLAGS"; then
    CFLAGS="$CIFLAGS $CFLAGS"
    CPPFLAGS="$CIFLAGS $CPPFLAGS"
fi

EXTRAOBJ=""
INSTALLTARGET=dv-install
PL=pl
RT=""
PLVERSION=`cat ../VERSION`

dnl ****************************************************************
dnl Some systems don't appear to have true.  Hope all like this ...
dnl ****************************************************************

true()
{ test a = a
}

AC_PROG_CC
AC_PROG_RANLIB
AC_CANONICAL_HOST
changequote(,)dnl
ARCH=`echo $host | sed 's/\([^-]*\)-[^-]*-\([^-]*\)/\1-\2/'`
changequote([,])dnl

AC_ARG_ENABLE(runtime, [  --enable-runtime	  Runtime system only],
	      [case "$enableval" in
		   yes) AC_DEFINE(O_RUNTIME)
		        runtime=yes
			RT=rt
			INSTALLTARGET=rt-install ;;
		   *)  ;;
	       esac])

AC_ARG_ENABLE(mt, [  --enable-mt		  Enable Multi-threading],
	      [case "$enableval" in
		   yes) MT=yes
		        AC_DEFINE(NO_SEGV_HANDLING)
			;;
		   *)  ;;
	       esac])

AC_ARG_ENABLE(readline, [  --disable-readline	  Do NOT include GNU readline],
	      configreadline="$enableval",
	      configreadline=yes)

AC_ARG_ENABLE(mapped-stacks, [  --disable-mapped-stacks Do not use mmap for stacks],
	      configmmappedstacks="$enableval",
	      configmmappedstacks=yes)

AC_ARG_ENABLE(segv-handling, [  --disable-segv-handling Do not use SIGSEGV to guard stacks],
	      if test "$enableval" = "no"; then
	          AC_DEFINE(NO_SEGV_HANDLING)
	      fi)

dnl ****************************************************************
dnl Checking for GNU make vpath construct
dnl ****************************************************************

AC_CHECK_PROGS(MAKE, make gmake, "make")
AC_MSG_CHECKING("whether make is GNU-make")
if ($MAKE -v 2>&1) | grep GNU > /dev/null; then
    AC_MSG_RESULT(yes)
    VPATH="vpath %.c"
else
    VPATH="VPATH = "
    AC_MSG_RESULT(no)
fi

dnl ****************************************************************
dnl Checking mkdir -p
dnl ****************************************************************

AC_MSG_CHECKING("whether mkdir accepts -p")
if (mkdir -p config.tmpdir/p && mkdir -p config.tmpdir/p 2>&1) > /dev/null; then
    AC_MSG_RESULT(yes)
    MKDIR="mkdir -p"
else
    AC_MSG_RESULT(no)
    MKDIR=mkdir
fi
rm -rf config.tmpdir

dnl ****************************************************************
dnl Finding various programs
dnl ****************************************************************

AC_CHECK_PROG(AR, ar, ar, ar)
AC_CHECK_PROGS(ETAGS, etags ctags, ":")
AC_PROG_AWK
AC_PROG_INSTALL
case "$INSTALL" in
    ./*) INSTALL="`pwd`/$INSTALL" ;;
esac
AC_PROG_CPP
AC_ISC_POSIX
AC_HEADER_STDC
if test "$GCC" = "yes"; then
    COFLAGS=-O2
    if test "$ac_cv_header_stdc" = "yes" ; then
        CWFLAGS=-Wall
    else
	CWFLAGS=
    fi
    CMFLAGS=
else
    COFLAGS=-O
    CWFLAGS=
    CMFLAGS=

    case "$ARCH" in
	*mips-irix*)
	    COFLAGS="-O -OPT:Olimit_opt=on"
	    ;;
    esac
fi

dnl ****************************************************************
dnl On SCO Unix, there are ELF and COFF binaries.  We want elf to
dnl provide for dlopen() and friends.  With help from Tamas Laufer!
dnl ****************************************************************

case "$ARCH" in
    *sco*|*SCO*)
	AC_MSG_CHECKING("SCO-Unix.  See if we need -belf")
	old_cflags="$CFLAGS"
	CFLAGS="$CFLAGS -belf"
	AC_TRY_COMPILE(
	[
	],
	[
	],
	CMFLAGS="$CMFLAGS -belf"
	AC_MSG_RESULT(yes),
	AC_MSG_RESULT(no)
	CFLAGS="$old_cflags")
	;;
esac

AC_C_INLINE
AC_FUNC_ALLOCA
AC_C_BIGENDIAN

dnl ****************************************************************
dnl Finding libraries
dnl ****************************************************************

if test "$MT" = "yes"; then
  cat << _EOM_

    #################################################################
    # You have requested support for MULTI-THREADING.  This feature #
    # highly experimental and for developers only.  If you are a    #
    # normal SWI-Prolog user, please rerun configure without the    #
    # --enable-mt option					    #
    #                                                               #
    # Currently multi-threading is developed using SuSE Linux 6.2   #
    # (glibc 2.1.1) and ported to Solaris 2.7 (egcs 1.1)            #
    #################################################################

_EOM_
  AC_CHECK_LIB(pthread, main)
  if test "$ac_cv_lib_pthread_main" = "yes"; then
    AC_DEFINE(O_PLMT)
  fi
  AC_CHECK_LIB(rt, sem_init)
  AC_CHECK_FUNCS(sem_init)
  if test ! "$ac_cv_lib_rt_sem_init" = "yes"; then
	AC_CHECK_LIB(thread,sema_init)
	AC_CHECK_FUNCS(sema_init)
  fi
  AC_CHECK_FUNCS(pthread_mutexattr_setkind_np pthread_mutexattr_settype)
  AC_MSG_CHECKING("wether mutex support recursive locking")
  echo '#include "confdefs.h"' > conftest.c
  cat $srcdir/test/recmutex.c >> conftest.c
  (eval $ac_link) 2>&5
  if test -s conftest && ./conftest; then
    AC_MSG_RESULT(yes)
    AC_DEFINE(RECURSIVE_MUTEXES)
  else
    AC_MSG_RESULT(no)
  fi
  AC_CHECK_FUNCS(pthread_sigmask)
fi

AC_CHECK_LIB(m, main)
AC_CHECK_LIB(ncurses, main)
if test ! "$ac_cv_lib_ncurses_main" = "yes"; then
  AC_CHECK_LIB(curses, main)
fi
if test ! "$ac_cv_lib_ncurses_main" = "yes" -a \
        ! "$ac_cv_lib_curses_main" = "yes"; then
  AC_CHECK_LIB(termcap, main)
  if test "$ac_cv_lib_termcap_main" = "yes"; then
    AC_MSG_CHECKING("whether -ltermcap needs -lucb")
    AC_TRY_LINK([],
		[ tgetent();
		],
		AC_MSG_RESULT(no)
		true,
		AC_MSG_RESULT(maybe ...)
		checkucb=yes)
  fi
fi
if test "$checkucb" = "yes"; then
    AC_CHECK_LIB(ucb, rindex)
fi


if test ! "$runtime" = "yes" -a "$configreadline" = "yes"; then
  oldlibs="$LIBS"
  AC_CHECK_LIB(readline, main)
  if test ! "$ac_cv_lib_readline_main" = "yes"; then
    AC_CHECK_LIB(ncurses, main)
    if test ! "$ac_cv_lib_ncurses_main" = "yes"; then
      AC_CHECK_LIB(curses, main)
    fi
    if test "$ac_cv_lib_ncurses_main" = "yes" || test "$ac_cv_lib_curses_main" = "yes"; then
      echo "Found curses library.  See if -lreadline works now"
      unset ac_cv_lib_readline_main
      AC_CHECK_LIB(readline, main)
    fi
  fi

  AC_CHECK_HEADERS(readline/readline.h)
  if test "$ac_cv_header_readline_readline_h" = no; then
      unset ac_cv_header_readline_readline_h
      CFLAGS="$CFLAGS -I/usr/local/include"
      AC_CHECK_HEADERS(readline/readline.h)
      if test "$ac_cv_header_readline_readline_h" = yes; then
          CIFLAGS="$CIFLAGS -I/usr/local/include"
      else
	  LIBS="$oldlibs"
      fi
  fi

  if test "$ac_cv_lib_readline_main" = "yes"; then
    AC_MSG_CHECKING(whether to install readline.3)
    INSTALLRLMAN=install_rlman
    if test -z "$MANPATH"; then MANPATH=/usr/man; fi
    for d in `echo $MANPATH | sed 's/:/ /g'`; do
      if test -r $d/man3/readline.3; then
        INSTALLRLMAN=""
      fi
    done
    if test -z "$INSTALLRLMAN"; then
      AC_MSG_RESULT(Already present)
    else
      AC_MSG_RESULT(yes)
    fi
  fi
fi

AC_ARG_ENABLE(shared, [  --disable-shared	  No shared-lib interface],
	      configshared="$enableval",
	      configshared=yes)

PLSHARED=no
if test "$configshared" = "yes"; then
  AC_CHECK_LIB(dl, dlopen)
  if test "$ac_cv_lib_dl_dlopen" = "yes"; then
    AC_CHECK_FUNCS(dlopen)
  else
    AC_CHECK_LIB(dld, shl_load)
    AC_CHECK_FUNCS(shl_load)
  fi
fi

dnl On SCO, dlopen is in libc.so and requires dlfcn.h to compile.  Thanks
dnl to Tamas Laufer.

if test ! "$ac_cv_func_dlopen" = "yes"; then
AC_MSG_CHECKING("dlopen in libc")
AC_TRY_COMPILE(
[
#include <dlfcn.h>
#ifndef RTLD_NOW
#define RTLD_NOW 0
#endif
],
[ dlopen("foobar", RTLD_NOW);
], AC_DEFINE(HAVE_DLOPEN)
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no))
fi

if test "$ac_cv_func_dlopen" = yes -o "$ac_cv_func_shl_load" = yes; then
  PLSHARED=yes
fi

AC_MSG_CHECKING(for cc -c -o)
  cat > conftest.c << _EOP_
main()
{ exit(0);
}
_EOP_
  if ${CC-cc} $CFLAGS -c conftest.c -o conftest.o 1>&5 2>&5 && test -r conftest.o; then
    COUTOPT='-o $@'
    AC_MSG_RESULT(yes)
  else
    COUTOPT=
    AC_MSG_RESULT(no)
  fi
  rm -rf conftest.*

AC_CHECK_HEADERS(unistd.h string.h memory.h sys/time.h sys/stat.h pwd.h)
AC_CHECK_HEADERS(sys/select.h sys/param.h sys/file.h sys/resource.h)
AC_CHECK_HEADERS(malloc.h sys/malloc.h)
AC_CHECK_HEADERS(sys/termios.h sys/termio.h bstring.h sys/mman.h)
AC_CHECK_HEADERS(mach-o/rld.h locale.h)

AC_CHECK_FUNCS(access chmod dossleep fstat readlink getwd getcwd)
AC_CHECK_FUNCS(getpagesize getpwnam getrlimit gettimeofday opendir sysconf)
AC_CHECK_FUNCS(popen putenv random remove rename rl_insert_close select)
AC_CHECK_FUNCS(signal sigset sigprocmask sleep usleep srand srandom stat)
AC_CHECK_FUNCS(memmove stricmp strlwr sigaction sigsetmask siggetmask sigblock)
AC_CHECK_FUNCS(rint aint floor ceil isnan tcsetattr cfmakeraw)
AC_CHECK_FUNCS(strerror tgetent times mmap unsetenv grantpt)
AC_CHECK_FUNCS(setlocale)
AC_HEADER_TIME
AC_HEADER_DIRENT
AC_HEADER_SYS_WAIT
AC_FUNC_VFORK

AC_CHECK_SIZEOF(long, 4)
AC_CHECK_SIZEOF(int,  4)

AC_MSG_CHECKING("Boot filename");
BOOTFILE=boot`expr ${ac_cv_sizeof_long} \* 8`.prc
AC_DEFINE_UNQUOTED(BOOTFILE, "$BOOTFILE")
AC_MSG_RESULT($BOOTFILE)

if test "$GCC" = "yes"; then
dnl See whether we need to force __register_frame_info to be included.
dnl This is a glibc/egcs portability problem for binaries accross
dnl installations.

AC_MSG_CHECKING("Static include of __register_frame_info")
if ar x `${CC-cc} -print-libgcc-file-name` frame.o &&
   nm frame.o | grep  __register_frame_info 2>&1 > /dev/null; then
   AC_MSG_RESULT(yes)
   EXTRAOBJ="$EXTRAOBJ frame.o"
else
   AC_MSG_RESULT(no)
fi
rm -f frame.o
fi

AC_MSG_CHECKING("alignment restrictions on doubles")
AC_TRY_RUN(
[ main()
  { long l[10];
    double *p;
    unsigned long x = (unsigned long) l;

#if defined(__alpha__) || defined(__alpha)
    exit(1);
#endif

    if ( x % sizeof(double) == 0 )
      x += sizeof(long);
    p = (double  *) x;
    *p = 1.0;

    exit(0);
  }
],
AC_MSG_RESULT(as long),
AC_MSG_RESULT(sizeof(double))
AC_DEFINE(DOUBLE_ALIGNMENT, sizeof(double)),
AC_MSG_RESULT(assuming sizeof(double))
AC_DEFINE(DOUBLE_ALIGNMENT, sizeof(double)))

AC_MSG_CHECKING("unaligned access")
AC_TRY_RUN(
[ main()
  { char l[sizeof(long)*2];
    long *p;
    int i;

#if defined(__alpha__) || defined(__alpha)
    exit(1);
#endif

    for(i=0; i<sizeof(long); i++)
    { p = (long *)(l+i);
      *p = 42;
    }

    exit(0);
  }
],
AC_MSG_RESULT(allowed)
AC_DEFINE(NON_ALIGNED_ACCESS),
AC_MSG_RESULT(not allowed),
AC_MSG_RESULT(assuming not allowed))

if test "$ac_cv_func_getrlimit" = "yes"; then
AC_MSG_CHECKING("type rlim_t")
AC_TRY_COMPILE(
[
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
],
[ rlim_t datasize;
], AC_DEFINE(HAVE_RLIM_T)
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no))
fi

AC_MSG_CHECKING("whether assert.h needs stdio.h")
AC_TRY_COMPILE(
[
#include <assert.h>
],
[ assert(1);
], AC_MSG_RESULT(no),
   AC_MSG_RESULT(yes);
   AC_DEFINE(ASSERT_H_REQUIRES_STDIO_H))

AC_CHECKING("for including sys/ioctl.h after sys/termios.h")
if test "$GCC" = "yes"; then
    ac_oldcflags="$CFLAGS"
    CFLAGS="$CFLAGS -Werror"
fi
AC_TRY_COMPILE(
[
#include <stdio.h>
#include <sys/types.h>
#include <sys/termios.h>
#include <sys/ioctl.h> 
],
[;
], true, AC_DEFINE(NO_SYS_IOCTL_H_WITH_SYS_TERMIOS_H))
if test "$GCC" = "yes"; then CFLAGS="$ac_oldcflags"; fi

if test -z "$ac_cv_header_sys_termio_h" -a -z "$ac_cv_header_sys_termios_h"; then
AC_MSG_CHECKING("struct sgttyb")
AC_TRY_COMPILE(
[
#include <sys/types.h>
#include <sys/ioctl.h>
],
[ struct sgttyb buf;
  ;
], AC_DEFINE(HAVE_SGTTYB)
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no))
fi


AC_MSG_CHECKING("for type uchar")
AC_TRY_COMPILE(
[
#include <sys/types.h>
typedef unsigned char uchar;
],
[;], AC_DEFINE(NEED_UCHAR)
     AC_MSG_RESULT("need typedef uchar"),
     AC_MSG_RESULT("uchar predefined"))

AC_MSG_CHECKING("whether we need extern int errno")
AC_TRY_COMPILE(
[
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
],
[ return errno;
], AC_MSG_RESULT(no),
   AC_DEFINE(NEED_DECL_ERRNO)
   AC_MSG_RESULT(yes))

AC_MSG_CHECKING("for symbolic links")
if touch conftest.x && ln -s conftest.x conftest.lns && cmp conftest.x conftest.lns; then
    AC_DEFINE(HAVE_SYMLINKS)
    AC_MSG_RESULT(yes)
else
    AC_MSG_RESULT(no)
fi

AC_TYPE_SIGNAL
AC_MSG_CHECKING("for BSD signal handlers")
AC_TRY_RUN(
[
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
static int signalled;
static RETSIGTYPE catch (s) int s; { signalled = 1; }
main()
{ signal(SIGINT, catch);
  kill(getpid(), SIGINT);
  while(!signalled)
    sleep(1);
  if ( signal(SIGINT, catch) == catch )
    exit(0);
  exit(1);
}], AC_DEFINE(BSD_SIGNALS)
    AC_MSG_RESULT("BSD signals"),
    AC_MSG_RESULT("Posix signals"),
    AC_MSG_RESULT("assuming Posix signals"))

AC_MSG_CHECKING("for SIGPROF profiler support")
AC_TRY_RUN([
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
#include <signal.h>
static RETSIGTYPE prof(s) int s; {exit(0);}
main()
{ struct itimerval value, ovalue;
  int n;
  value.it_interval.tv_sec  = 0;
  value.it_interval.tv_usec = 1;
  value.it_value.tv_sec  = 0;
  value.it_value.tv_usec = 1;
  
  signal(SIGPROF, prof);

  if (setitimer(ITIMER_PROF, &value, &ovalue) != 0)
    exit(1);

  for(n=0; n<10000000; n++)
    ;
  exit(1);
}], AC_DEFINE(O_PROFILE)
    AC_MSG_RESULT(yes),
    AC_MSG_RESULT(no),
    true)

AC_MSG_CHECKING("for union wait")
if test "$GCC" = yes; then
    ac_oldcflags="$CFLAGS"
    CFLAGS="$CFLAGS -Werror"
fi
AC_TRY_COMPILE(
[
#include <sys/wait.h>
],
[ int status;
  waitpid(1, &status, WNOHANG);
], AC_MSG_RESULT(yes),
   AC_DEFINE(UNION_WAIT)
   AC_MSG_RESULT(no))
if test "$GCC" = "yes"; then CFLAGS="$ac_oldcflags"; fi

AC_MSG_CHECKING("for tagged lvalues")
AC_TRY_COMPILE(
[
],
[ int x;
  *(char *)&x = 'a';
  exit(0);
], AC_DEFINE(TAGGED_LVALUE)
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no))

AC_MSG_CHECKING("for label addresses")
AC_TRY_COMPILE(
[ 
],
[ int x=0;
  void *addr;

  addr = &&label2;
  goto *addr;

  label1:
	x = 1;
	goto label3;
  label2:
	x = 2;
  
  label3:
	if ( x == 2 )
  	  exit(0);

        exit(1);
], AC_DEFINE(O_LABEL_ADDRESSES)
   AC_MSG_RESULT(yes),
   AC_DEFINE(O_LABEL_ADDRESSES, 0)
   AC_MSG_RESULT(no))


dnl ****************************************************************
dnl Very specific SWI-Prolog tests.  First of all, we'll try to determine
dnl the direction of the C-stack as well as the area where malloc()'ed data
dnl lifes.  We run test/memmodel.c for this.
dnl ****************************************************************

AC_MSG_CHECKING("memory model");
echo '#include "confdefs.h"' > conftest.c
cat $srcdir/test/memmodel.c >> conftest.c
(eval $ac_link) 2>&5
if test -s conftest && eval `./conftest`; then
    AC_DEFINE_UNQUOTED(STACK_DIRECTION, $STACK_DIRECTION)
    AC_MSG_RESULT(yes)
else
    AC_MSG_WARN("Failed to compile test/memmodel.c")
fi
rm -fr conftest*

dnl ****************************************************************
dnl Test whether we can use asm("nop") to separate instructions.
dnl Needed for gcc version 2 (see pl-wam.c)
dnl ****************************************************************

AC_MSG_CHECKING("for nop");
echo '#include "confdefs.h"' > conftest.c
cat $srcdir/test/nop.c >> conftest.c
(eval $ac_link) 2>&5
if test -s conftest && eval `./conftest`; then
    AC_MSG_RESULT(yes)
else
    AC_DEFINE(NO_ASM_NOP)
    AC_MSG_RESULT(no)
fi
rm -fr conftest*


if test "$ac_cv_func_mmap" = "yes" -a "$configmmappedstacks" = "yes"; then
dnl ****************************************************************
dnl First lets see whether our signal handler is POSIX.1b
dnl ****************************************************************

AC_CHECK_HEADERS(siginfo.h)

AC_MSG_CHECKING("for SA_SIGINFO");
AC_TRY_COMPILE(
[ #include <signal.h> 
  #ifdef HAVE_SIGINFO_H
  #include <siginfo.h>
  #endif
  extern void handler(int sig, siginfo_t *info, void *);
],
[ struct sigaction act;

  act.sa_sigaction = handler;
  act.sa_flags = SA_SIGINFO;
  
  sigaction(SIGSEGV, (void *)0, &act);
], AC_DEFINE(HAVE_SIGINFO)
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no))

dnl ****************************************************************
dnl More tricky stuff.  Try to find out whether or not we can use mmap()
dnl to implement dynamic stacks.  The program test/mmap.c tests for this.
dnl ****************************************************************

mmap_tried=segv;
while test ! "$MMAP_STACK" = "1" -a ! $mmap_tried = "stop"; do
AC_MSG_CHECKING("if we can use mmap for stack allocation")
echo '#include "confdefs.h"' > conftest.c
cat $srcdir/test/mmap.c >> conftest.c
(eval $ac_link) 2>&5
if test -s conftest; then
    if eval `./conftest`; then
	if test "$MMAP_STACK" = "1"; then
	    if test "$HAVE_MAP_ANON" = "1"; then
		AC_DEFINE(HAVE_MAP_ANON)
		AC_MSG_RESULT("Using MAP_ANON")
	    else
		AC_MSG_RESULT("Using MAP_PRIVATE")
	    fi
	    AC_DEFINE(MMAP_STACK)
	    AC_MSG_CHECKING("if SA_SIGINFO is usable")
	    if test "$SIGNAL_HANDLER_PROVIDES_ADDRESS" = "1"; then
	      AC_MSG_RESULT(yes)
	      AC_DEFINE(USE_SIGINFO)
	    else
	      AC_MSG_RESULT(no)
	    fi
	else
	    AC_MSG_RESULT("mmap is not good enough")
	fi
    else
        AC_MSG_RESULT("could not run mmap test");
    fi
else
    AC_MSG_RESULT("could not compile mmap test");
fi
rm -fr conftest*
if test ! "$MMAP_STACK" = "1" -a "$mmap_tried" = "segv"; then
    mmap_tried=nosegv
    AC_DEFINE(NO_SEGV_HANDLING)
    AC_MSG_CHECKING("Retrying without handling SEGV")
else
    mmap_tried=stop
fi
done
fi # mmap

if test "$ac_cv_func_dlopen" = "yes" || \
   test "$ac_cv_func_shl_load" = "yes"; then

dnl Some versions of (g)cc need to export the symbols from the main program
dnl using -export-dynamic or (older versions) -rdynamic.  See whether
dnl this gcc understands this flag.

AC_MSG_CHECKING("whether LDFLAGS need -export-dynamic")
  old_ldflags="$LDFLAGS"
  LDFLAGS="-export-dynamic $LDFLAGS"
  AC_TRY_RUN(
main()
{ exit(0);
},
     if grep 'unrecognized option `-export-dynamic'"'" config.log >/dev/null; then
       AC_MSG_RESULT(no)
       LDFLAGS="$old_ldflags"
     else
       AC_MSG_RESULT(yes)
       exportdynamic=done
     fi,
     AC_MSG_RESULT(no)
     LDFLAGS="$old_ldflags",
     AC_MSG_RESULT(no)
     LDFLAGS="$old_ldflags")

if test "$exportdynamic" != "done"; then
AC_MSG_CHECKING("whether LDFLAGS need -rdynamic")
  old_ldflags="$LDFLAGS"
  LDFLAGS="-rdynamic $LDFLAGS"
  AC_TRY_RUN(
main()
{ exit(0);
},
     if grep 'unrecognized option `-rdynamic'"'" config.log >/dev/null; then
       AC_MSG_RESULT(no)
       LDFLAGS="$old_ldflags"
     else
       AC_MSG_RESULT(yes)
     fi,
     AC_MSG_RESULT(no)
     LDFLAGS="$old_ldflags",
     AC_MSG_RESULT(no)
     LDFLAGS="$old_ldflags")
fi	dnl test for -exportdynamic

dnl Some specific OS stuff

case "$ARCH" in
  *hppa*|*hpux*)
	AC_DEFINE(SO_EXT, "sl")
	if test "$GCC" = "yes"; then
	    LDFLAGS="$LDFLAGS -Wl,-E"
	else
	    LDFLAGS="$LDFLAGS -E"
	fi
	;;
  *aix*)
	AC_DEFINE(SO_EXT, "so")
	if test "$GCC" = "yes"; then
	    LDFLAGS="$LDFLAGS -Wl,-bE:../include/SWI-Exports"
	else
	    LDFLAGS="$LDFLAGS -bE:../include/SWI-Exports"
	fi
	;;
  *)
	AC_DEFINE(SO_EXT, "so")
	;;
esac

fi	dnl dlopen or shl_load

echo "	CFLAGS=$CWFLAGS $COFLAGS $CIFLAGS $CMFLAGS"
echo "	LDFLAGS=$LDFLAGS"
echo "	LIBS=$LIBS"

OutputFiles="Makefile rc/Makefile pl.1"

# Prepare the package configuration if present

if test -r ../packages/Makefile.in; then
  OutputFiles="$OutputFiles ../packages/Makefile"
fi

AC_OUTPUT($OutputFiles)

cat << _EOM_
****************************************************************
* Now type the following to make and install the basic environment
*
*	% make
*	% make install
*
* To complete the environment, goto the packages directory and check
* the README file for further instructions.
****************************************************************

_EOM_

