# These definitions are generated by the kit builder
KIT := 1
TARGET_OS := windows
NEWCPP := 1
#
# Copyright (C) 2012-2023 Intel Corporation.
# SPDX-License-Identifier: MIT
#

##############################################################
#
# This file defines (indirectly via include files) all the variables necessary for building tools and running tests.
# A detailed list of the variables defined in each file is available below.
#
# In addition, this files defines the common make targets.
# 
# All aforementioned variables can be overridden from the command line, however it is not recommended to do so.
# In order to safely obtain more control over the build process, the user may define the following variables in
# the command line:
#
# PIN_ROOT      - Specify the location for the Pin kit when building a tool outside of the kit.
# CC            - Override the default c compiler for tools.
# CXX           - Override the default c++ compiler for tools
# APP_CC        - Override the default c compiler for applications and any additional object or shared object files.
#                 If not defined, APP_CC will be the same as CC.
# APP_CXX       - Override the default c++ compiler for applications and any additional object or shared object files.
#                 If not defined, APP_CXX will be the same as CXX.
# TARGET        - Override the default target architecture e.g. for cross-compilation.
# ICC           - When ICC=1 is specified, the test binaries will be built will be done with the Intel compiler.
# GCCVER        - On Linux, icc requires some utilities from the gcc toolchain. This specifies which gcc version to use.
#                 If a version is not specified, the default system libraries and compiler will be used.
#                 Relevant only on Linux when ICC=1.
# BINUTILS_PATH - Define a path to a different set of binutils for the linker. Relevant on Unix only.
# DEBUG         - When DEBUG=1 is specified, debug information will be generated and no optimizations will be performed.
# PIN_TESTING   - When PIN_TESTING=1 is specified, Pin will be invoked with additional flags which will add some internal
#                 testing. Note that this may incur a performance penalty.
# PINFLAGS      - Use this to add Pin flags when running the tests. Usage: make PINFLAGS=<extra flags> ...
# INSTALLER     - Specify a script for adding functionality to the "install" target. The script will run after the entire
#                 build completes successfully.
# INSTALL_FLAGS - Specify command line arguments to be passed to the installer script (see above).
# CMD_PREFIX    - A prefix which will be added to all executed shell commands.
# TEST_PREFIX   - A prefix which will be added to all executed tests.
# INSTALL_PREFIX- A prefix which will be added to all install commands (e.g. tools, apps) 
#
##############################################################


##############################################################
#
# The following variables are defined in win.vars / unix.vars:
#
# HOST_ARCH             - Architecture of the host system. One of: ia32/intel64.
# TARGET                - Architecture of the target system. This is usually the same as the host.
# BITS                  - Pointer size (in bits) of this architecture. One of 32/64.
# OBJDIR                - The output directory for the test rules.
# TARGET_OS             - Operating system of the target.
# OS_TYPE               - One of: unix/win.
# COMPILER              - The compiler used to build Pin. Some source trees' names depend on it. One of: gcc/icc/clang/msvc.
# LINKER                - Linker.
# ARCHIVER              - Static library (archive) builder. Should be followed by the requested archive name.
# ASMBLR                - Assembler.
# EXE_SUFFIX            - Extension of executables (apps).
# OBJ_SUFFIX            - Extension of an object file.
# DLL_SUFFIX            - Extension of a shared object file (dll).
# PINTOOL_SUFFIX        - Extension of a tool.
# SATOOL_SUFFIX         - Extension of static analysis tools.
# LIB_SUFFIX            - Extension of a static library (archive).
# ASM_SUFFIX            - Extension of asm source files (common convention).
# DLL_PREFIX            - Prefix to a shared object's name. This is only relevant on Unix platforms. The prefix is "lib" by
#                         common convention.
# DLL_LINKAGE_SUFFIX    - When linking an executable to a DLL, gcc/clang expect the .so file, and VS expects the .lib file.
# PYTHON                - Define the python executable.
# DIFF                  - Diff application.
# CMP                   - Compare application used by the default test rule.
# CMD                   - Creates a temporary shell for the following command. Relevant on Windows only.
# GREP                  - The grep command.
# QGREP                 - Quiet grep command - output is suppressed.
# CGREP                 - Count number of matching lines.
# EGREP                 - Use the grep command with regex support.
# LINECOUNT             - Count the number of lines in the given file.
# BASHTEST              - Equivalent of the bash "test" command.
# STRIP                 - Equivalent of the "strip" program.
# SED                   - Equivalent of the "sed" program.
# EXPR                  - Equivalent of the "expr" program.
# AWK                   - Equivalent of the "awk" program.
# SET_DLL_PATH          - Exports the LD_LIBRARY_PATH to point to OBJDIR. Relevant on Unix only.
# SUDO                  - Sudo utiliy in Unix
# CHOWN                 - Chown utility in Unix
# CHMOD                 - Chmod utility in Unix
#
##############################################################


