# -*- shell-script -*-
#
# Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
#                         University Research and Technology
#                         Corporation.  All rights reserved.
# Copyright (c) 2004-2005 The University of Tennessee and The University
#                         of Tennessee Research Foundation.  All rights
#                         reserved.
# Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
#                         University of Stuttgart.  All rights reserved.
# Copyright (c) 2004-2005 The Regents of the University of California.
#                         All rights reserved.
# $COPYRIGHT$
# 
# Additional copyrights may follow
# 
# $HEADER$
#


############################################################################
# Initialization and other random setup/init stuff
############################################################################

# Init autoconf

AC_INIT(@PARAM_INIT_FILE@)
AC_PREREQ(2.57)
AC_CONFIG_AUX_DIR(@PARAM_CONFIG_AUX_DIR@)

#
# Start it up
#

OMPI_CONFIGURE_SETUP
ompi_show_title "Configuring MCA @MCA_TYPE@ @MCA_COMPONENT_NAME@ component"
ompi_show_subtitle "Initialization, setup" 

#
# Init automake
# The third argument to AM_INIT_AUTOMAKE surpresses the PACKAGE and
# VERSION macros
#

AM_INIT_AUTOMAKE(@PARAM_AM_NAME@, irrelevant-version-number, 'no')

#
# GNU C and autotools are inconsistent about whether this is defined
# so let's make it true everywhere for now...
#
AC_GNU_SOURCE

# Setup the top-level MCA component config.h file

AH_TOP([/* -*- c -*-
 *
 * Copyright (c) 2004-2005 The Trustees of Indiana University.
 *                         All rights reserved.
 * Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
 *                         All rights reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * $COPYRIGHT$
 * 
 * Additional copyrights may follow
 * 
 * $HEADER$
 *
 * Open MPI configuation header file.
 * MCA @MCA_TYPE@: @MCA_COMPONENT_NAME@ component
 */

#ifndef @PARAM_VAR_PREFIX@_CONFIG_H
#define @PARAM_VAR_PREFIX@_CONFIG_H
])
AH_BOTTOM([#endif /* _@PARAM_VAR_PREFIX@_CONFIG_H */])


#
# Do the normal basics of setup:
# - set CLEANFILES
# - figure out the host type
# - set the default prefix
#
OMPI_BASIC_SETUP


#
# Check to see if the user wants this @MCA_TYPE@ to be the default
#

AC_MSG_CHECKING([if want the @MCA_COMPONENT_NAME@ @MCA_TYPE@ to be the default])
want_default=0
result=no
AC_ARG_WITH(@PROCESSED_MCA_TYPE@,
    AC_HELP_STRING([--with-@PROCESSED_MCA_TYPE@=name],
		   [if name is "@MCA_COMPONENT_NAME@", the @MCA_COMPONENT_NAME@ @MCA_TYPE@ will be the default]))
if test "$with_@PROCESSED_MCA_TYPE@" = "@MCA_COMPONENT_NAME@"; then
    want_default=1
    result=yes
fi
AC_DEFINE_UNQUOTED(@PARAM_VAR_PREFIX@_DEFAULT, $want_default,
    [Whether the @MCA_COMPONENT_NAME@ @MCA_TYPE@ is the default @PROCESSED_MCA_TYPE@ or not])
AC_MSG_RESULT([$result])

#
# Part one of libtool magic
#

AM_ENABLE_SHARED
AM_DISABLE_STATIC


#
# Find which components should be built as run-time loadable components
# Acceptable combinations:
#
# [default -- no option given]
# --enable-mca-dso
# --enable-mca-dso=[.+,]*COMPONENT_TYPE[.+,]*
# --enable-mca-dso=[.+,]*COMPONENT_TYPE-COMPONENT_NAME[.+,]*
# --disable-mca-dso
#

AC_MSG_CHECKING([if want component to be a DSO])
AC_ARG_ENABLE(mca-dso,
    AC_HELP_STRING([--enable-mca-dso=LIST],
		   [comma-separated list of types and/or type-component pairs that will be built as run-time loadable components (as opposed to statically linked in), if supported on this platform.  The default is to build all components as DSOs; the --disable-mca-dso(=LIST) form can be used to disable building all or some types/components as DSOs.  If LIST is "@MCA_TYPE@-@MCA_COMPONENT_NAME@" or "@MCA_COMPONENT_NAME@", then @MCA_COMPONENT_NAME@ will be compiled as a DSO (if supported on this platform).]))

# Manual conversion of $kind to its generic name (e.g., crmpi->cr,
# crompi->cr).

case "@MCA_TYPE@" in
crmpi)
    generic_type="cr"
    ;;
crompi)
    generic_type="cr"
    ;;
*)
    generic_type="@MCA_TYPE@"
;;
esac

