#
# $Id: configure.in,v 1.42 1997/12/17 22:28:11 grubba Exp $
#

prefix=$PWD
AC_INIT(Makefile.in)

AC_ARG_WITH(fullwarn, [  --with-fullwarn	enable as many warnings as possible],[],[with_fullwarn=no])

AC_SET_MAKE

AC_PROG_INSTALL

# installbsd seems to be broken on some versions of AIX
#
# /grubba 1997-08-16

case "$ac_cv_path_install" in
  *installbsd*)
    AC_MSG_CHECKING(if $ac_cv_path_install works)
    AC_CACHE_VAL(roxen_cv_install_works,
[
    if $ac_cv_path_install "$srcdir/configure.in" ./conftest >/dev/null 2>&1
    then
      if cmp "$srcdir/configure.in" ./conftest >/dev/null 2>&1; then
        roxen_cv_install_works=yes;
      else
        roxen_cv_install_works=no;
      fi
    else
      roxen_cv_install_works=no;
    fi
    if test -f ./conftest; then
      rm -f ./conftest
    fi
])
    AC_MSG_RESULT($roxen_cv_install_works)
    if test "$roxen_cv_install_works" = "no"; then
      echo "Warning: Falling back to $ac_install_sh"
      INSTALL="$ac_install_sh"
    fi
  ;;
esac

