#
# LDIR (defined from $PETSC_DIR) determines the libraries that are linked.
# PDIR (defined from $PETSC_DIR) determines where the libraries are built.
# LDIR and PDIR will usually be identical.
# LIBNAME - library name
# SOURCE - source files
# OBJS - object files
# PETSC_INCLUDE - locations of include files
#
LDIR	      = $(PETSC_DIR)/lib/lib$(BOPT)/$(PETSC_ARCH)
PDIR	      = $(PETSC_DIR)/lib/lib$(BOPT)/$(PETSC_ARCH)
LIBNAME	      = $(PDIR)/$(LIBBASE).a
SOURCE	      = $(SOURCEC) $(SOURCEF)
OBJS	      = $(OBJSC) $(OBJSF)

PETSC_INCLUDE = $(MPI_INCLUDE) \
	-I$(PETSC_DIR) -I$(PETSC_DIR)/include -I$(PETSC_DIR)/src 
PARPRE_INCLUDE = \
	-I$(PARPRE_DIR) -I$(PARPRE_DIR)/include -I$(PARPRE_DIR)/src \
	$(PETSC_INCLUDE)
#
# Defines all libraries needed for using linear and nonlinear solvers, 
# and all lower level PETSc components (such as vectors and matrices).
# The order of listing these libraries is important!
#
PETSC_LIB   = -L$(LDIR) -lpetscsnes  -lpetscsles -lpetscksp \
		-lpetscmat  -lpetscvec \
		-lpetscsys  $(X11_LIB) \
		$(BS_LIB) $(LAPACK_LIB) $(BLAS_LIB) \
		$(MPE_LIB) $(MPI_LIB) $(FC_LIB) $(SYS_LIB) -lm 
PARPRE_LIB   = -L$(LDIR) -lpetscsnes  -lpetscsles -lparpre \
		-lpetscmat  -lpetscvec \
		-lpetscsys  $(X11_LIB) \
		$(BS_LIB) $(LAPACK_LIB) $(BLAS_LIB) \
		$(MPE_LIB) $(MPI_LIB) $(SYS_LIB) $(FC_LIB) -lm 

#
# Defines all libraries needed for using linear solvers and all lower
# level PETSc components (such as vectors and matrices).  The order
# of listing these libraries is important!
#
SLES_LIB    =	-L$(LDIR) -lpetscsles -lpetscksp \
		-lpetscmat  -lpetscvec \
		-lpetscsys \
		$(SUPERLU_LIB) $(BS_LIB) $(LAPACK_LIB) $(BLAS_LIB) $(X11_LIB) \
		$(MPE_LIB) $(MPI_LIB) $(FC_LIB) $(SYS_LIB) -lm

#
# Link if you are using the PETSc Fortran interface.
#
PETSC_FORTRAN_LIB = -L$(LDIR) -lpetscfortran
#
# These include files set customized site, optimization, and version options.
# Do NOT remove any of these include files.  You should generally have to edit
# only $(PETSC_DIR)/bmake/$(PETSC_ARCH)/base.site for your particular
# machine configuration.  See the users manual for details.
#
include $(PARPRE_DIR)/bmake/$(PARPRE_ARCH)/base.site
# (This line MUST be present for certain machines (e.g., Cray T3D))
include $(PETSC_DIR)/bmake/$(PETSC_ARCH)/base.$(BOPT)

# Checks that PETSC_DIR variable is set and creates library directory
# if it does not exist
chkpetsc_dir:
	@if [ Holder = Holder$(BOPT) ] ; then \
	  echo "You must set the variable BOPT=[g,O, or Opg]" ; false; fi
	-@if [ ! -d $(PDIR) ]; then \
	  echo Making directory $(PDIR) for library; mkdir -p $(PDIR) ; fi

# Checks that user has set BOPT variable
chkopts: 
	@if [ Holder = Holder$(BOPT) ] ; then \
	  echo "You must set the variable BOPT=[g,O, or Opg]" ; false; fi
	@if [ ! -d $(LDIR) ]; then \
	  echo Libraries not built for this BOPT: $(BOPT); false ; fi

# Does nothing, obviously; need for some rules that require actions.
foo:

# Builds library
lib: chkpetsc_dir $(SOURCE)
	@-if [ "$(SOURCEC)" != "" ] ; then \
	   $(OMAKE) -f makefile PETSC_ARCH=$(PETSC_ARCH) BOPT=$(BOPT) \
				libc; fi
	@-if [ "$(SOURCEF)" != "" ] ; then \
		$(OMAKE) -f makefile PETSC_ARCH=$(PETSC_ARCH) BOPT=$(BOPT) libf; fi
	@-if [ "$(OBJS)" != " " ] ; then \
		$(RANLIB)  $(LIBNAME); \
		$(RM) -f $(OBJS); \
	fi
#
#  Does not work for some machines with .F fortran files.
#
# Builds library - fast version
libfast: chkpetsc_dir $(SOURCEC) $(SOURCEF)
	@-if [ "$(SOURCEC)" != "" ] ; then \
	     $(CC) -c $(CFLAGS) $(COPTFLAGS) $(SOURCEC) ;\
	fi
	@-if [ "$(SOURCEF)" != "" ] ; then \
	     $(FC) -c $(FFLAGS) $(FOPTFLAGS) $(SOURCEF) ;\
	fi
	@-if [ "$(OBJS)" != " " ] ; then \
	  $(AR) cr $(LIBNAME) $(OBJS); \
	  $(RM) -f $(OBJS); \
	fi

