# BEGIN_ICS_COPYRIGHT8 ****************************************
#
# Copyright (c) 2015-2020, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Intel Corporation nor the names of its contributors
#       may be used to endorse or promote products derived from this software
#       without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# END_ICS_COPYRIGHT8   ****************************************
# suffixes and commands to build targets for:
# BUILD_TARGET_OS=LINUX
# BUILD_TARGET_TOOLCHAIN=GNU
# BUILD_TARGET=X86_64
# Target specific defines for building LINUX target using GNU tools

SHLIB_VERSION		?= 0.0
LIB_PREFIX			= $(BUILDDIR)/lib
SHLIB_SUFFIX		= .so
SHLIB_VERSION_SUFFIX= .so.$(SHLIB_VERSION)
ARLIB_SUFFIX		= .a
EXE_SUFFIX			= 
OBJ_SUFFIX			= .o
ifdef BUILD_26
KOBJ_SUFFIX			= .ko
else
KOBJ_SUFFIX			= .o
endif
EXP_SUFFIX			= .exp
RES_SUFFIX			= .res
PRE_SUFFIX			= .pre

# Paths:
# let compiler default inclusion of /usr/include, otherwise g++ builds broken
SYS_INCLUDE_DIRS =
SYS_LIB_DIRS	 =

CC			= gcc
CCC			= g++
LD			= ld
LINT			= lint
AR			= gcc-ar
NM			= nm
OBJCOPY			= objcopy
GZIP		= gzip

include $(TL_DIR)/Makerules/Defs.GNU

#
# Override the GNU defaults.
# 
CPIE		= -fpie
CPIC		= -fpic

# for GNU we set an soname on shared libraries
SHLIB_SONAME=$(notdir $(CMD_TARGETS_SHLIB))

# C/C++ compilation
# ----------------------------
# -fcheck-new does the opposite of what the manual implies.
# -fcheck-new DISABLES the run-time test of return value from new
CMACH		= -pipe -DTARGET_CPU_FAMILY_X86 -D__X86_64__ -DCPU_LE -m64 -fno-strict-aliasing -fstack-protector-all -fstack-clash-protection -flto
CCMACH		= -pipe -DTARGET_CPU_FAMILY_X86 -D__X86_64__ -DCPU_LE -m64 -fno-strict-aliasing -fstack-protector-all -fstack-clash-protection -flto -fcheck-new -Wno-ctor-dtor-privacy
ifeq ($(shell expr $(GCC_MAJOR_VERSION) \>= 7), 1)
ifeq ($(shell expr $(GCC_MINOR_VERSION) \>= 0), 1)
CMACH+= -std=gnu90 -Wno-misleading-indentation -Wno-format-truncation
endif
endif
ifeq ($(shell expr $(GCC_MAJOR_VERSION) \>= 8), 1)
ifeq ($(shell expr $(GCC_MINOR_VERSION) \>= 0), 1)
CMACH+= -fcf-protection=full
CCMACH+= -fcf-protection=full
endif
endif

# Additions for SDLe Sep-2023

## Compiler warnings and detection
CMACH+= -Wall -Wextra
CCMACH+= -Wall -Wextra
ifeq "$(BUILD_CONFIG)" "release"
CMACH+= -Werror
CCMACH+= -Werror
endif

## Control Flow Integrity
#CMACH+= -fsanitize=cfi						# gcc: error: unrecognized argument to '-fsanitize=' option: 'cfi'
#CCMACH+= -fsanitize=cfi					# gcc: error: unrecognized argument to '-fsanitize=' option: 'cfi'
ifeq "$(BUILD_CONFIG)" "release"
CMACH+= -flto
CCMACH+= -flto
#CMACH+= -fvisibility=hidden				# causes issues with backtrace called inside a library or tool
#CCMACH+= -fvisibility=hidden				# causes issues with backtrace called inside a library or tool
endif

## Format String Defense
CMACH+= -Wformat -Wformat-security
CCMACH+= -Wformat -Wformat-security
ifeq "$(BUILD_CONFIG)" "release"
CMACH+= -Werror=format-security
CCMACH+= -Werror=format-security
endif

## Inexecutable Stack
ifeq "$(BUILD_CONFIG)" "release"
CMACH+= -z noexecstack
CCMACH+= -z noexecstack
endif

## Position Independent Execution
CMACH+= -fPIE -pie
CCMACH+= -fPIE -pie

## Pre-processor Macros
CMACH+= -D_FORTIFY_SOURCE=2
CCMACH+= -D_FORTIFY_SOURCE=2

## Spectre Variant 1 - Mitigating Bounds Check Bypass (gcc version 18.0.3 and later)
ifeq ($(shell expr $(GCC_MAJOR_VERSION) \>= 18),1)
ifeq ($(shell expr $(GCC_MINOR_VERSION) \>= 0),1)
ifeq ($(shell expr $(GCC_MACRO_VERSION) \>= 3),1)
CMACH+= -mconditional-branch=keep -mconditional-branch=pattern-report -mconditonal-branch=pattern-fix
CCMACH+= -mconditional-branch=keep -mconditional-branch=pattern-report -mconditonal-branch=pattern-fix
endif
endif
endif

## Spectre Variant 2 - Mitigating Branch Target Injection (gcc version 18.0.3 and later)
ifeq ($(shell expr $(GCC_MAJOR_VERSION) \>= 18),1)
ifeq ($(shell expr $(GCC_MINOR_VERSION) \>= 0),1)
ifeq ($(shell expr $(GCC_MACRO_VERSION) \>= 3),1)
CMACH+= -mfunction-return=thunk -mindirect-branch=thunk -mindirect-branch-register
CCMACH+= -mfunction-return=thunk -mindirect-branch=thunk -mindirect-branch-register
endif
endif
endif

