#
#-----------------------------------------------------------------------
#
# Makefile for building TGCM (tiegcm or timegcm) 
# Supported platforms include IBM-AIX, SGI-IRIX, Linux (serial), and Linux-mpi
#
#-----------------------------------------------------------------------
#
null  :=
space := $(null) $(null)
comma := $(null),$(null)
#
# Determine platform 
#
ifeq ($(UNAMES),$(null))
  UNAMES := $(shell uname -s)
endif
ifeq ($(UNAMEN),$(null))
  UNAMEN := $(shell uname -n| awk '/^ln[[:digit:]]+en/{print "lightning"}')
endif
#
# Assign defaults:
#
ifeq ($(EXECNAME),$(null))
  EXECNAME := tgcm
endif
ifeq ($(EXECDIR),$(null))
  EXECDIR := .
endif 
ifeq ($(LIB_NETCDF),$(null))
  LIB_NETCDF := /usr/local/lib
endif
ifeq ($(INC_NETCDF),$(null))
  INC_NETCDF := /usr/local/include
endif
ifeq ($(UTILDIR),$(null))
  UTILDIR := .
endif
#-----------------------------------------------------------------------
# Load dependency search paths.
#
dirs := . $(shell cat Filepath)

# Set cpp search path, include netcdf
cpp_dirs := $(dirs) $(INC_NETCDF)
cpp_path := $(foreach dir,$(cpp_dirs),-I$(dir)) # format for command line

# Expand any tildes in directory names. Change spaces to colons.
VPATH    := $(foreach dir,$(cpp_dirs),$(wildcard $(dir))) 
VPATH    := $(subst $(space),:,$(VPATH))               

#------------------------------------------------------------------------
# Primary target:
#
all: $(EXECDIR)/$(EXECNAME)

CURDIR   := $(shell pwd)
SOURCES  := $(shell cat Srcfiles)
OBJS     := $(addsuffix .o, $(basename $(SOURCES)))
LIBS     := -L$(LIB_NETCDF) -lnetcdf
LDFLAGS  :=

Depends: Filepath Srcfiles
	perl $(UTILDIR)/MkDepends Filepath

Srcfiles: Filepath
	perl $(UTILDIR)/MkSrcfiles
#
# Note FFLAGS and CPPFLAGS are included by gmake.
#
$(EXECDIR)/$(EXECNAME): $(OBJS)
	$(FC) -o $@ $(OBJS) $(LIBS) $(LDFLAGS)

#-----------------------------------------------------------------------
ifeq ($(UNAMES),AIX)

ifeq ($(MPI),TRUE)
  CPPFLAGS   := -WF,-DAIX,-DMSS,-DMPI
else
  CPPFLAGS   := -WF,-DAIX,-DMSS
endif
OPTIM      := -O3 -qstrict
ifeq ($(DEBUG),TRUE)
  CPPFLAGS += -DDEBUG,-DEXPO
  CPPFLAGS := $(subst $(space),$(comma),$(CPPFLAGS))
  OPTIM    := -g
  DBGFLAGS := -qinitauto=7FF7FFFF -qflttrap=zero:inv:en -qsigtrap=xl__trcedump
endif
FFLAGS     := $(cpp_path) $(CPPFLAGS) $(OPTIM) $(DBGFLAGS) \
              -qarch=auto -qrealsize=8
#
FREEFLAGS  := -qsuffix=f=f90:cpp=F90
FIXEDFLAGS := -qfixed
#
# dcs lib can be removed when all models use my_msrcp instead of call msrcp.
#LIBS       += -L/usr/local/lib64/r4i4 -ldcs -lmss -lmass
#ifeq ($(DEBUG),TRUE)
#  LIBS     += -L/usr/local/lib64/r4i4 -ldcs -lmss # no -lmass if debug
#endif
LDFLAGS    += -bloadmap:loadmap -bmaxdata:0X70000000
ifeq ($(MPI),TRUE)
  FC         := mpxlf_r
else
  FC         := xlf90
endif

.SUFFIXES:
.SUFFIXES: .F .F90 .c .o

.F.o:
	$(FC) -c $(FIXEDFLAGS) $(FFLAGS) $<

.F90.o:
	$(FC) -c $(FREEFLAGS) $(FFLAGS) $<
#
# -qsave is necessary for pdedif (glbmean model only):
#    Sets the default storage class for	local variables	to STATIC
#
pdedif.o: pdedif.F
	$(FC) -c $(FIXEDFLAGS) $(FFLAGS) -qsave $<

endif
#-----------------------------------------------------------------------
ifeq ($(UNAMES),IRIX64)

ifeq ($(MPI),TRUE)
  CPPFLAGS   := -DIRIX -DMSS -DMPI
else
  CPPFLAGS   := -DIRIX -DMSS
