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

# iutest root directory
IUTEST_DIR = ..

# Flags passed to the preprocessor.
CPPFLAGS += -I$(IUTEST_DIR)/include

# Optimize
#OPTIMIZE += -O2

#
# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra $(OPTIMIZE)

ifdef _DEBUG
CXXFLAGS += -ggdb
endif

ifeq ($(findstring clang, $(CXX)),clang)

#
# clang version
#
#

#CXXFLAGS += -std=c++0x

else

ifeq ($(CXX),g++)

#
# GCC version
#
#
GCCVERSION:=$(shell $(CXX) -dumpversion)

dot:=.
empty:=
space:=$(empty) $(empty)
GCCVERSION:=$(subst $(dot),$(space), $(GCCVERSION))
GCCMAJOR:=$(word 1, $(GCCVERSION))
GCCMINOR:=$(word 2, $(GCCVERSION))

ifeq (1,$(shell expr \( $(GCCMAJOR) \> 4 \) \| \( $(GCCMAJOR) \>= 4 \& $(GCCMINOR) \>= 8 \)))
# 4.8 later
CXXFLAGS += -std=c++1y
else
ifeq (1,$(shell expr \( $(GCCMAJOR) \> 4 \) \| \( $(GCCMAJOR) \>= 4 \& $(GCCMINOR) \>= 7 \)))
# 4.7 later
CXXFLAGS += -std=c++11
else
#ifeq (1,$(shell expr \( $(GCCMAJOR) \> 4 \) \| \( $(GCCMAJOR) \>= 4 \& $(GCCMINOR) \>= 4 \)))
# 4.4 later
#CXXFLAGS += -std=c++0x
#endif
endif
endif

else

#
# Unkown Compiler
#
#
CXXFLAGS = 

endif

endif

CXXFLAGS += $(DEFS)
LIBS_ = $(LIBS:%=-l%)
LDFLAGS += $(LIBS_)

TARGETS1= iutest_all_tests \
          iutest_assertion_return_tests \
          iutest_assume_tests \
          iutest_catch_exceptions_tests \
          iutest_disabled_tests \
          iutest_environment_tests \
          iutest_extension_tests \
          iutest_filter_tests \
          iutest_help_tests \
          iutest_japanese_tests \
          iutest_list_test_tests \
          iutest_listener_tests \
          iutest_minimum_tests \
          iutest_namespace_tests \
          iutest_no_fatalfailure_tests \
          iutest_no_test_tests \
          iutest_output_xml_tests \
          iutest_printers_tests \
          iutest_progress_tests \
          iutest_record_property_tests \
          iutest_repeat_tests \
          iutest_setup_failure_tests \
          iutest_setup_testcase_failure_tests \
          iutest_shuffle_tests \
          iutest_skip_tests \
          iutest_spi_failure_tests \
          iutest_throw_on_assert_failure_tests \
          iutest_throw_on_failure_tests \
          iutest_tr1_tests \
          iutest_uninitialize_tests

TARGETS2= \
          iutest_assertion_only_tests \
#          iutest_exception_tests

TARGETS3 = \
          iutest_env_var_tests \
          iutest_env_var_gtest_tests \

BUILD_ONLY = iutest_break_on_failure_tests \

          
ifdef USE_GTEST
RUN_TARGETS = $(TARGETS1) $(TARGETS2)
else
RUN_TARGETS = $(TARGETS1) $(TARGETS2) $(TARGETS3)
endif
BUILD_TARGETS = $(RUN_TARGETS) $(BUILD_ONLY)
TARGETS = $(BUILD_TARGETS)

SRCS   = $(BUILD_TARGETS:%=%.cpp)
RUNNER = $(RUN_TARGETS:%=run_%)


# All iutest headers.  Usually you shouldn't change this
# definition.
IUTEST_HEADERS = $(IUTEST_DIR)/include/*.hpp \
                 $(IUTEST_DIR)/include/impl/*.ipp \
                 $(IUTEST_DIR)/include/internal/*.hpp \
                 $(IUTEST_DIR)/include/listener/*.hpp \
                 $(IUTEST_DIR)/include/util/*.hpp \
                 $(IUTEST_DIR)/include/gtest/*.hpp

# House-keeping build targets.

.PHONY: clean default all run test

default : $(TARGETS)

all : clean default test

clean :
	rm -f $(TARGETS) *.o

run : test
test : $(RUNNER)

run_% : %
	@echo $<
	./$< $(RUN_OPTION)

# Builds a sample test.  

ifdef USE_GTEST

GTEST_ROOT_=$(subst \,/,$(GTEST_ROOT))
CXXFLAGS += -DIUTEST_USE_GTEST -DIUTEST_HAS_SOCKET=0

$(BUILD_ONLY) : $(SRCS) $(IUTEST_HEADERS) Makefile
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $@.cpp -I$(GTEST_ROOT_)/include $(GTEST_ROOT_)/make/gtest-all.o

$(TARGETS1) : $(SRCS) $(IUTEST_HEADERS) Makefile
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $@.cpp -I$(GTEST_ROOT_)/include $(GTEST_ROOT_)/make/gtest-all.o

$(TARGETS2) : $(SRCS) $(IUTEST_HEADERS) Makefile
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $@.cpp -I$(GTEST_ROOT_)/include

else

ifdef USE_LIB
CXXFLAGS += -DIUTEST_USE_LIB=1 -L../lib
$(TARGETS) : $(SRCS) $(IUTEST_HEADERS) Makefile
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $@.cpp -liutest $(LDFLAGS)

else

$(TARGETS) : $(SRCS) $(IUTEST_HEADERS) Makefile
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $@.cpp $(LDFLAGS)

endif

endif

