##############################################################
#
# This file includes all the test targets as well as all the
# non-default build rules and test recipes.
#
##############################################################


##############################################################
#
# Test targets
#
##############################################################

###### Place all generic definitions here ######

# This defines tests which run tools of the same name.  This is simply for convenience to avoid
# defining the test name twice (once in TOOL_ROOTS and again in TEST_ROOTS).
# Tests defined here should not be defined in TOOL_ROOTS and TEST_ROOTS.
TEST_TOOL_ROOTS := icount filter alarm_icount alarm_symbol control alarm_image_offset control_detach

# This defines the tests to be run that were not already defined in TEST_TOOL_ROOTS.
TEST_ROOTS :=

# This defines a list of tests that should run in the "short" sanity. Tests in this list must also
# appear either in the TEST_TOOL_ROOTS or the TEST_ROOTS list.
# If the entire directory should be tested in sanity, assign TEST_TOOL_ROOTS and TEST_ROOTS to the
# SANITY_SUBSET variable in the tests section below (see example in makefile.rules.tmpl).
SANITY_SUBSET := icount control

# This defines the tools which will be run during the the tests, and were not already defined in
# TEST_TOOL_ROOTS.
TOOL_ROOTS :=

# This defines the static analysis tools which will be run during the the tests. They should not
# be defined in TEST_TOOL_ROOTS. If a test with the same name exists, it should be defined in
# TEST_ROOTS.
# Note: Static analysis tools are in fact executables linked with the Pin Static Analysis Library.
# This library provides a subset of the Pin APIs which allows the tool to perform static analysis
# of an application or dll. Pin itself is not used when this tool runs.
SA_TOOL_ROOTS :=

# This defines all the applications that will be run during the tests.
APP_ROOTS :=

# This defines any additional object files that need to be compiled.
OBJECT_ROOTS :=

# This defines any additional dlls (shared objects), other than the pintools, that need to be compiled.
DLL_ROOTS :=

# This defines any static libraries (archives), that need to be built.
LIB_ROOTS :=

###### Place OS-specific definitions here ######

# Android
ifeq ($(TARGET_OS),android)
    TEST_TOOL_ROOTS += follow_child
    TEST_ROOTS += marker_test int3_test multi_start_stop_test
    APP_ROOTS += itext-marker-test int3-test multi-start-stop-test
endif

# Linux
ifeq ($(TARGET_OS),linux)
    TEST_TOOL_ROOTS += follow_child
    TEST_ROOTS += marker_test int3_test multi_start_stop_test
    APP_ROOTS += itext-marker-test int3-test multi-start-stop-test
    ifeq ($(TARGET),intel64)
        # mt3_test has problems on old linux runtimes where thread stack 
        # is not aligned as the compiler assumes it should be aligned.
        # Restricting testing to 64b systems.
        TEST_ROOTS += mt3_test
        APP_ROOTS += mt3-test
    endif
endif

# Mac
ifeq ($(TARGET_OS),mac)
    TEST_TOOL_ROOTS += follow_child
    TEST_ROOTS += marker_test int3_test multi_start_stop_test_mac mt3_test
    APP_ROOTS += itext-marker-test int3-test multi-start-stop-test mt3-test
endif

###### Handle exceptions here ######

# TODO: These tests fail - fix and remove the following:
# See Mantis 2963
ifeq ($(TARGET),mic)
    TEST_ROOTS := $(filer-out int3_test marker_test, $(TEST_ROOTS))
endif


##############################################################
#
# Test recipes
#
##############################################################

# This section contains recipes for tests other than the default.
# See makefile.default.rules for the default test rules.
# All tests in this section should adhere to the naming convention: <testname>.test

marker_test.test: $(OBJDIR)itext-marker-test$(EXE_SUFFIX) $(OBJDIR)control$(PINTOOL_SUFFIX)
	$(PIN) -t $(OBJDIR)control$(PINTOOL_SUFFIX) -controller-start-itext 0f1ff3 -controller-stop-itext 0f1ff4 \
	  -- $(OBJDIR)itext-marker-test$(EXE_SUFFIX) > $(OBJDIR)marker_test.out 2>&1
	$(QGREP) -E '0x.*Start' $(OBJDIR)marker_test.out
	$(QGREP) -E '0x.*Stop' $(OBJDIR)marker_test.out
	$(RM) $(OBJDIR)marker_test.out

