#!/bin/sh
#
# This makefile is makes the CMStab library and example code
# on the following architectures:
#
#          CM-5 with Vector Units
#          CM-5 with SPARC-only
#          CM-2 Slicewise Model
#          CM-2 Paris Model
#          CM-200 Slicewise Model
#          CM-200 Paris Model
#
# ******** INSTRUCTIONS (such as they are) ********
#
# "make cm5vu"  -- makes CMStab and example code on a CM-5 with Vector Units
#                  in the current directory (subject to definitions below).
# "make"        -- uses environment variable SYSTEM to set the type
#                  system to compile for.
# "make clean"  -- removes all object files and any intermediate files
#		   that may have been left behind.
#
#     Edit the OBJD, LIBD, EXED, FFLAGS, LDFLAGS  and CMD definition as
# you like for placing things where you want them.  The directories
# can even be symbolic links if you like.  
#
# FFLAGS are flags for the CM Fortran compiler.  LDFLAGS are flags for the
# loader.  OBJD, LIBD, and EXED define directories for object files,
# library files and executable files respectively.  CMD gives the filename
# for the executable file.
#


#
#  Define an object file subdirectory,
# a library file subdirectory and 
# an executable file subdirectory.
OBJD = ./obj
LIBD = ./lib
EXED = ./exe

# Change the following lines as necessary
FFLAGS =  -O -double_precision
LDFLAGS = -L$(LIBD)

# Define the name of the executable file
CMD =	poisson

# Define source  and object files for example code
EXSRCS = poisson.fcm driver.fcm flops.fcm usrsubs.fcm 
EXOBJS = $(EXSRCS:%.fcm=$(OBJD)/%.o)
INCLUDES = poisson.h driver.h

##########
# This has to come first so most versions of 'make' work properly.  
# Don't edit unless you know what you are doing.
default:
	@if test -z "$(MAKE)"; then\
		if test -z "$(SYSTEM)";\
		then make ERROR;\
		else make $(SYSTEM) MAKE=make ;\
		fi;\
	else\
		if test -z "$(SYSTEM)";\
		then $(MAKE) ERROR;\
		else $(MAKE) $(SYSTEM);\
		fi;\
	fi
#
##########


# Define the make rules for the example code

$(EXED)/$(CMD): $(EXOBJS)
	$(FC) $(LDFLAGS) -o $(EXED)/$(CMD) $(EXOBJS) $(EXLIBS) -l$(CMSTAB) 


$(OBJD)/poisson.o : poisson.fcm poisson.h
	$(FC) $(FFLAGS) -c poisson.fcm
	mv -f $(@F) $(OBJD)
	@if (test -f $(@F:%.o=%.pe.o) ); then\
		mv -f $(@F:%.o=%.pe.o) $(OBJD) ;\
	fi

$(OBJD)/driver.o : driver.fcm driver.h
	$(FC) $(FFLAGS) -c driver.fcm
	mv -f $(@F) $(OBJD)
	@if (test -f $(@F:%.o=%.pe.o) ); then\
		mv -f $(@F:%.o=%.pe.o) $(OBJD) ;\
	fi

$(OBJD)/flops.o : flops.fcm
	$(FC) $(FFLAGS) -c flops.fcm
	mv -f $(@F) $(OBJD)
	@if (test -f $(@F:%.o=%.pe.o) ); then\
		mv -f $(@F:%.o=%.pe.o) $(OBJD) ;\
	fi

$(OBJD)/usrsubs.o : usrsubs.fcm
	$(FC) $(FFLAGS) -c usrsubs.fcm
	mv -f $(@F) $(OBJD)
	@if (test -f $(@F:%.o=%.pe.o) ); then\
		mv -f $(@F:%.o=%.pe.o) $(OBJD) ;\
	fi

#
#
#  The rest of this Makefile is not for the weak at heart.  Be careful
# in edititng it if you decide to do so.
#

SHELL = /bin/sh

# List of supported systems

