dnl ------------------------------------------------------------
dnl   $Id: configure.in,v 1.5.2.2 2000/07/02 15:55:08 yamauchi Exp $
dnl 	configure.in for MPI version
dnl	(C) Copyright 1999 By YAMAUCHI Hitoshi 山内斉
dnl   X を仮定しにくくなってきたので，環境に依存する部分を imake から 
dnl   configure へと移行する．山内 1999年10月16日(土)
dnl
dnl  autoheader を用いると，configure.in から config.h.in を作成してく
dnl  れる
dnl
dnl  Reference: mpiJava configure.in (C) 1999 Guansong Zhang, Sung-Hoon Ko 
dnl
dnl ------------------------------------------------------------

dnl ------------------------------------------------------------
dnl Process this file with autoconf to produce a configure script.
dnl ------------------------------------------------------------
AC_INIT(main.cc)

dnl ------------------------------------------------------------
dnl Use config.h for define stuffs
dnl ------------------------------------------------------------
AC_CONFIG_HEADER(config.h)

dnl ------------------------------------------------------------
dnl Checks for programs.
dnl ------------------------------------------------------------
dnl AC_PROG_CC
dnl AC_PROG_CPP
AC_PROG_CXX
AC_PROG_LN_S
AC_PROG_MAKE_SET
dnl AC_PROG_RANLIB
AC_CHECK_PROG(RM, rm, rm)
AC_PATH_PROGS(TESTPROG, test, test)

dnl ------------------------------
dnl If I can use g++, set -Wall option
dnl ------------------------------
AC_MSG_CHECKING([Can we use -Wall flag (g++ or not)])
if test "X"${GXX} = "X""yes" ; then
   WALLFLAG="-Wall"
else
   WALLFLAG=""
fi
AC_MSG_RESULT(${WALLFLAG})
AC_SUBST(WALLFLAG)

dnl ------------------------------------------------------------
dnl Checks for libraries.
dnl ------------------------------------------------------------
dnl AC_CHECK_LIB(m, sqrt)
AC_CHECK_LIB(nsl, gethostbyname)
AC_CHECK_LIB(socket, socket)

dnl ------------------------------------------------------------
dnl Checks for header files.
dnl ------------------------------------------------------------
AC_HEADER_STDC
AC_CHECK_HEADERS(limits.h unistd.h)

dnl ------------------------------------------------------------
dnl Checks for typedefs, structures, and compiler characteristics.
dnl ------------------------------------------------------------
dnl AC_C_CONST
dnl AC_C_INLINE

dnl ------------------------------------------------------------
dnl Checks for library functions.
dnl ------------------------------------------------------------
dnl AC_CHECK_FUNCS(strtod) これで今のところ困ったことはないな．．．
AC_CHECK_FUNCS(snprintf)
AC_CHECK_FUNCS(nanosleep)
AC_CHECK_FUNCS(usleep)

dnl ------------------------------------------------------------
dnl enable Profile Trace
dnl ------------------------------------------------------------
AC_MSG_CHECKING(if --enable-profile option specified)
AC_ARG_ENABLE(profile,
        [  --enable-profile	Build a profiling version.],
        [mpi2_profile_mon="yes"], [mpi2_profile_mon="no"])
if test "$mpi2_profile_mon" = yes; then
        AC_DEFINE(USE_PROFILE_MON)
fi
AC_MSG_RESULT($mpi2_profile_mon)


AC_MSG_CHECKING(if --enable-IBMpmarray option specified)
AC_ARG_ENABLE(IBMpmarray,
        [  --enable-IBMpmarray	Build a using IBM pmarray version.],
        [IBM_pmarray="yes"], [IBM_pmarray="no"])
if test "$IBM_pmarray" = yes; then
        AC_DEFINE(USE_IBM_PMARRAY)
fi
AC_MSG_RESULT($IBM_pmarray)

AC_MSG_CHECKING(if --enable-MPI2pmarray option specified)
AC_ARG_ENABLE(MPI2pmarray,
        [  --enable-MPI2pmarray	Build a using MPI2 pmarray version.],
        [MPI2_pmarray="yes"], [MPI2_pmarray="no"])
if test "$MPI2_pmarray" = yes; then
        AC_DEFINE(USE_MPI2_PMARRAY)
fi
AC_MSG_RESULT($MPI2_pmarray)


dnl ------------------------------------------------------------
dnl Checks for MPI
dnl ------------------------------------------------------------
dnl which : Solaris's /bin/which is broken. (That assume user uses csh.)
dnl  良く考えてみたら，AC_PATH_PROGS を使えば良い
dnl ------------------------------
dnl which_for_sh () {
dnl     FNAME=$1;
dnl     PUSHD_IFS=$IFS;
dnl     IFS=':';
dnl 
dnl     for pathdir in $PATH 
dnl     do
dnl 	if test -x ${pathdir}/${FNAME} -a -f ${pathdir}/${FNAME} ; then
dnl 	    FOUND=${pathdir}/${FNAME};
dnl 	    echo ${FOUND};
dnl 	    break;
dnl 	fi
dnl     done
dnl }

