#Edit Makefile_compiler to change compiler options

FORUTILS_SRC_DIR ?= .

ifeq ($(HASINCLUDED),)
include $(FORUTILS_SRC_DIR)/Makefile_compiler
HASINCLUDED=T
endif

ifeq ($(MPIF90C),)
all: Release Debug
else
all: Release Debug ReleaseMPI DebugMPI
endif

# export all variables to the sub-make below
export

SRCS = MiscUtils.f90 MpiUtils.f90 StringUtils.f90 ArrayUtils.f90 FileUtils.f90 \
	   IniObjects.f90 RandUtils.f90 ObjectLists.f90 MatrixUtils.f90 RangeUtils.f90 \
	   Interpolation.f90

OBJS = $(patsubst %.f90,%.o,$(SRCS))

Release:
	@$(MAKE) OUTPUT_DIR=Release F90FLAGS="$(F90RELEASEFLAGS)" $(MAKEOPT) directories

Releaselib:
	@$(MAKE) OUTPUT_DIR=Releaselib $(MAKEOPT) directories

ReleaseMPI:
	@$(MAKE) OUTPUT_DIR=ReleaseMPI F90C="$(MPIF90C)" F90FLAGS="$(F90RELEASEFLAGS) -DMPI" $(MAKEOPT) directories

Debug:
	@$(MAKE) OUTPUT_DIR=Debug F90FLAGS="$(F90DEBUGFLAGS)" $(MAKEOPT) directories

Debuglib:
	@$(MAKE) OUTPUT_DIR=Debuglib $(MAKEOPT) directories

DebugMPI:
	@$(MAKE) OUTPUT_DIR=DebugMPI F90C="$(MPIF90C)" F90FLAGS="$(F90DEBUGFLAGS) -DMPI" $(MAKEOPT) directories

cur_ver := $(shell cat compiler.ver 2>&1 || echo "")
ifneq ($(cur_ver),$(compiler_ver))
$(shell rm -f compiler.ver *.d)
endif

compiler.ver:
	@echo $(compiler_ver) > compiler.ver
	@rm -f *.d

%.o: $(FORUTILS_SRC_DIR)/%.f90 compiler.ver
	$(F90C) $(F90COMMONFLAGS) $(F90FLAGS) -o $*.o -c $<

libforutils.a: $(OBJS) | silent
	$(AREXE) -r libforutils.a $(OBJS)

clean:
	-rm -fr Debug* Release*

silent:
	@:

# Build in output dir to get file dependencies correctly and make use
# of delta builds, where only changed files and their dependents are
# rebuild.
directories:
	@mkdir -p $(OUTPUT_DIR)
	$(MAKE) -C $(OUTPUT_DIR) $(MAKEOPT) -f../Makefile FORUTILS_SRC_DIR=.. libforutils.a

# Include dependency files generated by a previous run of make
-include $(SRCS:.f90=.d)

.PHONY: directories clean Release Debug ReleaseMPI DebugMPI Releaselib Debuglib

#Avoid problems with intel gen-dep
%.h: ;
