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

# Copyright 1998-1999, University of Notre Dame.
# Authors: Brian W. Barrett, Arun F. Rodrigues, Jeffrey M. Squyres,
# 	 and Andrew Lumsdaine
#
# This file is part of XMPI
#
# You should have received a copy of the License Agreement for XMPI 
# along with the software; see the file LICENSE.  If not, contact 
# Office of Research, University of Notre Dame, Notre Dame, IN 46556.
# 
# Permission to modify the code and to distribute modified code is
# granted, provided the text of this NOTICE is retained, a notice that
# the code was modified is included with the above COPYRIGHT NOTICE and
# with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE
# file is distributed with the modified code.
#
# LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
# By way of example, but not limitation, Licensor MAKES NO
# REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
# PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
# OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
# OR OTHER RIGHTS.
#
# Additional copyrights may follow.


dnl Disable caching.
define([AC_CACHE_LOAD], )dnl
define([AC_CACHE_SAVE], )dnl

AC_INIT(./xmpi.1)
AC_CONFIG_AUX_DIR(./config)

dnl Header configuration
AC_CONFIG_HEADER(./xmpi_config.h:./config/xmpi_config.h.in)

AC_ARG_ENABLE(echo, [  --enable-echo           echo configure script commands],
[set -x])

lam_echo () {
    echo $ac_n $1"... $ac_c" 1>&6
}

AC_CANONICAL_HOST
AC_DEFINE_UNQUOTED(XMPI_ARCH,"$host")

