#! /bin/sh

#########################################################################
#                                                                       #
#                            Objective Caml                             #
#                                                                       #
#            Xavier Leroy, projet Cristal, INRIA Rocquencourt           #
#                                                                       #
#   Copyright 1999 Institut National de Recherche en Informatique et    #
#   en Automatique.  All rights reserved.  This file is distributed     #
#   under the terms of the GNU Library General Public License, with     #
#   the special exception on linking described in file LICENSE.         #
#                                                                       #
#########################################################################

# $Id: configure,v 1.154 2001/12/07 14:23:01 xleroy Exp $

configure_options="$*"
prefix=/usr/local
bindir=''
libdir=''
mandir=''
manext=1
host_type=unknown
ccoption=''
cclibs=''
curseslibs=''
mathlib='-lm'
dllib=''
x11_include_dir=''
x11_lib_dir=''
tk_wanted=yes
tk_defs=''
tk_libs=''
posix_threads=no
verbose=no
withcurses=yes
withsharedlibs=yes

gcc_warnings="-Wall -Wno-unused"

# Try to turn internationalization off, can cause config.guess to malfunction!
unset LANG
unset LC_ALL LC_CTYPE LC_COLLATE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME 

# Parse command-line arguments

while : ; do
  case "$1" in
    "") break;;
    -prefix|--prefix)
        prefix=$2; shift;;
    -bindir|--bindir)
        bindir=$2; shift;;
    -libdir|--libdir)
        libdir=$2; shift;;
    -mandir|--mandir)
        mandir=$2
        manext=`echo $mandir | sed -e 's/^.*\(.\)$/\1/'`
        shift;;
    -host*|--host*)
        host_type=$2; shift;;
    -cc*)
        ccoption="$2"; shift;;
    -lib*)
        cclibs="$2 $cclibs"; shift;;
    -no-curses)
        withcurses=no;;
    -no-shared-libs)
        withsharedlibs=no;;
    -x11include*|--x11include*)
        x11_include_dir=$2; shift;;
    -x11lib*|--x11lib*)
        x11_lib_dir=$2; shift;;
    -with-pthread*|--with-pthread*)
        posix_threads=yes;;
    -notk|--notk)
        tk_wanted=no;;
    -tkdefs*|--tkdefs*)
        tk_defs=$2; shift;;
    -tklibs*|--tklibs*)
        tk_libs=$2; shift;;
    -verbose|--verbose)
        verbose=yes;;
    *) echo "Unknown option \"$1\"." 1>&2; exit 2;;
  esac
  shift
done

# Sanity checks

