# SYNOPSIS:
#   make [all]  - makes and runs tests.
#   make tests  - makes tests.
#   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))

COVERAGE_FLAGS += -fprofile-arcs -ftest-coverage -O0
TRICK_SYSTEM_LDFLAGS  += ${COVERAGE_FLAGS}

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

# ==================================================================================
# All tests produced by this Makefile. Add new tests you create to this list.
# ==================================================================================
TESTS = MM_creation_unittest \
        MM_declare_var_unittest \
        MM_declare_var_2_unittest \
        MM_declare_extern_var_unittest \
        MM_delete_var_unittest \
        MM_ref_attributes_unittest \
		MM_resize_array_unittest \
		MM_strdup_unittest \
        MM_write_var_unittest \
        MM_sizeof_type_unittest\
        MM_read_checkpoint\
        MM_clear_var_unittest\
        MM_alloc_deps\
        MM_write_checkpoint\
        MM_write_checkpoint_hexfloat \
		MM_get_enumerated\
		MM_ref_name_from_address \
        Bitfield_tests \
		MM_stl_checkpoint \
		MM_stl_restore \
        MM_trick_type_char_string \
		MM_JSON_Intf

# List of XML files produced by the tests.
unittest_results = $(patsubst %,%.xml,$(TESTS))

# List if Test-specific object files.
unittest_objects = $(patsubst %,%.o,$(TESTS))

# ==================================================================================
# The header files that need to be ICG'ed , and compiled for the unittests above.
# Add the header file that need to be ICG'ed here.
# ==================================================================================
io_headers = MM_user_defined_types.hh \
             MM_alloc_deps.hh \
             MM_write_checkpoint.hh \
             MM_get_enumerated.hh \
             MM_ref_name_from_address.hh \
             MM_stl_testbed.hh \
             MM_delete_var.hh

# List of .cpp files produced by ICG from the io_headers.
io_source  = $(patsubst %.hh,io_%.cpp,$(io_headers))

# List of .o files produced by  compiling the io_source files.
io_objects = $(patsubst %.cpp,%.o,$(io_source))

# ==================================================================================
# TARGETS
# ==================================================================================
all : test

test: unit_tests $(unittest_results)

unit_tests: $(TESTS)

clean :
	rm -f $(TESTS)
	rm -f *.o
	# Remove gcov/gprof files.
	rm -f *.gcno
	rm -f *.gcda
	rm -rf io_src xml

# ==================================================================================
# Generate JUNIT (XML) Test Results
# ==================================================================================
$(unittest_results): %.xml: %
	./$< --gtest_output=xml:${TRICK_HOME}/trick_test/$@

# ==================================================================================
# Build io_src objects needed by tests.
# ==================================================================================
io_source  = $(patsubst %.hh,io_%.cpp,$(io_headers))
io_objects = $(patsubst %.cpp,%.o,$(io_source))

$(io_objects): %.o: %.cpp
	$(TRICK_CXX) $(TRICK_CPPFLAGS) -c io_src/$<

$(io_source): io_%.cpp: %.hh
	${TRICK_HOME}/bin/trick-ICG -sim_services -o ./io_src $(TRICK_CPPFLAGS) $<

# ==================================================================================
# Build Unit test Objects
# ==================================================================================
$(unittest_objects): %.o: %.cc
	$(TRICK_CXX) $(TRICK_CPPFLAGS) -c $<

MM_stl_restore.o :    MM_stl_testbed.hh MM_test.hh
MM_stl_checkpoint.o : MM_stl_testbed.hh MM_test.hh

# ==================================================================================
# Build Unit test programs
# ==================================================================================
$(TESTS) : %: %.o
	$(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) -o $@ $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS)

# ----------------------------------------------------------------------------------
# The following unittest programs are also dependent on the indicated object files.
# ----------------------------------------------------------------------------------
MM_delete_var_unittest :         io_MM_delete_var.o
MM_declare_var_unittest :        io_MM_user_defined_types.o
MM_declare_var_2_unittest :      io_MM_user_defined_types.o
MM_declare_extern_var_unittest : io_MM_user_defined_types.o
MM_ref_attributes_unittest :     io_MM_user_defined_types.o
MM_resize_array_unittest :       io_MM_user_defined_types.o
MM_write_var_unittest :          io_MM_user_defined_types.o
MM_sizeof_type_unittest :        io_MM_user_defined_types.o
MM_read_checkpoint :             io_MM_user_defined_types.o
MM_clear_var_unittest :          io_MM_user_defined_types.o
MM_JSON_Intf :                   io_MM_user_defined_types.o
MM_alloc_deps :                  io_MM_alloc_deps.o
MM_write_checkpoint :            io_MM_write_checkpoint.o
MM_ref_name_from_address :       io_MM_ref_name_from_address.o
MM_get_enumerated :              io_MM_get_enumerated.o
MM_write_checkpoint_hexfloat :   io_MM_write_checkpoint.o
MM_stl_restore :                 io_MM_stl_testbed.o
MM_stl_checkpoint :              io_MM_stl_testbed.o
