#-----------------------------------------------------------------------------
#
#      File         :  Common.def
#      Abstract     :  Common definitions between all targets
#
#-----------------------------------------------------------------------------



#=============================================================================
# Set architecture, CPU properties, cross-compiler, kernel source dir
#    ARCH           : Set to Linux standard architecture name
#    PLX_CPU_BITS   : Set to 32 or 64 depending upon CPU type
#    PLX_CPU_ENDIAN : Set to Little or Big
#    CROSS_COMPILE  : Set to GCC prefix of cross-compiler
#    KDIR           : Set to root folder of kernel source to target
#=============================================================================
# Attempt to determine architecture if not set
ifndef ARCH
    ARCH := $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86_64/ \
                                   -e s/sun4u/sparc64/ \
                                   -e s/arm.*/arm/ -e s/sa110/arm/ \
                                   -e s/s390x/s390/ -e s/parisc64/parisc/ \
                                   -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \
                                   -e s/sh[234].*/sh/ -e s/aarch64.*/arm64/ )

    # Starting with 32-bit kernel v3.9, "i386" was replaced with "x86". If
    # the HW platform is reported as i386, revert to that instead of x86
    ifeq ($(shell uname -i),i386)
        ARCH := i386
    endif

    #
    # For Ubuntu VM, ARCH must be i386
    #
    ifeq ($(ARCH),x86)
        ARCH := i386
    endif
endif

ifeq ($(ARCH),$(filter $(ARCH), x86 i386 i686))
    PLX_CPU_BITS   = 32
    PLX_CPU_ENDIAN = Little
endif
ifeq ($(ARCH),x86_64)
    PLX_CPU_BITS   = 64
    PLX_CPU_ENDIAN = Little
endif
ifeq ($(ARCH),ia64)
    PLX_CPU_BITS   = 64
    PLX_CPU_ENDIAN = Little
endif
ifeq ($(ARCH),alpha)
    PLX_CPU_BITS   = 32
    PLX_CPU_ENDIAN = Big
endif
ifeq ($(ARCH),ppc)
    PLX_CPU_BITS   = 32
    PLX_CPU_ENDIAN = Big
endif
ifeq ($(ARCH),mips)
    PLX_CPU_BITS   = 32
    PLX_CPU_ENDIAN = Big
endif
ifeq ($(ARCH),mips64)
    PLX_CPU_BITS   = 64
    PLX_CPU_ENDIAN = Big
endif
ifeq ($(ARCH),arm)
    PLX_CPU_BITS   = 32
    PLX_CPU_ENDIAN = Little
    ifeq ($(CROSS_COMPILE),)
        CROSS_COMPILE = arm-linux-gnu-
    endif
endif
ifeq ($(ARCH),arm64)
    PLX_CPU_BITS   = 64
    PLX_CPU_ENDIAN = Little
endif
ifeq ($(ARCH),powerpc)
    PLX_CPU_BITS   = 64
    PLX_CPU_ENDIAN = Little
endif



#=============================================================================
# Setup for Cross-compiling if necessary
#=============================================================================
# Set kernel source folder if not set
ifndef KDIR
    ifeq ($(TGT_TYPE), Driver)
        KDIR := /lib/modules/$(shell uname -r)/build
    endif
endif

# Get kernel version
KERNEL_VER := $(shell uname -r)
ifdef KDIR
    # Attempt to get version from header
    ifneq ($(wildcard $(KDIR)/include/generated/utsrelease.h),)
        KERNEL_VER := ver $(shell awk '{ print $$3 }' ${KDIR}/include/generated/utsrelease.h)
    endif
endif

# Auto-detect platform name if not set
ifndef PLATFORM_NAME
    ifneq ($(CROSS_COMPILE),)
        PLATFORM_NAME := "$(shell echo $(ARCH) | tr "[:lower:]" "[:upper:]") Target Platform"
    else ifneq ($(wildcard /etc/redhat-release),)
        PLATFORM_NAME := "$(shell cat /etc/redhat-release)"
    else ifneq ($(wildcard /etc/SuSE-release),)
        PLATFORM_NAME := "$(shell cat /etc/SuSE-release | grep SUSE)"
    else ifneq ($(wildcard /etc/lsb_release),)
        PLATFORM_NAME := "$(shell lsb_release -s -d)"
    else ifneq ($(wildcard /usr/bin/lsb_release),)
        PLATFORM_NAME := "$(shell lsb_release -s -d)"
    else
        PLATFORM_NAME := "Linux ver ??"
    endif
endif

# Set current folder for driver build
ifeq ($(TGT_TYPE), Driver)
    PWD := $(shell pwd)
endif



#=============================================================================
# Check for errors
#=============================================================================
ifndef PLX_SDK_DIR
    $(error ERROR: Variable 'PLX_SDK_DIR' not set ***)
endif

ifndef PLX_DIR
    PLX_DIR=$(PLX_SDK_DIR)
endif

ifndef ImageName
    $(error ERROR: Variable 'ImageName' not set ***)
endif

ifndef TGT_TYPE
    $(error ERROR: Variable 'TGT_TYPE' not set ***)
endif

ifndef PLX_CHIP
    ifeq ($(TGT_TYPE), Driver)
        $(error ERROR: Variable 'PLX_CHIP' not set ***)
    else ifeq ($(TGT_TYPE), Library)
        PLX_CHIP := API
    else ifeq ($(TGT_TYPE), App)
        PLX_CHIP := Sample
    else
        PLX_CHIP := n/a
    endif
endif