case "$prefix" in
  /*) ;;
   *) echo "The -prefix directory must be absolute." 1>&2; exit 2;;
esac
case "$bindir" in
  /*) ;;
  "") ;;
   *) echo "The -bindir directory must be absolute." 1>&2; exit 2;;
esac
case "$libdir" in
  /*) ;;
  "") ;;
   *) echo "The -libdir directory must be absolute." 1>&2; exit 2;;
esac
case "$mandir" in
  /*) ;;
  "") ;;
   *) echo "The -mandir directory must be absolute." 1>&2; exit 2;;
esac

# Generate the files

cd config/auto-aux
rm -f s.h m.h Makefile
touch s.h m.h Makefile

# Write options to Makefile

echo "# generated by ./configure $configure_options" >> Makefile

# Where to install

echo "PREFIX=$prefix" >> Makefile
case "$bindir" in
  "") echo 'BINDIR=$(PREFIX)/bin' >> Makefile
      bindir="$prefix/bin";;
   *) echo "BINDIR=$bindir" >> Makefile;;
esac
case "$libdir" in
  "") echo 'LIBDIR=$(PREFIX)/lib/ocaml' >> Makefile
      libdir="$prefix/lib/ocaml";;
   *) echo "LIBDIR=$libdir" >> Makefile;;
esac
case "$mandir" in
  "") echo 'MANDIR=$(PREFIX)/man/man1' >> Makefile
      mandir="$prefix/man/man1";;
   *) echo "MANDIR=$mandir" >> Makefile;;
esac
echo "MANEXT=$manext" >> Makefile

# Determine the system type

if test "$host_type" = "unknown"; then
  if host_type=`../gnu/config.guess`; then :; else
    echo "Cannot guess host type"
    echo "You must specify one with the -host option"
    exit 2
  fi
fi
if host=`../gnu/config.sub $host_type`; then :; else
  echo "Please specify the correct host type with the -host option"
  exit 2
fi
echo "Configuring for a $host ..."

# Do we have gcc?

if test -z "$ccoption"; then
  if sh ./searchpath gcc; then
    echo "gcc found"
    cc=gcc
  else
    cc=cc
  fi
else
  cc="$ccoption"
fi

# Check for buggy versions of GCC

case "$host,$cc" in
  i[3456]86-*-*,gcc*)
    case `$cc --version` in
      2.7.2.1) cat <<'EOF'

WARNING: you are using gcc version 2.7.2.1 on an Intel x86 processor.
This version of gcc is known to generate incorrect code for the
Objective Caml runtime system on some Intel x86 machines. (The symptom
is a crash of boot/ocamlc when compiling stdlib/pervasives.mli.)
In particular, the version of gcc 2.7.2.1 that comes with
Linux RedHat 4.x / Intel is affected by this problem.
Other Linux distributions might also be affected.
If you are using one of these configurations, you are strongly advised
to use another version of gcc, such as 2.7.2.3 or 2.95, which are
known to work well with Objective Caml.

Press <enter> to proceed or <interrupt> to stop.
EOF
        read reply;;
    esac;;
esac

# Configure the bytecode compiler

bytecc="$cc"
bytecccompopts=""
bytecclinkopts=""
ostype="Unix"
exe=""

case "$bytecc,$host" in
  cc,*-*-nextstep*)
    # GNU C extensions disabled, but __GNUC__ still defined!
    bytecccompopts="-fno-defer-pop $gcc_warnings -U__GNUC__ -posix"
    bytecclinkopts="-posix";;
  *,*-*-rhapsody*)
    # Almost the same as NeXTStep
    bytecccompopts="-fno-defer-pop $gcc_warnings -DSHRINKED_GNUC"
    mathlib="";;
  *,*-*-darwin*)
    # Almost the same as rhapsody
    bytecccompopts="-fno-defer-pop $gcc_warnings"
    mathlib="";;
  *,*-*-beos*)
    bytecccompopts="-fno-defer-pop $gcc_warnings"
    # No -lm library
    mathlib="";;
  gcc,alpha*-*-osf*)
    bytecccompopts="-fno-defer-pop $gcc_warnings"
    if cc="$bytecc" sh ./hasgot -mieee; then
      bytecccompopts="-mieee $bytecccompopts";
    fi
    # Put code and static data in lower 4GB
    bytecclinkopts="-Wl,-T,12000000 -Wl,-D,14000000"
    # Tell gcc that we can use 32-bit code addresses for threaded code
    echo "#define ARCH_CODE32" >> m.h;;
  cc,alpha*-*-osf*)
    bytecccompopts="-std1 -ieee";;
  gcc,alpha*-*-linux*)
    if cc="$bytecc" sh ./hasgot -mieee; then
      bytecccompopts="-mieee $bytecccompopts";
    fi;;
  cc,mips-*-irix6*)
    # Add -n32 flag to ensure compatibility with native-code compiler
    bytecccompopts="-n32"
    # Turn off warning "unused library"
    bytecclinkopts="-n32 -Wl,-woff,84";;
  cc*,mips-*-irix6*)
    # (For those who want to force "cc -64")
    # Turn off warning "unused library"
    bytecclinkopts="-Wl,-woff,84";;
  *,alpha*-*-unicos*)
    # For the Cray T3E
    bytecccompopts="-DUMK";;
  gcc*,powerpc-*-aix4.3*)
    # Avoid name-space pollution by requiring Unix98-conformant includes
    bytecccompopts="-fno-defer-pop $gcc_warnings -D_XOPEN_SOURCE=500";;
  *,powerpc-*-aix4.3*)
    bytecccompopts="-D_XOPEN_SOURCE=500";;
  gcc*,*-*-cygwin*)
    bytecccompopts="-fno-defer-pop $gcc_warnings -U_WIN32"
    exe=".exe"
    ostype="Cygwin";;
  gcc*)
    bytecccompopts="-fno-defer-pop $gcc_warnings";;
esac

# Configure compiler to use in further tests

cc="$bytecc -O $bytecclinkopts"
export cc cclibs verbose

# Check C compiler

sh ./runtest ansi.c
case $? in
  0) echo "The C compiler is ANSI-compliant.";;
  1) echo "The C compiler $cc is not ANSI-compliant."
     echo "You need an ANSI C compiler to build Objective Caml."
     exit 2;;
  *) echo "Unable to compile the test program."
     echo "Make sure the C compiler $cc is properly installed."
     exit 2;;
esac

# Check the sizes of data types

echo "Checking the sizes of integers and pointers..."
set `sh ./runtest sizes.c`
case "$2,$3" in
  4,4) echo "OK, this is a regular 32 bit architecture."
       echo "#undef ARCH_SIXTYFOUR" >> m.h;;
  8,8) echo "Wow! A 64 bit architecture!"
       echo "#define ARCH_SIXTYFOUR" >> m.h;;
  *,8) echo "Wow! A 64 bit architecture!"
       echo "Unfortunately, Objective Caml cannot work in the case"
       echo "sizeof(long) != sizeof(long *)."
       echo "Objective Caml won't run on this architecture."
       exit 2;;
  *,*) echo "This architecture seems to be neither 32 bits nor 64 bits."
       echo "Objective Caml won't run on this architecture."
       exit 2;;
    *) echo "Unable to compile the test program."
       echo "Make sure the C compiler $cc is properly installed."
       exit 2;;
esac
if test $1 != 4 && test $2 != 4 && test $4 != 4; then
  echo "Sorry, we can't find a 32-bit integer type"
  echo "(sizeof(short) = $4, sizeof(int) = $1, sizeof(long) = $2)"
  echo "Objective Caml won't run on this architecture."
  exit 2
fi

echo "#define SIZEOF_INT $1" >> m.h
echo "#define SIZEOF_LONG $2" >> m.h
echo "#define SIZEOF_SHORT $4" >> m.h

if test $2 = 8; then
     echo "#define ARCH_INT64_TYPE long" >> m.h
     echo "#define ARCH_UINT64_TYPE unsigned long" >> m.h
     echo '#define ARCH_INT64_PRINTF_FORMAT "l"' >> m.h
     int64_supported=true
else
  sh ./runtest longlong.c
  case $? in
  0) echo "64-bit \"long long\" integer type found (printf with \"%ll\")."
     echo "#define ARCH_INT64_TYPE long long" >> m.h
     echo "#define ARCH_UINT64_TYPE unsigned long long" >> m.h
     echo '#define ARCH_INT64_PRINTF_FORMAT "ll"' >> m.h
     int64_supported=true;;
  1) echo "64-bit \"long long\" integer type found (printf with \"%q\")."
     echo "#define ARCH_INT64_TYPE long long" >> m.h
     echo "#define ARCH_UINT64_TYPE unsigned long long" >> m.h
     echo '#define ARCH_INT64_PRINTF_FORMAT "q"' >> m.h
     int64_supported=true;;
  *) echo "No suitable 64-bit integer type found, Int64.t will not be supported."
     int64_supported=false;;
  esac
fi

# Determine endianness

sh ./runtest endian.c
case $? in
  0) echo "This is a big-endian architecture."
     echo "#define ARCH_BIG_ENDIAN" >> m.h;;
  1) echo "This is a little-endian architecture."
     echo "#undef ARCH_BIG_ENDIAN" >> m.h;;
  2) echo "This architecture seems to be neither big endian nor little endian."
     echo "Objective Caml won't run on this architecture."
     exit 2;;
  *) echo "Something went wrong during endianness determination."
     echo "You'll have to figure out endianness yourself"
     echo "(option ARCH_BIG_ENDIAN in m.h).";;
esac

# Determine alignment constraints

sh ./runtest dblalign.c
case $? in
  0) echo "Doubles can be word-aligned."
     echo "#undef ARCH_ALIGN_DOUBLE" >> m.h;;
  1) echo "Doubles must be doubleword-aligned."
     echo "#define ARCH_ALIGN_DOUBLE" >> m.h;;
  *) echo "Something went wrong during alignment determination for doubles."
     echo "I'm going to assume this architecture has alignment constraints over doubles."
     echo "That's a safe bet: Objective Caml will work even if"
     echo "this architecture has actually no alignment constraints."
     echo "#define ARCH_ALIGN_DOUBLE" >> m.h;;
esac

if $int64_supported; then
  sh ./runtest int64align.c
  case $? in
    0) echo "64-bit integers can be word-aligned."
       echo "#undef ARCH_ALIGN_INT64" >> m.h;;
    1) echo "64-bit integers must be doubleword-aligned."
       echo "#define ARCH_ALIGN_INT64" >> m.h;;
    *) echo "Something went wrong during alignment determination for 64-bit integers."
       echo "I'm going to assume this architecture has alignment constraints."
       echo "That's a safe bet: Objective Caml will work even if"
       echo "this architecture has actually no alignment constraints."
       echo "#define ARCH_ALIGN_INT64" >> m.h;;
  esac
fi

# Check semantics of division and modulus

sh ./runtest divmod.c
case $? in
  0) echo "Native division and modulus have round-towards-zero semantics, will use them."
     echo "#undef NONSTANDARD_DIV_MOD" >> m.h;;
  1) echo "Native division and modulus do not have round-towards-zero semantics, will use software emulation."
     echo "#define NONSTANDARD_DIV_MOD" >> m.h;;
  *) echo "Something went wrong while checking native division and modulus, please report it."
     echo "#define NONSTANDARD_DIV_MOD" >> m.h;;
esac

# Shared library support

shared_libraries_supported=false
sharedcccompopts=''
mksharedlib=''
byteccrpath=''
mksharedlibrpath=''

if test $withsharedlibs = "yes"; then
  case "$host" in
    *-*-linux-gnu|*-*-linux|*-*-freebsd[3-9]*)
      sharedcccompopts="-fPIC"
      mksharedlib="gcc -shared -o"
      bytecclinkopts="$bytecclinkopts -Wl,-E"
      byteccrpath="-Wl,-rpath,"
      mksharedlibrpath="-Wl,-rpath,"
      shared_libraries_supported=true;;
    alpha*-*-osf*)
      case "$bytecc" in
        cc*) sharedcccompopts="";;
        gcc*) sharedcccompopts="-fPIC";;
      esac
      mksharedlib="ld -shared -expect_unresolved '*' -o"
      byteccrpath="-Wl,-rpath,"
      mksharedlibrpath="-rpath "
      shared_libraries_supported=true;;
    *-*-solaris2*)
      case "$bytecc" in
        gcc*)
          sharedcccompopts="-fPIC"
          if sh ./solaris-ld; then
            mksharedlib="$bytecc -shared -o"
            byteccrpath="-Wl,-R"
            mksharedlibrpath="-R"
          else
            mksharedlib="$bytecc -shared -o"
            bytecclinkopts="$bytecclinkopts -Wl,-E"
            byteccrpath="-Wl,-rpath,"
            mksharedlibrpath="-rpath "
          fi
	  shared_libraries_supported=true;;
        *)
          sharedcccompopts="-KPIC"
          byteccrpath="-Wl,-R"
	  mksharedlib="/usr/ccs/bin/ld -G -o"
	  shared_libraries_supported=true;;
      esac;;
    mips-sgi-irix[56]*)
      case "$bytecc" in
        cc*) sharedcccompopts="";;
        gcc*) sharedcccompopts="-fPIC";;
      esac
      mksharedlib="ld -shared -rdata_shared -o"
      byteccrpath="-Wl,-rpath,"
      mksharedlibrpath="-rpath "
      shared_libraries_supported=true;;
  esac
fi

if $shared_libraries_supported; then
  echo "Dynamic loading of shared libraries is supported."
  echo "#define SUPPORT_DYNAMIC_LINKING" >> s.h
fi

# Configure the native-code compiler

arch=none
model=default
system=unknown

case "$host" in
  alpha*-*-osf*)                arch=alpha; system=digital;;
  alpha*-*-linux*)              arch=alpha; system=linux;;
  alpha*-*-freebsd*)            arch=alpha; system=freebsd;;
  alpha*-*-netbsd*)             arch=alpha; system=netbsd;;
  alpha*-*-openbsd*)            arch=alpha; system=openbsd;;
  sparc-*-sunos4.*)             arch=sparc; system=sunos;;
  sparc-*-solaris2.*)           arch=sparc; system=solaris;;
  sparc-*-*bsd*)                arch=sparc; system=bsd;;
  sparc-*-linux*)               arch=sparc; system=linux;;
  i[3456]86-*-linux*)           arch=i386; system=linux_`sh ./runtest elf.c`;;
  i[3456]86-*-*bsd*)            arch=i386; system=bsd_`sh ./runtest elf.c`;;
  i[3456]86-*-nextstep*)        arch=i386; system=nextstep;;
  i[3456]86-*-solaris*)         arch=i386; system=solaris;;
  i[3456]86-*-beos*)            arch=i386; system=beos;;
  i[3456]86-*-cygwin*)          arch=i386; system=cygwin;;
  mips-*-irix6*)                arch=mips; system=irix;;
  hppa1.1-*-hpux*)              arch=hppa; system=hpux;;
  hppa1.1-*-nextstep*)          arch=hppa; system=nextstep;;
  rs6000-*-aix*)                arch=power; model=rs6000; system=aix;;
  powerpc-*-aix*)               arch=power; model=ppc; system=aix;;
  powerpc-*-linux*)             arch=power; model=ppc; system=elf;;
  powerpc-*-rhapsody*)          arch=power; model=ppc; system=rhapsody;;
  powerpc-*-darwin*)            arch=power; model=ppc; system=rhapsody;;
  arm*-*-linux*)                arch=arm; system=linux;;
  ia64-*-linux*)                arch=ia64; system=linux;;
esac

if test -z "$ccoption"; then
  case "$arch,$system,$cc" in
    alpha,digital,gcc*) nativecc=cc;;
    mips,*,gcc*) nativecc=cc;;
    *) nativecc="$bytecc";;
  esac
else
  nativecc="$ccoption"
fi

nativecccompopts=''
nativecclinkopts=''
nativeccrpath="$byteccrpath"

case "$arch,$nativecc,$system,$host_type" in
  alpha,cc*,digital,*) nativecccompopts=-std1;;
  mips,cc*,irix,*)     nativecccompopts=-n32
                       nativecclinkopts="-n32 -Wl,-woff,84";;
  power,gcc*,aix,*aix4.3*)
                       nativecccompopts="$gcc_warnings -D_XOPEN_SOURCE=500";;
  power,*,aix,*aix4.3*)
                       nativecccompopts="-D_XOPEN_SOURCE=500";;
  *,*,nextstep,*)      nativecccompopts="$gcc_warnings -U__GNUC__ -posix"
                       nativecclinkopts="-posix";;
# SHRINKED_GNUC is not necessary for MacOS 10.1 (don't know about 10.0)
# (but it doesn't matter much for native code anyway)
  *,*,rhapsody,*)      nativecccompopts="$gcc_warnings -DSHRINKED_GNUC";;
#  *,*,rhapsody,*)      nativecccompopts="$gcc_warnings";;
  *,gcc*,cygwin,*)     nativecccompopts="$gcc_warnings -U_WIN32";;
  *,gcc*,*,*)          nativecccompopts="$gcc_warnings";;
esac

asflags=''
aspp='$(AS)'
asppflags=''
asppprofflags='-DPROFILING'

case "$arch,$model,$system" in
  alpha,*,digital)  asflags='-O2'; asppflags='-O2 -DSYS_$(SYSTEM)';
                    asppprofflags='-pg -DPROFILING';;
  alpha,*,linux)    aspp='gcc'; asppflags='-c -DSYS_$(SYSTEM)';;
  alpha,*,freebsd)  aspp='gcc'; asppflags='-c -DSYS_$(SYSTEM)';;
  alpha,*,netbsd)   aspp='gcc'; asppflags='-c -DSYS_$(SYSTEM)';;
  alpha,*,openbsd)  aspp='gcc'; asppflags='-c -DSYS_$(SYSTEM)';;
  mips,*,irix)      asflags='-n32 -O2'; asppflags="$asflags";;
  sparc,*,bsd)      aspp='gcc'; asppflags='-c -DSYS_$(SYSTEM)';;
  sparc,*,linux)    aspp='gcc'; asppflags='-c -DSYS_$(SYSTEM)';;
  sparc,*,*)        case "$cc" in
                      gcc*) aspp='gcc'; asppflags='-c -DSYS_$(SYSTEM)';;
                         *) asppflags='-P -DSYS_$(SYSTEM)';;
                    esac;;
  i386,*,solaris)   # case "$cc" in
                    # Intel/solaris assembler doesn't like GNU cpp output
                    # gcc*) aspp='gcc'; asppflags='-c -DSYS_$(SYSTEM)';;
                    aspp='/usr/ccs/bin/as'; asppflags='-P -DSYS_$(SYSTEM)';;
  i386,*,*)         aspp='gcc'; asppflags='-c -DSYS_$(SYSTEM)';;
  hppa,*,*)         aspp="$cc"; asppflags='-traditional -c -DSYS_$(SYSTEM)';;
  power,rs6000,aix) asflags='-u -m pwr -w'; asppflags="$asflags";;
  power,ppc,aix)    asflags='-u -m ppc -w'; asppflags="$asflags";;
  power,*,elf)      aspp='gcc'; asppflags='-c';;
  power,*,rhapsody) ;;
  arm,*,linux)      aspp='gcc'; asppflags='-c -DSYS_$(SYSTEM)';;
  ia64,*,linux)     asflags=-xexplicit
                    aspp='gcc'; asppflags='-c -DSYS_$(SYSTEM) -Wa,-xexplicit';;
esac

case "$arch,$model,$system" in
  alpha,*,digital) profiling='prof';;
  i386,*,linux_elf) profiling='prof';;
  i386,*,bsd_elf) profiling='prof';;
  *) profiling='noprof';;
esac

# Where is ranlib?

if sh ./searchpath ranlib; then
  echo "ranlib found"
  echo "RANLIB=ranlib" >> Makefile
  echo "RANLIBCMD=ranlib" >> Makefile
else
  echo "ranlib not used"
  echo "RANLIB=ar rs" >> Makefile
  echo "RANLIBCMD=" >> Makefile
fi

# Do #! scripts work?

if (SHELL=/bin/sh; export SHELL; ./sharpbang > /dev/null); then
  echo "#! appears to work in shell scripts"
  case "$host" in
    *-*-sunos*|*-*-unicos*)
      echo "We won't use it, though, because under SunOS and Unicos it breaks"
      echo "on pathnames longer than 30 characters"
      echo "SHARPBANGSCRIPTS=false" >> Makefile;;
    *-*-cygwin*)
      echo "We won't use it, though, because of conflicts with .exe extension"
      echo "under Cygwin"
      echo "SHARPBANGSCRIPTS=false" >> Makefile;;
    *)
      echo "SHARPBANGSCRIPTS=true" >> Makefile;;
  esac
else
  echo "No support for #! in shell scripts"
  echo "SHARPBANGSCRIPTS=false" >> Makefile
fi

# Write the OS type (Unix or Cygwin)

echo "#define OCAML_OS_TYPE \"$ostype\"" >> s.h
echo "#define OCAML_STDLIB_DIR \"$libdir\"" >> s.h

# Check the semantics of signal handlers

if sh ./hasgot sigaction sigprocmask; then
  echo "POSIX signal handling found."
  echo "#define POSIX_SIGNALS" >> s.h
else
  if sh ./runtest signals.c; then
    echo "Signals have the BSD semantics."
    echo "#define BSD_SIGNALS" >> s.h
  else
    echo "Signals have the System V semantics."
  fi
  if sh ./hasgot sigsetmask; then
    echo "sigsetmask() found"
    echo "#define HAS_SIGSETMASK" >> s.h
  fi
fi

# For the sys module

if sh ./hasgot strerror; then
  echo "strerror() found."
  echo "#define HAS_STRERROR" >> s.h
fi

if sh ./hasgot times; then
  echo "times() found."
  echo "#define HAS_TIMES" >> s.h
fi

# For the terminfo module

if test "$withcurses" = "yes"; then
  for libs in "" "-lcurses" "-ltermcap" "-lcurses -ltermcap" "-lncurses"; do
    if sh ./hasgot $libs tgetent tgetstr tgetnum tputs; then
      echo "termcap functions found (with libraries '$libs')"
      echo "#define HAS_TERMCAP" >> s.h
      curseslibs="${libs}"
      break
    fi
  done
fi

# Configuration for the libraries

otherlibraries="unix str num dynlink bigarray"

# For the Unix library

has_sockets=no
if sh ./hasgot socket socketpair bind listen accept connect; then
  echo "You have BSD sockets."
  echo "#define HAS_SOCKETS" >> s.h
  has_sockets=yes
elif sh ./hasgot -lnsl -lsocket socket socketpair bind listen accept connect; then
  echo "You have BSD sockets (with libraries '-lnsl -lsocket')"
  cclibs="$cclibs -lnsl -lsocket"
  echo "#define HAS_SOCKETS" >> s.h
  has_sockets=yes
fi

if sh ./hasgot -i sys/socket.h -t socklen_t; then
  echo "socklen_t is defined in <sys/socket.h>"
  echo "#define HAS_SOCKLEN_T" >> s.h
fi

if sh ./hasgot inet_aton; then
  echo "inet_aton() found."
  echo "#define HAS_INET_ATON" >> s.h
fi

if sh ./hasgot -i unistd.h; then
  echo "unistd.h found."
  echo "#define HAS_UNISTD" >> s.h
fi

if sh ./hasgot -i sys/types.h -i dirent.h; then
  echo "dirent.h found."
  echo "#define HAS_DIRENT" >> s.h
fi

if sh ./hasgot rewinddir; then
  echo "rewinddir() found."
  echo "#define HAS_REWINDDIR" >> s.h
fi

if sh ./hasgot lockf; then
  echo "lockf() found."
  echo "#define HAS_LOCKF" >> s.h
fi

if sh ./hasgot mkfifo; then
  echo "mkfifo() found."
  echo "#define HAS_MKFIFO" >> s.h
fi

if sh ./hasgot getcwd; then
  echo "getcwd() found."
  echo "#define HAS_GETCWD" >> s.h
fi

if sh ./hasgot getwd; then
  echo "getwd() found."
  echo "#define HAS_GETWD" >> s.h
fi

if sh ./hasgot getpriority setpriority; then
  echo "getpriority() found."
  echo "#define HAS_GETPRIORITY" >> s.h
fi

if sh ./hasgot -i sys/types.h -i utime.h && sh ./hasgot utime; then
  echo "utime() found."
  echo "#define HAS_UTIME" >> s.h
fi

if sh ./hasgot utimes; then
  echo "utimes() found."
  echo "#define HAS_UTIMES" >> s.h
fi

if sh ./hasgot dup2; then
  echo "dup2() found."
  echo "#define HAS_DUP2" >> s.h
fi

if sh ./hasgot fchmod fchown; then
  echo "fchmod() found."
  echo "#define HAS_FCHMOD" >> s.h
fi

if sh ./hasgot truncate ftruncate; then
  echo "truncate() found."
  echo "#define HAS_TRUNCATE" >> s.h
fi

select_include=''
if sh ./hasgot -i sys/types.h -i sys/select.h; then
  echo "sys/select.h found."
  echo "#define HAS_SYS_SELECT_H" >> s.h
  select_include='-i sys/select.h'
fi

has_select=no
if sh ./hasgot select && \
   sh ./hasgot -i sys/types.h $select_include -t fd_set ; then
  echo "select() found."
  echo "#define HAS_SELECT" >> s.h
  has_select=yes
fi

if sh ./hasgot symlink readlink lstat;  then
  echo "symlink() found."
  echo "#define HAS_SYMLINK" >> s.h
fi

has_wait=no
if sh ./hasgot waitpid;  then
  echo "waitpid() found."
  echo "#define HAS_WAITPID" >> s.h
  has_wait=yes
fi

if sh ./hasgot wait4;  then
  echo "wait4() found."
  echo "#define HAS_WAIT4" >> s.h
  has_wait=yes
fi

if sh ./hasgot -i limits.h && sh ./runtest getgroups.c; then
  echo "getgroups() found."
  echo "#define HAS_GETGROUPS" >> s.h
fi

if sh ./hasgot -i termios.h &&
   sh ./hasgot tcgetattr tcsetattr tcsendbreak tcflush tcflow; then
  echo "POSIX termios found."
  echo "#define HAS_TERMIOS" >> s.h
fi

# Async I/O under OSF1 3.x are so buggy that the test program hangs...
testasyncio=true
if test -f /usr/bin/uname; then
  case "`/usr/bin/uname -s -r`" in
    "OSF1 V3."*) testasyncio=false;;
  esac
fi
if $testasyncio && sh ./runtest async_io.c; then
  echo "Asynchronous I/O are supported."
  echo "#define HAS_ASYNC_IO" >> s.h
fi

has_setitimer=no
if sh ./hasgot setitimer; then
  echo "setitimer() found."
  echo "#define HAS_SETITIMER" >> s.h
  has_setitimer="yes"
fi

if sh ./hasgot gethostname; then
  echo "gethostname() found."
  echo "#define HAS_GETHOSTNAME" >> s.h
fi

if sh ./hasgot -i sys/utsname.h && sh ./hasgot uname; then
  echo "uname() found."
  echo "#define HAS_UNAME" >> s.h
fi

has_gettimeofday=no
if sh ./hasgot gettimeofday; then
  echo "gettimeofday() found."
  echo "#define HAS_GETTIMEOFDAY" >> s.h
  has_gettimeofday="yes"
fi

if sh ./hasgot mktime; then
  echo "mktime() found."
  echo "#define HAS_MKTIME" >> s.h
fi

case "$host" in
  *-*-cygwin*) ;;  # setsid emulation under Cygwin breaks the debugger
  *) if sh ./hasgot setsid; then
       echo "setsid() found."
       echo "#define HAS_SETSID" >> s.h
     fi;;
esac

if sh ./hasgot putenv; then
  echo "putenv() found."
  echo "#define HAS_PUTENV" >> s.h
fi

if sh ./hasgot -i locale.h && sh ./hasgot setlocale; then
  echo "setlocale() and <locale.h> found."
  echo "#define HAS_LOCALE" >> s.h
fi

if sh ./hasgot -ldl dlopen; then
  echo "dlopen() found."
  dllib=-ldl
fi

if sh ./hasgot -i sys/types.h -i sys/mman.h && sh ./hasgot mmap munmap; then
  echo "mmap() found."
  echo "#define HAS_MMAP" >> s.h
fi

# Determine if the debugger is supported

if test "$has_sockets" = "yes"; then
  echo "Replay debugger supported."
  debugger="ocamldebugger"
else
  echo "No replay debugger (missing system calls)"
  debugger=""
fi

# Determine if system stack overflows can be detected

case "$arch,$model,$system" in
  i386,*,*)
    if ./runtest -DTARGET_$arch -DSYS_$system stackov.c; then
      echo "System stack overflow can be detected."
      echo "#define HAS_STACK_OVERFLOW_DETECTION" >> s.h
    else
      echo "Cannot detect system stack overflow."
    fi;;
  *)
      echo "Cannot detect system stack overflow.";;
esac

# Determine the target architecture for the "num" library

case "$host" in
  mips-*-ultrix*) bignum_arch=mips;;
  alpha-*-osf*) bignum_arch=alpha;;
  i[3456]86-*-linux*)
    case `sh ./runtest elf.c` in
      elf) bignum_arch=x86;;
        *) bignum_arch=C;;
    esac;;
  i[3456]86-*-beos) bignum_arch=x86;;
  i[3456]86-*-*bsd*)
    case `sh ./runtest elf.c` in
      elf) bignum_arch=x86;;
        *) bignum_arch=C;;
    esac;;
  sparc-*-sunos*) bignum_arch=supersparc;;
  sparc-*-solaris*) bignum_arch=supersparc-solaris;;
  sparc-*-*bsd*) bignum_arch=sparc;;
  m68k-*-sunos*) bignum_arch=68K;;
  *) bignum_arch=C
esac

# Some systems (e.g. Solaris) don't have an assembler!
if sh ./searchpath as; then :; else bignum_arch=C; fi

echo "BIGNUM_ARCH=$bignum_arch" >> Makefile

# Determine if the threads library is supported

if test "$posix_threads" = "yes"; then
  echo "Threads library supported (using POSIX system threads)."
  otherlibraries="$otherlibraries systhreads"
  bytecccompopts="$bytecccompopts -D_REENTRANT"
  nativecccompopts="$nativecccompopts -D_REENTRANT"
  case "$host" in
    *-*-solaris*)
        pthread_link="-lpthread -lposix4";;
    *-*-freebsd*)
        pthread_link="-pthread"
        bytecccompopts="$bytecccompopts -D_THREAD_SAFE"
        nativecccompopts="$nativecccompopts -D_THREAD_SAFE";;
    *-*-openbsd*)
        pthread_link="-pthread"
        bytecccompopts="$bytecccompopts -pthread"
        asppflags="$asppflags -pthread"
        nativecccompopts="$nativecccompopts -pthread";;
    *)
        pthread_link="-lpthread";;
  esac
  echo "Options for linking with POSIX threads: $pthread_link"
  echo "PTHREAD_LINK=$pthread_link" >> Makefile
elif test "$has_select" = "yes" \
&& test "$has_setitimer" = "yes" \
&& test "$has_gettimeofday" = "yes" \
&& test "$has_wait" = "yes"; then
  echo "Threads library supported (using bytecode-level scheduling)."
  otherlibraries="$otherlibraries threads"
else
  echo "No threads library (missing system calls)"
fi

# Determine the location of X include files and libraries

x11_include="not found"
x11_link="not found"

for dir in \
    $x11_include_dir          \
                              \
    /usr/X11R6/include        \
    /usr/include/X11R6        \
    /usr/local/X11R6/include  \
    /usr/local/include/X11R6  \
    /opt/X11R6/include        \
                              \
    /usr/X11/include          \
    /usr/include/X11          \
    /usr/local/X11/include    \
    /usr/local/include/X11    \
    /opt/X11/include          \
                              \
    /usr/X11R5/include        \
    /usr/include/X11R5        \
    /usr/local/X11R5/include  \
    /usr/local/include/X11R5  \
    /usr/local/x11r5/include  \
    /opt/X11R5/include        \
                              \
    /usr/X11R4/include        \
    /usr/include/X11R4        \
    /usr/local/X11R4/include  \
    /usr/local/include/X11R4  \
                              \
    /usr/X386/include         \
    /usr/x386/include         \
    /usr/XFree86/include/X11  \
                              \
    /usr/include              \
    /usr/local/include        \
    /usr/unsupported/include  \
    /usr/athena/include       \
    /usr/lpp/Xamples/include  \
                              \
    /usr/openwin/include      \
    /usr/openwin/share/include \
    ; \
do
  if test -f $dir/X11/X.h; then
    x11_include=$dir
    break
  fi
done

if test "$x11_include" = "not found"; then
  x11_try_lib_dir=''
else
  x11_try_lib_dir=`echo $x11_include | sed -e 's|include|lib|'`
fi

for dir in \
    $x11_lib_dir          \
    $x11_try_lib_dir      \
                          \
    /usr/X11R6/lib        \
    /usr/lib/X11R6        \
    /usr/local/X11R6/lib  \
    /usr/local/lib/X11R6  \
    /opt/X11R6/lib        \
                          \
    /usr/X11/lib          \
    /usr/lib/X11          \
    /usr/local/X11/lib    \
    /usr/local/lib/X11    \
    /opt/X11/lib          \
                          \
    /usr/X11R5/lib        \
    /usr/lib/X11R5        \
    /usr/local/X11R5/lib  \
    /usr/local/lib/X11R5  \
    /usr/local/x11r5/lib  \
    /opt/X11R5/lib        \
                          \
    /usr/X11R4/lib        \
    /usr/lib/X11R4        \
    /usr/local/X11R4/lib  \
    /usr/local/lib/X11R4  \
                          \
    /usr/X386/lib         \
    /usr/x386/lib         \
    /usr/XFree86/lib/X11  \
                          \
    /usr/lib              \
    /usr/local/lib        \
    /usr/unsupported/lib  \
    /usr/athena/lib       \
    /usr/lpp/Xamples/lib  \
    /lib/usr/lib/X11	  \
                          \
    /usr/openwin/lib      \
    /usr/openwin/share/lib \
    ; \
do
  if test -f $dir/libX11.a || \
     test -f $dir/libX11.so || \
     test -f $dir/libX11.sa; then
    if test $dir = /usr/lib; then
      x11_link="-lX11"
    else
      x11_link="-L$dir -lX11"
      x11_libs="-L$dir"
    fi
    break
  fi
done


if test "$x11_include" = "not found" || test "$x11_link" = "not found"
then
  echo "X11 not found, the \"graph\" library will not be supported."
  x11_include=""
else
  echo "Location of X11 include files: $x11_include/X11"
  echo "Options for linking with X11: $x11_link"
  otherlibraries="$otherlibraries graph"
  if test "$x11_include" = "/usr/include"; then
    x11_include=""
  else
    x11_include="-I$x11_include"
  fi
  echo "X11_INCLUDES=$x11_include" >> Makefile
  echo "X11_LINK=$x11_link" >> Makefile
fi

# See if we can compile the dbm library

dbm_include="not found"
dbm_link="not found"
for dir in /usr/include /usr/include/db1 /usr/include/gdbm; do
  if test -f $dir/ndbm.h; then
    dbm_include=$dir
    if sh ./hasgot dbm_open; then
      dbm_link=""
    elif sh ./hasgot -lndbm dbm_open; then
      dbm_link="-lndbm"
    elif sh ./hasgot -ldb1 dbm_open; then
      dbm_link="-ldb1"
    elif sh ./hasgot -lgdbm dbm_open; then
      dbm_link="-lgdbm"
    else
      dbm_include="not found"
    fi
    break
  fi
done
if test "$dbm_include" = "not found"; then
  echo "NDBM not found, the \"dbm\" library will not be supported."
else
  echo "NDBM found (in $dbm_include)"
  if test "$dbm_include" = "/usr/include"; then
    dbm_include=""
  else
    dbm_include="-I$dbm_include"
  fi
  echo "DBM_INCLUDES=$dbm_include" >> Makefile
  echo "DBM_LINK=$dbm_link" >> Makefile
  otherlibraries="$otherlibraries dbm"
fi

# Look for tcl/tk

echo "Configuring LablTk..."

if test $tk_wanted = no; then
  has_tk=false
elif test "$ostype" = "Cygwin"; then
  has_tk=true
elif test "$x11_include" = "not found" || test "$x11_link" = "not found"; then
  echo "X11 not found."
  has_tk=false
else
  tk_x11_include="$x11_include"
  tk_x11_libs="$x11_libs -lX11"
  has_tk=true
fi

if test $has_tk = true; then
  tcl_version=''
  tcl_version=`sh ./runtest $tk_defs $tk_x11_include tclversion.c`
  if test -z "$tcl_version" && test -z "$tk_defs"; then
    tk_defs=-I/usr/local/include
    tcl_version=`sh ./runtest $tk_defs $tk_x11_include tclversion.c`
  fi
  if test -z "$tcl_version"; then
    tk_defs="-I/usr/include/tcl8.2 -I/usr/include/tk8.2"
    tcl_version=`sh ./runtest $tk_defs $tk_x11_include tclversion.c`
  fi
  if test -z "$tcl_version"; then
    tk_defs="-I/usr/include/tcl8.3 -I/usr/include/tk8.3"
    tcl_version=`sh ./runtest $tk_defs $tk_x11_include tclversion.c`
  fi
  if test -n "$tcl_version"; then
    echo "tcl.h version $tcl_version found."
    case $tcl_version in
    7.5) tclmaj=7 tclmin=5 tkmaj=4 tkmin=1 ;;
    7.6) tclmaj=7 tclmin=6 tkmaj=4 tkmin=2 ;;
    8.0) tclmaj=8 tclmin=0 tkmaj=8 tkmin=0 ;;
    8.1) tclmaj=8 tclmin=1 tkmaj=8 tkmin=1 ;;
    8.2) tclmaj=8 tclmin=2 tkmaj=8 tkmin=2 ;;
    8.3) tclmaj=8 tclmin=3 tkmaj=8 tkmin=3 ;;
    *) echo "This version is not known."; has_tk=false ;;
    esac
  else
    echo "tcl.h not found."
    has_tk=false
  fi
fi

if test $has_tk = true; then
  if sh ./hasgot $tk_x11_include $tk_defs -i tk.h; then
    echo "tk.h found."
  else
    echo "tk.h not found."
    has_tk=false
  fi
fi

tkauxlibs="$mathlib $dllib"
tcllib=''
tklib=''
if test $has_tk = true; then
  if sh ./hasgot $tk_libs $tk_x11_libs $tkauxlibs Tcl_DoOneEvent
  then tk_libs="$tk_libs $dllib"
  elif sh ./hasgot $tk_libs -ltcl$tclmaj.$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj.$tkmin -ltcl$tclmaj.$tclmin $dllib"
  elif sh ./hasgot $tk_libs -ltcl$tclmaj$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin $dllib"
  elif test -z "$tk_libs" && tk_libs=-L/usr/local/lib && \
    sh ./hasgot $tk_libs -ltcl$tclmaj.$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj.$tkmin -ltcl$tclmaj.$tclmin $dllib"
  elif sh ./hasgot $tk_libs -ltcl$tclmaj$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin $dllib"
# elif sh ./hasgot $tk_libs -ltcl $tkauxlibs Tcl_DoOneEvent; then
#   tk_libs="$tk_libs -ltk -ltcl"
  else
    echo "Tcl library not found."
    has_tk=false
  fi
fi
if test $has_tk = true; then
  if sh ./hasgot $tk_libs $tk_x11_libs $tkauxlibs Tk_SetGrid; then
    echo "Tcl/Tk libraries found."
  else
    echo "Tcl library found."
    echo "Tk library not found."
    has_tk=false
  fi
fi

if test $has_tk = true; then
  echo "TK_DEFS=$tk_defs" >> Makefile
  echo "TK_LINK=$tk_libs" >> Makefile
  otherlibraries="$otherlibraries labltk"
else
  echo "Configuration failed, LablTk will not be built."
fi

# Camlp4

(
cd ../../camlp4/config
EXE=$exe ./configure_batch -bindir "$bindir" -libdir "$libdir/camlp4" -mandir "$mandir" -ocaml-top ../.. > /dev/null
)

# Finish generated files

cclibs="$cclibs $mathlib"

echo "BYTECC=$bytecc" >> Makefile
echo "BYTECCCOMPOPTS=$bytecccompopts" >> Makefile
echo "BYTECCLINKOPTS=$bytecclinkopts" >> Makefile
echo "BYTECCLIBS=$cclibs $dllib $curseslibs $pthread_link" >> Makefile
echo "BYTECCRPATH=$byteccrpath" >> Makefile
echo "EXE=$exe" >> Makefile
echo "SUPPORTS_SHARED_LIBRARIES=$shared_libraries_supported" >> Makefile
echo "SHAREDCCCOMPOPTS=$sharedcccompopts" >> Makefile
echo "MKSHAREDLIB=$mksharedlib" >> Makefile
echo "MKSHAREDLIBRPATH=$mksharedlibrpath" >> Makefile
echo "ARCH=$arch" >> Makefile
echo "MODEL=$model" >> Makefile
echo "SYSTEM=$system" >> Makefile
echo "NATIVECC=$nativecc" >> Makefile
echo "NATIVECCCOMPOPTS=$nativecccompopts" >> Makefile
echo "NATIVECCLINKOPTS=$nativecclinkopts" >> Makefile
echo "NATIVECCRPATH=$nativeccrpath" >> Makefile
echo "NATIVECCLIBS=$cclibs" >> Makefile
echo "ASFLAGS=$asflags" >> Makefile
echo "ASPP=$aspp" >> Makefile
echo "ASPPFLAGS=$asppflags" >> Makefile
echo "ASPPPROFFLAGS=$asppprofflags" >> Makefile
echo "PROFILING=$profiling" >> Makefile

echo "DYNLINKOPTS=$dllib" >> Makefile
echo "OTHERLIBRARIES=$otherlibraries" >> Makefile
echo "DEBUGGER=$debugger" >> Makefile

rm -f tst hasgot.c
rm -f ../m.h ../s.h ../Makefile
mv m.h s.h Makefile ..

# Print a summary

echo
echo "** Configuration summary **"
echo
echo "Directories where Objective Caml will be installed:"
echo "        binaries.................. $bindir"
echo "        standard library.......... $libdir"
echo "        manual pages.............. $mandir (with extension .$manext)"

echo "Configuration for the bytecode compiler:"
echo "        C compiler used........... $bytecc"
echo "        options for compiling..... $bytecccompopts"
echo "        options for linking....... $bytecclinkopts $cclibs $dllib $curseslibs $pthread_link"
if $shared_libraries_supported; then
echo "        shared libraries are supported"
echo "        options for compiling..... $sharedcccompopts $bytecccompopts"
echo "        command for building...... $mksharedlib lib.so $mksharedlibrpath/a/path objs"
else
echo "        shared libraries not supported"
fi

echo "Configuration for the native-code compiler:"
if test "$arch" = "none"; then
  echo "        (not supported on this platform)"
else
  if test "$model" = "default"; then
    echo "        hardware architecture..... $arch"
  else
    echo "        hardware architecture..... $arch ($model)"
  fi
  if test "$system" = "unknown"; then : ; else
  echo "        OS variant................ $system"
  fi
  echo "        C compiler used........... $nativecc"
  echo "        options for compiling..... $nativecccompopts"
  echo "        options for linking....... $nativecclinkopts $cclibs"
  echo "        assembler ................ \$(AS) $asflags"
  echo "        preprocessed assembler ... $aspp $asppflags"
  if test "$profiling" = "prof"; then
  echo "        profiling with gprof ..... supported"
  else
  echo "        profiling with gprof ..... not supported"
  fi
fi

if test "$debugger" = "ocamldebugger"; then
  echo "Source-level replay debugger: supported"
else
  echo "Source-level replay debugger: not supported"
fi

echo "Configuration for the external libraries:"
echo "        libraries supported ...... $otherlibraries"

echo "The \"num\" library:"
echo "        target architecture ...... $bignum_arch"

if test "$x11_include" != "not found" && test "$x11_lib" != "not found"; then
echo "The \"graph\" library:"
echo "        options for compiling .... $x11_include"
echo "        options for linking ...... $x11_link"
fi

if test $has_tk = true; then
echo "The \"labltk\" library:"
echo "        use tcl/tk version ....... $tcl_version"
echo "        options for compiling .... $tk_defs"
echo "        options for linking ...... $tk_libs"
else
echo "The \"labltk\" library: not found"
fi
