# specify the target file
TARGET = covrt2d
# directories containing source files
DIRS = . src
# include path
INCPATH = 
# the build directory
BUILD = build
# the FORTRAN dependency file
FDEPS := $(BUILD)/Depends
# libraries that need linked
LIBS = 
# library paths
LIBPATH = 
# dynamic libraries that need linked
DLIBS = 
# extra files to include in archive
EXTRA_FILES = Makefile fortdeps.pl

# file endings used in this project

# C++
SRCCPP = .cpp
OBJCPP = .o
HCPP = .h
DEPCPP = .d

# C
SRCC = .c
OBJC = .o
HC = .h
DEPC = .d

# FORTRAN 90
SRCF90 = .f90
OBJF90 = .o

# the compilers
CPP = g++
CPPFLAGS = -Wall

CC = gcc
CFLAGS = -Wall

# F90 = pgf90
# F90FLAGS = -module $(BUILD) -mp
# 
# ifeq (debug,$(filter debug,$(CONFIG)))
#   CPPFLAGS = -g -Wall
#   CFLAGS = -g -Wall
#   F90FLAGS := $(F90FLAGS) -g 
#   LDFLAGS = 
# endif

F90 = ifort
F90FLAGS = -module $(BUILD) 

# if config is set to openmp turn on openmp
ifeq (openmp,$(filter openmp,$(CONFIG)))
  F90FLAGS := $(F90FLAGS) -openmp -openmp-report1 -override-limits -parallel -par-report1
endif

# if config is set to debug turn on debugging
ifeq (debug,$(filter debug,$(CONFIG)))
  CPPFLAGS = -g -Wall
  CFLAGS = -g -Wall
  F90FLAGS := $(F90FLAGS) -g -traceback -CB 
  LDFLAGS = 
endif

# build a list of the source files
F90_SRCS := $(foreach DIR, $(DIRS), $(wildcard $(DIR)/*$(SRCF90)))

# build a list of the object files
F90_OBJS := $(addprefix $(BUILD)/, $(F90_SRCS:$(SRCF90)=$(OBJF90)))

# RULES

.PHONY : all clean deps dirs print archive
all: $(TARGET)

$(TARGET): dirs $(F90_OBJS)
	@ echo linking $(TARGET)
	@ $(F90) $(F90FLAGS) $(LDFLAGS) -o $(TARGET) $(F90_OBJS)

$(BUILD)/%$(OBJF90): %$(SRCF90)
	@ echo building $@
	@ $(F90) $(F90FLAGS) -c $< -o $@

dirs:
	@- if [ ! -e $(BUILD) ]; then mkdir $(BUILD); fi;
	@- $(foreach DIR,$(DIRS), if [ ! -e $(BUILD)/$(DIR) ]; then mkdir $(BUILD)/$(DIR); fi;)

archive:
	@ tar -zcf $(TARGET)_`date +%Y-%m-%d_%H%M%S`.tgz $(F90_SRCS) $(EXTRA_FILES)

$(FDEPS) deps: 
	@- if [ ! -e $(BUILD) ]; then mkdir $(BUILD); fi;
	@ ./fortdeps.pl -prefix $(BUILD) $(F90_SRCS) -o $(FDEPS)

clean:
	@- rm -rf build $(TARGET)

print:	
	@echo F90_SRCS = $(F90_SRCS)
	@echo F90_OBJS = $(F90_OBJS)

-include $(FDEPS)
