# -*- Mode: makefile; -*-
SUBDIRS = . src @EXTERNAL_SRC_DIRS@ examples
# Do not include test in the basic build.  
OTHER_DIRS = test
DOC_SUBDIRS = doc
# The all-local target makes sure that when different subsystems are selected
# for the configure step, the libraries are removed and the object files 
# cleaned out.  Without this step, it is possible to run make and encounter
# errors caused by out-of-date objects in the directories and in the 
# library.  We do not replace the pkglist file until the make completes,
# using the all-postamble target.
#
# Here is the logic:
#   A new configure creates lib/newconfig
#   A cleanobjsandlibs step removes that file, as well as removing the
#     libraries and object files
#   The test for whether the clean step is performed is this:
#      Create a file lib/pkglist.new
#      Compare lib/pkglist.new to lib/pkglist
#      If the same, rm lib/pkglist.new and done.
#      If different, then cleanout the libraries and object files,
#          then mv lib/pkglist.new lib/pkglist
#          
all-local:
	@if [ ! -d lib ] ; then mkdir lib ; date > lib/.mpich2created ; \
	else \
	    echo "@subsystems@" > lib/pkglist.new ; \
	    echo "@CC@ @CFLAGS@" >>lib/pkglist.new ;\
	    echo "@CXX@ @CXXFLAGS@" >> lib/pkglist.new ;\
	    echo "@F77@ @FFLAGS@" >> lib/pkglist.new ;\
	    echo "@F90@ @F90FLAGS@" >> lib/pkglist.new ;\
	    echo "@CONFIGURE_ARGUMENTS@" >>lib/pkglist.new ; \
	    echo "@LDFLAGS@" >> lib/pkglist.new ;\
	    if diff lib/pkglist lib/pkglist.new >/dev/null 2>&1 ; then \
	        rm -f lib/pkglist.new ; \
            else \
		${MAKE} cleanobjsandlibs ; \
		rm -f lib/pkglist ; \
		mv -f lib/pkglist.new lib/pkglist ; \
	    fi ; \
	fi

clean-preamble:
	${MAKE} cleanlibs

cleanobjsandlibs:
	${MAKE} clean && cd test && ${MAKE} clean
	rm -f lib/newconfig
# cleanlibs makes sure that we clean all of the library files
cleanlibs:
	rm -f lib/lib${MPILIBNAME}.a lib/lib${PMPILIBNAME}.a
	rm -f lib/lib${MPILIBNAME}cxx.a 
	rm -f lib/libf${MPILIBNAME}.a 
	rm -f lib/lib${MPILIBNAME}.@SHLIB_EXT@* lib/lib${PMPILIBNAME}.@SHLIB_EXT@*
	rm -f lib/lib${MPILIBNAME}cxx.@SHLIB_EXT@* 
	rm -f lib/libf${MPILIBNAME}.@SHLIB_EXT@* 
	rm -f lib/lib${MPILIBNAME}.la* lib/lib${PMPILIBNAME}.la*
	rm -f lib/lib${MPILIBNAME}cxx.la* 
	rm -f lib/libf${MPILIBNAME}.la* 

all-preamble:
	@echo "Beginning make"

# Add any final extras:
#    1) Build the debugger interface if requested
# Move the "pkglist.new" file to "pkglist" on successful completion
all-postamble:
	@if [ "@BUILD_TVDLL@" = "yes" ] ; then \
	( cd src/mpi/debugger && $(MAKE) ./libtvmpich2.@SHLIB_EXT@ ) ; fi
	@if [ -s lib/pkglist.new ] ; then mv -f lib/pkglist.new lib/pkglist ; fi
	@echo "Make completed"

# The test on lib/.mpich2created lets us delete the local lib directory
# if we created it, and not otherwise
distclean-local:
	-@rm -f lib/lib${MPILIBNAME}.a lib/pkglist lib/pkglist.new
	-@rm -f lib/lib${MPILIBNAME}.@SHLIB_EXT@* lib/lib${MPILIBNAME}.la
	-@if [ -s lib/.mpich2created ] ; then rm -rf lib ; fi
	-@rm -f src/include/${MPILIBNAME}conf.h
	-@rm -f src/include/mpio.h
	-@rm -f bin/mpicc
	-@rm -f bin/mpich2version
	-@cd test && $(MAKE) distclean

