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

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

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

# Mac
ifeq ($(TARGET_OS),mac)
    TEST_TOOL_ROOTS += thcnt bundle_tool 
    ifeq ($(TARGET),intel64)
        TEST_ROOTS += section_offset
    endif
    TOOL_ROOTS += section_tool
    APP_ROOTS += wqtimer libdep bundle_app section_app
    DLL_ROOTS += mylib bundlebundle 
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

thcnt.test: $(OBJDIR)thcnt$(PINTOOL_SUFFIX) $(OBJDIR)wqtimer$(EXE_SUFFIX)
	$(PIN) -t $(OBJDIR)thcnt$(PINTOOL_SUFFIX) -- $(OBJDIR)wqtimer$(EXE_SUFFIX)

bundle.test: $(OBJDIR)bundle_tool$(PINTOOL_SUFFIX) $(OBJDIR)bundle_app$(EXE_SUFFIX) $(OBJDIR)$(DLL_PREFIX)bundlebundle$(DLL_SUFFIX)
	$(PIN) -t $(OBJDIR)bundle_tool$(PINTOOL_SUFFIX) -o $(OBJDIR)bundle_tool.out -- $(OBJDIR)bundle_app$(EXE_SUFFIX) \
          $(OBJDIR)$(DLL_PREFIX)bundlebundle$(DLL_SUFFIX)
	$(QGREP) "_my_function" $(OBJDIR)bundle_tool.out

ifeq ($(KIT),1)
    # When running from the kit, set the current directory.
    LIBDEP_DEFS = DYLD_LIBRARY_PATH=.
else
    # not in kit, set pin shell definitions
    LIBDEP_DEFS = PIN_APP_DYLD_LIBRARY_PATH=. PIN_DYLD_RESTORE_REQUIRED=t
endif

libdep.test: $(OBJDIR)libdep$(EXE_SUFFIX) $(OBJDIR)libmylib$(DLL_SUFFIX)
	cd $(OBJDIR) ; env $(LIBDEP_DEFS) $(PIN) -- $(OBJDIR)libdep$(EXE_SUFFIX)

#Special test against a library that is loaded into memory and requests a specific offset.
# This caused an issue with how we calculate section offsets.
# The library exists on all OS X* supported versions
section_offset.test: $(OBJDIR)section_tool$(PINTOOL_SUFFIX) $(OBJDIR)section_app$(EXE_SUFFIX)
	$(PIN) -t $(OBJDIR)section_tool$(PINTOOL_SUFFIX) -o $(OBJDIR)section_offset.out -- $(OBJDIR)section_app$(EXE_SUFFIX) \
	  /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Metadata



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

# wqtimer is using __block, always use /usr/bin/gcc for it
$(OBJDIR)wqtimer$(EXE_SUFFIX): wqtimer.c
	/usr/bin/gcc $(APP_CXXFLAGS) $(COMP_EXE)$@ $< $(APP_LDFLAGS) $(APP_LIBS)

$(OBJDIR)libdep$(EXE_SUFFIX): libdep.c $(OBJDIR)$(DLL_PREFIX)mylib$(DLL_SUFFIX)
	$(APP_CC) $(APP_CXXFLAGS) $(COMP_EXE)$@ $^ $(APP_LDFLAGS) $(APP_LIBS)

$(OBJDIR)bundle_app$(EXE_SUFFIX): bundle_app.c
	$(APP_CC) $(APP_CXXFLAGS) $(COMP_EXE)$@ $^ $(APP_LDFLAGS) $(APP_LIBS)

###### Special dlls' build rules ######

$(OBJDIR)$(DLL_PREFIX)mylib$(DLL_SUFFIX): mylib.c
	$(APP_CC) $(APP_CXXFLAGS) $(DLL_CXXFLAGS) $(COMP_EXE)$@ $< $(APP_LDFLAGS) $(DLL_LDFLAGS) $(APP_LIBS)

# Because it is not exactly a dylib, the naming convention should be .bundle, but this would require
# more modifications to the makefile system than required. The resulting binary still has the MH_BUNDLE bit on.
$(OBJDIR)$(DLL_PREFIX)bundlebundle$(DLL_SUFFIX): bundle_bundle.c
	$(APP_CC) -flat_namespace -bundle -undefined suppress $(COMP_EXE)$@ $< 
