AC_PREREQ(2.59)
AC_INIT(configure.in)

dnl
dnl Set the directory that contains support scripts such as install-sh and
dnl config.guess
dnl
AC_CONFIG_AUX_DIR(../../../../../confdb)

dnl
dnl Definitions will be placed in this file rather than in the DEFS variable
dnl
AC_CONFIG_HEADER(include/mpidi_ch3i_shm_conf.h)
AH_TOP([/* -*- Mode: C; c-basic-offset:4 ; -*- */
/*  
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */
#ifndef MPIDI_CH3I_SHM_CONF_H_INCLUDED
#define MPIDI_CH3I_SHM_CONF_H_INCLUDED
])
AH_BOTTOM([#endif])

echo "RUNNING CONFIGURE FOR THE SHARED MEMORY CHANNEL"

dnl
dnl First check that we have a clean build if we are doing a VPATH build
PAC_VPATH_CHECK()

PAC_LOAD_BASE_CACHE

PAC_PROG_MAKE

#
# Note: if we plan to use the sys/ipc.h header, we need to assert that
# we're using sysv or xopen varities of unix
# To check for this, we compile sys/ipc.h (if present) and check for
# a warning about _SVID_SOURCE or _XOPEN_SOURCE.  If either is found,
# we set _XOPEN_SOURCE now, before we do much additional testing
AC_MSG_CHECKING([whether we need to define _XOPEN_SOURCE for sys/ipc.h])
pac_cv_ipc_h_needs_xopen_source=no
rm -f conftest*
echo "#include <sys/ipc.h>" > conftest.c
if $CC -c $CFLAGS $CPPFLAGS conftest.c 2>conftest.er1 1>/dev/null ; then
    if test -s conftest.er1 && \
        grep _XOPEN_SOURCE conftest.er1 >/dev/null 2>&1 ; then
        if $CC -c $CFLAGS $CPPFLAGS -D_XOPEN_SOURCE=500 conftest.c \
	             2>conftest.er1 1>/dev/null ; then
            pac_cv_ipc_h_needs_xopen_source=yes
        fi
    fi
fi
AC_MSG_RESULT($pac_cv_ipc_h_needs_xopen_source)
if test "$pac_cv_ipc_h_needs_xopen_source" = yes ; then
    AC_DEFINE(_XOPEN_SOURCE,500,[Define to access sys/ipc.h cleanly])
fi
rm -f conftest*

dnl AC_DEFINE(USE_PROCESS_LOCKS,,[Define to use process locks])
dnl AC_DEFINE(HAVE_SHARED_PROCESS_READ,,[Define to use /proc directory for shared process reads])
dnl AC_DEFINE(MPIDI_CH3_CHANNEL_RNDV,,[Define to use rdma channel functions])
dnl AC_DEFINE(USE_RDMA_GET,,[Define to use rdma get mechanisms])

AC_CHECK_FUNCS( sysconf sysctl shmget shmat shmdt shmctl mmap )

save_libs=$LIBS
LIBS=
AC_SEARCH_LIBS( shm_open, rt, have_shm_open=yes, have_shm_open=no)
AC_SEARCH_LIBS( shm_unlink, rt, have_shm_unlink=yes, have_shm_unlink=no)
if test $have_shm_open = "yes" -a $have_shm_unlink = "yes" ; then
    AC_MSG_CHECKING(that shm_open has been implemented)
    AC_TRY_RUN([
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

int main(int argc, char ** argv)
{
   int fd;

   fd = shm_open("/mpich2conf", O_RDWR | O_CREAT, 0666);
   if (fd == -1)
   {
       if (errno == ENOSYS)
       {
           return 1;
       }
   }
   else
   {
       shm_unlink("/mpich2conf");
       close(fd);
   }
   return 0;
}],shm_open_impl=yes,shm_open_impl=no,shm_open_impl=${CROSS_HAVE_SHM_OPEN:-no})
    AC_MSG_RESULT($shm_open_impl)
    if test $shm_open_impl = "yes" ; then
        AC_DEFINE(HAVE_SHM_OPEN,,[Define if shm_open is available])
        AC_DEFINE(HAVE_SHM_UNLINK,,[Define if shm_unlink is available])
    else
        LIBS=
    fi
else
    LIBS=
fi
SHM_LIBS=$LIBS
LIBS=$save_libs


AC_CHECK_HEADERS(				\
    assert.h					\
    errno.h					\
    fcntl.h					\
    stdlib.h					\
    sys/param.h					\
    sys/types.h					\
    unistd.h    \
    pthread.h   \
    sys/ipc.h sys/shm.h sys/param.h \
    sys/socket.h netinet/in.h \
    fcntl.h sys/mman.h \
    sys/ptrace.h \
    sys/sysctl.h \
)

AC_MSG_CHECKING([for x86 mfence instruction])
AC_TRY_RUN([
int main(int argc, char *argv[])
{
    __asm__ __volatile__  ( ".byte 0x0f, 0xae, 0xf0" ::: "memory" );
    exit(0);
}
],
AC_MSG_RESULT(yes); lac_cv_have_gcc_asm_and_x86_mfence=yes,
AC_MSG_RESULT(no); lac_cv_have_gcc_asm_and_x86_mfence=no,
AC_MSG_RESULT(not checking when cross compiling), lac_cv_have_gcc_asm_and_x86_mfence=no)
if test "$lac_cv_have_gcc_asm_and_x86_mfence" = "yes" ; then
    AC_DEFINE(HAVE_GCC_ASM_AND_X86_MFENCE, 1, [Define if using gcc on a x86 system with the mfence instruction])
fi

AC_MSG_CHECKING([for x86 sfence instruction])
AC_TRY_RUN([
int main(int argc, char *argv[])
{
    __asm__ __volatile__  ( ".byte 0x0f, 0xae, 0xf8" ::: "memory" );
    exit(0);
}
],
AC_MSG_RESULT(yes); lac_cv_have_gcc_asm_and_x86_sfence=yes,
AC_MSG_RESULT(no); lac_cv_have_gcc_asm_and_x86_sfence=no,
AC_MSG_RESULT(not checking when cross compiling), lac_cv_have_gcc_asm_and_x86_sfence=no)
if test "$lac_cv_have_gcc_asm_and_x86_sfence" = "yes" ; then
    AC_DEFINE(HAVE_GCC_ASM_AND_X86_SFENCE, 1, [Define if using gcc on a x86 system with the sfence instruction])
fi

AC_MSG_CHECKING([for x86 lfence instruction])
AC_TRY_RUN([
int main(int argc, char *argv[])
{
    __asm__ __volatile__  ( ".byte 0x0f, 0xae, 0xe8" ::: "memory" );
    exit(0);
}
],
AC_MSG_RESULT(yes); lac_cv_have_gcc_asm_and_x86_lfence=yes,
AC_MSG_RESULT(no); lac_cv_have_gcc_asm_and_x86_lfence=no,
AC_MSG_RESULT(not checking when cross compiling), lac_cv_have_gcc_asm_and_x86_lfence=no)
if test "$lac_cv_have_gcc_asm_and_x86_lfence" = "yes" ; then
    AC_DEFINE(HAVE_GCC_ASM_AND_X86_LFENCE, 1, [Define if using gcc on a x86 system with the lfence instruction])
fi

AC_SUBST(device_name)
AC_SUBST(channel_name)

if test -z "$ENABLE_SHLIB" ; then
    CC_SHL="$CC"
    ENABLE_SHLIB=none
fi

AC_SUBST(SHM_LIBS)
AC_SUBST(AR)
AC_SUBST(RANLIB)
AC_SUBST(MPILIBNAME)
AC_SUBST(CC)
AC_SUBST(CFLAGS)
AC_SUBST(CPPFLAGS)
PAC_CC_SUBDIR_SHLIBS
AC_SUBST(master_top_srcdir)
AC_SUBST(master_top_builddir)
dnl
dnl Dependency handling
AC_SUBST(MAKE_DEPEND_C)

dnl
dnl Etags
AC_SUBST(ETAGS)
AC_SUBST(ETAGSADD)
export ETAGS
export ETAGSADD

PAC_UPDATE_BASE_CACHE

AC_OUTPUT(Makefile src/Makefile localdefs)
