##############################################################################
#
#    Copyright (c) 2005 - 2021 by Vivante Corp.  All rights reserved.
#
#    The material in this file is confidential and contains trade secrets
#    of Vivante Corporation. This is proprietary information owned by
#    Vivante Corporation. No part of this work may be disclosed,
#    reproduced, copied, transmitted, or used in any way for any purpose,
#    without the express written permission of Vivante Corporation.
#
##############################################################################

################################################################
# Arch.

ARCH_TYPE           ?= arm64
CPU_TYPE            ?= cortex-a55
CPU_ARCH            ?=
ABI                 ?=
ENDIANNESS          ?=
FPU                 ?=
FLOAT_ABI           ?=

gcdSTATIC_LINK      ?= 0

  CROSS_COMPILE     ?= aarch64-linux-gnu-
  CC                = $(CROSS_COMPILE)gcc
  CXX               = $(CROSS_COMPILE)g++
  AR                = $(CROSS_COMPILE)ar
  AS                = $(CROSS_COMPILE)as
  LD                = $(CROSS_COMPILE)ld
  RANLIB            = $(CROSS_COMPILE)ranlib
  STRIP             = $(CROSS_COMPILE)strip

PKG_CONFIG          ?= $(CROSS_COMPILE)pkg-config


################################################################
# Resource.

VIVANTE_SDK_DIR     ?= $(AQROOT)
VIVANTE_SDK_INC     ?= $(VIVANTE_SDK_DIR)/include
VIVANTE_SDK_LIB     ?= $(VIVANTE_SDK_DIR)/lib/$(ARCH_TYPE)/$(SOC_BOARD)

################################################################
# Target directory.

ifeq ($(DEBUG),1)
  OBJ_DIR           ?= bin_d
else
  OBJ_DIR           ?= bin_r
endif

###############################################################
# Common flags.
LDFLAGS += $(LFLAGS)
ifneq ($(ROOTFS),)
LDFLAGS += --sysroot=$(ROOTFS)
endif

ifneq (,$(findstring -mcpu=,$(CC) $(CFLAGS)))
CPU_TYPE=0
CPU_ARCH=0
endif

  ifneq ($(ABI),)
    ifneq ($(ABI),0)
      CFLAGS        += -mabi=$(ABI)
    endif
  endif

  ifneq ($(ENDIANNESS),)
    CFLAGS          += $(ENDIANNESS)
#   LDFLAGS         += $(ENDIANNESS)
    PFLAGS          += $(ENDIANNESS)
  endif

  CFLAGS            += -DLINUX
  CFLAGS            += -Wall -D_REENTRANT -fno-strict-aliasing

  ifneq ($(CPU_TYPE),)
    ifneq ($(CPU_TYPE),0)
      CFLAGS        += -mtune=$(CPU_TYPE)
#     LDFLAGS       += -mtune=$(CPU_TYPE)
      PFLAGS        += -mtune=$(CPU_TYPE)
    endif
  endif

  ifneq ($(CPU_ARCH),)
    ifneq ($(CPU_ARCH),0)
      CFLAGS        += -march=$(CPU_ARCH)
#     LDFLAGS       += -march=$(CPU_ARCH)
      PFLAGS        += -march=$(CPU_ARCH)
    endif
  endif

ifneq ($(FPU),)
  CFLAGS            += -mfpu=$(FPU)
  CXXFLAGS          += -mfpu=$(FPU)
endif

ifneq ($(FLOAT_ABI),)
  CFLAGS            += -mfloat-abi=$(FLOAT_ABI)
  CXXFLAGS          += -mfloat-abi=$(FLOAT_ABI)
# LDFLAGS           += -mfloat-abi=$(FLOAT_ABI)
  PFLAGS            += -mfloat-abi=$(FLOAT_ABI)
endif

ifeq ($(DEBUG),1)
# CFLAGS            += -g3 -ggdb3 -O0 -DDEBUG -D_DEBUG -DgcdDEBUG=1
  CFLAGS            += -g -O1 -DDEBUG -D_DEBUG -DgcdDEBUG=1
else
  ifeq ("$(DEBUG)", "valgrind")
    CFLAGS          += -g -O -DgcdBUILT_FOR_VALGRIND=1
  else
    CFLAGS          += -O2
  endif
endif

ifeq ($(gcdSTATIC_LINK),1)
  CFLAGS            += -DgcdSTATIC_LINK=1
else
  CFLAGS            += -DgcdSTATIC_LINK=0
endif
