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

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

EDODIR	= edos

FILES_C = test1.c \
	test2.c \
	test3.c

TARGET 	= test3.exe

DTARGET = $(BUILDDIR)/$(TARGET)

FILES_O =$(subst .c,.o,$(FILES_C))
#DFILES_O=$(addprefix $(BUILDDIR)/,$(FILES_O))
DFILES_O=$(FILES_O)

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


.SUFFIXES: .c .o .exe

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

%.exe: %.o | src build_dir
	$(CC) $(SRCDIR)/*.o $< -o $(BUILDDIR)/$@ $(LFLAGS)
	
#$(TARGET): $(FILES_O) Makefile
#	$(CC) $(SRCDIR)/*.o $(BUILDDIR)/$< -o $(BUILDDIR)/$@ $(LFLAGS)

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

.PHONY: all
all: build_dir
	-make $(FILES_EXE)
	-make -C $(EDODIR) all

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

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

.PHONY: clean 
clean:
	-rm -f $(DFILES_O) $(DTARGET) $(DFILES_EXE)
	-rm -r $(BUILDDIR)
	-make -C $(SRCDIR) clean
	-make -C $(EDODIR) clean

.PHONY: cleanobj
cleanobj:
	-rm -f $(DFILES_O)
	-make -C $(EDODIR) cleanobj
	-make -C $(SRCDIR) 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