dnl Copyright (C) 2005-2006 EDF
dnl
dnl This file is part of the FVM software package.  For license
dnl information, see the COPYING file in the top level directory of the
dnl FVM source distribution.

# FVM_AC_TEST_MPI
#----------------
# optional MPI support (use CC=mpicc with configure if necessary)
# modifies or sets fvm_have_mpi, MPI_CPPFLAGS, MPI_LDFLAGS, and MPI_LIBS
# depending on libraries found

AC_DEFUN([FVM_AC_TEST_MPI], [

saved_CPPFLAGS="$CPPFLAGS"
saved_LDFLAGS="$LDFLAGS"
saved_LIBS="$LIBS"

fvm_have_mpi=no

AC_ARG_ENABLE(mpi,
  [  --disable-mpi           Do not use MPI when available],
  [
    case "${enableval}" in
      yes) mpi=true ;;
      no)  mpi=false ;;
      *)   AC_MSG_ERROR([bad value ${enableval} for --enable-mpi]) ;;
    esac
  ],
  [ mpi=true ]
)

AC_ARG_WITH(mpi-prefix,
  [  --with-mpi-prefix=DIR   MPI installed under DIR]
)

if test "x$mpi" = "xtrue" -a "x$with_mpi_prefix" != "x" ; then
  MPI_CPPFLAGS="$MPI_CPPFLAGS -I$with_mpi_prefix/include"
  MPI_LDFLAGS="$MPI_LDFLAGS -L$with_mpi_prefix/lib"
fi

# Just in case, remove excess whitespace from existing flag and libs variables.

if test "$MPI_CPPFLAGS" != "" ; then
  MPI_CPPFLAGS=`echo $MPI_CPPFLAGS | sed 's/^[ ]*//;s/[ ]*$//'`
fi
if test "$MPI_LDFLAGS" != "" ; then
  MPI_LDFLAGS=`echo $MPI_LDFLAGS | sed 's/^[ ]*//;s/[ ]*$//'`
fi
if test "$MPI_LIBS" != "" ; then
  MPI_LIBS=`echo $MPI_LIBS | sed 's/^[ ]*//;s/[ ]*$//'`
fi

# If we use mpicc or a variant, we have nothing to add

if test "x$mpi" = "xtrue" -a "x$CC" != "x" ; then
  CCNAME=`basename "$CC"`
  # Test for standard wrappers
  if test "$CCNAME" = "mpicc" -o "$CCNAME" = "mpiCC" \
                              -o "$CCNAME" = "mpic++" ; then
    fvm_have_mpi=yes
  # Else test for other known wrappers
  elif test "$CCNAME" = "mpcc" -o "$CCNAME" = "mpCC" ; then
    fvm_have_mpi=yes
  fi
fi

# If we do not use an MPI compiler wrapper, we must add compilation
# and link flags; we try to detect the correct flags to add.

if test "x$mpi" = "xtrue" -a "x$fvm_have_mpi" = "xno" \
                          -a "x$with_mpi_prefix" != "xno" ; then

  # try several tests for MPI

  # Basic test
  AC_MSG_CHECKING([for MPI (basic test)])
  if test "$MPI_LIBS" = "" ; then
    MPI_LIBS="-lmpi"
  fi
  CPPFLAGS="$saved_CPPFLAGS $MPI_CPPFLAGS"
  LDFLAGS="$saved_LDFLAGS $MPI_LDFLAGS"
  LIBS="$saved_LIBS $MPI_LIBS"
  AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <mpi.h>]],
                 [[ MPI_Init(0, (void *)0); ]])],
                 [fvm_have_mpi=yes],
                 [fvm_have_mpi=no])
  AC_MSG_RESULT($fvm_have_mpi)

  # If failed, test for mpich
  if test "x$fvm_have_mpi" = "xno"; then
    AC_MSG_CHECKING([for MPI (mpich test)])
    # First try (simplest)
    MPI_LIBS="-lmpich"
    LIBS="$saved_LIBS $MPI_LIBS"
    AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <mpi.h>]],
                   [[ MPI_Init(0, (void *)0); ]])],
                   [fvm_have_mpi=yes],
                   [fvm_have_mpi=no])
    if test "x$fvm_have_mpi" = "xno"; then
      # Second try (with lpmpich)
      MPI_LIBS="-Wl,-lpmpich -Wl,-lmpich -Wl,-lpmpich -Wl,-lmpich"
      LIBS="$saved_LIBS $MPI_LIBS"
      AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <mpi.h>]],
                     [[ MPI_Init(0, (void *)0); ]])],
                     [fvm_have_mpi=yes],
                     [fvm_have_mpi=no])
    fi
    AC_MSG_RESULT($fvm_have_mpi)
  fi

  # If failed, test for lam-mpi
  if test "x$fvm_have_mpi" = "xno"; then
    AC_MSG_CHECKING([for MPI (lam-mpi test)])
    # First try (without MPI-IO)
    MPI_LIBS="-lmpi -llam -lpthread"
    LIBS="$saved_LIBS $MPI_LIBS"
    AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <mpi.h>]],
                   [[ MPI_Init(0, (void *)0); ]])],
                   [fvm_have_mpi=yes],
                   [fvm_have_mpi=no])
    if test "x$fvm_have_mpi" = "xno"; then
      # Second try (with MPI-IO)
      MPI_LIBS="-lmpi -llam -lutil -ldl -lpthread"
      LIBS="$saved_LIBS $MPI_LIBS"
      AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <mpi.h>]],
                     [[ MPI_Init(0, (void *)0); ]])],
                     [fvm_have_mpi=yes],
                     [fvm_have_mpi=no])
    fi
    AC_MSG_RESULT($fvm_have_mpi)
  fi

  LDFLAGS="$saved_LDFLAGS"
  LIBS="$saved_LIBS"

  if test "x$fvm_have_mpi" = "xno"; then
    MPI_CPPFLAGS=""
    MPI_LDFLAGS=""
    MPI_LIBS=""
  fi

fi

CPPFLAGS="$saved_CPPFLAGS"
LDFLAGS="$saved_LDFLAGS"
LIBS="$saved_LIBS"

unset saved_CPPFLAGS
unset saved_LDFLAGS
unset saved_LIBS

AC_SUBST(MPI_CPPFLAGS)
AC_SUBST(MPI_LDFLAGS)
AC_SUBST(MPI_LIBS)

])dnl



syntax highlighted by Code2HTML, v. 0.9.1