##############################################################
#
# The following variables are defined in makefile.win.config / makefile.unix.config:
#
# ## Output control
#
# COMP_OBJ              - Flags for compiling an object file. Should be followed by the requested output object's name.
# COMP_EXE              - Flags for building an executable from source and object files. Should be followed by the requested
#                         output executable's name.
# LINK_EXE              - Flags for linking an executable from object files. Should be followed by the requested output
#                         executable's name.
#
# ## Special compilation/linkage directives
#
# STATIC                - Linker directive for building a static executable.
# NO_RANDOM             - Linker directive which disables voluntary image relocation. Relevant only on Windows since VS2010.
# PIC                   - Compiler directive for generating position independent code. Irrelevant on Windows.
# NO_PIC                - Compiler directive for generating a non-position-independent code. Irrelevant on Windows.
# PIE                   - Linker directive for generating a position independent executable. Irrelevant for Windows.
# NO_PIE                - Linker directive for building a non-position-independent execuatble. Irrelevant on Windows.
# LDFLAGS_DEFAULT_NO_PIE- Linker directive for building a non-position-independent execuatble, or empty string
#                         for linkers that build non-position-independent execuatble by default.
# ATTACH		- Perform tests that require Pin attach feature. Relevant only on Linux.
#                         Unless a value is provided by the user, this option's value is auto-detected.
# SSE2                  - Compiler directive for generating sse2 instructions.
# FP387                 - Compiler directive for generating x87 legacy fp code.
# ASMCPP                - Specifies that the following files' language is assembly with embedded c++. It is included in
#                         ASM_FLAGS (see below). Relevant on Unix platforms.
# NO_STDLIBS            - Do not link with any of the standard libraries.
# EXPORT                - A function that creates a directive to export a symbol. Relevant only on windows.
#
# ## Include paths
#
# COMPONENT_INCLUDES    - Include paths for additional Pin components. This is added to the tools' build rules automatically.
#
# ## Library paths
#
# APP_LPATHS            - Defines the locations of the additional Pin components.
# TOOL_LPATHS           - Defines the locations of the shared libraries needed to link a tool.
# SATOOL_LPATHS         - Defines the locations of the shared libraries needed to link a static analysis tool.
# CXX_LPATHS            - Defines the locations of c++-specific shared libraries.
#
## Libraries to link
#
# APP_LIBS              - Defines some shared objects that may be needed by an application. On Unix platforms this includes
#                         the pthread library.
# DL_LIB                - This defines the library which implements dynamic loading APIs (dl/thr). Relevant only on Unix.
# APP_LIB_ATOMIC        - Link the application with Pin's atomic library. To use this, one must add $(COMPONENT_INCLUDES) to
#                         the compilation rules and $(APP_LPATHS) to the link rule.
# APP_LIB_XED           - Link the application with Xed. To use this, one must add $(COMPONENT_INCLUDES) to the compilation
#                         rules and $(APP_LPATHS) to the link rule.
# TOOL_LIBS             - Defines the libraries needed by the tool.
# SATOOL_LIBS           - Defines the libraries needed by static analysis tools.
# CXX_LIBS              - Defines c++-specific libraries needed by some applications and tools.
#
## Compiler flags
#
# APP_CXXFLAGS          - Application's / object's compiler options. This may include optimization and debugging flags
#                         depending on whether DEBUG=1 was specified.
# APP_CXXFLAGS_NOOPT    - Application's / object's compiler options with no optimizations regardless of the DEBUG flag.
# ASM_FLAGS             - Application's / object's assembler options.
# TOOL_CXXFLAGS         - Tool's compiler options. This may include optimization and debugging flags depending on whether
#                         DEBUG=1 was specified.
# SATOOL_CXXFLAGS       - TOOL_CXXFLAGS for static analysis tools.
# TOOL_CXXFLAGS_NOOPT   - Tool's compiler options with no optimizations regardless of the DEBUG flag.
# SATOOL_CXXFLAGS_NOOPT - TOOL_CXXFLAGS_NOOPT for static analysis tools.
# DLL_CXXFLAGS          - Flags for compiling a dll. Comes in addition to to other compiler flags like APP_CXXFLAGS.
# ENABLE_DEPRECATED     - Using this flag will suppress compiler warnings when using deprecated Pin APIs.
#
## Linker flags
#
# APP_LDFLAGS           - Application's linker options.
# APP_LDFLAGS_NOOPT     - Application's linker options with no optimizations.
# APP_LDFLAGS_LINK_ASM  - Application's linker options when linking a single object which originated from an assembly
#                         source file.
# TOOL_LDFLAGS          - Tool's linker options.
# TOOL_LDFLAGS_NOOPT    - Tool's linker options with no optimizations.
# SATOOL_LDFLAGS        - Static analysis tool's linker options. Relevant on Unix only.
# SATOOL_LDFLAGS_NOOPT  - Static analysis tool's linker options with no optimizations. Relevant on Unix only.
# DLL_LDFLAGS           - Flags for linking a dll. Comes in addition to to other linker flags like APP_LDFLAGS.
#
## Debugging flags
#
# DBG_INFO_CXX          - If DEBUG=1, this variable holds compiler flags for generating debug information. Automatically
#                         included in all the compilation flags e.g. TOOL_CXXFLAGS_NOOPT, APP_CXXFLAGS, etc).
# DBG_INFO_LD           - Same as DBG_INFO_CXX but for linking.
# DBG_INFO_CXX_ALWAYS   - When this is specified in the compilation rule, debug information will be generated regardless of
#                         the value of the DEBUG variable. This is currently disabled on Windows due to some issues with
#                         cygwin/ssh.
# DBG_INFO_LD_ALWAYS    - Same as DBG_INFO_CXX_ALWAYS but for linking.
# DWARF4                - Produce debugging information in DWARF version 4 format. Relevant only on Linux.
#
# ## General
#
# PIN_ROOT              - Path to the Pin root directory.
# TOOLS_ROOT            - Path to the Pin tools root directory.
# PIN                   - Pin launcher. When PIN_TESTING=1, this may be followed by some additional flags.
# PINBIN                - The architecture-specific Pin executable. Depends on the value of the TARGET variable.
# PIN32                 - The 32 bit Pin executable.
# PIN64                 - The 32 bit Pin executable.
# PIN_TESTFLAGS         - Additional arguments to Pin when PIN_TESTING=1.
# MAKE_TESTFLAGS        - Additional arguments passed to "make" when running the .test recipes. See makefile.default.rules
#                         for usage.
# INSTALLER             - Default installer (see details at the beginning of this files).
# INSTALL_FLAGS         - Default installer flags (see details at the beginning of this files).
#
# The following variables are defined at the bottom of this file:
#
# APP_CC                - The applications' c compiler. This enables compiling tools and apps with different compilers.
# APP_CXX               - The applications' c++ compiler. This enables compiling tools and apps with different compilers.
# TESTAPP               - Test application used in the default test rule.
# HELLO_APP             - Hello world application which is used by several tests.
# HELLO_APP_DBG_INFO    - Hello world application's debug info, to be used with -dwarf_file. Relevant on Unix only. 
# HELLO_APP_DWARF4      - Hello world application which is compiled with dwarf4.
# DISABLE_ASLR          - Program that executes another program with ASLR disabled.
# ATTACH_LAUNCHER       - Generic utility for running an application and attaching Pin to it.
# THREAD_APP            - Simple application that creates multiple threads that do some work.
# INSCOUNT_TOOL         - Basic instruction counting tool, essentially the Pintool equivalent of "hello world".
# CHECKAVX              - Small program which checks the hardware for avx support.
# CHECKAVX2             - Small program which checks the hardware for avx2 support.
# CHECKTSX              - Small program which checks the hardware for tsx support.
# CHECKAVX512F          - Small program which checks the hardware for avx512 foundation support.
# CHECKAMX              - Small program which checks the hardware and OS for amx support.
# CHECKMOVDIR64B        - Small program which checks the hardware and OS for support of movdir64b instruction.
# THREADLIB             - Thread utilities library used by applications.
# SUPPORTS_AVX_OBJ      - Object file which contains a function for checking avx support. This is used by several files from
#                          a few different directories.
# SUPPORTS_AVX2_OBJ     - Object file which contains a function for checking avx2 support.
# SUPPORTS_AVX512F_OBJ  - Object file which contains a function for checking avx512 foundation support.
# PYCOMPARE             - A comparison script written in python.
#
# The following make targets are defined at the bottom of this file:
#
# all   - Rule for building all the binaries specified in a per-directory makefile.rules file. The make program uses the
#         first rule not preceded by a period "." as the default target. Since we want the "all" target to be the default,
#         it must be specified here (before the per-directory makefile.rules file is called). All the other common make
#         targets are defined in makefile.default.rules.
#
# The following variables and make targets are defined in makefile.default.rules:
#
# dir           - Rule for creating the output directory. Depends on the value of the TARGET variable.
# obj-<arch>    - Rule for building an output directory for a specific architecture.
# tools         - Builds all the tools in the directory.
# apps          - Builds all the applications in the directory.
# objects       - Builds all the objects which were explicitly specified in the directory.
# dlls          - Builds all the dlls in the directory.
# libs          - Builds all the libraries (archives) in the directory.
# avxcheck      - Builds a program which checks for avx support.
# avx2check     - Builds a program which checks for avx2 support.
# tsxcheck      - Builds a program which checks for tsx support.
# avx512fcheck  - Builds a program which checks for avx512f support.
# amxcheck      - Builds a program which checks for amx support.
# install       - Builds everything in the directory and runs the install script where applicable.
# test          - Runs all the tests in the directory.
# sanity        - Runs the sanity subset of tests in the directory.
# serial        - Runs the subset of tests that should run serially. This must be run with make -j1
# clean         - Cleanup.
# 
# $(OBJDIR)%$(PINTOOL_SUFFIX) - Default rule for building tools.
#    Example: make obj-intel64/mytool.so
#
# $(OBJDIR)% - Default rule for building applications.
#    Example: make obj-intel64/myapp
#
# %.test - Rule for running the default test template.
#    Example: make mytest.test
#
##############################################################


