#-----------------------------------------------------------------------
# This Makefile is for building MOZART2 Pre-processor
#------------------------------------------------------------------------

#-----------------------------------------------------------------------
# Set up special characters
#-----------------------------------------------------------------------
null  :=
space := $(null) $(null)

#-----------------------------------------------------------------------
# Check for directory in which to put executable
#-----------------------------------------------------------------------
ifeq ($(MODEL_EXEDIR),$(null))
  MODEL_EXEDIR := ../bin
endif

#-----------------------------------------------------------------------
# Check for name of executable
#-----------------------------------------------------------------------
ifeq ($(EXENAME),$(null))
  EXENAME := moz_pp
endif

#-----------------------------------------------------------------------
# Check for source list
#-----------------------------------------------------------------------
ifeq ($(SRCLIST),$(null))
  SRCLIST := Base_Srclist_f
endif

#-----------------------------------------------------------------------
# Load dependency search path.
# Check for source directories
#-----------------------------------------------------------------------
ifeq ($(SRCDIRS),$(null))
  dirs := ./
else
  dirs := ./ $(SRCDIRS)
endif

#-----------------------------------------------------------------------
# Set cpp search path, include netcdf
#-----------------------------------------------------------------------
cpp_dirs := $(dirs)
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),$(dir)) 
VPATH    := $(subst $(space),:,$(VPATH))               

#-----------------------------------------------------------------------
# Get list of files and determine objects and dependency files
#-----------------------------------------------------------------------
base_srclist_f := $(shell cat ./$(SRCLIST))
OBJS      := $(foreach file,$(base_srclist_f),OBJ/$(file:.f=.o))

$(MODEL_EXEDIR)/$(EXENAME):	$(OBJS)
	$(FC) -o $@ $(OBJS)

.SUFFIXES:
.SUFFIXES: .f .F .c .o

OBJ/%.o : %.f
	$(FC) $(FFLAGS) -o $@ $<

all: $(MODEL_EXEDIR)/$(EXENAME)

#======================================================
# Architecture-specific flags and rules
#======================================================
#------------------------------------------------------------------------
# AIX ?
#------------------------------------------------------------------------
ifeq ($(OP_SYS),AIX)

  objflag    = -o $@
  FC          := xlf95
  FFLAGS      := -g -c -qarch=auto -qnosave -qfree=f90 -qmoddir=./OBJ -I./OBJ -O3 -qstrict

endif

#------------------------------------------------------------------------
# Intel Pentium ? ; default to Portland Group compiler
#------------------------------------------------------------------------
ifeq ($(OP_SYS),LINUX)

  ifeq ($(COMPILER),Lahey)
    FC        := lf95
    ifneq ($(DEBUG),ON)
      FFLAGS    := --nfix --nap --nchk --ng --npca --nsav --ntrace -c --mod ./OBJ -O
    else
      FFLAGS    := --nfix --nap --nchk --g --npca --nsav --trace --trap -c --mod ./OBJ -O
    endif
  endif

  ifeq ($(COMPILER),PGI)
    FC        := pgf90
    FFLAGS    := -g -O2 -c -Mfree -module ./OBJ
  endif

  ifeq ($(COMPILER),Intel)
    FC        := ifort
    FFLAGS    := -g -O -c -module ./OBJ -free -extend_source
  endif

endif

#------------------------------------------------------------------------
# Default rules
#------------------------------------------------------------------------
RM := rm

clean:
	$(RM) -f *.o $(MODEL_EXEDIR)/$(EXENAME)

realclean:
	$(RM) -f *.o *.d $(MODEL_EXEDIR)/$(EXENAME)
