
#SYNOPSIS:
#
#   make [all]  - makes everything.
#   make TARGET - makes the given target.
#   make clean  - removes all files generated by make.

include $(dir $(lastword $(MAKEFILE_LIST)))../../../../share/trick/makefiles/Makefile.common

# Replace -isystem with -I so ICG doesn't skip Trick headers
TRICK_SYSTEM_CXXFLAGS := $(subst -isystem,-I,$(TRICK_SYSTEM_CXXFLAGS)) -Wno-unused-command-line-argument

# Flags passed to the preprocessor.
TRICK_CXXFLAGS += -I$(GTEST_HOME)/include -I$(TRICK_HOME)/include -g -Wall -Wextra -Wno-sign-compare -std=c++11 ${TRICK_SYSTEM_CXXFLAGS} ${TRICK_TEST_FLAGS}
TRICK_LIBS =  -L${TRICK_LIB_DIR} -ltrick_mm -ltrick_units -ltrick_comm -ltrick_pyip -ltrick -ltrick_mm -ltrick_units -ltrick_comm -ltrick_pyip -ltrick -ltrick_var_binary_parser -ltrick_connection_handlers -ltrick_comm 
TRICK_EXEC_LINK_LIBS += -L${GTEST_HOME}/lib64 -L${GTEST_HOME}/lib -lgtest -lgtest_main -lgmock -lpthread					  

# All tests produced by this Makefile.  Remember to add new tests you
# created to the list.

VARIABLE_REFERENCE_TESTS = VariableReference_test \
		VariableReference_writeValueAscii_test \
		VariableReference_writeValueBinary_test

VARIABLE_SESSION_TESTS = VariableServerSession_test 

TESTS = $(VARIABLE_REFERENCE_TESTS) $(VARIABLE_SESSION_TESTS) VariableServerSessionThread_test VariableServerListenThread_test VariableServer_test

TEST_OBJS = $(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(TESTS)))

TO_ICG = VariableReference_test 

ICG_OBJS = $(addprefix $(OBJ_DIR)/io_, $(addsuffix .o, $(TO_ICG)))

OBJ_DIR = obj

print:
	echo $(ICG_OBJS)

# House-keeping build targets.

all : test

test: $(TESTS)
	for TEST in $(TESTS) ; do \
		./$$TEST --gtest_output=xml:${TRICK_HOME}/trick_test/$$TEST.xml ; \
	done

$(OBJ_DIR):
	mkdir $(OBJ_DIR)

$(TEST_OBJS): $(OBJ_DIR)/%.o: %.cc $(OBJ_DIR)
	$(TRICK_CXX) $(TRICK_CXXFLAGS) -c $< -o $@ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS)

$(ICG_OBJS): $(OBJ_DIR)/io_%.o : %.hh $(OBJ_DIR)
	${TRICK_HOME}/bin/trick-ICG -sim_services -o ./io_src $(TRICK_CXXFLAGS) $<
	$(TRICK_CXX) $(TRICK_CXXFLAGS) -c io_src/io_$*.cpp -o $@ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS)

$(VARIABLE_REFERENCE_TESTS): %: $(OBJ_DIR)/%.o $(OBJ_DIR)/io_VariableReference_test.o
	$(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) $(TRICK_CXXFLAGS) -o $@ $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS)

$(VARIABLE_SESSION_TESTS): %: $(OBJ_DIR)/%.o 
	$(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) $(TRICK_CXXFLAGS) $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS) -o $@

VariableServerSessionThread_test: %: $(OBJ_DIR)/%.o 
	$(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) $(TRICK_CXXFLAGS) -o $@ $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS)

VariableServerListenThread_test: %: $(OBJ_DIR)/%.o 
	$(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) $(TRICK_CXXFLAGS) -o $@ $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS)

VariableServer_test: %: $(OBJ_DIR)/%.o 
	$(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) $(TRICK_CXXFLAGS) -o $@ $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS)


code-coverage: test
	# Give rid of any old code-coverage HTML we may have.
	rm -rf lcov_html
	# Gather coverage information about the src code.
	lcov --capture \
	--directory ../object_${TRICK_HOST_CPU} \
	--base-directory ../ \
	--output-file src_coverage.info
	# Filter out information about directories that we don't care about.
	lcov --remove src_coverage.info '/Applications/*' '/usr/include/*' '/Library/*' \
	--output-file VariableServer_code_coverage.info
	# Generate HTML
	genhtml VariableServer_code_coverage.info \
	--output-directory lcov_html
	# Clean up
	# rm *.info
	lcov --list VariableServer_code_coverage.info

clean :
	rm -f $(TESTS)
	rm -f *.gcno *.gcda
	rm -rf io_src xml obj