##############################################################
#
# Include files
#
##############################################################

ifeq ($(OS),Windows_NT)
    include $(CONFIG_ROOT)/win.vars
    include $(CONFIG_ROOT)/makefile.win.config
else
    include $(CONFIG_ROOT)/unix.vars
    include $(CONFIG_ROOT)/makefile.unix.config
endif

##############################################################
#
# Variable definitions
#
##############################################################

# Define the applications' compiler
APP_CC := $(CC)
APP_CXX := $(CXX)
APP_LINKER := $(APP_CXX)

ifeq ($(OS),Windows_NT)
	# CC and CXX are defined here to make sure that the default on Windows is cl 
	# regardless of what compiler is used to build the pintools.
	APP_CC := cl
	APP_CXX := cl
	APP_LINKER := link
endif

# Define the common test applications, utilities and Pintools
TESTAPP := $(TOOLS_ROOT)/Utils/$(OBJDIR)cp-pin$(EXE_SUFFIX)
HELLO_APP := $(TOOLS_ROOT)/Utils/$(OBJDIR)hello$(EXE_SUFFIX)
ifeq ($(TARGET_OS),linux)
    HELLO_APP_DBG_INFO := hello$(EXE_SUFFIX):$(HELLO_APP)
endif
ifeq ($(TARGET_OS),mac)
    # On macOS* the debug info is located in an external file
    HELLO_APP_DBG_INFO := hello$(EXE_SUFFIX):$(HELLO_APP).dSYM/Contents/Resources/DWARF/hello$(EXE_SUFFIX)