PACKAGE = mpich2
# Version < 1 is pre MPI-1 release.
# Version >= 2 contains MPI-2
# Version between 1 and 2 contains all of MPI1 and parts of MPI-2
# Version is now set in the Configure file so that we can easily include the
# version on the configure output and to match the common practice of
# current autoconfs
VERSION = 1.0.5
MPICVSHOME = @MPICVSHOME@
#
# Build a distribution tarfile
#  Extract into a temporary directory
#     cvs update output into cvsexp.log to reduce the volume of noise
#     in the output
#  Rename the directory
#  Run updatefiles with distribution options
#  Tar it up
dist: dist-check
	@version=${VERSION} ; \
	prever=`echo ${VERSION} | sed -e 's/^[^-]*//'` ; \
	if [ "$$prever" = "-CVS" ] ; then version=$${version}-`date "+%Y%m%d%H%M"` ; fi ; \
	if [ -z "${TAG}" ] ; then tag=HEAD ; else tag=${TAG}; fi ; \
	$(MAKE) version=$${version} tag=$${tag} doc=yes dist-with-version

# snap creates a simple snapshot of the currently checked in MPICH2 sources.
# No documentation is included.
snap:
	@version=snap-`date "+%Y%m%d%H%M"` ; \
	$(MAKE) version=$${version} tag=HEAD doc=no dist-with-version

