#makefile Betti Oesterholz

ifdef debug
   DEBUG_FLAG:=-DDEBUG=$(debug)
endif


#directory wher the application which should be tested is
DIR_APPLICATION:=../
DIR_TEST_OBJ:=./obj/
DIR_TESTCASE:=./testcase/
DIR_TEST_CODE:=./code/


SHELL:=/bin/sh
CC:=gcc
#debugg flags
CFLAG:=-g -Wall $(DEBUG_FLAG)
#optimize compile flags
#CFLAG:=-O -O2
#CFLAG:=-O -O2 -O3
LIBS:=-lstdc++
INCL:=-I $(DIR_APPLICATION)

APPLICATION_OBJ:=$(DIR_APPLICATION)cUniversTools.o $(DIR_APPLICATION)cUniversNeib.o\
	$(DIR_APPLICATION)nReadValues.o $(DIR_APPLICATION)cRandomMersenne.o

APPLICATION_CODE:=$(DIR_APPLICATION)cUniversTools.cpp $(DIR_APPLICATION)cUniversNeib.cpp \
	$(DIR_APPLICATION)cPoint.cpp $(DIR_APPLICATION)cPointD.cpp $(DIR_APPLICATION)cPointP.cpp \
	$(DIR_APPLICATION)nReadValues.cpp $(DIR_APPLICATION)cRandomMersenne.cpp $(DIR_APPLICATION)cKeyboard.cpp

# test of wich application 
ifndef TEST
    TEST:=univers
endif

UNIVERS_OBJ:=$(DIR_APPLICATION)cUnivers.o $(DIR_APPLICATION)cPoint.o $(COMMON_OBJ)
UNIVERS_D_OBJ:=$(DIR_APPLICATION)cUniversD.o $(DIR_APPLICATION)cPointD.o $(COMMON_OBJ)
UNIVERS_P_OBJ:=$(DIR_APPLICATION)cUniversP.o $(DIR_APPLICATION)cPointP.o $(COMMON_OBJ)

ifeq "$(TEST)" "universD"
    APPLICATION_OBJ+=$(UNIVERS_D_OBJ)
else
ifeq "$(TEST)" "universP"
    APPLICATION_OBJ+=$(UNIVERS_P_OBJ)
else
 # $(TEST) univers
    APPLICATION_OBJ+=$(UNIVERS_OBJ)
endif
endif

ifdef HOME
# if linux create cKeyboard-object
APPLICATION_OBJ+=$(DIR_APPLICATION)cKeyboard.o
endif

TEST_PURE_EXE:=tSaveStore tcPoint tcUniversEqual tcUniversToolsSetOp
# add the testcasepath befor the pure Testexecutebels
TEST_EXE:=$(foreach EXE, $(TEST_PURE_EXE), $(addprefix $(DIR_TESTCASE),$(EXE) ) )

# add the testobjectpath befor the pure Testexecutebels and add an ".o" behind
TEST_OBJ:=$(foreach EXE, $(TEST_PURE_EXE), \
	$(addprefix $(DIR_TEST_OBJ), $(addsuffix .o, $(EXE) ) ) )
# add the testcodepath befor the pure Testexecutebels and add an ".cpp" behind
TEST_CODE:=$(foreach EXE, $(TEST_PURE_EXE), \
	$(addprefix $(DIR_TEST_CODE),$(addsuffix .cpp, $(EXE) ) ) )

all: $(TEST_PURE_EXE)

#Pseudorules
.PHONY: all clean $(TEST_PURE_EXE)


###########################################################
#
# rules to create the test applications objects
#
###########################################################

# call $(call creat_pure_exe_dependencies,EXE_PURE)
define creat_pure_exe_dependencies
    $1:$(DIR_TESTCASE)$1
endef

$(foreach EXE, $(TEST_PURE_EXE), $(eval $(call creat_pure_exe_dependencies,$(EXE) )) )


$(DIR_TEST_OBJ)%.o: $(DIR_TEST_CODE)%.cpp
	$(CC) $(CFLAG) $(INCL) -c $< -o $@

$(DIR_TESTCASE)%: $(DIR_TEST_OBJ)%.o $(APPLICATION_OBJ)
	$(CC) $(CFLAG) $(LDFLAGS) -o $@ $^ $(LIBS)

###########################################################
#
# rules to create the original application objects
#
###########################################################


$(DIR_APPLICATION)%.o: $(DIR_APPLICATION)%.cpp
	$(CC) $(CFLAG) -c $< -o $@

#create objects
$(DIR_APPLICATION)cUnivers.o: CFLAG+= -DUNIVERS
$(DIR_APPLICATION)cUnivers.o: $(DIR_APPLICATION)cUnivers.cpp $(DIR_APPLICATION)cUnivers.h

$(DIR_APPLICATION)cUniversD.o: CFLAG+= -DUNIVERS_D
$(DIR_APPLICATION)cUniversD.o: $(DIR_APPLICATION)cUnivers.cpp $(DIR_APPLICATION)cUnivers.h
	$(CC) $(CFLAG) -c $< -o $@

$(DIR_APPLICATION)cUniversP.o: CFLAG+= -DUNIVERS_P
$(DIR_APPLICATION)cUniversP.o: $(DIR_APPLICATION)cUnivers.cpp $(DIR_APPLICATION)cUnivers.h
	$(CC) $(CFLAG) -c $< -o $@


###########################################################
#
# create dependencies
#
###########################################################

test.deps: $(TEST_CODE) $(APPLICATION_CODE)
	( for i in $(TEST_CODE) $(APPLICATION_CODE) ; do $(CPP) $(CFLAG) $(INCL) -MM $$i ; done ) | \
		sed 's#^\([a-zA-Z0-9_]\+\.o\):#$(OBJ)/\1:#g' > test.deps



include test.deps


###########################################################
#
# clean rules
#
###########################################################

clean:
	rm -f $(DIR_APPLICATION)*.o $(DIR_OBJ)*.o $(DIR_CODE)*~ *~