dnl ------------------------------
dnl function to find mpi.h
dnl ------------------------------
find_mpich_include () {
    WHICH_MPIRUN=$1 
    MPI_PREFIX=`echo ${WHICH_MPIRUN} | sed 's%/bin/mpirun%%'`
    # MPI_PREFIX=`which_for_sh mpirun | sed 's%/bin/mpirun%%'`
    if test -f $MPI_PREFIX/include/mpi.h ; then
        # echo "find MPI include at $MPI_PREFIX/include"
	MPI_INCLUDE=$MPI_PREFIX/include
    else
	# MPI_PREFIX=`which_for_sh mpirun | sed 's%/lib/[^/]*/[^/]*/mpirun%%'`
	MPI_PREFIX=`echo ${WHICH_MPIRUN} | sed 's%/lib/[^/]*/[^/]*/mpirun%%'`
	if test -f $MPI_PREFIX/include/mpi.h ; then
	    # echo "find MPI include at $MPI_PREFIX/include"
	    MPI_INCLUDE=$MPI_PREFIX/include
	else
	    echo "Please input the abosolute mpich include path:"
	    read MPI_INCLUDE
	fi
    fi
    for dir in $MPI_PREFIX/build/*/*/include; do
	if test -d $dir ; then
	    MPI_INCLUDE="${MPI_INCLUDE} -I${dir}"
	fi
    done
}

dnl ------------------------------
dnl function to find libmpich.a (mpich 1.1.2)
dnl ------------------------------
find_mpich_lib () {
    WHICH_MPIRUN=$1;
    MPI_PREFIX=`echo ${WHICH_MPIRUN} | sed 's%/bin/mpirun%%'`
    # MPI_PREFIX=`which_for_sh mpirun | sed 's%/bin/mpirun%%'`
    findlibnum=0
    MPI_LIB="-lmpich"
# 
# -lmpich を探してみる．複数あるかもしれない
# 
    for dir in $MPI_PREFIX/build/*/*/lib $MPI_PREFIX/lib ; do 
        if test -f $dir/libmpich.a ; then
	    findlibnum=`expr $findlibnum + 1`
	    MPI_LIB_DIR=$dir
	fi       
    done
#
# 複数存在した場合
#
    if test `expr $findlibnum` -gt 1 ; then
        echo "found $findlibnum mpich libraries."
	echo "Please input the absolute mpich library path which you want"
	echo "to use with $MPI_PREFIX/bin/mpirun:"
	read MPI_LIB_DIR
	return
    fi
#
# 無い場合
#
    if test `expr $findlibnum` -lt 1 ; then
        echo "not found mpi libraries."
	echo "Please input the absolute mpich library directory"
	read MPI_LIB_DIR
	return
    fi
}

AC_MSG_CHECKING(MPI package)
AC_ARG_WITH(MPI, [
--with-MPI=mpich          Use mpich MPI (the defualt setting)
--with-MPI=sunhpc         Use HPC MPI from SUN],
	MPI=${withval}, MPI=mpich )
AC_MSG_RESULT($MPI)

case "$MPI" in   
mpich )
    AC_PATH_PROGS(WHICH_MPIRUN, mpirun)
    echo "which mpirun res = ${WHICH_MPIRUN}"
    if test ! -x ${WHICH_MPIRUN} ; then
	echo "I can not find mpirun on path. Please check PATH.";
	exit 1;
    fi
dnl find the mpich include 
    AC_MSG_CHECKING(MPI include dir)
    find_mpich_include ${WHICH_MPIRUN};
    AC_MSG_RESULT($MPI_INCLUDE)
    AC_SUBST(MPI_INCLUDE)
    AC_SUBST(MPI_PREFIX)
dnl find the mpich library
    AC_MSG_CHECKING(searching MPI lib dir)
    find_mpich_lib ${WHICH_MPIRUN}
    AC_MSG_RESULT($MPI_LIB_DIR)
    AC_SUBST(MPI_LIB_DIR) 
    AC_SUBST(MPI_LIB) 

dnl be consistent with SUNHPC
    MULTITHREADFLAG=""
    AC_SUBST(MULTITHREADFLAG)     	
    ;;

sunhpc )
    AC_CHECK_PROG(MPIRUN, tmrun, tmrun)
    if test X"$MPIRUN" = X"tmrun" ; then
        AC_CHECK_PROGS(CXX, CC, g++)
    else
	echo "I can not find Sun HPC environment nor GNU environment."
	exit
    fi
    AC_PATH_PROGS(WHICH_MPIRUN, tmrun)
    MPI_PREFIX=`echo ${WHICH_MPIRUN} | sed 's%/bin/tmrun%%'`
    MPI_INCLUDE="$MPI_PREFIX/include"
    AC_SUBST(MPI_INCLUDE)

    MPI_LIB_DIR="$MPI_PREFIX/lib -L/opt/SUNWspro/lib"
    AC_SUBST(MPI_LIB_DIR) 

    MPI_LIB="-lmpi"
    AC_SUBST(MPI_LIB)

    if test "X"${CXX} = "X""CC" ; then
	# multithread 関係．multithread しなくても必要．
	# multithread する場合には -lmpi を -lmpi_mt に変更する必要あり
	MULTITHREADFLAG="-mt"
	SYSTEM_LIBRARY=""
    else
	MULTITHREADFLAG="-D_REENTRANT"
	SYSTEM_LIBRARY="-lnsl -lsocket -lC -lm -lw -lthread -lcx -lc"
    fi
    AC_SUBST(MULTITHREADFLAG)
    AC_SUBST(SYSTEM_LIBRARY)
    ;;

* )
    echo "Sorry, this configure does not support $MPI MPI."
    exit 
    ;;
esac

AC_SUBST(MPI)

AC_OUTPUT(Makefile linkformpi.sh dellink.sh)