## Stack and Heap Overlap Protection
ifeq "$(BUILD_CONFIG)" "release"
CMACH+= -Wl,-z,relro,-z,now
CCMACH+= -Wl,-z,relro,-z,now
endif

## Stack Protection
ifeq "$(BUILD_CONFIG)" "debug"
CMACH+= -fstack-protector
CCMACH+= -fstack-protector
endif
ifeq "$(BUILD_CONFIG)" "release"
CMACH+= -fstack-protector-strong
CCMACH+= -fstack-protector-strong
endif

# Macro to aid marking parameters and variables as unused (to suppress warning)
CMACH+= -D_UNUSED_="__attribute__((unused))"
CCMACH+= -D_UNUSED_="__attribute__((unused))"

# Macro to suppress fallthrough warning
CMACH+= -D_FALLTHRU_="__attribute__((fallthrough))"
CCMACH+= -D_FALLTHRU_="__attribute__((fallthrough))"


# End: Additions for SDLe Sep-2023

CSYS		= -DLINUX -Dlinux -D__LINUX__
CCSYS		= $(CSYS)
CCLIBS		= 
release_CCOPT_Flags	=-O3 -fno-strength-reduce -D_FORTIFY_SOURCE=2
debug_CCOPT_Flags	= -O0
release_COPT_Flags	=-O3 -fno-strength-reduce -D_FORTIFY_SOURCE=2
debug_COPT_Flags	= -O0
# Assemble the optimization flags based upon desired build configuration.
CCOPT		= $($(strip $(BUILD_CONFIG))_CCOPT_Flags)
COPT		= $($(strip $(BUILD_CONFIG))_COPT_Flags)
release_CCDEBUG_Flags	=-g
release_CDEBUG_Flags	=-g
debug_CCDEBUG_Flags	=-g -DIB_DEBUG -DIB_TRACE $(CCPROJDEBUG) $(CCLOCALDEBUG)
debug_CDEBUG_Flags	=-g -DIB_DEBUG -DIB_TRACE $(CPROJDEBUG) $(CLOCALDEBUG)
# Assemble the debug flags based upon desired build configuration.
CCDEBUG		= $($(strip $(BUILD_CONFIG))_CCDEBUG_Flags)
CDEBUG		= $($(strip $(BUILD_CONFIG))_CDEBUG_Flags)

# Kernel build settings
#---------------------------------------------
# use CKERNEL and KERNEL_INCLUDE_DIRS to augment CLOCAL, LOCAL_INCLUDE_DIRS when
# building a kernel module

# strict-aliasing - disables optimizations related to unions/casts
# no-common - will put globals uninitialized in .bss instead of common
# 		hence get link error if multiple declaration
# preferred-stack-boundary=2 -align stack to 2^2 byte boundary,
#		possible performance penalty for 64 bit data
# 		TBD, what does kernel use
# -D_I386_PTRACE_H was a workaround for a ptrace.h header file inclusion issue
# 		identify where needed and just put in that one makefile
# -include picks up special flags for SMP and MODVERSIONS, this allows portable
# kernel modules to be built for multiple system types from a single running
# system

ifndef BUILD_26

CKERNEL=-D_KERNEL -D__KERNEL__ -DMODULE -DEXPORT_SYMTAB -mcmodel=kernel -Wno-trigraphs -mno-red-zone -fno-reorder-blocks -finline-limit=2000 -fno-strict-aliasing -fno-common -msoft-float
# omit frame pointer for simple functions
# take advantage of pentium pro instructions, default is i386 compatible code

ifeq "$(BUILD_CONFIG)" "release"
CKERNEL+= -fomit-frame-pointer -O2 -fno-strength-reduce
else
# due to extern inline virt_to_phys in io.h in kernel, we must use at least -O1
# COPT will follow CLOCAL in CFLAGS, so it can request a different level
# of optimization if needed
CKERNEL+= -O1
endif

ifeq "$(GCC_MAJOR_VERSION)" "3"
ifeq "$(GCC_MINOR_VERSION)" "3"
ifeq "$(BUILD_PLATFORM_OS_VENDOR)" "SuSE"
CKERNEL+=-fno-unit-at-a-time
endif
endif
endif

endif
CKERNEL+=-include $(TL_DIR)/Makerules/LINUX/$(BUILD_TARGET).$(BUILD_TARGET_OS_VENDOR).$(BUILD_TARGET_OS_VERSION).h

# Include directories for kernel
KERNELDIR=/lib/modules/$(BUILD_TARGET_OS_VERSION)/build
KERNEL_INCLUDE_DIRS=$(KERNELDIR)/include

# linking
# ----------------------------
LDMACH		= 
LDSYS		= -Wl,-z,noexecstack -z relro -z now
MKSHLIB		= $(CCC) $(CCDEBUG) $(COPT) -shared
ifeq "$(strip $(LIBFILES))" ""
MKARLIB		= $(AR) crus$(space)
else
MKARLIB		= $(MAKETOOLS_DIR)/mkarlib.sh $(AR) crus$(space)
endif
MKPARTIAL	= $(LD) -r -o
MKMODULE	= $(LD) -r -o

LDLIBSRCH	+= $(foreach lib,$(LD_LIB_DIRS),-Xlinker -rpath-link -Xlinker $(lib))
SYSLIBS		= -lpthread
LDLIBS		= -Xlinker --start-group $(foreach lib,$(LOCALLIBS) $(LOCALDEPLIBS) $(MODLIBS) $(MODDEPLIBS) $(PROJLIBS) $(PROJDEPLIBS),-l$(lib)) $(EXPLICITLIBS) -Xlinker --end-group $(SYSLIBS)

# libraries for partial link of a kernel loadable module
KERNELLIBS		= 
