#
# test/Makefile
#	Makefile
#
# by: Joan Gimeno
#
# This is the makefile that generates the objects and the executables
SRCDIR	=../../src
INCDIR	=$(SRCDIR)/include
BUILDDIR=../build
TAYLOR 	=taylor
DIFF 	=diff

CC 	= gcc
CFLAGS 	=-Wall -Werror -O2 -fopenmp -DDEBUG -I$(INCDIR) -I$(TAYLOR)
LFLAGS	=-lm -fopenmp

PARAVIEW = paraview
FILES_C = pendol.c \
	$(PARAVIEW).c

TARGET 	= pendol.exe

DTARGET = $(BUILDDIR)/$(TARGET)

FILES_O = $(subst .c,.o,$(FILES_C))

FILES_EXE=$(subst .c,.exe,$(FILES_C))
DFILES_EXE=$(addprefix $(BUILDDIR)/,$(FILES_EXE))

.SUFFIXES: .c .o .exe

%.o: %.c 
	$(MAKE) -C $(TAYLOR) $@ $*-$(DIFF).o
	$(CC) $(CFLAGS) -c $< -o $@

%.exe: %.o | $(PARAVIEW).o src build_dir
	$(CC) $(SRCDIR)/*.o $(TAYLOR)/$*.o $(TAYLOR)/$*-$(DIFF).o $< $(PARAVIEW).o -o $(BUILDDIR)/$@ $(LFLAGS)

$(PARAVIEW).o: $(PARAVIEW).c
	$(CC) $(CFLAGS) -c $< -o $@

# Default target executed when no arguments are given to make.
.PHONY: default_target
default_target: all

.PHONY: all
all: build_dir
	$(MAKE) $(TARGET)

.PHONY: build_dir
build_dir:
	-mkdir -p $(BUILDDIR)

.PHONY: src
src:
	$(MAKE) -C $(SRCDIR)

.PHONY: clean 
clean:
	-rm -f $(FILES_O) $(DTARGET) $(DFILES_EXE) -r $(BUILDDIR)
	$(MAKE) -C $(SRCDIR) clean
	$(MAKE) -C $(TAYLOR) clean

.PHONY: cleanobj
cleanobj:
	-rm $(FILES_O)
	$(MAKE) $(SRCDIR) cleanobj
	$(MAKE) $(TAYLOR) cleanobj


# Help Target
.PHONY : help
help:
	@echo "The following are some of the valid targets for this Makefile:"
	@echo "... all (the default if no target is provided)"
	@echo "... src"
	@echo "... clean"
	@echo "... cleanobj"

# gprof ./system
# gprof ./system | less