int3_test.test: $(OBJDIR)int3-test$(EXE_SUFFIX) $(OBJDIR)control$(PINTOOL_SUFFIX)
	$(PIN) -t $(OBJDIR)control$(PINTOOL_SUFFIX) -controller-start-int3 2 -controller-stop-int3 3 \
	  -- $(OBJDIR)int3-test$(EXE_SUFFIX) > $(OBJDIR)int3_test.out 2>&1
	$(QGREP) -E '0x.*Start' $(OBJDIR)int3_test.out
	$(QGREP) -E '0x.*Stop' $(OBJDIR)int3_test.out
	$(RM) $(OBJDIR)int3_test.out

multi_start_stop_test.test: $(OBJDIR)multi-start-stop-test$(EXE_SUFFIX) $(OBJDIR)control$(PINTOOL_SUFFIX)
	$(PIN) -t $(OBJDIR)control$(PINTOOL_SUFFIX) -controller-start-address mark1 -controller-stop-address foo \
	  -controller-start-address mark2 -controller-stop-address bar \
	    -- $(OBJDIR)multi-start-stop-test$(EXE_SUFFIX) > $(OBJDIR)multi_start_stop_test.out 2>&1
	test `$(CGREP) -E '0x.*Start' $(OBJDIR)multi_start_stop_test.out` -eq 2
	test `$(CGREP) -E '0x.*Stop' $(OBJDIR)multi_start_stop_test.out` -eq 2
	$(RM) $(OBJDIR)multi_start_stop_test.out

# On OS X*, the convention is that function names are prefixed with an underscore
# This is the exact same test as above, only with the fixed function names 
multi_start_stop_test_mac.test: $(OBJDIR)multi-start-stop-test$(EXE_SUFFIX) $(OBJDIR)control$(PINTOOL_SUFFIX)
	$(PIN) -t $(OBJDIR)control$(PINTOOL_SUFFIX) -controller-start-address _mark1 -controller-stop-address _foo \
	  -controller-start-address _mark2 -controller-stop-address _bar \
	    -- $(OBJDIR)multi-start-stop-test$(EXE_SUFFIX) > $(OBJDIR)multi_start_stop_test.out 2>&1
	test `$(CGREP) -E '0x.*Start' $(OBJDIR)multi_start_stop_test.out` -eq 2
	test `$(CGREP) -E '0x.*Stop' $(OBJDIR)multi_start_stop_test.out` -eq 2
	$(RM) $(OBJDIR)multi_start_stop_test.out

mt3_test.test: $(OBJDIR)mt3-test$(EXE_SUFFIX) $(OBJDIR)control$(PINTOOL_SUFFIX)
	$(PIN) -t $(OBJDIR)control$(PINTOOL_SUFFIX) -controller-start-ssc-mark 44332211 -controller-stop-ssc-mark 55332211 \
	  -- $(OBJDIR)mt3-test$(EXE_SUFFIX) > $(OBJDIR)mt3_test.out 2>&1
	$(QGREP) -E '0x.*Start' $(OBJDIR)mt3_test.out
	$(QGREP) -E '0x.*Stop' $(OBJDIR)mt3_test.out
	$(RM) $(OBJDIR)mt3_test.out


##############################################################
#
# Build rules
#
##############################################################

# This section contains the build rules for all binaries that have special build rules.
# See makefile.default.rules for the default build rules.

###### Special applications' build rules ######

$(OBJDIR)multi-start-stop-test$(EXE_SUFFIX): multi-start-stop-test.c
	$(APP_CC) $(APP_CXXFLAGS_NOOPT) $(COMP_EXE)$@ $< $(APP_LDFLAGS_NOOPT) $(APP_LIBS)

$(OBJDIR)mt3-test$(EXE_SUFFIX): test-mt3.cpp
	$(APP_CXX) $(APP_CXXFLAGS) $(SSE2) $(COMP_EXE)$@ $< $(APP_LDFLAGS) $(APP_LIBS)