#
# Note on dist process
# Some directories and files are not included with the distribution.
# These may be old experimental versions or internal projects.
# Some may have other copyrights which we wish to keep out of the "clean" 
# code (Zeroconf.py is an example of this).
# 
# Note that for the documentation, we need to configure with romio enabled
# to ensure that the documentation pages are built
#
dist-with-version:
	@if [ -z "$(version)" ] ; then \
	    echo "Must invoke with make version=value tag=value dist-with-version" ;\
	    exit 1 ; fi 
	@if [ -z "$(tag)" ] ; then \
	    echo "Must invoke with make version=value tag=value dist-with-version" ;\
	    exit 1 ; fi 
	@echo ""; echo "Cutting version ${version} of the MPICH2 distribution from tag ${tag}" ; echo ""
	if [ ! -d tmpdir ] ; then mkdir tmpdir ; fi 
	rm -f cvsexp.log 
	( cd tmpdir && cvs -d $(MPICVSHOME) export -r "${tag}" mpich2allWithMPE >cvsexp.log 2>&1 ) || (echo "cvs export failed; see tmpdir/cvsexp.log" && exit 1 )
	( cd tmpdir/mpich2 && sed -e "s/^VERSION = .*/VERSION = ${version}/" Makefile.sm >.tmp && mv .tmp Makefile.sm )
	echo "${version}" >tmpdir/mpich2/maint/Version
	mv tmpdir/mpich2 tmpdir/${PACKAGE}-${version}
	( cd tmpdir/${PACKAGE}-${version}/src/mpid && rm -rf mm rdma globus)
	( cd tmpdir/${PACKAGE}-${version}/doc && rm -rf notes develop )
	( cd tmpdir/${PACKAGE}-${version}/src/pm && rm -rf forker rmpd ompd )
	( cd tmpdir/$(PACKAGE)-$(version)/src/pm/mpd && rm -f Zeroconf.py)
	( cd tmpdir/$(PACKAGE)-$(version)/src/mpid/ch3/channels && rm -rf ib rdma gasnet sshm)
	( cd tmpdir/$(PACKAGE)-$(version)/src/mpid/ch3/channels/nemesis/nemesis/net_mod && rm -rf newtcp_module/* sctp_module/* ib_module/*)
	( cd tmpdir/$(PACKAGE)-$(version)/src/mpid/ch3/channels/nemesis/nemesis/net_mod && \
	echo "# Stub Makefile" > newtcp_module/Makefile.sm && \
	echo "# Stub Makefile" > ib_module/Makefile.sm && \
	echo "# Stub Makefile" > sctp_module/Makefile.sm )
	( cd tmpdir/${PACKAGE}-${version} && rm -f ToDo* )
	( cd tmpdir/${PACKAGE}-${version} && rm -rf archive )
	( cd tmpdir/${PACKAGE}-${version} && maint/updatefiles )
	( cd tmpdir/${PACKAGE}-${version} && rm -f unusederr.txt )
	( cd tmpdir/${PACKAGE}-${version} && find . -name 'autom4te.cache' \
	  -print | xargs -r rm -rf)
	( cd tmpdir/${PACKAGE}-${version} && rm -rf src/mpe2/src/slog2sdk/doc/jumpshot-4/tex  )
	if [ "$(doc)" = "yes" ] ; then \
	    cp -r tmpdir/${PACKAGE}-${version} tmpdir2 ; \
	    rm -f manlog.txt ; \
	    ( cd tmpdir2 && ./configure --without-mpe --disable-f90 \
              --disable-f77 --disable-cxx && \
              ${MAKE} mandoc && ${MAKE} htmldoc && \
              ${MAKE} latexdoc ) >manlog.txt 2>&1 ; \
	    if [ ! -d tmpdir2/man ] ; then echo "WARNING: unable to build manpages" ; \
		(cd tmpdir2 && ls ) ; \
	    fi ; \
	    cp -r tmpdir2/man tmpdir/${PACKAGE}-${version}/man ; \
	    cp -r tmpdir2/www tmpdir/${PACKAGE}-${version}/www ; \
	    cp tmpdir2/doc/userguide/user.pdf tmpdir/${PACKAGE}-${version}/doc/userguide/user.pdf ; \
	    cp tmpdir2/doc/installguide/install.pdf tmpdir/${PACKAGE}-${version}/doc/installguide/install.pdf ; \
	    cp tmpdir2/doc/smpd/smpd_pmi.pdf tmpdir/${PACKAGE}-${version}/doc/smpd/smpd_pmi.pdf ; \
	    cp tmpdir2/doc/logging/logging.pdf tmpdir/${PACKAGE}-${version}/doc/logging/logging.pdf ; \
	    cp tmpdir2/doc/windev/windev.pdf tmpdir/${PACKAGE}-${version}/doc/windev/windev.pdf ; \
	    rm -rf tmpdir2 ; \
	fi
	( cd tmpdir/${PACKAGE}-${version} && maint/updatefiles -distrib )
	( cd tmpdir/${PACKAGE}-${version} && find . -name 'CVS' -print | \
	    xargs -r rm -rf )
	( cd tmpdir && tar cf ${PACKAGE}-${version}.tar ${PACKAGE}-${version} )
	mv -f tmpdir/${PACKAGE}-${version}.tar . 
	rm -rf tmpdir

# Check that all modules are present in the development version
MODULE_DIRS = confdb src/util/thread src/mpi/romio src/mpe2 src/pm/smpd src/pm/mpd doc/notes 
dist-check:
	@for dir in $(MODULE_DIRS) ; do \
	    if [ ! -d $(master_top_srcdir)/$$dir ] ; then \
		echo "Missing $$dir in $(master_top_srcdir) !" ; \
		exit 1 ; \
	    fi ; \
	done

# 
# We skip the mpid/mm directories because mm is no longer active
# mpe2 is an outside project and is tested against the default
# checks
coding-check: 
	@if [ -z $(CODINGCHECK) ] ;then \
	    echo "You must define CODINGCHECK for this target" ; \
	else \
	   echo "Checking the MPICH2 sources" ;\
	   $(CODINGCHECK) -addchecks=mpich2,funcstates,funcnests \
		-skipfiles=src/mpe2,src/mpid/mm src examples test ;\
	   if [ -d src/mpe2 ] ; then \
	        echo "Checking mpe2 sources" ;\
		$(CODINGCHECK) src/mpe2 ; \
	   fi ; \
	fi

# Set the document sources and types (empty lists still cause recursion)
# These should be empty; any source files should be described in the
# directory that contains those source files.  The presence of these
# items simply indicates that there are such documents
doc_HTML_SOURCES  =
doc_MAN_SOURCES   = 
doc_LATEX_SOURCES = 

# Add the www index pages
htmldoc-local:
	doc/mansrc/createhtmlindex

# Testing requires installation first
testing:
	(cd test && $(MAKE) testing)

profilelib_${MPILIBNAME} = ${PMPILIBNAME}
MPICXXLIBNAME = @MPICXXLIBNAME@
MPIFLIBNAME   = @MPIFLIBNAME@
MPIFWRAPNAME  = @FWRAPNAME@
install_LIB = lib/lib${MPILIBNAME}.a
optinstall_LIB = lib/lib${MPIFLIBNAME}.a
optinstall_LIB = lib/lib${MPICXXLIBNAME}.a
optinstall_LIB = lib/lib${MPIFWRAPNAME}.a
#optinstall_SHLIB = lib/lib${MPIFLIBNAME}.@SHLIB_EXT@
#optinstall_SHLIB = lib/lib${MPICXXLIBNAME}.@SHLIB_EXT@

# Extra information on creating shared libraries, needed on some platforms
# On the platforms that require these options, we require that 
# PMPILIBNAME and MPILIBNAME be the same.
# UNUSED, example for experimentation only
#lib${MPILIBNAME}_so_EXPORTS = maint/mpi.def
#libf${MPILIBNAME}_so_EXPORTS = maint/fmpi.def
libf${MPILIBNAME}_so_LIBS = -L. -l$(MPILIBNAME)

install_INCLUDE = src/include/mpi.h

optinstall_LIB = lib/lib${PMPILIBNAME}.a
optinstall_SHLIB = lib/lib${MPILIBNAME}.@SHLIB_EXT@
optinstall_SHLIB = lib/lib${PMPILIBNAME}.@SHLIB_EXT@
optinstall_INCLUDE = src/mpi/romio/include/mpiof.h src/mpi/romio/include/mpio.h
optinstall_LIB = lib/libf${MPILIBNAME}.a
optinstall_SHLIB = lib/libf${MPILIBNAME}.@SHLIB_EXT@

# Some of the subsidiary packages may need to be installed.  This target will
# invoke their install targets as part of the installation process
INSTALL_SUBDIRS = src/util/thread src/env @bindings_dirs@ @other_install_dirs@

# Install preamble checks for possible problems
install-preamble:
	@if [ "@ENABLE_SHLIB@" = none ] ; then\
             found=no ; \
	     for file in ${libdir}/lib*${MPILIBNAME}.@SHLIB_EXT@ ; do \
                 if [ -f $$file ] ; then \
		     echo "Remove $$file and rerun ${MAKE} install" ; \
		     found=yes ; \
		 fi ; \
	     done ; \
	     if [ $$found != no ] ; then exit 1 ; fi ;\
	fi

# Install optional features which require special actions:
#   The debugger interface (the "Totalview" DLL)
#   The "other" process managers when more that one are built
install_local_DIR = libdir
install-local:
	-if [ "@BUILD_TVDLL@" = "yes" ] ; then \
	    $(INSTALL_DATA) src/mpi/debugger/libtvmpich2.@SHLIB_EXT@ \
			${DESTDIR}${libdir}/libtvmpich2.@SHLIB_EXT@ ; fi
	-if [ -n "@other_pm_names@" ] ; then \
	    for pm in @other_pm_names@ /dev/null ; do \
		if [ -d src/pm/$$pm ] ; then \
		     (cd src/pm/$$pm && $(MAKE) install-alt ) ; fi ; \
	    done ; \
	fi

# 
# The install-examples target may be used if you want to install the cpi
# program for others to use.  This is nto part of the default installation
# because there isn't a good place to put this in the GNU recommended 
# installation targets (its a kind of hardware-architecture-specific
# documentation)
install-examples:
	if [ ! -d ${DESTDIR}${exec_prefix}/examples ] ; then \
	    $(MKDIR_P) ${DESTDIR}${exec_prefix}/examples ; \
	fi
	$(INSTALL_PROGRAM) examples/cpi ${DESTDIR}${exec_prefix}/examples/cpi

install-devdocs:
	(cd doc && $(MAKE) install-devdocs)

# installcheck-postable adds checks that may require special
# handling.  
# FIXME: Some install checks require that the Process Manager be
# started first.  This step does not handle that.
installcheck-postamble:
	@if [ -n "@mpe_dir@" -a -d "src/@mpe_dir@" ] ; then \
	    (cd src/@mpe_dir@ && $(MAKE) installcheck-all ; ) ; fi
#
# The documents are shipped as part of the release, and so should be 
# installed from the source directory, not a VPATH build directory
install_DOC = 
optinstall_DOC = \
    $(srcdir)/doc/userguide/user.pdf \
    $(srcdir)/doc/installguide/install.pdf \
    $(srcdir)/doc/smpd/smpd_pmi.pdf \
    $(srcdir)/doc/logging/logging.pdf
installdir_HTML = $(srcdir)/www
installdir_MAN  = $(srcdir)/man
#install_BIN = 
#install_SBIN = 	
# Still to do: figure out the appropriate target for installing the html
# versions of the documents.
