.SUFFIXES: .c .f .f90 .F90 .o
.DEFAULT_GOAL := all

FC=mpif90
#FFLAGS=-Wall -mcmodel=large -O0 -g -fbacktrace -fcheck=all -ffpe-trap=invalid,zero,overflow,underflow,denormal 
FFLAGS=-O3 -g

OBJDIR=obj
SRCS := $(wildcard *.f90)
OBJECTS := $(addprefix $(OBJDIR)/,$(SRCS:%.f90=%.o))



#----------------- DEPENDENCIES -----------------#
# GENERATED USING https://github.com/ZedThree/fort_depend.py **requires python3**
# > pip install fortdepend
# > fortdepend -o Makefile.dep -i mpi -b obj/
include Makefile.dep

#----------------- DEFAULTS -----------------#
all: cacmb

.PHONY: deps
cacmb: $(OBJECTS) $(OBJDIR)/main.o
	$(FC) $(FFLAGS) $(OBJECTS) -o $@

$(OBJDIR)/%.o: %.f90
	@mkdir -p $(@D)
	$(FC) $(FFLAGS) -c -o $@ $< -J$(OBJDIR)

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


deps:
	@fortdepend -o Makefile.dep -i mpi -b obj -w

#----------------- CLEAN UP -----------------#

.PHONY: clean
clean:
	$(RM) *.mod *.o
	$(RM) $(OBJDIR)/*.mod $(OBJDIR)/*.o CAC
	@$(RM) -rf obj/

.PHONY: clean-all
clean-all: clean 

# DEBUGGING VARIABLE PRINT
print-%  : ; @echo $* = $($*)
