##############################################################
#
# 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 := alignchk

# 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 :=

# 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 := misaligned

# 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 += movdqa_test2
    TEST_ROOTS += movdqa_test3 movdqa_test4
endif

# Linux
ifeq ($(TARGET_OS),linux)
    ifeq ($(TARGET),ia32)
        TEST_TOOL_ROOTS += movdqa_test2
        TEST_ROOTS += movdqa_test3 movdqa_test4
    endif
endif

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

# KNC does not support the movdqa instruction, therefore don't test this directory on that platform.
ifeq ($(TARGET),mic)
    TEST_TOOL_ROOTS :=
    TEST_ROOTS :=
    SANITY_SUBSET :=
    TOOL_ROOTS :=
    SA_TOOL_ROOTS :=
    APP_ROOTS :=
    OBJECT_ROOTS :=
    DLL_ROOTS :=
    LIB_ROOTS :=
endif

# These tests fail - fix and remove the following:
# Mantis 3159 - alignchk
ifeq ($(TARGET_OS),windows)
    ifeq ($(TARGET),intel64)
        TEST_TOOL_ROOTS := $(filter-out alignchk, $(TEST_TOOL_ROOTS))
    endif
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

alignchk.test: $(OBJDIR)alignchk$(PINTOOL_SUFFIX) $(OBJDIR)misaligned$(EXE_SUFFIX)
	$(RM) -f alignchk.out
	@echo This is supposed to fail and report a problem about an unaligned MOVDQA
	-$(PIN) -t $(OBJDIR)alignchk$(PINTOOL_SUFFIX) -- $(OBJDIR)misaligned$(EXE_SUFFIX) > $(OBJDIR)alignchk.out 2>&1
	$(GREP) "Misaligned MOVDQA at instruction" $(OBJDIR)alignchk.out
	$(RM) $(OBJDIR)alignchk.out

movdqa_test3.test: $(OBJDIR)movdqa_test2$(PINTOOL_SUFFIX)
	touch $(OBJDIR)$(@:.test=.makefile.copy); $(RM) $(OBJDIR)$(@:.test=.makefile.copy)
	$(PIN) -xyzzy -inline_maxlen 1 -t $(OBJDIR)movdqa_test2$(PINTOOL_SUFFIX) -- $(TESTAPP) makefile $(OBJDIR)$(@:.test=.makefile.copy)
	$(CMP) makefile $(OBJDIR)$(@:.test=.makefile.copy)
	$(RM) $(OBJDIR)$(@:.test=.makefile.copy)

movdqa_test4.test: $(OBJDIR)movdqa_test2$(PINTOOL_SUFFIX)
	touch $(OBJDIR)$(@:.test=.makefile.copy); $(RM) $(OBJDIR)$(@:.test=.makefile.copy)
	$(PIN) -xyzzy -inline 0 -t $(OBJDIR)movdqa_test2$(PINTOOL_SUFFIX) -- $(TESTAPP) makefile $(OBJDIR)$(@:.test=.makefile.copy)
	$(CMP) makefile $(OBJDIR)$(@:.test=.makefile.copy)
	$(RM) $(OBJDIR)$(@:.test=.makefile.copy)


##############################################################
#
# 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 tools' build rules ######

$(OBJDIR)movdqa_test2$(PINTOOL_SUFFIX): $(OBJDIR)movdqa_test2$(OBJ_SUFFIX) $(OBJDIR)movdqa_test2_$(TARGET)$(OBJ_SUFFIX)
	$(LINKER) $(TOOL_LDFLAGS) $(LINK_EXE)$@ $^ $(TOOL_LPATHS) $(TOOL_LIBS)

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

ifeq ($(TARGET_OS),windows)
$(OBJDIR)misaligned$(EXE_SUFFIX): misaligned.c $(OBJDIR)movdqa_test_$(TARGET)$(OBJ_SUFFIX)
	$(APP_CC) $(APP_CXXFLAGS_NOOPT) $(SSE2) $(COMP_EXE)$@ $^ $(APP_LDFLAGS_NOOPT) $(APP_LIBS)
else
$(OBJDIR)misaligned$(EXE_SUFFIX): misaligned.c
	$(APP_CC) $(APP_CXXFLAGS_NOOPT) $(SSE2) $(COMP_EXE)$@ $< $(APP_LDFLAGS_NOOPT) $(APP_LIBS)
endif