endif
HELLO_APP_DWARF4 := $(TOOLS_ROOT)/Utils/$(OBJDIR)hello_dwarf4$(EXE_SUFFIX)
DISABLE_ASLR := $(TOOLS_ROOT)/Utils/$(OBJDIR)disable-aslr$(EXE_SUFFIX)
ATTACH_LAUNCHER := $(TOOLS_ROOT)/Utils/$(OBJDIR)attachLauncher$(EXE_SUFFIX)
THREAD_APP := $(TOOLS_ROOT)/Utils/$(OBJDIR)thread_app$(EXE_SUFFIX)
INSCOUNT_TOOL := $(TOOLS_ROOT)/ManualExamples/$(OBJDIR)inscount0$(PINTOOL_SUFFIX)

# Define the HW check utilities
CHECKAVX := $(TOOLS_ROOT)/Utils/$(OBJDIR)avx_check$(EXE_SUFFIX)
CHECKAVX2 := $(TOOLS_ROOT)/Utils/$(OBJDIR)avx2_check$(EXE_SUFFIX)
CHECKTSX := $(TOOLS_ROOT)/Utils/$(OBJDIR)tsx_check$(EXE_SUFFIX)
CHECKAVX512F := $(TOOLS_ROOT)/Utils/$(OBJDIR)avx512f_check$(EXE_SUFFIX)
CHECKAMX := $(TOOLS_ROOT)/Utils/$(OBJDIR)amx_check$(EXE_SUFFIX)
CHECKMOVDIR64B := $(TOOLS_ROOT)/Utils/$(OBJDIR)movdir64b_check$(EXE_SUFFIX)