# We want an absolute path to the source-dir.
case "$srcdir" in
    /*)
    ;;
    *)
	oldsrcdir="$srcdir"
	srcdir="`cd \"$srcdir\";pwd`"
	AC_MSG_WARN([ Converted $oldsrcdir to $srcdir,
If this does not work, please use an absolute path to the configure script. ])
    ;;
esac

# Check the arguments

AC_ARG_WITH(gmp,     [  --with(out)-gmp        Support bignums],[],[with_gmp=yes])
AC_ARG_WITH(zlib,     [  --with(out)-zlib       Support gzip compression],[],[with_zlib=yes])
AC_ARG_WITH(gdbm,    [  --with(out)-gdbm       GNU database manager support ],[],[with_gdbm=yes])


# Check compilers and such
#--------------------------

if test ! -z "$roxen_cv_prog_CC" -a ! -z "$CC" -a "$CC" != "$roxen_cv_prog_CC"
then
  AC_MSG_WARN([CC is different than last time Roxen was compiled,
It is probably best to delete ./config.cache before proceeding])
fi

#
# NOTE: These tests must be before AC_PROG_CC
# since AC_PROG_CC sets CFLAGS by itself.
#

if test "x${CFLAGS-}" = "x" ; then
  cflags_is_set=no
else
  cflags_is_set=yes
fi

if test "x${CPPFLAGS-}" = "x" ; then
  cppflags_is_set=no
else
  cppflags_is_set=yes
fi

if test "x${LDFLAGS-}" = "x" ; then
  ldflags_is_set=no
else
  ldflags_is_set=yes
fi

roxen_cv_prog_CC="$CC"

AC_PROG_CC
AC_AIX
AC_MINIX

# Set up smartlink

case $CC in
 *smartlink*) ;;
 *)
   CC="$srcdir/pike/bin/smartlink $CC"
   ac_cv_prog_CC="$CC"
 ;;
esac

# We need some special hacks when running slowaris
AC_PATH_PROG(uname_prog,uname,no)
AC_MSG_CHECKING(operating system)
AC_CACHE_VAL(roxen_cv_sys_os,
[
if test "$uname_prog" != "no"; then
  roxen_cv_sys_os="`uname`"

  case "$roxen_cv_sys_os" in
    SunOS)
      case "`uname -r`" in
        5.*) roxen_cv_sys_os="Solaris";
      esac
    ;;
  esac
else
  roxen_cv_sys_os="Not Solaris"
fi
])
AC_MSG_RESULT($roxen_cv_sys_os)

case "$roxen_cv_sys_os" in
  SCO*)
     case "$CFLAGS" in
       *-belf*) ;;
       *)
	  echo "Adding -belf option to CFLAGS."
          CFLAGS="$CFLAGS -belf"
       ;;
     esac
     case "$LDFLAGS" in
       *-belf*) ;;
       *)
	  echo "Adding -belf option to LDFLAGS."
          LDFLAGS="$LDFLAGS -belf"
       ;;
     esac
  ;;
esac

#############################################################################

# Set some of the compiler flags.

for ac_site_file in $CONFIG_SITE; do
  if test -r "$ac_site_file"; then
    AC_MSG_WARN([You are using a site file to initialize configure, please
note that this can lead to problems if anything in
$ac_site_file is not correct. If Roxen does not compile, or
does not run, please try this to compile Roxen again:
rm ./config.cache ; CONFIG_SITE=x ./configure ; make])
  fi
done

#############################################################################

## Search for some popular places where libraries may be hidden.

#############################################################################

if test $cppflags_is_set = no -a x${C_INCLUDE_PATH-} = x ; then
  for d in /usr/local /usr/gnu /opt/gnu /sw/gnu /home/GNU /sw /usr/freeware
  do
    if test -d "$d/include" ; then
      echo Adding $d/include to the include-path
      CPPFLAGS="${CPPFLAGS} -I$d/include"
    else
      :
    fi
  done
else
  :
fi
if test $ldflags_is_set = no ; then
  for d in /usr/local /usr/gnu /opt/gnu /sw/gnu /home/GNU /sw /usr/freeware
  do
    if test -d "$d/lib" ; then
      echo Adding $d/lib to the library-path
      LDFLAGS="${LDFLAGS} -R$d/lib -L$d/lib"
    else
      :
    fi
  done
else
  :
fi

#############################################################
# Optimization flags...

AC_DEFUN(AC_SYS_COMPILER_FLAG,
[
  AC_MSG_CHECKING($1)
  OLD_CFLAGS="[$]CFLAGS"
  AC_CACHE_VAL(roxen_cv_option_$2,
  [
    CFLAGS="[$]OLD_CFLAGS $1"
    AC_TRY_RUN([int main(){exit(0);}],roxen_cv_option_$2=yes,roxen_cv_option_$2=no)
  ])
  
  CFLAGS="[$]OLD_CFLAGS"
  
  if test x"[$]roxen_cv_option_$2" = "xyes" ; then
    $3="[$]$3 $1"
    AC_MSG_RESULT(yes)
  else
    AC_MSG_RESULT(no)
  fi
])

AC_DEFUN(AC_SYS_CPU_COMPILER_FLAG,
[
 if test "`uname -m 2>/dev/null`" = "$1" ; then
    AC_SYS_COMPILER_FLAG($2,$3,$4)
 fi
])

AC_DEFUN(AC_SYS_OS_COMPILER_FLAG,
[
 if test "x$roxen_cv_sys_os" = "x$1" ; then
    AC_SYS_COMPILER_FLAG($2,$3,$4)
 fi
])

WARN=""
OPTIMIZE=""

if test $cflags_is_set = no; then
 if test "x${GCC-}" = xyes ; then
  WARN="-W -Wall -Wpointer-arith -Wno-unused -Wstrict-prototypes"
  AC_SYS_COMPILER_FLAG(-O2,O2,OPTIMIZE)
  AC_SYS_COMPILER_FLAG(-ffast-math,ffast_math,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(AIX,-mprototype,mprototype,WARN)
### Optimize for different SUN machines. If there is no working 'uname'
### no extra optimization will be done, but nothing should break either.
  AC_SYS_CPU_COMPILER_FLAG(sun4c,-msparclite,sparclite,OPTIMIZE)
  AC_SYS_CPU_COMPILER_FLAG(sun4m,-mv8,microsparc,OPTIMIZE)
  AC_SYS_CPU_COMPILER_FLAG(sun4d,-msupersparc,supersparc,OPTIMIZE)
  AC_SYS_CPU_COMPILER_FLAG(sun4u,-msupersparc,ultrasparc,OPTIMIZE)
 else

### Non-gcc thingies. This should probably be enhanced...

  # Solaris
  AC_SYS_OS_COMPILER_FLAG(Solaris,-dalign,dalign,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(Solaris,-fast,fast,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(Solaris,-native,native,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(Solaris,-xdepend,xdepend,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(Solaris,-fd,fd,WARN)
  AC_SYS_OS_COMPILER_FLAG(Solaris,-xtransition,xtransition,WARN)
  AC_SYS_OS_COMPILER_FLAG(Solaris,-xs,xs,CFLAGS)
  # Causes __STDC__ to be 1 which causes several headerfiles to behave
  # differently.
  # AC_SYS_OS_COMPILER_FLAG(Solaris,-Xc,Xc,WARN)

  # HP-UX
  AC_SYS_OS_COMPILER_FLAG(HP-UX,+Odataprefetch,plus_Odataprefetch,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(HP-UX,+Ofailsafe,plus_Ofailsafe,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(HP-UX,+O3,plus_O3,OPTIMIZE)
  if test x$ac_with_fullwarn = xyes ; then
    # Ignore warnings 530 and 414 if you turn this on.
    AC_SYS_OS_COMPILER_FLAG(HP-UX,+w1,plus_w1,WARN)
  fi

  # OSF/1
  AC_SYS_OS_COMPILER_FLAG(OSF1,-fast,fast,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-g3,g3,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-host,host,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-std1,std1,WARN)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-warnprotos,warnprotos,WARN)

  # IRIX
  # sopt disabled since it takes too much time on interpret.c (>45min on an OČ)
  # AC_SYS_OS_COMPILER_FLAG(IRIX,-sopt,sopt)
  AC_SYS_OS_COMPILER_FLAG(IRIX,-n32,n32,CFLAGS)
  if test x$roxen_cv_option_n32 = xyes ; then
    echo Adding -n32 to LDFLAGS.
    LDFLAGS="$LDFLAGS -n32"
  fi
  AC_SYS_OS_COMPILER_FLAG(IRIX,-g3,g3,OPTIMIZE)
  if test x$ac_with_fullwarn = xyes ; then
    # This warns about a lot of standard constructs, so it isn't enabled
    # by default.
    AC_SYS_OS_COMPILER_FLAG(IRIX,-fullwarn,fullwarn,WARN)
  fi
  if test x$with_debug = xyes ; then
    AC_SYS_OS_COMPILER_FLAG(IRIX,-trapuw,trapuw,WARN)
  fi

dnl
dnl This does not work with Digital UNIX cc/ld (cc thinks it is ok, ld does not)
dnl  
dnl AC_SYS_COMPILER_FLAG(-native,native)
dnl

dnl ### Ok. This is for apcc...
case "$CC" in
  *apcc)
    AC_SYS_COMPILER_FLAG(-XO=5,apcc_opt,OPTIMIZE)
    AC_SYS_COMPILER_FLAG(-Xcallmod=2,apcc_opt_2,OPTIMIZE)
  ;;
esac


  AC_SYS_CPU_COMPILER_FLAG(sun4m,-Xpipe=micro,microsparc,OPTIMIZE)
  AC_SYS_CPU_COMPILER_FLAG(sun4d,-Xpipe=super,supersparc,OPTIMIZE)
  AC_SYS_CPU_COMPILER_FLAG(sun4u,-Xpipe=super,ultrasparc,OPTIMIZE)

  case "${CFLAGS-} $OPTIMIZE" in
     *-O*) ;;
     *+O*) ;;
     *-fast*) ;;
     *)
      AC_SYS_COMPILER_FLAG(-O2,O2,OPTIMIZE)
      if test "x$pike_cv_option_O2" != "xyes"; then
        AC_SYS_COMPILER_FLAG(-O,O,OPTIMIZE)
      fi
     ;;
  esac
 fi
fi

if test "$GCC" = "yes"; then
# Disabled since -pipe causes gcc to hang on some systems
# (gcc-2.7.2 on Slackware Linux 3.2 when compiling assembler files).
# Thanks to Michael Finken <finken@nentec.de> for pointing it out.
#	/grubba 1997-10-07
#   if test $cflags_is_set = no; then
#     AC_SYS_COMPILER_FLAG(-pipe, pipe, CFLAGS)
#   fi
    :
else
#
# Special hack for HP-UX stupid cc
#
  if test "$roxen_cv_sys_os" = "HP-UX"; then
    AC_TRY_COMPILE([int foo(int bar);],[],[],
    [
      OLD_CC="${CC-cc}"
      OLD_CFLAGS="$CFLAGS"
      if test "$CC" = "$BINDIR/smartlink cc" -a -x /usr/bin/cc ; then
        CC="$BINDIR/smartlink /usr/bin/cc"
      fi
      CFLAGS="-Aa -D_HPUX_SOURCE +Olibcalls $CFLAGS"
      AC_TRY_COMPILE([int foo(int bar);],[],[],[
        CC="$OLD_CC"
	CFLAGS="$OLD_CFLAGS"
      ])
    ])
  fi
fi

AC_MSG_CHECKING(LINKFORSHARED)

AC_CACHE_VAL(roxen_cv_sys_linkforshared,[
# LINKFORSHARED are the flags passed to the $(CC) command that links
# the pike executable -- this is only needed for a few systems
if test -z "$LINKFORSHARED"
then
  if test "$GCC" = yes ; then
    case $roxen_cv_sys_os in
	AIX*)	LINKFORSHARED="-Wl,-bexpall -Wl,-brtl";;
	hp*|HP*) LINKFORSHARED="-Wl,-E";;
	Linux*) LINKFORSHARED="-rdynamic";;
        GNU*) LINKFORSHARED="-rdynamic";;
	next) LINKFORSHARED="-u libsys_s";;
	SCO_SV*) LINKFORSHARED="-fpic -Wl,-Bexport";;
    esac
  else
    case $roxen_cv_sys_os in
	AIX*)	LINKFORSHARED="-Wl,-bexpall -Wl,-brtl";;
	hp*|HP*) LINKFORSHARED="-Wl,-E";;
	Linux*) LINKFORSHARED="-rdynamic";;
        GNU*) LINKFORSHARED="-rdynamic";;
	next) LINKFORSHARED="-u libsys_s";;
	SCO_SV*) LINKFORSHARED="-Bdynamic -dy -Wl,-Bexport";;
    esac
  fi
fi


   OLD_CFLAGS="$CFLAGS"
   CFLAGS="$CFLAGS $LINKFORSHARED"
   AC_TRY_RUN([int main() { exit(0); }],
 roxen_cv_sys_linkforshared="$LINKFORSHARED", roxen_cv_sys_linkforshared='')
   CFLAGS="$OLD_CFLAGS"
])

LINKFORSHARED="$roxen_cv_sys_linkforshared"

AC_MSG_RESULT($LINKFORSHARED)
AC_SUBST(LINKFORSHARED)

# Used to pass CFLAGS to Pike
REALCFLAGS="$CFLAGS"
AC_SUBST(REALCFLAGS)

# Pass a joined CFLAGS to the other configure-scripts.
CFLAGS="$CFLAGS $WARN $OPTIMIZE"

export CC CFLAGS REALCFLAGS WARN OPTIMIZE

###################################################################
## Libraries that are used by Pike
###################################################################

roxen_lib_subdirs=""


# GMP -- Gnu Multi-Precision library
#------------------------------------

MAKE_GMP=""
AC_SUBST(MAKE_GMP)
if test -d $srcdir/gmp/.; then
  unset ac_cv_header_gmp_h
  unset ac_cv_lib_gmp_mpz_set_d
  if test x$with_gmp = xyes; then
    AC_CHECK_HEADERS(gmp.h)
    AC_CHECK_LIB(gmp, mpz_set_d)
    if test "$ac_cv_header_gmp_h$ac_cv_lib_gmp_mpz_set_d" = "yesyes"; then
      echo Gmp already installed.
    else
      echo Enabling supplied gmp...
      MAKE_GMP=gmp
      roxen_lib_subdirs="$roxen_lib_subdirs gmp"
      # Fool Pike into believing Gmp is installed.
      # It will be when Pike is being built.
      ac_cv_header_gmp_h=yes
      ac_cv_lib_gmp_mpz_set_d=yes
      PWD=`pwd`
      CPPFLAGS="-I$srcdir/gmp -I$PWD/gmp ${CPPFLAGS}"
      LDFLAGS="-L$PWD/gmp ${LDFLAGS}"
    fi
  fi
fi

# Zlib -- Compression library
#-----------------------------

MAKE_ZLIB=""
AC_SUBST(MAKE_ZLIB)
if test -d $srcdir/zlib/.; then
  # We don't trust the config-cache for these two.
  unset ac_cv_header_zlib_h
  unset ac_cv_lib_z_compress
  if test x$with_zlib = xyes; then
    AC_CHECK_HEADERS(zlib.h)
    if test "$ac_cv_header_zlib_h" = "yes"; then
      echo Zlib already installed.
      AC_MSG_CHECKING([if the installed libz.h is new enough])
      AC_TRY_COMPILE([
#include <zlib.h>
      ],[
int foo = (int)(Z_NO_COMPRESSION | Z_VERSION_ERROR);
      ],[
        AC_MSG_RESULT(yes)
	OLDLIBS="$LIBS"
	if test x$roxen_cv_sys_os = xIRIX ; then
	  # The libz.so supplied with IRIX 6.3 needs this obscure symbol
          # C++?
          AC_CHECK_LIB(Csup, __vtbl__9type_info)
          AC_CHECK_LIB(C, __vtbl__9type_info)
        fi
	AC_CHECK_LIB(z, compress, [], [
          echo Using supplied zlib anyway.
          ac_cv_header_zlib_h=no
        ])
	LIBS="$OLDLIBS"
      ],[
        AC_MSG_RESULT(no)
	echo Using supplied zlib anyway.
        ac_cv_header_zlib_h=no
      ])
    fi
    if test "$ac_cv_header_zlib_h" != "yes"; then
      echo Enabling supplied zlib...
      MAKE_ZLIB=zlib
      roxen_lib_subdirs="$roxen_lib_subdirs zlib"
      # Fool Pike into believing Zlib is installed.
      # It will be when Pike is being built.
      ac_cv_header_zlib_h=yes
      ac_cv_lib_z_compress=yes
      PWD=`pwd`
      CPPFLAGS="-I$srcdir/zlib -I$PWD/zlib ${CPPFLAGS}"
      LDFLAGS="-L$PWD/zlib ${LDFLAGS}"
    fi
  fi
fi

# Gdbm -- Gnu DataBase Manager library
#--------------------------------------

MAKE_GDBM=""
AC_SUBST(MAKE_GDBM)
if test -d $srcdir/gdbm/.; then
  unset ac_cv_header_gdbm_h
  unset ac_cv_lib_gdbm_gdbm_open
  if test x$with_gdbm = xyes; then
    AC_CHECK_HEADERS(gdbm.h)
    AC_CHECK_LIB(gdbm, gdbm_open)
    if test "$ac_cv_header_gdbm_h$ac_cv_lib_gdbm_gdbm_open" = "yesyes"; then
      echo Gdbm already installed.
    else
      echo Enabling supplied gdbm...
      MAKE_GDBM=gdbm
      roxen_lib_subdirs="$roxen_lib_subdirs gdbm"
      # Fool Pike into believing Gdbm is installed.
      # It will be when Pike is being built.
      ac_cv_header_gdbm_h=yes
      ac_cv_lib_gdbm_gdbm_open=yes
      PWD=`pwd`
      CPPFLAGS="-I$srcdir/gdbm -I$PWD/gdbm ${CPPFLAGS}"
      LDFLAGS="-L$PWD/gdbm ${LDFLAGS}"
    fi
  fi
fi

export CPPFLAGS LDFLAGS

AC_CONFIG_SUBDIRS($roxen_lib_subdirs extern pike)
AC_OUTPUT(Makefile, echo foo >stamp-h)

