# CONFIG (overwritable)
FC := gfortran
# to compile with support for netCDF climate input set to any value
CLIMNCDF :=

## debug friendly flags, but enable FFLAGS commandline appending
FASTFFLAGS := -cpp -w -Ofast $(FFLAGS)
override FFLAGS += -cpp -Wall -Og -fbounds-check -ffpe-trap=invalid -fbacktrace \
                   -ffree-line-length-none --debug
IFORTFFLAGS := -extend_source -ipo -O3 -no-prec-div -fp-model fast=2 -xHost $(FASTFFLAGS)
PROG := swim
## files other files depend on (in order)
DEPENDS := common.f90 mod_snow.f90 mod_water_mgt.f90 mod_reservoir.f90 mod_climncdf.f90

# SOURCES AND OBJECTS
FSRCS = ${wildcard *.f}
F90SRCS = ${filter-out $(DEPENDS), ${wildcard *.f90}}
DEPENDOBJS = ${patsubst %.f90, %.o, $(DEPENDS)}
OBJS = ${patsubst %.f, %.o, $(FSRCS)} ${patsubst %.f90, %.o, $(F90SRCS)}

ifdef CLIMNCDF
	override FFLAGS += ${shell nc-config --fflags} -Dclimncdf
	override LDFLAGS += ${shell nc-config --flibs} ${shell nc-config --libs}
endif

# TARGETS

default: $(PROG)

fast:
	$(MAKE) FFLAGS="$(FASTFFLAGS)" $(PROG)

cluster:
	$(MAKE) FC=ifort FFLAGS="$(IFORTFFLAGS)" $(PROG)

clean:
	rm -f $(PROG) *.o *.mod


# LINKING
$(PROG): $(OBJS)
	$(FC) $(FFLAGS) $(DEPENDOBJS) $(OBJS) -o $@ $(LDFLAGS)

# DEPENDENCIES
$(OBJS): $(DEPENDOBJS)

# COMPILATIONS
%.o: %.f
	$(FC) $(FFLAGS) -c $<

%.o: %.f90
	$(FC) $(FFLAGS) -c $<