#
# Makefile for compiling test.
#
# Currently there are following kinds of tests: 
#
#    size test: produce a minimal non-executable image 
#    
#
#




# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# VARIABLES                                                                 #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

# relative path to root directory of CiAO 
CIAO_DIR := ..

# import architecture depend settings
include $(CIAO_DIR)/make/architecture.mk
# import gcc depend settings
include $(CIAO_DIR)/make/gcc.mk

# CiAO library file
CIAO_LIB := $(CIAO_DIR)/lib/$(ARCH)/libciao.so

# directory for executable testprogramm
BIN_DIR:=bin
OBJ_DIR:=obj

# Source of Test Programms
SRC_SIZE :=  $(wildcard sizes/*.cpp)
OBJ_SIZE := $(SRC_SIZE:%.cpp=$(OBJ_DIR)/$(ARCH)/%.o)
PROG_SIZE := $(SRC_SIZE:%.cpp=$(BIN_DIR)/$(ARCH)/%)

SRC_TUT := $(shell find tut -name '*.cpp')
OBJ_TUT := $(SRC_TUT:%.cpp=$(OBJ_DIR)/%.o)
PROG_TUT:= $(BIN_DIR)/unit_test



# additional CXX flags when we are compiling size test
CXXFLAGS += -I $(CIAO_DIR)/src


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# TARGETS                                                                   #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

all:  tut size 

clean: 
	rm -rf $(BIN_DIR) $(OBJ_DIR) 

#
# Targets for template unit test
# 

tut: $(PROG_TUT)

tut_run: tut
	export LD_LIBRARY_PATH="$(CXX_PATH)/../lib";\
	$(PROG_TUT)

#
# Targets for size test
# 

size: $(PROG_SIZE)

# prints a summary of .text-section size of all test applications
SIZE_LOGFILE := size.log
TIME := $(shell date +"%D;%T") 
SED_SCRIPT := '/^\s*[0-9]\+/ { s?^\s\+?$(TIME) $(ARCH) ? ; s/\s\+/;/g p }'

size_print: size
	@echo;
	@echo "ARCH=$(ARCH)"  ; 
	@echo '------------------------------------------------------------------';
	@$(SIZE) `find $(BIN_DIR)/$(ARCH) -type f`
	@echo '------------------------------------------------------------------';	 
	@echo;
	
size_store: size
	@$(SIZE) `find $(BIN_DIR) -type f` | sed -ne $(SED_SCRIPT) >> $(SIZE_LOGFILE)




# some CXXFLAGS are configuration dependend 
include $(CIAO_DIR)/make/variants.mk

# helper rule 
test/%: $(BIN_DIR)/% 



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# RULES                                                                     #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

#
# Rules for template unit test
# 

# compile template unit test application
$(OBJ_DIR)/tut/%.o: tut/%.cpp 
	@echo "(CXX_TUT) $@" 
	@if [ ! -e $(@D) ];then mkdir -p $(@D);fi
	@$(CXX) -b i686-pc-linux-gnu -I tut -I $(CIAO_DIR)/src -c $< -o $@	
	
# link template unit test application
$(BIN_DIR)/unit_test: $(OBJ_TUT) $(CIAO_LIB)
	@echo "(LD_TUT)  $@ " 
	@if [ ! -e $(@D) ];then mkdir -p $(@D);fi 
	@$(CXX) -b i686-pc-linux-gnu -lstdc++ $+  -o $@ 

#
# Rules for size tests
#

# compile size test application
$(OBJ_DIR)/$(ARCH)/%.o: %.cpp 
	@echo "(CXX_TST) $@" 
	@if [ ! -e $(@D) ];then mkdir -p $(@D);fi
	@$(CXX) $(CXXFLAGS)  -c $< -o $@
	
# link size test application
$(BIN_DIR)/$(ARCH)/%: $(OBJ_DIR)/$(ARCH)/%.o $(CIAO_LIB)
	@echo "(LD_TST)  $@ " 
	@if [ ! -e $(@D) ];then mkdir -p $(@D);fi 
	@$(CXX) $(CXXFLAGS) -o $@ $+



# ensure that intermediate .o files are not deleted
.PRECIOUS:  $(OBJ_DIR)/$(ARCH)/%.o $(BIN_DIR)/$(ARCH) /%