# Define the thread utilities library
THREADLIB := $(TOOLS_ROOT)/Utils/$(OBJDIR)threadlib$(OBJ_SUFFIX)

# Define the regval utilities library
REGVALLIB := $(TOOLS_ROOT)/Utils/$(OBJDIR)regvallib$(LIB_SUFFIX)

# Define the runnable utilities library
RUNNABLE := $(TOOLS_ROOT)/Utils/$(OBJDIR)runnable$(OBJ_SUFFIX)

# Define the thread_pool utilities library
THREADPOOL := $(TOOLS_ROOT)/Utils/$(OBJDIR)thread_pool$(OBJ_SUFFIX)

# Define the sys_memory utilities library
SYSMEMORY := $(TOOLS_ROOT)/Utils/$(OBJDIR)sys_memory$(OBJ_SUFFIX)

# Define the controller utilities library
CONTROLLERLIB := $(TOOLS_ROOT)/InstLib/$(OBJDIR)controller$(LIB_SUFFIX)

# Common object files
SUPPORTS_AVX_OBJ := $(TOOLS_ROOT)/Utils/$(OBJDIR)supports_avx$(OBJ_SUFFIX)
SUPPORTS_AVX2_OBJ := $(TOOLS_ROOT)/Utils/$(OBJDIR)supports_avx2$(OBJ_SUFFIX)
SUPPORTS_AVX512F_OBJ := $(TOOLS_ROOT)/Utils/$(OBJDIR)supports_avx512f$(OBJ_SUFFIX)
SUPPORTS_MOVDIR64B_OBJ := $(TOOLS_ROOT)/Utils/$(OBJDIR)supports_movdir64b$(OBJ_SUFFIX)

ifeq ($(TARGET), intel64)
SUPPORTS_AMX_OBJ := $(TOOLS_ROOT)/Utils/$(OBJDIR)supports_amx_intel64$(OBJ_SUFFIX)
endif

SET_XMM_SCRATCHES_OBJ := $(TOOLS_ROOT)/Utils/$(OBJDIR)set_xmm_scratches_asm$(OBJ_SUFFIX)

# Python comparison script
PYCOMPARE := $(PYTHON) $(TOOLS_ROOT)/Utils/compare.py

# A set of targets that may have additional implicit dependencies. 'clean' target for example is not one of them.  
ifeq ($(MAKECMDGOALS),) # If no target was specified, "all" is assumed.
    TARGET_HAS_DEPENDENCIES := all
else
    TARGET_HAS_DEPENDENCIES := $(filter test sanity install all objects libs dlls apps tools %.test %.wrap $(OBJDIR)%, \
                               $(MAKECMDGOALS))
endif

##############################################################
#
# Default make target
#
##############################################################

# Build everything.
all: dir
	$(INSTALL_PREFIX) $(MAKE) objects
	$(INSTALL_PREFIX) $(MAKE) libs
	$(INSTALL_PREFIX) $(MAKE) dlls
	$(INSTALL_PREFIX) $(MAKE) apps
	$(INSTALL_PREFIX) $(MAKE) tools

# Accelerate the make process and prevent errors.
.PHONY: all
