executable = ../bin/runoff-mapper.exe

FC      = mpif90
FFLAGS  = -r8 -O3 -march=core-avx2 

LD      = mpif90
LDFLAGS = 

OASIS_BASE_DIR = ${HOME}/models/ecearth_3.3.3.2/sources/oasis3-mct/ecconf
OASIS_MOD_DIR  = $(OASIS_BASE_DIR)/build/lib/psmile.MPI1
OASIS_INC_DIR  = $(OASIS_BASE_DIR)/lib/psmile/include
OASIS_LIB_DIR  = $(OASIS_BASE_DIR)/lib
OASIS_LIBS     = -lpsmile.MPI1 -lmct -lmpeu -lscrip

MPI_BASE_DIR            := 
ifneq ($(strip $(MPI_BASE_DIR)),)
MPI_INC_DIR             := $(MPI_BASE_DIR)$(addprefix /,)
MPI_LIB_DIR             := $(MPI_BASE_DIR)$(addprefix /,)
endif
MPI_LIBS                := 

ifneq ($(strip /),)
NETCDF_LIB_DIR          := $(EBROOTNETCDF)/lib64 $(EBROOTNETCDFMINFORTRAN)/lib
NETCDF_INC_DIR          := $(EBROOTNETCDF)/include $(EBROOTNETCDFMINFORTRAN)/include
endif
NETCDF_LIBS             := netcdff netcdf

VPATH = cplng

cplng_srcs = \
    cplng_data_mod.F90 \
    cplng_init_mod.F90 \
    cplng_config_mod.F90 \
    cplng_exchange_mod.F90 \
    cplng_finalize_mod.F90 \
    cplng_mod.F90

rnfmap_srcs = runoff_mapper_mod.F90 runoff_mapper.F90

$(executable): config_mod.o $(cplng_srcs:.F90=.o) $(rnfmap_srcs:.F90=.o)
	$(LD) $(LDFLAGS) -o $@ $^ -L$(OASIS_LIB_DIR) $(OASIS_LIBS) \
	    $(addprefix -L,$(NETCDF_LIB_DIR))  $(addprefix -l,$(NETCDF_LIBS))  \
	    $(addprefix -L,$(MPI_LIB_DIR))     $(addprefix -l,$(MPI_LIBS))

%.o: %.F90
	$(FC) $(FFLAGS) -c -I$(OASIS_MOD_DIR) $(addprefix -I,$(NETCDF_INC_DIR)) $(addprefix -I,$(MPI_INC_DIR)) $<

.PHONY: clean
clean:
	@rm -f *.o *.mod