SYSTEMS=        cm5vu cm5sparc cm2slice cm2paris cm200slice cm200paris


ERROR:
	@echo
	@echo \
'Must type "make <system>", where <system> is one of the following:'
	@echo
	@echo  "        $(SYSTEMS)"
	@echo
	@echo \
'Otherwise set the shell variable SYSTEM to one of these and just type "make".'
	@echo

#
# Make rules for partitcular type of systems
#
cm5vu: $(OBJD) $(LIBD) $(EXED)
	$(MAKE) cmstab FC='cmf -cm5 -vu' EXLIBS='-lcmsslcm5vu' \
		CMSTABLIB='libcmstabcm5vu.a' \
		CMSTABLIB_PE='libcmstabcm5vu_pe.a' CMSTAB='cmstabcm5vu'

cm5sparc: $(OBJD) $(LIBD) $(EXED)
	$(MAKE) cmstab FC='cmf -cm5 -sparc' EXLIBS='-lcmsslcm5' \
		CMSTABLIB='libcmstabcm5.a' \
		CMSTABLIB_PE='libcmstabcm5_pe.a' CMSTAB='cmstabcm5'

cm2slice: $(OBJD) $(LIBD) $(EXED)
	$(MAKE) cmstab FC='cmf -cm2 -slice' EXLIBS='-lcmssl-s' \
		CMSTABLIB='libcmstab-cm2-s.a' CMSTAB='cmstab-cm2-s'

cm2paris: $(OBJD) $(LIBD) $(EXED)
	$(MAKE) cmstab FC='cmf -cm2 -paris' EXLIBS='-lcmssl' \
		CMSTABLIB='libcmstab.a' CMSTAB='cmstab'

cm200slice: $(OBJD) $(LIBD) $(EXED)
	$(MAKE) cmstab FC='cmf -cm200 -slice' EXLIBS='-lcmssl-s' \
		CMSTABLIB='libcmstab-cm200-s.a' CMSTAB='cmstab-cm200-s'

cm200paris: $(OBJD) $(LIBD) $(EXED)
	$(MAKE) cmstab FC='cmf -cm200 -paris' EXLIBS='-lcmssl' \
		CMSTABLIB='libcmstab-cm200.a' CMSTAB='cmstab-cm200'

#
# Make sure the directories exist
#
$(OBJD):
	mkdir $(OBJD)

$(LIBD):
	mkdir $(LIBD)

$(EXED):
	mkdir $(EXED)

#
# Build the CMStab library
#

#Define source and object files for CMStab library
LIBSRCS = cmstab1.fcm cmstab2.fcm cmstab3.fcm cmstab5.fcm
LIBOBJS = $(LIBSRCS:%.fcm=$(OBJD)/%.o)
LIBOBJS_PE = $(LIBSRCS:%.fcm=$(OBJD)/%.pe.o)

cmstab: $(LIBD)/$(CMSTABLIB) $(EXED)/$(CMD)

# Build the CMStab library if we need to
$(LIBD)/$(CMSTABLIB): $(LIBOBJS)
	ar rv $(LIBD)/$(CMSTABLIB) $(LIBOBJS)
	ranlib $(LIBD)/$(CMSTABLIB)
	@if (test -f $(OBJD)/cmstab1.pe.o); then\
		ar rv $(LIBD)/$(CMSTABLIB_PE) $(LIBOBJS_PE);\
		ranlib $(LIBD)/$(CMSTABLIB_PE);\
	fi

#
# How to build the CMStab library object files
$(OBJD)/cmstab%.o: cmstab%.fcm
	$(FC) $(FFLAGS) -c $<
	mv -f $(@F) $(OBJD)
	@if (test -f $(@F:%.o=%.pe.o) ); then\
		mv -f $(@F:%.o=%.pe.o) $(OBJD) ;\
	fi

#
# Provide a cleanup of object files
clean:
	/bin/rm -f $(OBJD)/*.o *.s *.o

