#
#-----------------------------------------------------------------------
#
# Makefile for building TGCM (tiegcm or timegcm) 
# Supported platforms include IBM-AIX, SGI-IRIX, Linux (serial), and Linux-mpi
#
#-----------------------------------------------------------------------
#
# The following variables are added to this template with the appropriate
# values by tgcm_config. If tgcm_config is not called, or the Makefile 
# needs to be debugged or customized, the variables can be set manually here.
#
# UNAMES     := [operating system from "uname -s" command]
# UNAMEN     := [machine name from "uname -n" command]
# EXECNAME   := [executable name (usually model name with version number)]
# MODEL_CLASS:= [model class name (tiegcm, timegcm, glbmean, mtgcm, etc)]
# EXECDIR    := [executable directory]
# UTILDIR    := [location of script utilities, Makefile, tgcm_config, etc]
# LIB_NETCDF := [location of netcdf library libnetcdf.a]
# INC_NETCDF := [location of netcdf fortran include file]
# DEBUG      := [TRUE or FALSE]
# MPI        := [TRUE if running parallel code]
# MSS        := [TRUE if NCAR mass store is available]
# NBIT       := [32 or 64 bit Linux machine]
#
#-----------------------------------------------------------------------
#
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.exe
endif
ifeq ($(EXECDIR),$(null))
  EXECDIR := .
endif 
ifeq ($(UTILDIR),$(null))
  UTILDIR := .
endif
#
# Default MPI is TRUE, except on hao linux systems:
#
ifeq ($(UNAMES),Linux)
  ifneq ($(UNAMEN),lightning)
    ifeq ($(MPI),$(null))
      MPI := FALSE
    endif
  endif
endif
ifeq ($(MPI),$(null))
  MPI := TRUE
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))               

#------------------------------------------------------------------------
# Default goal (force Mkdepends before making exec):
#
all: Depends 
#
# 9/6/05: MAKEFILE_LIST is empty if bluesky /usr/bin/gmake is used, 
#         but is properly defined by /usr/local/bin/gmake. For now, 
#         I have put full /usr/local/bin path in *ibm.job job scripts.
#
Depends::
	perl $(UTILDIR)/Mkdepends
	$(MAKE) -f $(MAKENAME) $(EXECDIR)/$(EXECNAME)

CURDIR   := $(shell pwd)
MAKENAME := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
SOURCES  := $(shell cat Srcfiles)
OBJS     := $(addsuffix .o, $(basename $(SOURCES)))
LIBS     := -L$(LIB_NETCDF) -lnetcdf
LDFLAGS  :=
#
# 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
# DBGFLAGS := -C -qzerosize -qinitauto=7FF7FFFF -qflttrap=zero:inv:en \
#             -qsigtrap=xl__trcedump
endif
#
# 2/5/09: For "Illegal instruction" errors on NCAR IBM bluefire, 
#         try replacing -qarch=auto with -qarch=pwr6
#
FFLAGS     := $(cpp_path) $(CPPFLAGS) $(OPTIM) $(DBGFLAGS) \
              -qarch=pwr6 -qrealsize=8 -q64
#
FREEFLAGS  := -qsuffix=f=f90:cpp=F90
FIXEDFLAGS := -qfixed
#
ifeq ($(DEBUG),FALSE)
  LIBS     += -lmass
else
  LIBS     +=          # no -lmass if debug
endif
#
# 11/24/08: Use 64-KB Paging:
LDFLAGS    += -bloadmap:loadmap -q64 $(OPTIM)
LDFLAGS    += -bdatapsize:64K -bstackpsize:64K -btextpsize:64K

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
#
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
#
 LIBS    += 
#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):
#
CPPFLAGS := -DLINUX -DMSS
ifeq ($(MPI),TRUE)
  CPPFLAGS := -DLINUX -DMPI -DMSS
  cpp_path += -I/usr/local/include
endif
OPTIM    := -O2
ifeq ($(DEBUG),TRUE)
  CPPFLAGS += -DDEBUG
  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
#
# Machine specific sets, within Linux OS:
#
# Default is pgf90 (e.g., serial linux systems at HAO),
# or mpif90 if MPI is set:
#
  FC       := pgf90
  ifeq ($(MPI),TRUE)
    FC := mpif90
  endif
  CPPFLAGS += -I/usr/local/include
  FFLAGS   := $(cpp_path) $(OPTIM) -r8
#
# 64-bit netcdf4 hdf libs:
  ifeq ($(NBIT),64)
    LIBS += -L$(LIB_NETCDF) -lhdf5_hl -lhdf5 -lsz -L/usr/lib64 -lz
  endif
#
# SCD Linux system "gale" using ifort compiler (serial):
#
  ifeq ($(UNAMEN),gale)                     # CISL/DASG Linux "gale" (no mpi)
    FC       := ifort
    FFLAGS := $(cpp_path) $(OPTIM) -r8 -fixed -diag-disable remark
# heap-arrays size is integer kilobytes
    FFLAGS += -heap-arrays 5000
    ifeq ($(DEBUG),TRUE)
      FFLAGS += -fp-stack-check
      FFLAGS += -ftrapuv
    endif
    gw_drag.o: gw_drag.F
	$(FC) -c $(FFLAGS) -free $<
  else                                 # not gale -- probably pgf90
    gw_drag.o: gw_drag.F
	$(FC) -c $(FFLAGS) -Mfreeform $<
  endif
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