BUILD_@MCA_TYPE@_@MCA_COMPONENT_NAME@_DSO=0
msg=no

if test "$enable_shared" = "no"; then
    msg=no
elif test -z "$enable_mca_dso" -o "$enable_mca_dso" = "yes" -o \
 "$enable_mca_dso" = "@MCA_TYPE@" -o "$enable_mca_dso" = "$generic_type"; then
    BUILD_@MCA_TYPE@_@MCA_COMPONENT_NAME@_DSO=1
    msg=yes
else
    ifs_save="$IFS"
    IFS="${IFS}$PATH_SEPARATOR,"
    for item in $enable_mca_dso; do
	if test "$item" = "@MCA_TYPE@-@MCA_COMPONENT_NAME@" -o \
	    "$item" = "@MCA_TYPE@" -o "$item" = "$generic_type"; then
            BUILD_@MCA_TYPE@_@MCA_COMPONENT_NAME@_DSO=1
	    msg=yes
	fi
    done
    IFS="$ifs_save"
fi
AC_MSG_RESULT([$msg])
unset msg


###########################################################################
# Check for compilers and preprocessors
############################################################################

##################################
# Compiler characteristics
##################################

@C_COMPILER_SETUP@
@CXX_COMPILER_SETUP@

##################################
# Find Open MPI header files
##################################

# Are we building as part of the Open MPI source tree, or outside of it?

AC_ARG_WITH(openmpi, 
    AC_HELP_STRING([--with-openmpi=DIR],
                   [specify the location of the Open MPI header files and library.  The default is to build within a source tree or be able to find the headers/library without additional CPPFLAGS/LDFLAGS.  This option is generally only necessary when components are being configured/built outside of an Open MPI source tree]))

# Establish the top-level OMPI directory.  If --with-openmpi was not
# specified, then see if we're building a) within an Open MPI source
# tree, or b) can find the Open MPI headers and library file without
# any additional CPPFLAGS/LDFLAGS.

top_ompi_srcdir=
top_ompi_builddir=
found_ompi_headers=

# Split this into multiple tests because we may have multiple
# possibilities here:
#
# - build in the Open MPI source tree
# - build outside of the Open MPI source tree, and have valid
#   --with-openmpi
# - build outside of the Open MPI source tree, and not have a valid
#   --with-openmpi

AC_MSG_CHECKING([for Open MPI header files])

# First case: we do not have --with-openmpi, so check and see if we're
# building inside the Open MPI source tree

if test -z "$with_openmpi"; then

    # Are we within an Open MPI source tree?

    if test -f "$srcdir/../../../../config/mca_configure.ac"; then

        # This is needed for VPATH builds, so that it will -I the
        # appropriate include directory (don't know why automake
        # doesn't do this # automatically).

        top_ompi_srcdir='$(top_srcdir)/../../../..'
        top_ompi_builddir='$(top_builddir)/../../../..'
        INCFLAGS='-I$(top_ompi_srcdir) -I$(top_ompi_builddir) -I$(top_ompi_srcdir)/opal/include -I$(top_ompi_builddir)/opal/include -I$(top_ompi_srcdir)/orte/include -I$(top_ompi_builddir)/orte/include -I$(top_ompi_srcdir)/ompi/include -I$(top_ompi_builddir)/ompi/include'" $CPPFLAGS"
        found_ompi_headers=1
        AC_MSG_RESULT([already in Open MPI source tree])
    fi
fi

# If we haven't found the headers yet, then we know that we're *not*
# building within the Open MPI source tree.

if test -z "$found_ompi_headers"; then
    top_ompi_srcdir=""
    top_ompi_builddir="$top_ompi_srcdir"
fi

# Now see if a) we haven't found the headers yet, and b) we have
# --with-openmpi

