#
# Copyright (C) 2012-2021 Intel Corporation.
# SPDX-License-Identifier: MIT
#

##############################################################
#
# This file contains general variable definitions used by the PinTools makefiles.
# See makefile.unix.config for an explanation of each variable defined in this file.
#
##############################################################

###### Architecture ######

# Identify the architecture of the host
ifndef HOST_ARCH
    HST := $(shell  uname -m)
    ifeq ($(HST),x86)
        HOST_ARCH := ia32
    endif
    ifeq ($(HST),i386)
        HOST_ARCH := ia32
    endif
    ifeq ($(HST),i686)
        HOST_ARCH := ia32
    endif
    ifeq ($(HST),x86_64)
        HOST_ARCH := intel64
    endif
    ifeq ($(HST),amd64)
        HOST_ARCH := intel64
    endif

    # Verify known host
    ifndef HOST_ARCH
        $(error Could not detect the host architecture. Please define HOST_ARCH from the command line.)
    endif
endif

# Define the architecture of the target
TARGET ?= $(HOST_ARCH)
ifeq ($(TARGET),ia32)
    BITS := 32
else
    BITS := 64
endif

# Define the output directory
OBJDIR := obj-$(TARGET)/

# Seperator for list of paths
PATHS_SEPERATOR := :

###### Operating system ###### 

# Identify the operating system
ifndef HOST_OS
    OS := $(shell uname -s)
    ifeq ($(OS),Darwin)
        HOST_OS := mac
    endif
    ifeq ($(OS),Linux)
        HOST_OS := linux
    endif
endif
TARGET_OS ?= $(HOST_OS)

# Verify known operating system
ifndef TARGET_OS
    $(error Could not identify the host operating system. Please define TARGET_OS from the command line.)
endif

# Define the general OS type
OS_TYPE := unix

###### Program definitions ######

# COMPILER may have already been defined during the build so don't override it with the hardcoded defaults below.
ifdef ICCPIN
    COMPILER ?= icc
endif
ifeq ($(TARGET_OS),mac)
    COMPILER ?= clang
endif
COMPILER ?= gcc

ifeq ($(ICC),1)
    # This makefile assumes that the iccvars script has been run. So if the user didn't
    # specify a full path to CC and CXX, we override them now and let the PATH set by the
    # iccvars script to find the appropriate compiler.
    ifeq ($(origin CC), default)
        CC := icc
        CXX := icpc
    endif
endif

# If the native compiler and binutils are too new, we must link the tools with older
# versions of binutils to test tools in probe mode. To do that we put their directory
# in the front of the PATH environment variable.
ifdef BINUTILS_PATH
    LINKER := env PATH=$(BINUTILS_PATH):$(PATH) $(CXX)
else
    LINKER := $(CXX)
    APP_LINKER := $(APP_CXX)
endif

ARCHIVER := /usr/bin/ar cr 

ifeq ($(TARGET_OS),mac)
    ASMBLR := $(CXX)
else
    ASMBLR := $(CC)
endif

###### File extensions ######

EXE_SUFFIX := .exe
OBJ_SUFFIX := .o
DLL_SUFFIX := .so
ifeq ($(TARGET_OS),mac)
    # macOS* has a different suffix for dlls, so override DLL_SUFFIX.
    DLL_SUFFIX := .dylib
endif
PINTOOL_SUFFIX := $(DLL_SUFFIX)
SATOOL_SUFFIX := .exe
LIB_SUFFIX := .a
ASM_SUFFIX := .s
DLL_PREFIX := lib
DLL_LINKAGE_SUFFIX := $(DLL_SUFFIX)
DBG_SUFFIX :=

###### Function pre/suffixes ######

ifeq ($(TARGET_OS),mac)
    # The function naming convention on mac is to have all public functions start with '_'
    GLOBALFUN_PREFIX := _
else
    GLOBALFUN_PREFIX :=
endif

###### Attach support ######

# Some linux security modules make attach mode unusable
ifeq ($(TARGET_OS),linux)
    ifeq ($(shell cat /proc/sys/kernel/yama/ptrace_scope 2>&1),1)
        ATTACH := 0
    endif
endif
ATTACH ?= 1

###### Additional utilities ######

RM := $(CMD_PREFIX) rm
MV := $(CMD_PREFIX) mv
SYNC := $(CMD_PREFIX) sync
PYTHON := $(CMD_PREFIX) python3
DIFF := $(CMD_PREFIX) diff -w
# macOS* has a different compare program.
# It is overridden in makefile.unix.config since it depends on TOOLS_ROOT which is defined in that file.
CMP := $(CMD_PREFIX) cmp
CMD :=
SH := $(CMD_PREFIX) sh
TR := $(CMD_PREFIX) tr
GREP := $(CMD_PREFIX) grep
QGREP := $(CMD_PREFIX) grep -q
CGREP := $(CMD_PREFIX) grep -c
EGREP := $(CMD_PREFIX) egrep
PGREP := $(CMD_PREFIX) pgrep
LINECOUNT := $(CMD_PREFIX) wc -l
BASHTEST := $(CMD_PREFIX) test
TOUCH := $(CMD_PREFIX) touch
STRIP := /usr/bin/strip
CP := $(CMD_PREFIX) cp
OBJCOPY := $(CMD_PREFIX) objcopy
LS_VERBOSE := $(CMD_PREFIX) ls -l
READLINK := $(CMD_PREFIX) readlink

ifeq ($(TARGET_OS),mac)
    SETARCH := $(CMD_PREFIX) arch
    SET_TOOL_DLL_PATH := export PIN_VM_DYLD_LIBRARY_PATH=:$(OBJDIR);
else
    SETARCH := $(CMD_PREFIX) setarch
    ifeq ($(TARGET),ia32)
        SET_TOOL_DLL_PATH := export PIN_VM32_LD_LIBRARY_PATH=:$(OBJDIR);
    else
        SET_TOOL_DLL_PATH := export PIN_VM64_LD_LIBRARY_PATH=:$(OBJDIR);
    endif
    # Remove the time stamp for ls to make it more deterministic
    LS_VERBOSE += --time-style=+""
endif

SORT := $(CMD_PREFIX) sort
SED := $(CMD_PREFIX) sed
EXPR := $(CMD_PREFIX) expr
UNAME := $(CMD_PREFIX) uname
AWK := $(CMD_PREFIX) awk
XARGS := $(CMD_PREFIX) xargs
SUDO := $(CMD_PREFIX) sudo
CHOWN := $(CMD_PREFIX) chown
CHMOD := $(CMD_PREFIX) chmod
