#!/bin/sh
#
#	HP High Performance Systems Division
#	(c)Copyright 1999 Hewlett-Packard Co.
#
#	$Revision: 5922 $
#
#	Function:	- compile MPI C code
#
#	"@(#)HP MPI 02.02.05.00 (10/03/2006) Linux x86-64"
#

{
	PATH=${PATH}:/bin:/usr/bin:/usr/pgi/linux86-64/5.1/bin:/opt/pgi/linux86-64/5.1/bin

	if [ "$HPMPI_MPICC_EXECED" != "1" ] ; then
		if [ ! -z "${HPMPI_CC:-}" ] ; then
			MPI_CC="$HPMPI_CC"
		fi
	else
		if [ ! -z "${MPI_CXX:-}" ] ; then
			MPI_CC="$MPI_CXX"
		fi
		if [ ! -z "${HPMPI_CXX:-}" ] ; then
			MPI_CC="$HPMPI_CXX"
		fi
	fi
	mpicc=${MPI_CC:-""}
	sed=$(which sed 2>/dev/null)
	mpiroot=${MPI_ROOT:-""}
        if [ -z "$mpiroot" ] ; then
                mpiroot=$(cd $(dirname "$0")/.. ; pwd)
        fi
        if [ ! -x ${mpiroot}/bin/mpirun ] ; then
                mpiroot=/opt/hpmpi
        fi
        if [ ! -x ${mpiroot}/bin/mpirun ] ; then
                mpiroot=/opt/mpi
        fi

	case "$0" in
	*.mpich)
		mpiroot=${mpiroot}/MPICH1.2
		mpichmode=1 ;;
	*)
		mpichmode=0 ;;

	esac

	mpih=${mpiroot}/include

	palevel=linux_amd64
	prelib=""
	postlib=""
	ldopt=""
	linker=1
	dmpi=0
	mtmpi=0
	addpthread=0
	notv=0
	evalcmd=eval
	linknompimalloc=0
	mpiCClib=-lmpiCC

	args=""
	while [ $# -ge 1 ] ; do
		arg="$1"
		case "$arg" in
		-c | -E | -P | -S)	linker=0;;
		-ldmpi)			dmpi=1; arg="";;
		-lmtmpi)		mtmpi=1; arg="";;
		-lmtmpi_hmp)		mtmpi=1; arg="";;
		-notv)			notv=1; arg="";;
		-show)			evalcmd=echo; arg="";;
		-nompimalloc)		linknompimalloc=1; arg="";;
		-mpiCClib)		mpiCClib="$2"; shift; arg="";;
		-mpicxxlib)		mpiCClib="$2"; shift; arg="";;
		-mpicc)
			if [ "$HPMPI_MPICC_EXECED" != "1" ] ; then
				mpicc="$2"
			fi
			shift; arg="";;
		-mpicxx)
			if [ "$HPMPI_MPICC_EXECED" = "1" ] ; then
				mpicc="$2"
			fi
			shift; arg="";;

		-mpi32)
			palevel=linux_ia32; arg="";;
		-mpi64)
			arg="";;
		-static | -Wl,*-static)
			addpthread=1 ;;

		*\'* | *\\)
			if [ ! -z "$sed" ] ; then
				arg=$(printf "%s\n" "$arg" | $sed \
					-e 's/\([^a-zA-Z0-9/._]\)/\\\1/g');
			else
				arg=\""$arg"\"
			fi ;;
		*[!a-zA-Z0-9/._]*)
			arg=\'"$arg"\' ;;
		esac
		shift

		args="$args $arg"
	done

	if [ -z "$mpicc" ] ; then
		mpicc=
		if [ -z "$mpicc" ] ; then
			mpicc=$(which icc 2>/dev/null)
		fi
		if [ -z "$mpicc" ] ; then
			if [ "$HPMPI_MPICC_EXECED" = "1" ] ; then
				mpicc=$(which pgCC 2>/dev/null)
			else
				mpicc=$(which pgcc 2>/dev/null)
			fi
		fi
		if [ -z "$mpicc" ] ; then
			if [ "$HPMPI_MPICC_EXECED" = "1" ] ; then
				mpicc=g++
			else
				mpicc=gcc
			fi
		fi
	fi

	mpilib=${mpiroot}/lib/${palevel}

	exists_mpio=0
	exists_tvobj=0
	tvobj=""

	if [ -r ${mpilib}/libmpio.a -o -r ${mpilib}/libmpio.so ]; then
		exists_mpio=1
	fi

	if [ $mtmpi -eq 0 ]; then
		tvobj=${mpilib}/tv.o
	else
		tvobj=${mpilib}/mttv.o
	fi
	if [ -r $tvobj ]; then
		exists_tvobj=1
	fi

#
# Add in the mpi compilation options.
#
	if [ $linker -eq 1 ]; then
		if [ $notv -eq 1 -a $exists_tvobj -eq 1 ]; then
			prelib="${prelib} $tvobj"
		fi
		if [ $linknompimalloc -eq 1 ] ; then
			mpilib=${mpilib}/nomalloc:${mpilib}
		fi
		prelib="${prelib} -L${mpilib}"

#
# Link with the MPI thread-compliant libraries or the regular libraries?
#
		if [ $mtmpi -eq 1 ]; then

			postlib="-lmtmpi -lpthread -ldl"

			if [ $dmpi -eq 1 ]; then
				postlib="-ldmpi ${postlib}";
			fi

		else
			postlib="-lhpmpi -ldl"
			if [ $mpichmode -eq 1 ]; then
				postlib="-Wl,-rpath-link,${mpilib} -lmpich -ldl"
			fi

			if [ $dmpi -eq 1 ]; then
				postlib="-ldmpi ${postlib}";
			fi

			if [ $exists_mpio -ne 0 ]; then
				mpiolib="-lhpmpio"
				postlib="${mpiolib} ${postlib}";
			fi
		fi

		if [ $addpthread -eq 1 -a $mtmpi -eq 0 ] ; then
			postlib="${postlib} -lpthread"
		fi

		if [ "$HPMPI_MPICC_EXECED" = "1" ] ; then
			postlib="${postlib} ${mpiCClib}";
			case "/$mpicc" in
			*/icc)
				postlib="${postlib} -Kc++" ;;
			esac
		fi

	fi

#
# Submit the resulting compilation command
#

	cmd="$mpicc $prelib $args -I${mpih} $postlib $ldopt"

	$evalcmd "$cmd"
	ret=$?

	exit $ret
}