if test -z "$found_ompi_headers"; then

    # We're outside the Open MPI build tree, and we have been provided
    # a top-level directory where the header files live.

    if test -n "$with_openmpi"; then
        if test -d $with_opemnpi ; then
            if test -d "$with_openmpi/include/openmpi" -a \
                -f "$with_openmpi/include/openmpi/include/ompi_socket_errno.h"; then
                INCFLAGS="-I$with_openmpi/include/openmpi -I$with_openmpi/include"
                found_ompi_headers=1
                AC_MSG_RESULT([$with_openmpi/include/openmpi])
            elif test -d "$with_openmpi/openmpi" -a \
                -f "$with_openmpi/openmpi/include/ompi_socket_errno.h"; then
                INCFLAGS="-I$with_openmpi/openmpi I$with_openmpi/openmpi"
                found_ompi_headers=1
                AC_MSG_RESULT([$with_openmpi/openmpi])
            elif test -f "$with_openmpi/include/ompi_socket_errno.h"; then
                INCFLAGS="-I$with_openmpi -I$with_openmpi/.."
                found_ompi_headers=1
                AC_MSG_RESULT([$with_openmpi])
            else
                AC_MSG_RESULT([got bogus --with-openmpi value])
                AC_MSG_WARN([*** Directory $with_openmpi exists])
                AC_MSG_WARN([*** But cannot seem to find Open MPI headers in it])
                AC_MSG_WARN([*** Looking elsewhere...])
            fi
        else
            AC_MSG_RESULT([got bogus --with-openmpi value])
            AC_MSG_WARN([*** Directory $with_openmpi does not seem to exist])
            AC_MSG_WARN([*** Trying to find Open MPI headers without it...])
        fi
    fi

    # If we've gotten this far and haven't found the Open MPI headers
    # yet, then just try compiling a C program with some of the Open
    # MPI headers and see if they're found (i.e., if they're in the
    # preprocessor's default search path)

    if test -z "$found_ompi_headers"; then
        AC_COMPILE_IFELSE(AC_LANG_PROGRAM([[#include "include/ompi_socket_errno.h"]], 
                          [[]]), 
            [found_ompi_headers=1])
        if test -n "$found_ompi_headers"; then
            AC_MSG_RESULT([default preprocessor path])
        fi            
    fi
fi

# If we got this far and haven't found the Open MPI headers, stick a
# fork in us, 'cause we're done.

if test -z "$found_ompi_headers"; then
    AC_MSG_WARN([*** Could not find the Open MPI headers])
    AC_MSG_ERROR([*** Cannot continue])
fi

AC_SUBST(top_ompi_srcdir)
AC_SUBST(top_ompi_builddir)

##################################
# @MCA_TYPE@ @MCA_COMPONENT_NAME@ component specific setup
##################################

#
# Unset all *_EXTRA_* flags
#

unset ABORT
for scope in LIBMPI WRAPPER; do
    for flags in CFLAGS CXXFLAGS FFLAGS LDFLAGS LIBS; do
        str="unset ${scope}_EXTRA_${flags}"
        eval $str
    done
done

#
# Invoke configure.stub code stubs, if they exist.  Have at least one
# statement (well, m4 macro) in each "if" block so that some older sh
# flavors don't complain.
#

if test "$OMPI_WANT_DIST" = "no"; then
    OMPI_LOG_MSG([Running configure stub macro])
    @CONFIGURE_STUB_MACRO@
else
    OMPI_LOG_MSG([Running configure dist stub macro])
    @CONFIGURE_DIST_STUB_MACRO@
fi

#
# If any *_EXTRA_* flags were set, save them in $srcdir/post_configure.sh.
# This will be examined by the top-level configure script.
#

create_post_configure_file() {
    if test ! -f post_configure.sh; then
        cat > post_configure.sh <<EOF
# This file is automatically generated by configure.
# Manual changes will be lost!
# Generated by: `whoami`
# On machine:   `hostname`
# On:           `date`

EOF
    fi
}

rm -f post_configure.sh
if test -n "$ABORT"; then
    create_post_configure_file
    echo "ABORT=\"$ABORT\"" >> post_configure.sh
    AC_MSG_ERROR([*** ABORT flag is set; aborting])
fi
for scope in LIBMPI WRAPPER; do
    for flags in CFLAGS CXXFLAGS FFLAGS LDFLAGS LIBS; do
        str="found=\$${scope}_EXTRA_${flags}"
        eval $str
        if test -n "$found"; then
            create_post_configure_file
            echo "${scope}_EXTRA_${flags}=\"$found\"" >> post_configure.sh
        fi
    done
done


#
# We delayed doing it until now just so that long include paths don't
# show up in any of the configure output -- purely aesthetic.
#

CPPFLAGS="$CPPFLAGS $INCFLAGS"
CXXCPPFLAGS="$CXXCPPFLAGS $INCFLAGS"


#
# Delayed the substitution of these until now because they may have
# been modified throughout the course of this script.
#

AC_SUBST(CPPFLAGS)
AC_SUBST(CXXCPPFLAGS)


############################################################################
# libtool magic
############################################################################

ompi_show_subtitle "GNU libtool setup" 

AM_PROG_LIBTOOL
AM_CONDITIONAL(OMPI_BUILD_@MCA_TYPE@_@MCA_COMPONENT_NAME@_DSO, 
    test "$BUILD_@MCA_TYPE@_@MCA_COMPONENT_NAME@_DSO" = "1")


############################################################################
# Party on
############################################################################

ompi_show_subtitle "Final output" 

AM_CONFIG_HEADER([@PARAM_CONFIG_HEADER_FILE@])
AC_CONFIG_FILES([@PARAM_CONFIG_FILES@])
AC_OUTPUT