ifndef PLX_CPU_BITS
    $(error ERROR: Variable 'PLX_CPU_BITS' not set for ARCH=$(ARCH) ***)
endif

ifndef PLX_CPU_ENDIAN
    $(error ERROR: Variable 'PLX_CPU_ENDIAN' not set for ARCH=$(ARCH) ***)
endif



#=============================================================================
# Conditional to enable verbose mode (display commands)
#
#  V defined     = Commands will be displayed
#  V not defined = Commands will not be displayed
#=============================================================================
ifndef V
    Q=@
else
    VERBOSE=-v
    OPTIONAL_LF=@echo ""
endif



#=============================================================================
# The directory locations:
#
#  OUT_DIR      = Contains the final build images
#  OBJ_DIR      = Contains all intermediate build files (e.g. obj, map, etc)
#  COMMON_DIR   = Directory where common shared files reside
#  PLX_CHIP_DIR = Directory where chip-specific driver files are located
#=============================================================================
ifeq ($(TGT_TYPE), Driver)
    OUT_DIR = $(COMMON_DIR)/Output
else
    OUT_DIR = $(TGT_TYPE)
endif

OBJ_DIR = $(OUT_DIR)/Obj$(DBG)_$(ARCH)_$(PLX_CPU_BITS)b

ifeq ($(TGT_TYPE), App)
    COMMON_DIR := $(PLX_DIR)/Samples/Shared
endif

ifeq ($(TGT_TYPE), Driver)
    # Set default source directory
    COMMON_DIR := Source.Plx$(PLX_CHIP)

    # 9000-series if chip folder exists
    ifneq ($(wildcard $(PLX_DIR)/Driver/Source.Plx9000/Chip/$(PLX_CHIP)),)
        COMMON_DIR   := Source.Plx9000
        PLX_CHIP_DIR := $(COMMON_DIR)/Chip/$(PLX_CHIP)
    endif
endif



#=============================================================================
# Additional include directories
# Note: Put '-I' before Include directories
#=============================================================================
PLX_INC_DIR += -I$(PLX_DIR)/Include

ifeq ($(TGT_TYPE), App)
    PLX_INC_DIR +=  -I. -I$(COMMON_DIR)
endif

ifeq ($(TGT_TYPE), Driver)
    PLX_INC_DIR += \
       -I$(KDIR)/include \
       -I$(PLX_DIR)/Driver/$(COMMON_DIR)

    ifneq ($(PLX_CHIP_DIR),)
        PLX_INC_DIR += -I$(PLX_DIR)/Driver/$(PLX_CHIP_DIR)
    endif
endif



#=============================================================================
# Additional application Library files
#   -lm  : Adds math library (for pow() function)
#   -ldl : Add support for dynamic library loading functions (used in Aardvark.c)
#=============================================================================
ifeq ($(TGT_TYPE), App)
    LIBS += $(PLX_DIR)/PlxApi/Library/PlxApi$(DBG).a
    LIBS += -lm -ldl
endif



#=============================================================================
# Additional PLX-specific command-line options
#=============================================================================
ifeq ($(PLX_CPU_ENDIAN),Big)
    PLX_OPT += -DPLX_BIG_ENDIAN
else
    PLX_OPT += -DPLX_LITTLE_ENDIAN
endif

ifdef PLX_DEBUG
    PLX_OPT += -DPLX_DEBUG
endif

ifeq ($(TGT_TYPE), App)
    PLX_OPT += -DPLX_LINUX
endif

# Add Position Independent Code (-fPIC) to support Shared Object libaries
ifeq ($(TGT_TYPE), Library)
    PLX_OPT += -DPLX_LINUX -fPIC
endif

ifeq ($(TGT_TYPE), Driver)
    # Flag RedHat/Fedora/CentOS due to variations from standard kernels
    ifeq (1,$(shell [ -r /etc/redhat-release ] && echo 1 || echo 0))
        PLX_OPT += -DRED_HAT_LINUX_KERNEL
    endif

    # Flag to display SGL details for drivers that support SGL DMA
    ifdef PLX_DISPLAY_SGL
        PLX_OPT += -DPLX_DISPLAY_SGL
    endif

    PLX_OPT += -DPLX_DRIVER -DPLX_LINUX_DRIVER -DPLX_CHIP=$(PLX_CHIP)
endif

# Set flag for 32/64-bit system
ifeq ($(PLX_CPU_BITS), 32)
    PLX_OPT += -DPLX_32BIT
else
    ifeq ($(PLX_CPU_BITS), 64)
        PLX_OPT += -DPLX_64BIT
    endif
endif

# If PLX Co-Sim environment
ifdef PLX_COSIM
    PLX_OPT += -DPLX_COSIM=$(PLX_COSIM)
endif

EXTRA_CFLAGS += -D$(ARCH) -DARCH=$(ARCH) $(PLX_OPT) $(PLX_INC_DIR)



#=============================================================================
# PLX specific definitions
#=============================================================================
ifdef PLX_DEBUG
    DBG        = _dbg
    BUILD_TYPE = Debug
else
    BUILD_TYPE = Release
endif

# Build SDK version string
SDK_VER = X.YY
VER_FILE = $(PLX_SDK_DIR)/Include/Plx.h
ifneq ($(wildcard $(VER_FILE)),)
    VER_MAJOR = $(shell cat ${VER_FILE} | grep VERSION_MAJOR | awk '{print $$3}')
    VER_MINOR = $(shell cat ${VER_FILE} | grep VERSION_MINOR | awk '{print $$3}')
    SDK_VER=$(VER_MAJOR).$(VER_MINOR)
endif
