#
# am 08/2017 this is NOT the original tiegcm2.0 Makfile
#  it was modified to also use it for the new NSF/NCAR machine cheyenne 
#  
# On Input:
#   File Srcfiles contains list of source files
#   File $(MAKE_MACHINE) (e.g. generic_hao64) contains compiler specific Make vars
#     F90: f90 compiler (e.g., pgf90, ifort, etc)
#     MPIF90: f90 compiler for MPI jobs (e.g., mpif90)
#     FFLAGS: compiler flags (e.g., -r8)
#     OPTIM: optimization flag (e.g., -O3)
#     DBGFLAGS: Debug flags
#     INC_NETCDF: Include directory for netcdf
#     LIB_NETCDF: Library directory for netcdf
#   File Make.env contains additional information from the job script
#     MAKE_MACHINE: File containing machine-specific compiler flags, etc          
#     DIRS:     List of source-code directories (in Make.env)
#     MPI:      TRUE or FALSE
#     EXECNAME: Name of executable to make
#     DEBUG:    TRUE or FALSE
#
SHELL = /bin/sh

all: Depends prereq

include Make.env         # made by job script before gmake
include $(MAKE_MACHINE)  # machine-specific file with compiler/make info

null     :=
comma    := $(null),$(null)
space    := $(null) $(null)

OS        := $(shell uname -s)
VPATH     := $(subst $(space),:,$(DIRS))
INC_DIRS  := $(foreach dir,$(DIRS),-I$(dir)) -I$(INC_NETCDF)
SOURCES   := $(shell cat Srcfiles)
OBJS      := $(addsuffix .o, $(basename $(SOURCES)))
# LIBS may be specified in Make.machine file, so append here
LIBS      += -L$(LIB_NETCDF) -lnetcdff
COUPLING  := NO
FC        := $(F90)

# Add ESMF stuff (LIB_ESMF is set in MAKE_MACHINE)
# See also targets below for source files that depend on esmf.
ifeq ($(MPIRUN),mpiexec_mpt)
LIBS += -lesmf
else
LIBS += -L $(LIB_ESMF) -lesmf
endif


include $(LIB_ESMF)/esmf.mk
INC_DIRS += $(ESMF_F90COMPILEPATHS)
LDFLAGS := $(ESMF_F90LINKOPTS) $(ESMF_F90LINKPATHS) $(ESMF_F90LINKRPATHS) $(ESMF_F90ESMFLINKLIBS) $(LDFLAGS)
#
# With gfortran compiler, esmf compiler is mpifort instead of mpif90,
# so force it to use $(MPIF90), which is set by Make.gfort_hao64.
#
ifeq ($(F90),/usr/bin/gfortran)
  ESMF_F90COMPILER = $(MPIF90)
  @echo "set ESMF_F90COMPILER = $(ESMF_F90COMPILER)"
endif
# On cheyenne set esmf compiler to mpif90
ifeq ($(MPIRUN),mpiexec_mpt)
  ESMF_F90COMPILER = $(MPIF90)
endif
#
Depends::
	@echo "OS           =$(OS)" 
	@echo "MAKE_MACHINE =$(MAKE_MACHINE)"
	@echo "DIRS         =$(DIRS)"
	@echo "INC_DIRS     =$(INC_DIRS)"
	@echo "VPATH        =$(VPATH)"
	@echo "FC           =$(FC)"
	@echo "MPI          =$(MPI)"
	@echo "LIB_NETCDF   =$(LIB_NETCDF)"
	@echo "INC_NETCDF   =$(INC_NETCDF)"
	@echo "EXECNAME     =$(EXECNAME)"
	@echo "DEBUG        =$(DEBUG)"
	perl mkdepends $(DIRS)
	$(MAKE) $(EXECNAME)
#
# When creating a release, this should be hard-coded
# to the SVN repository release number
svn_revision.inc:
	@echo "Making svn_revision.inc: svn_revision = ${SVN_REVISION}"
	@echo "! This file is auto-generated by the TIEGCM build process" >$@
	@echo "      character(len=16) :: svn_revision = '${SVN_REVISION}'" >>$@

nchist.F:  $(shell rm -f svn_revision.inc) svn_revision.inc

$(EXECNAME): $(OBJS)
	$(FC) -o $@ $(OBJS) $(LIBS) $(LDFLAGS)

CPPFLAGS := $(null)
ifeq ($(MPI),TRUE)
  CPPFLAGS += -DMPI
  FC := $(MPIF90)
endif
ifeq ($(COUPLING), INTERCOMM)
  CPPFLAGS += -DINTERCOMM
endif
ifeq ($(COUPLING), ADHOC)
  CPPFLAGS += -DCISMAH
endif
ifeq ($(DEBUG),TRUE)
  CPPFLAGS += -DDEBUG
  OPTIM    := -g
else
  DBGFLAGS := $(null)
endif
ifeq ($(OS),Linux)
  CPPFLAGS := -DLINUX $(CPPFLAGS)
endif
ifeq ($(OS),AIX)
  CPPFLAGS := -WF -DAIX$(CPPFLAGS)
  CPPFLAGS := $(subst $(space),$(comma),$(CPPFLAGS))
endif

FFLAGS += $(INC_DIRS) $(OPTIM) $(DBGFLAGS)

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

.F90.o:
	$(FC) -c $(FFLAGS) $(CPPFLAGS) $<

esmf.o: esmf.F
	$(ESMF_F90COMPILER) -c $(ESMF_F90COMPILEOPTS) $(ESMF_F90COMPILEPATHS) \
	$(ESMF_F90COMPILECPPFLAGS) $(FIXEDFLAGS) $(CPPFLAGS) $(FFLAGS) $<
pdynamo.o: pdynamo.F
	$(FC) -c $(FFLAGS) $(CPPFLAGS) $(ESMF_F90COMPILEPATHS) $<
tgcm.o: tgcm.F
	$(FC) -c $(FFLAGS) $(CPPFLAGS) $(ESMF_F90COMPILEPATHS) $<
util.o: util.F
	$(FC) -c $(FFLAGS) $(CPPFLAGS) $(ESMF_F90COMPILEPATHS) $<

clean:
	rm -f Depends Srcfiles *.o *.mod $(EXECNAME)
veryclean:
	rm -f Depends Srcfiles *.o *.mod $(EXECNAME) Makefile Make.env $(MAKE_MACHINE) machines.ini mkdepends defs.h cwd fort*

include Depends