#
# Copyright (C) 2012-2025 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),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.
COMPILER ?= gcc

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

ARCHIVER := /usr/bin/ar cr 
ASMBLR := $(CC)

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

EXE_SUFFIX := .exe
OBJ_SUFFIX := .o
APP_OBJ_SUFFIX := .app.o
DLL_SUFFIX := .so
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 ######

GLOBALFUN_PREFIX :=

###### 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
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
SETARCH := $(CMD_PREFIX) setarch

ifeq ($(TARGET),ia32)
    SET_TOOL_DLL_PATH := export PIN_32_LD_LIBRARY_PATH=:$(OBJDIR);
else
    SET_TOOL_DLL_PATH := export PIN_64_LD_LIBRARY_PATH=:$(OBJDIR);
endif
# Remove the time stamp for ls to make it more deterministic
LS_VERBOSE += --time-style=+""

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