case "$prefix" in
  /*) echo installing to directory \"$prefix\" ;;
  NONE) prefix=/usr/local/lam
	echo installing to default location \"$prefix\" ;;
  *) AC_MSG_ERROR(prefix \"$prefix\" must be an absolute directory path) ;;
esac

dnl check for where LAMHOME is
dnl AC_ARG_WITH(lamhome, [  --with-lamhome=LAMHOME  location of LAMHOME])
LAMHOME=$prefix;
AC_SUBST(LAMHOME)

dnl Checks for --with-sgi
AC_ARG_WITH(with_sgi,
    [  --with-sgi                  build with the default SGI libxmpi])

dnl Checks for if you are using a vendor lib, else, sets CC to hcc
dnl hcc is the default compiler
AC_ARG_WITH(vendor_libxmpi, 
    [  --with-vendor-libxmpi=DIR   build with vendor supplied libxmpi in DIR])
if test -n "$with_vendor_libxmpi"; then
    dnl do nothing...yet....
    echo;
elif test -n "$with_sgi"; then
    AC_MSG_WARN(*** You have selected the SGI libxmpi. We will use the )
    AC_MSG_WARN(*** MIPSpro cc compiler.)
    CC=cc;
else
    dnl if you are not using a vendor lib you are using lam libxmpi
    dnl if so, set CC to hcc

    CC=hcc;

fi

dnl check for specification of C compiler
dnl overrules default hcc with lam
AC_ARG_WITH(cc, [  --with-cc=CC            use C compiler CC])
AC_ARG_WITH(lam,[  --with-lam              builds the LAM libxmpi with the hcc compiler])

if test -n "$with_cc"; then
    if test $with_cc = yes; then
	AC_MSG_ERROR(the --with-cc option requires the compiler name)
    fi
    
    dnl warns them for dissing our compiler or the vendor compiler
    if test -z "$with_vendor_libxmpi"; then
	AC_MSG_WARN(*** You are not using the hcc compiler recommended)
	AC_MSG_WARN(*** for building the LAM libxmpi. Be sure to set)
	AC_MSG_WARN(*** the approprate include paths to the needed files)
    fi
    dnl warns them for dissing the MIPS compiler
    if test -z "$with_sgi"; then
	AC_MSG_WARN(*** You are not using the cc compiler recommended)
	AC_MSG_WARN(*** for building with the SGI libxmpi.)
    fi

    CC=$with_cc;
else
    if test -z "$with_vendor_libxmpi" -a -z "$with_sgi" -a -z "$with_lam"; then
	 AC_MSG_WARN(*** You have not specified a vendor library to use)
	 AC_MSG_WARN(*** or a specific C compiler so XMPI will use the)
	 AC_MSG_WARN(*** LAM hcc C compiler.)
    fi
fi

dnl check for specification of C compiler flags
AC_ARG_WITH(cflags, [  --with-cflags=CFLAGS    use C compiler flags CFLAGS])

if test -n "$with_cflags"; then
    CFLAGS=$with_cflags;
fi

lam_cflags_save=$CFLAGS

dnl Check for compilers
AC_PROG_CC
AC_PROG_GCC_TRADITIONAL

if test -z "$lam_cflags_save"; then
    lam_cflags_save="$CFLAGS"
    if test "$GCC" = yes; then
	CFLAGS=-O
    else
	case "$host_os" in
	    solaris) CFLAGS=-fast;;
	    *) CFLAGS=-O;;
	esac
    fi
    LAM_TEST_COMPILE
    if test "$lam_test_compile_ok" = no; then
	CFLAGS="$lam_cflags_save";
    fi
fi

SOLINKFLAGS=
LIBXMPI=
LIBXMPIDIR=
LIBXMPIRPATH=
LIBEXT=
 
AC_ARG_WITH(with_shared,
    [  --with-shared           build the LAM libxmpi as a shared library])

dnl check for use with vendor libxmpi
dnl decides when and how to build the libxmpi
if test -n "$with_vendor_libxmpi"; then
    if test ! -d "$with_vendor_libxmpi"; then
	AC_MSG_ERROR(Vendor libxmpi dir \"$with_vendor_libxmpi\" does not exist)
    else
	LIBXMPIDIR="$with_vendor_libxmpi"
    fi
elif test -n "$with_sgi"; then
    AC_MSG_WARN(*** Using SGI libxmpi.)
    CFLAGS="$CFLAGS -n32"
    LIBXMPIDIR="./libxmpi -L/usr/lib32"
elif test -n "$with_shared"; then
    dnl if you are not using a vendor library, default to LAM libxmpi
    if test -z "$with_lam"; then
	AC_MSG_WARN(*** You have not specified a vendor xmpi library)
	AC_MSG_WARN(*** to use so XMPI will compile the LAM libxmpi)
	AC_MSG_WARN(*** as a shared library.)
    fi
    LIBXMPIDIR=libxmpi
    LIBXMPI=lib-lam

    if test "$GCC" = yes; then
	LIBEXT=so
        SOLINKFLAGS=-shared
        LIBXMPIRPATH='-Wl,-rpath,$LAMHOME/lib'
	case "$host_os" in
	    *hpux*) AC_MSG_ERROR(Dynamic library creation is not supported for HP-UX systems. You cannot use the --with-shared option. );;
	esac
    else
	case "$host_os" in 
	    *solaris*)  LIBEXT=so
			SOLINKFLAGS=-G;;
	    *hpux*) LIBEXT=sl
		    SOLINKFLAGS=-Wl,-b
		    AC_MSG_ERROR(Dynamic library creation is not supported for HP-UX systems. You cannot use the --with-shared option. );;
	    *irix*) LIBEXT=so
		    SOLINKFLAGS=-shared;;
	    *linux*) LIBEXT=so;;
	    *) LAM_SHARED_LIBS_UNSUPPORTED;;
	esac
    fi
else
    dnl default to libxmpi.a - static   
    if test -z "$with_lam"; then
	AC_MSG_WARN(*** You have not specified a vendor xmpi library)
	AC_MSG_WARN(*** to use so XMPI will compile the LAM libxmpi.)
	AC_MSG_WARN(*** as a static library.)
    fi


    LIBXMPIDIR=libxmpi
    LIBXMPI=lib-lam
    LIBEXT=a
fi

dnl override soname and link options
AC_ARG_WITH(with_soname,
    [  --with-soname=SONAME           Shared library major number])
AC_ARG_WITH(with_soversion,
    [  --with-soversion=SOVERSION           Shared library minor number])

SOVERSION=0
SONAME=0
if ! test -z "$with_soversion" ; then
    SOVERSION=$with_soversion;
fi
if ! test -z "$with_soname" ; then
    if test -z "$with_shared" ; then
	AC_MSG_WARN(*** Ignoring soname specified without --with-shared)
    else
	SONAME=$with_soname
	LIBEXT=so.$SONAME.$SOVERSION
	SOLINKFLAGS="-shared -Wl,-soname=libxmpi.so.$SONAME"
	LIBXMPIRPATH=''
    fi
fi
AC_SUBST(SONAME)
AC_SUBST(SOVERSION)


dnl takes care of o32/n32 problem
case "$host_os" in
    *irix*) CFLAGS="$CFLAGS -n32"
	    LIBXMPIDIR="./libxmpi -L/usr/lib32";;
esac

AC_SUBST(LIBXMPI)
AC_SUBST(LIBXMPIDIR)
AC_SUBST(LIBXMPIRPATH)
AC_SUBST(LIBEXT)
AC_SUBST(SOLINKFLAGS)

dnl Take care of some specific compiler options. 
dnl Force use of ANSI C under HP-UX.
if test "$ac_cv_prog_gcc" = no; then
    us=`(uname -s) 2>/dev/null` || us=unknown
    case "$us" in
        HP-UX) CFLAGS="$CFLAGS -Ae" ;;
	*)	;;
    esac
fi

AC_SUBST(CFLAGS)

if test "$with_shared_libxmpi" = yes; then
    LAM_SHARED_OBJ_FLAGS
fi

LAM_ANSI_PROTOS
if test "$lam_ac_ansi_protos" = no; then
    AC_MSG_ERROR(LAM requires a compiler which understands ANSI C prototypes)
fi

dnl How to specify a directory for the library archive search path.
us=`(uname -s) 2>/dev/null` || us=unknown
case "$us" in
    HP-UX) FLIBPATH="-Wl,-L" ;;
	*) FLIBPATH="-L" ;;
esac
AC_SUBST(FLIBPATH)

dnl Check for X11
AC_PATH_XTRA
if test "$no_x" = yes; then
    AC_MSG_ERROR([Could not find X includes or libs.  Cannot continue.])
fi

dnl Look for motif includes and lib
dnl Look where X ones found or where specified by
dnl --with-motif-includes=DIR and --with-motif-libraries=DIR
LAM_MOTIF_INCLUDES
LAM_MOTIF_LIBRARIES
if test "$lam_ac_motif_includes" = no; then
    AC_MSG_ERROR([Could not find Motif includes.  Cannot continue.])
fi
if test "$lam_ac_motif_libraries" = no; then
    AC_MSG_ERROR([Could not find Motif librares.  Cannot continue.])
fi
X_CFLAGS="$X_CFLAGS -I$lam_ac_motif_includes"

if test -n "$with_sgi"; then
    X_LIBS="-L/usr/lib32 -L$lam_ac_motif_libraries"
else
    X_LIBS="$X_LIBS -L$lam_ac_motif_libraries"
fi

dnl Check for programs.
dnl Abort if we cannot find one of them

AC_PROG_CPP
if test "$CPP" = ""; then
    AC_MSG_ERROR([Could not find C preprocessor.  Cannot continue.])
fi

AC_PATH_PROG(RM,rm)
if test "$RM" = ""; then
    AC_MSG_ERROR([Could not find rm.  Cannot continue.])
fi

AC_PROG_RANLIB
if test "$RANLIB" = ""; then
    AC_MSG_ERROR([Could not find ranlib.  Cannot continue.])
fi
 
AC_PATH_PROG(AR,ar)
if test "$AR" = ""; then
    AC_MSG_ERROR([Could not find ar.  Cannot continue.])
fi

AC_PATH_PROG(SHELL_CMD,sh)
if test "$SHELL_CMD" = ""; then
    AC_MSG_ERROR([Could not find Bourne shell.  Cannot continue.])
fi

dnl Used to Check for typedefs, structures, and compiler characteristics.
AC_C_BIGENDIAN

dnl Check for library functions.
AC_CHECK_FUNCS(atexit getcwd)

AC_CACHE_CHECK([if need to include sys/select.h],
lam_cv_need_sys_select_h,
[AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/socket.h>], [fd_set s; FD_ZERO(&s);],
lam_cv_need_sys_select_h=no, lam_cv_need_sys_select_h=yes)])
if test $lam_cv_need_sys_select_h = yes; then
  AC_DEFINE(NEED_SYS_SELECT_H)
fi

LAM_CHECK_TYPE(int4)
LAM_CHECK_TYPE(float8)

AC_CHECK_SIZEOF(short, 2)
AC_CHECK_SIZEOF(int, 4)
AC_CHECK_SIZEOF(long, 4)
AC_CHECK_SIZEOF(float, 4)
AC_CHECK_SIZEOF(double, 4)


dnl Checks for header files
AC_HEADER_STDC
dnl Only check MPI headers if you are not using a vendor lib
if test -z "$with_vendor_lib" -a -z "$with_sgi"; then
    LSC_CHECK_MPI_H
fi

dnl if test "$with_fc" != no; then
dnl     LAM_GET_FORTRAN_EXT_NAME_CONVENTION($FCOMPILER)
dnl fi

AC_OUTPUT(
    ./config.mk:./config/config.mk.in
    ./XMPI
    ./Makefile
    ./libxmpi/Makefile
)
