#
#
# Copyright Intel Corporation.
# 
# This software and the related documents are Intel copyrighted materials, and
# your use of them is governed by the express license under which they were
# provided to you (License). Unless the License provides otherwise, you may
# not use, modify, copy, publish, distribute, disclose or transmit this
# software or the related documents without Intel's prior written permission.
# 
# This software and the related documents are provided as is, with no express
# or implied warranties, other than those that are expressly stated in the
# License.
#
# Makefile to build fortran module
LIB = ar cr
NAME=pgf
NAME_MPI=mpi${NAME}
LIBNAME = lib${NAME_MPI}
LD = gcc

ifeq ($(MPI_INST), )
    $(error MPI_INST shoud be defined and point to MPI installation)
endif

all: makemod makedriver

ifeq ($(F90), )
    $(error F90 = f90 compiler should be defined)
endif

mpifnoext.h: ${MPI_INST}/include/mpif.h
	rm -f mpifnoext.h
	sed -e 's/^C/\!/g' -e '/EXTERNAL/d' \
		-e '/REAL\*8/d' \
		-e '/DOUBLE PRECISION/d' \
		-e '/MPI_WTICK/d' ${MPI_INST}/include/mpif.h > mpifnoext.h

makemod: mpi.f90 mpi_constants.f90 mpi_sizeofs.f90 mpi_base.f90 pmpi_base.f90 mpifnoext.h
	${F90} ${MIC} -c -fPIC -I${MPI_INST}/include mpi_constants.f90
	${F90} ${MIC} -c -fPIC -I${MPI_INST}/include mpi_sizeofs.f90
	${F90} ${MIC} -c -fPIC -I${MPI_INST}/include mpi_base.f90
	${F90} ${MIC} -c -fPIC -I${MPI_INST}/include pmpi_base.f90
	${F90} ${MIC} -c -fPIC -I${MPI_INST}/include mpi.f90
	mkdir -p lib && ar cr lib/${LIBNAME}.a mpi.o mpi_base.o pmpi_base.o mpi_sizeofs.o mpi_constants.o
	mkdir -p include/${NAME} && mv *.mod include/${NAME}
	${F90} ${MIC} -shared -Xlinker -x -Xlinker -soname=${LIBNAME}.so -o lib/${LIBNAME}.so mpi.o mpi_base.o pmpi_base.o  mpi_sizeofs.o mpi_constants.o -lrt -ldl
	if [ "x${ILP64}" != "x" ] ; then \
	    ${F90} ${MIC} -c -fPIC ${ILP64} -I${MPI_INST}/include mpi_constants.f90 -o mpi_constants_ilp64.o ; \
	    ${F90} ${MIC} -c -fPIC ${ILP64} -I${MPI_INST}/include mpi_sizeofs_ilp64.f90 -o mpi_sizeofs_ilp64.o ; \
	    ${F90} ${MIC} -c -fPIC ${ILP64} -I${MPI_INST}/include mpi_base.f90 -o mpi_base_ilp64.o ; \
	    ${F90} ${MIC} -c -fPIC ${ILP64} -I${MPI_INST}/include pmpi_base.f90 -o pmpi_base_ilp64.o ; \
	    ${F90} ${MIC} -c -fPIC ${ILP64} -I${MPI_INST}/include mpi.f90 -o mpi_ilp64.o ; \
	    mkdir -p include/ilp64/${NAME} && mv *.mod include/ilp64/${NAME} ; \
	    ar cr lib/${LIBNAME}_ilp64.a mpi_ilp64.o mpi_base_ilp64.o pmpi_base_ilp64.o mpi_sizeofs_ilp64.o mpi_constants_ilp64.o ; \
	    ${F90} ${MIC} -shared -Xlinker -x -Xlinker -soname=${LIBNAME}_ilp64.so -o lib/${LIBNAME}_ilp64.so mpi_ilp64.o mpi_base_ilp64.o pmpi_base_ilp64.o mpi_sizeofs_ilp64.o mpi_constants_ilp64.o -lrt -ldl ; \
	fi

makedriver:
	@mkdir -p bin
	sed -e 's/FC\=\"gfortran/FC\=\"${F90}/' \
	-e 's/rpath_opt\=.*/rpath_opt\=/' \
	-e 's/gfortran\$${gver}/${NAME}/' \
	-e 's/-lpthread/-lpthread -l${NAME_MPI}/' ${MPI_INST}/bin/mpif90 > bin/${NAME_MPI}
	if [ "x${ILP64}" != "x" ] ; then \
	    mv bin/${NAME_MPI} bin/${NAME_MPI}_tmp ; \
	    sed -e 's/-lmpi_ilp64/-l${NAME_MPI}_ilp64 -lmpi_ilp64/' \
	    -e 's/$$libdir\/libmpi_ilp64.a/$$libdir\/${LIBNAME}_ilp64.a $$libdir\/libmpi_ilp64.a/' bin/${NAME_MPI}_tmp > bin/${NAME_MPI} ; \
	fi
	chmod 755 bin/${NAME_MPI}
	-rm -f bin/${NAME_MPI}_tmp

clean:
	-rm -f *.o
	-rm -rf bin
	-rm -rf mpifnoext.h