# Removes garbage files
clean:
	@-$(RM) -f *.o *~ $(CLEANFILES) $(EXAMPLES_1) $(EXAMPLES_2) \
	       $(EXAMPLES_3) $(EXAMPLES_4) $(EXAMPLES_5) $(TESTS) \
	       PI* *.ln l.outa* mputil.mp_* core *.tmp *.map *.log gmon.out \
	       trashz \#*\# *.mex* *.stolen *.trace Log.* joe jeff *.stolen \
	       output/*~ .mpirtmp mon.out *.aus

# Builds and runs examples; then cleans directory.
examples: examples_1 examples_2
testexamples_1: examples_1 runexamples_1 clean
testexamples_2: examples_2 runexamples_2 clean
testexamples_3: examples_3 runexamples_3 clean
testexamples_4: examples_4 runexamples_4 clean
testexamples_5: examples_5 runexamples_5 clean
buildexamples_1: examples_1 clean
buildexamples_2: examples_2 clean
buildexamples_3: examples_3 clean
buildexamples_4: examples_4 clean
buildexamples_5: examples_5 clean

# Performs the specified action throughout the directory tree
tree: $(ACTION)
	@-if [ "$(DIRS)" != "" ]; then \
	for dir in $(DIRS) foo ; do if [ -s $$dir ]; then \
	(cd $$dir ; echo $(ACTION) in: `pwd`; \
	$(OMAKE) -f makefile tree ACTION=$(ACTION) BOPT=$(BOPT) \
	PETSC_ARCH=$(PETSC_ARCH)  ) ;fi; \
	done ; fi

# --------------------------------------------------------------------
#
# All remaining actions are intended for PETSc developers only.
# PETSc users should not generally need to use these commands.
#

# RCS file check-in
ci: 
	@-/usr/local/bin/ci -u -q -mAutoCheckin $(SOURCEH) $(SOURCE) makefile\
	  $(EXAMPLESC) $(EXAMPLESF) $(TESTSC) $(TESTSF) 

# RCS file check-out
co:
	@-/usr/local/bin/co -l -q $(SOURCEH) $(SOURCE) makefile $(EXAMPLESC)\
	  $(EXAMPLESF) $(TESTSC) $(TESTSF)

# Uses lint; true is to prevent make error message from empty grep
lint:
	@-if [ "$(SOURCEC)" != "" ] ; then \
	   lint -cvhu -DLINT -DPETSC_ARCH_$(PETSC_ARCH) $(CFLAGS) $(SOURCEC)\
		| grep -v "never defined"\
		| grep -v "pointer alignment problem" | grep -v\
		"function prototype not in scope"; true ;\
	   $(RM) -f *.ln ; fi

# Builds Fortran-77 wrappers
fortranstubs:
	@-/home/bsmith/bin/sun4/bfort -dir /home/bsmith/petsc/src/fortran/auto \
	  -mnative -ansi -nomsgs -anyname -mapptr -mpi -ferr -ptrprefix MPIR_\
	  -ptr64 HAVE_64BITS -fcaps HAVE_FORTRAN_CAPS \
          -fuscore HAVE_FORTRAN_UNDERSCORE $(SOURCEC)


# Builds man pages (xman version)
manpages:
	@-if [ "$(MANSEC)" != "" ] ; then \
	/home/gropp/bin/sun4/doctext -mpath \
               $(PETSC_DIR)/docs/man/man$(MANSEC) -ext $(MANSEC) \
                -locdir $(LOCDIR) -heading PETSc \
		readme $(SOURCEC) $(SOURCEH); \
		chmod g+w $(PETSC_DIR)/docs/man/man$(MANSEC)/*; fi 

# Builds man pages (LaTeX version)
latexpages:
	@-if [ "$(MANSEC)" != "" ] ; then \
	/home/gropp/bin/sun4/doc2lt	\
	  $(SOURCEC) $(SOURCEH) >> \
		    $(PETSC_DIR)/docs/tex/rsum/rsum$(MANSEC).tex ; fi 

# Builds man pages (HTML version) in two stages
# See rule for allwwwpages in $(PETSC_DIR)/makefile 
#   1.) wwwpages_buildcite: builds the file www.cit for hyperlinks
#   2.) wwwpages: builds the html pages, complete with hyperlinks
wwwpages_buildcite:
	@-if [ "$(MANSEC)" != "" ] ; then \
	/home/gropp/bin/sun4/doctext -html -indexdir ../man$(MANSEC) \
		-index $(PETSC_DIR)/docs/www/www.cit \
		-mpath $(PETSC_DIR)/docs/www/man$(MANSEC) $(SOURCEC) $(SOURCEH); \
		chmod g+w $(PETSC_DIR)/docs/www/man$(MANSEC)/*; fi 

wwwpages:
	@-if [ "$(MANSEC)" != "" ] ; then \
	/home/gropp/bin/sun4/doctext -html \
		-mpath $(PETSC_DIR)/docs/www/man$(MANSEC) -heading PETSc \
		-locdir $(LOCDIR) -mapref $(PETSC_DIR)/docs/www/www.cit \
		-defn $(PETSC_DIR)/docs/www/sec/eop.$(MANSEC) \
		$(SOURCEC) $(SOURCEH); \
		chmod g+w $(PETSC_DIR)/docs/www/man$(MANSEC)/*; fi 