endif
OPTIM      := -O3
ifeq ($(DEBUG),TRUE)
  CPPFLAGS += -DDEBUG,-DEXPO
  OPTIM    := -g
endif
FFLAGS     := $(cpp_path) $(OPTIM) -r8 -macro_expand -OPT:Olimit=4479 \
              $(CPPFLAGS) -v
LIBS       += -L/usr/lib32/mips4 -lffio -lcomplib.sgimath -lblas_mp -lfpe
ifeq ($(MPI),TRUE)
  LIBS       += -L/lusr/local/lib32/r4i4 -lmpi -lmss
else
  LIBS       += -L/lusr/local/lib32/r4i4 -lmss
endif
#
# dcs lib can be removed when all models use my_msrcp instead of call msrcp.
#LIBS       += -L/usr/local/dcs-3.3/lib -ldcs
LDFLAGS    += -mips4 -r10000
FC         := f90

gw_drag.o: gw_drag.F
	$(FC) -c $(FFLAGS) -freeform $<
#
# -static is necessary for pdedif (glbmean model only):
#    Sets the default storage class for	local variables	to STATIC
#
pdedif.o: pdedif.F
	$(FC) -c $(FIXEDFLAGS) $(FFLAGS) -static $<

endif
#-----------------------------------------------------------------------
ifeq ($(UNAMES),Linux)
ifeq ($(UNAMEN),lightning)
#
# For SCD linux cluster "lightning"
#
ifeq ($(MPI),TRUE)
  CPPFLAGS := -DLINUX -DMPI -DMSS
#
# 5/31/05: path to mpif.h was changed from /opt to /usr after lightning OS upgrade:
# cpp_path += -I/opt/mpich-gm/include
  cpp_path += -I/usr/local/mpich-gm/include
else
  CPPFLAGS := -DLINUX -DMSS
endif

# 1/05 btf: fastsse may cause problems in dynamo. Use -fast instead.
#OPTIM    := -O2 -fastsse 
 OPTIM    := -O2 -fast
ifeq ($(DEBUG),TRUE)
  CPPFLAGS += -DDEBUG,-DEXPO
  OPTIM    := -g
endif
#
# Note: Apparently gmake under Linux includes both FFLAGS and CPPFLAGS by 
#       default, so CPPFLAGS is not included in FFLAGS here.
#
FFLAGS   := $(cpp_path) $(OPTIM) -r8
#
# dcs lib can be removed when all models use my_msrcp instead of call msrcp.
#LIBS     += -L/usr/local/dcs-ssg-3.3/lib64 -ldcs
#LDFLAGS +=

ifeq ($(MPI),TRUE)
  FC := mpif90
else
  FC := pgf90
endif

gw_drag.o: gw_drag.F
	$(FC) -c $(FFLAGS) -Mfreeform $<
#
# -Msave is necessary for pdedif (glbmean model only):
#    Sets the default storage class for	local variables	to STATIC
#
pdedif.o: pdedif.F
	$(FC) -c $(FIXEDFLAGS) $(FFLAGS) -Msave $<
#
#-----------------------------------------------------------------------
else 
#
# Non-lightning Linux (probably hao Linux):
#
# 6/05: This works for serial linux with Fedora core 3 (sundog, suncat, etc)
#       Trying to make it work w/ MPI, e.g., on hao 4-proc machine "tor".

CPPFLAGS := -DLINUX -DMSS
ifeq ($(MPI),TRUE)
  CPPFLAGS := -DLINUX -DMPI -DMSS
  cpp_path += -I/usr/local/include
endif
OPTIM    := -O2
ifeq ($(DEBUG),TRUE)
  CPPFLAGS += -DDEBUG,-DEXPO
  OPTIM    := -g
endif
#
# Note: Apparently gmake under Linux includes both FFLAGS and CPPFLAGS by 
#       default, so CPPFLAGS is not included in FFLAGS here.
#
FFLAGS   := $(cpp_path) $(OPTIM) -r8
#
# dcs lib can be removed when all models use my_msrcp instead of call msrcp.
#LIBS     += -L/opt/local/dcs-3.3/rh9/lib -ldcs
ifeq ($(MPI),TRUE)
  LIBS   += -L/usr/local/lib -lmpich
endif
#LDFLAGS +=
FC       := pgf90

gw_drag.o: gw_drag.F
	$(FC) -c $(FFLAGS) -Mfreeform $<
#
# -Msave is necessary for pdedif (glbmean model only):
#    Sets the default storage class for	local variables	to STATIC
#
pdedif.o: pdedif.F
	$(FC) -c $(FIXEDFLAGS) $(FFLAGS) -Msave $<

endif # lightning
endif # linux
#-----------------------------------------------------------------------
#
RM := rm
clean:
	$(RM) -f Depends Srcfiles *.o *.mod $(EXECDIR)/$(EXECNAME)
#
# Include object file targets and dependencies made by MkDepends:
#
include Depends
