#########################################################################
#
# Makefile used for building application.
#
# The default target (all) builds application in three formats :
#   *.rec : Image in S-record format.
#   *.bin : Image in binary format.
#   *.elf : Image in ELF format.
#   *.map : Linker generated map file.
#   *.dis : Disassembly of image.
#   *.sym : Symbols.
#
# Other targets are :
#   clean :    Deletes all files generated by makefile.
#
#########################################################################

CONFIG_FILE ?= .config

-include $(CONFIG_FILE)

include config_by_chip.mk

# **********************************************
# Build Options
# **********************************************
# Version: Debug or Retail
VERSION  ?= Retail
R2_BACKTRACE ?= 0


#==================================================================================================
# Image name
AP_NAME = AP
BL_NAME = BL
AP_COMPRESS_NAME = AP_C

BL_UPGRADE_FILE:=$(shell ./GetUpFile.py)
ifeq ($(BL_UPGRADE_FILE),MSD3463GU_30.BIN)
        MERGE_NAME = MSD3463GU_30
else ifeq ($(BL_UPGRADE_FILE),MSD3463GU_33.BIN)
        MERGE_NAME = MSD3463GU_33
else ifeq ($(BL_UPGRADE_FILE),MSD3463GU_60.BIN)
        MERGE_NAME = MSD3463GU_60
else
MERGE_NAME = MERGE
endif

RES_NAME = RES

ifeq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
_BL_SIZE = $(firstword $(shell du -b $(BINPATH)_BLOADER/MERGE.bin))
BL_SIZE = $(shell _BL_SIZE1=$(_BL_SIZE); _BL_SIZE2=131072; printf '0x%X' $$((_BL_SIZE1-_BL_SIZE2)))
endif

# Decide sboot binary file name
ifeq ($(PROJ),MIPS_EMERALD_ZUI_MM_128MB_DVBT_CI_PLUS_T2)
    BD_SBOOT_FILE_NAME = sboot_T2.bin

else ifeq ($(MEMORY_SIZE),MEMORY_32MB)
    BD_SBOOT_FILE_NAME = sboot_32.bin

else ifeq ($(MEMORY_SIZE),MEMORY_64MB)
    BD_SBOOT_FILE_NAME = sboot_64.bin

else # 128M or other memory size
    ifeq ($(CHIP_FAMILY),MILAN)
      ifeq ($(MEMORY_SIZE),MEMORY_128MB)
        BD_SBOOT_FILE_NAME = sboot_128.bin
      else
        #BD_SBOOT_FILE_NAME = sboot_.bin
      endif
    else
        BD_SBOOT_FILE_NAME = sboot.bin
    endif
endif


# **********************************************
# Tool Chain
# **********************************************
ifeq ($(OS_TYPE),nos_aeon)
    AEON_TYPE = aeonR2
endif

ifeq ($(PARA), 1)
    CROSSCOMPILE =
    CORSSCOMPILE_VER = -V 4.4.3
    AEON_FLAG    =
else ifeq ($(OS_TYPE),nos_mips)
    CROSSCOMPILE = mips-sde-elf-
    CORSSCOMPILE_VER = -V 4.3.2
    AEON_FLAG    =
else ifeq ($(OS_TYPE),nos_aeon)
    ifeq ($(USE_NEW_AEON_TOOLCHAIN),1)
        CROSSCOMPILE = r2-elf-
        CORSSCOMPILE_VER =
    else
        CROSSCOMPILE = aeon-
        CORSSCOMPILE_VER =
    endif
    AEON_FLAG    = -march=$(AEON_TYPE) -mhard-div -mhard-mul -EL -mredzone-size=4
endif

# AEON_FLAG   = -march=aeon1 -mhard-div -mhard-mul -fno-delayed-branch -minsert-nop-before-branch

ifeq ($(PARA), 1)
    CC    = cpptestscan --cpptestscanProjectName=UTProject $(CROSSCOMPILE)gcc $(CROSSCOMPILE_VER)
    CPP = cpptestscan --cpptestscanProjectName=UTProject $(CROSSCOMPILE)cpp
    LD    = cpptestscan --cpptestscanProjectName=UTProject $(CROSSCOMPILE)ld
else
    CC    = $(CROSSCOMPILE)gcc $(CROSSCOMPILE_VER)
    CPP = $(CROSSCOMPILE)cpp
    LD    = $(CROSSCOMPILE)ld
endif
OBJCOPY = $(CROSSCOMPILE)objcopy
OBJDUMP = $(CROSSCOMPILE)objdump
SIZE    = $(CROSSCOMPILE)size
AR    = $(CROSSCOMPILE)ar
NM    = $(CROSSCOMPILE)nm
READELF    = $(CROSSCOMPILE)readelf

AWK     = /bin/gawk

#-------------------------------------------------------------------------------------------------
ifeq ($(shell uname -s), Linux)
    FLINT   = scripts/flint
    REALPATH= echo
else
    FLINT   = scripts/LINT-NT.EXE
    REALPATH:= cygpath -w
endif

# **********************************************
# Directories
# **********************************************
PREFIXDIR?=
ROOT    = .
BINDIR    = ./$(PREFIXDIR)/Bin_$(PROJ)
BINPATH = $(BINDIR)
OBJDIR    = ./$(PREFIXDIR)/Obj_$(PROJ)
OBJPATH = $(OBJDIR)
LZSSDIR = ./scripts/lzss
MSCOMPDIR = ./scripts/util

BOOTDIR = ./boot
COREDIR ?= ./core
MIDDLEWARE_DIR = ./core/middleware
PROJECT_DIR = ./core/project
CHECK_BIN_DIR = ./scripts/check_bin
#-------------------------------------------------------------------------------------


###==================================================================================
## Option for gcc
#
CC_TVOPTS += -DMS_BOARD_TYPE_SEL=$(BOARD_TYPE_SEL)

# Link option: ENABLE_DVBS <--> ENABLE_S2
ifeq ($(CHIP_FAMILY),MILAN)
    ifneq ($(ENABLE_DVBS),)
        CC_TVOPTS += -DENABLE_S2=$(ENABLE_DVBS)
    else
        CC_TVOPTS += -DENABLE_S2=DISABLE
    endif
endif

#-------------------------------------------------------------
# Link option: MEMORY_SIZE <--> MEMORY_MAP
ifeq ($(MEMORY_SIZE),MEMORY_32MB)
    CC_TVOPTS += -DMEMORY_MAP=MMAP_32MB

else ifeq ($(MEMORY_SIZE),MEMORY_64MB)
    CC_TVOPTS += -DMEMORY_MAP=MMAP_64MB

else ifeq ($(MEMORY_SIZE),MEMORY_128MB)
    CC_TVOPTS += -DMEMORY_MAP=MMAP_128MB

else ifeq ($(MEMORY_SIZE),MEMORY_128_128MB)
    CC_TVOPTS += -DMEMORY_MAP=MMAP_128_128MB

else ifeq ($(MEMORY_SIZE),MEMORY_256_256MB)
    CC_TVOPTS += -DMEMORY_MAP=MMAP_256_256MB

endif
#-------------------------------------------------------------
ifeq ($(AUTOBOOT),1)
    CC_TVOPTS += -DAUTOBOOT=1 -DAUTOBOOTCL=$(CL)
endif

ifeq ($(BUILD_TARGET),ORGINAL_ALL_SYSTEM)
    CC_TVOPTS += -DORGINAL_ALL_MERGE=1
    CC_TVOPTS += -DBLOADER=0
else
    CC_TVOPTS += -DORGINAL_ALL_MERGE=0
    ifeq ($(BUILD_TARGET),BLOADER_SYSTEM)
        CC_TVOPTS += -DBLOADER=1
    else
        CC_TVOPTS += -DBLOADER=0
    endif
endif

ifeq ($(SECURE_BOOT),1)
    CC_TVOPTS += -DSECURE_BOOT=1
else
    CC_TVOPTS += -DSECURE_BOOT=0
endif

CC_TVOPTS += -DUSE_SW_I2C=1 -DCOMB_3D -DBOOTLOADER_BANK_NUM

ifeq ($(R2_BACKTRACE),1)
    CC_TVOPTS += -DENABLE_R2_BACKTRACE=1
endif
#===========================================================================================
# Driver BSP header file

ifeq ($(OS_TYPE),nos_mips)
    DRV_BSP_INC = $(COREDIR)/drv_bsp/$(chip_name)_nos_mips/include
else
    DRV_BSP_INC = $(COREDIR)/drv_bsp/$(chip_name)_nos_r2m/include
endif

# Add "Header (include) file" directories here ...
INC_DIR =   \
        -I$(ROOT)/include                                   \
        -I$(BOOTDIR)                                        \
        -I$(COREDIR)/api/Arabic_Parser/Include              \
        -I$(COREDIR)/api/include                            \
        -I$(COREDIR)/api/Thai_Parser/Include                \
        -I$(COREDIR)/api/utl                                \
        -I$(COREDIR)/driver/sys/include                     \
        -I$(MIDDLEWARE_DIR)/closedcaption/include            \
        -I$(DRV_BSP_INC)                                    \
        -I$(DRV_LEGACY_INC)                                 \
        -I$(PROJECT_DIR)/image/dvbt


ifeq ($(OS_TYPE),nos_aeon)
    INC_DIR += -I$(ROOT)/include/std
endif

#-----------------------------------------------------------------------------------------------

WARN_FLAGS = -Wall -Wextra -Wcast-align -Wpointer-arith -Wstrict-prototypes -Winline -Wundef -Wno-format -Wshadow


YOGA_GLOBAL_CFLAGS = -pipe -fno-exceptions -ffunction-sections $(WARN_FLAGS)

#===========================================================================================
# Source files ...

SRC_FILE = $(COREDIR)/api/utl/NoOS.c

ifneq ($(BUILD_FILE),)
    include $(BUILD_FILE)
else
    include project/build/FILES_$(PROJ).mk
endif

#============================================================================================
ifeq ($(CHIP_FAMILY),EULER)
    IMGINFO_OFFSET = 70016
    BOOTLOADER ?= project/loader/bootloader_EULER_R2.ld

else ifeq ($(CHIP_FAMILY),EDEN)
    IMGINFO_OFFSET = 70016
    BOOTLOADER ?= project/loader/bootloader_EDEN_R2.ld

else ifeq ($(CHIP_FAMILY),NASA)
    IMGINFO_OFFSET = 70016
    BOOTLOADER ?= project/loader/bootloader_NASA_R2.ld

else ifeq ($(CHIP_FAMILY),EMERALD)
    BOOTLOADER = project/loader/bootloader_emerald.ld
    IMGINFO_OFFSET = 69632

else ifeq ($(CHIP_FAMILY),NUGGET)
    BOOTLOADER = project/loader/bootloader_nugget.ld
    IMGINFO_OFFSET = 69632

else ifeq ($(CHIP_FAMILY),NIKON)
    BOOTLOADER = project/loader/bootloader_nikon.ld
    IMGINFO_OFFSET = 69632

else ifeq ($(CHIP_FAMILY),MILAN)
    BOOTLOADER = project/loader/bootloader_milan.ld
    IMGINFO_OFFSET = 69632

endif


# **********************************************
# Image file names
# **********************************************
AP_BIN = $(BINPATH)/$(AP_NAME).bin
###AP_BIN2= $(BINPATH)/$(AP_NAME)_2.bin
APC_BIN = $(BINPATH)/$(AP_COMPRESS_NAME).bin
AP_ELF = $(BINPATH)/$(AP_NAME).elf
AP_MAP = $(BINPATH)/$(AP_NAME).map
AP_DIS = $(BINPATH)/$(AP_NAME).dis
AP_SYM = $(BINPATH)/$(AP_NAME).sym
AP_OBJ = $(OBJPATH)/$(AP_NAME).o

ifeq ($(R2_BACKTRACE),1)
AP_BT_SYM =  $(BINPATH)/bt_symbol.S
AP_BT_OBJ = $(BINPATH)/bt_symbol.o
BT_TOOL_TEXT_SYNS = ./scripts/util/text_syms
#TOOL_CHAIN = $(shell ($(CC) -v &> gcc_version.txt | grep "gcc version" gcc_version.txt | cut -d" " -f3 |cat ))
TOOL_CHAIN = 4.1.2
endif

ifeq ($(OS_TYPE),nos_mips)
BT_TOOL_TEXT_SYNS = ./scripts/util/text_syms
AP_BT_SYM = $(BINPATH)/bt_symbol.S
AP_ELF_NM_1 = $(BINPATH)/ap.nm.txt
endif


MERGE_BIN = $(BINPATH)/$(MERGE_NAME).bin
#MERGE_ELF = $(BINPATH)/$(MERGE_NAME).elf
MERGE_DIS = $(BINPATH)/$(MERGE_NAME).dis
MERGE_MAP = $(BINPATH)/$(MERGE_NAME).map
MERGE_BIN_2 = $(BINPATH)/$(MERGE_NAME)_2.bin
###HK51_BOOT = $(BINPATH)/$(MERGE_NAME).bin


RES_BIN = $(BINPATH)/$(RES_NAME).bin

ifeq ($(LOADER),)
    ifeq ($(OS_TYPE),nos_mips)
        #LOADER ?= project/loader/loader_nos_mips_$(chip_name).ld
        LOADER ?= $(BINPATH)/loader_nos_mips_$(PROJ).ld

    else ifeq ($(OS_TYPE),nos_aeon)
        #LOADER ?= project/loader/loader_nos_aeon_$(chip_name).ld
        LOADER ?= $(BINPATH)/loader_nos_aeon_$(PROJ).ld
    endif
endif


# **********************************************
# Tools
# **********************************************
#SWAP      = perl $(TOOLS)/byteswap.pl

ifeq ($(BUILD_TARGET),BLOADER_SYSTEM)
    ifeq ($(BIN_FORMAT),COMPRESS)
        BinIDPackFiles = python scripts/PadLoaderCrc_OAD.py
    else
        ifeq ($(BIN_FORMAT),COMPRESS7)
            BinIDPackFiles = python scripts/PadLoaderCrc_OAD.py
        else
            BinIDPackFiles = python scripts/PadLoaderCrc.py
        endif
    endif
else
    ifeq ($(BUILD_TARGET),ORGINAL_ALL_SYSTEM)
        ifeq ($(BIN_FORMAT),COMPRESS)
            BinIDPackFiles = python scripts/BinIDPackFiles_Compress.py
        else
            ifeq ($(BIN_FORMAT),COMPRESS7)
                BinIDPackFiles = python scripts/BinIDPackFiles_Compress.py
            else
                BinIDPackFiles = python scripts/BinIDPackFiles.py
            endif
        endif
    else
        ifeq ($(BIN_FORMAT),COMPRESS)
            ifeq ($(SECURE_BOOT),1)
                BinIDPackFiles = python scripts/BinIDPackFilesForNewMerge_Compress_Secure.py
            else
                BinIDPackFiles = python scripts/BinIDPackFilesForNewMerge_Compress.py
            endif
        else
            ifeq ($(BIN_FORMAT),COMPRESS7)
                BinIDPackFiles = python scripts/BinIDPackFilesForNewMerge_Compress.py
            else
                BinIDPackFiles = python scripts/BinIDPackFilesForNewMerge.py
            endif
        endif
    endif
endif
BinIDPackResources = python scripts/BinIDPackResources.py
AddBin = python scripts/Addbin.py

# Pack bin files parameter...
BLOADER_PACK__BIN_ALIGN = 8
BLOADER_PACK__FILE_ALIGN = 65536

AP_PACK__BIN_ALIGN = 8
AP_PACK__FILE_ALIGN = 8

ifeq ($(CHIP_FAMILY),MILAN)
    AP_PACK__FILE_ALIGN = 1024
endif


# **********************************************
# Files to be compiled
# **********************************************
SRC_S  = $(filter %.S, $(SRC_FILE))
SRC_C  = $(filter %.c, $(SRC_FILE))
SRC_O  = $(filter %.o,  $(SRC_FILE))
SRC_B  = $(filter %.bin, $(SRC_FILE))

OBJ_S  = ${SRC_S:%.S=$(OBJPATH)/%.o}
OBJ_C  = ${SRC_C:%.c=$(OBJPATH)/%.o}
OBJ_B  = ${SRC_B:%.bin=$(OBJPATH)/%.o}

OBJ    = $(OBJ_C) $(OBJ_B) $(OBJ_S)

BL_SRC_S  = $(filter %.S, $(BL_SRC_FILE))
BL_SRC_C  = $(filter %.c, $(BL_SRC_FILE))
BL_SRC_O  = $(filter %.o,  $(BL_SRC_FILE))
BL_SRC_B  = $(filter %.bin, $(BL_SRC_FILE))

BL_OBJ_S  = ${BL_SRC_S:%.S=$(OBJPATH)/%.o}
BL_OBJ_C  = ${BL_SRC_C:%.c=$(OBJPATH)/%.o}
BL_OBJ_B  = ${BL_SRC_B:%.bin=$(OBJPATH)/%.o}

BL_OBJ    = $(BL_OBJ_C) $(BL_OBJ_B) $(BL_OBJ_S)

SRC    = $(SRC_C) $(BL_SRC_C)

# ***********************************************************************
# Libraries
# ***********************************************************************

# Standard Libraries Path
ifeq ($(OS_TYPE),nos_mips)
    OS_LIB_DIR = $(ROOT)/core/lib/$(chip_name)
endif

# **********************************************
# Compiler and linker options
# **********************************************

#===============================================================================================================
# Option of compiler
#--------------------------------------------

INCLUDE   = $(INC_DIR)

YOGA_GLOBAL_LDFLAGS = -msoft-float -g -nostdlib -Wl,--gc-sections -Wl,-static


ifeq ($(PARA), 1)
    CC_OPTS0    +=
    CC_OPTS0    += -c $(INCLUDE) $(AEON_FLAG) $(CC_TVOPTS) $(YOGA_GLOBAL_CFLAGS) $(MALLOC_CFLAGS)
else

    ifeq ($(OS_TYPE),nos_mips)
        CC_OPTS0    += -EL
        CC_OPTS0    += -fno-optimize-sibling-calls -mips32r2 -msoft-float -Wall -Wpointer-arith -Wstrict-prototypes -Winline -Wundef -ffunction-sections -fdata-sections -fno-exceptions -G0
        CC_OPTS0    += -DMSOS_TYPE_NOS
    endif

    CC_OPTS0  += -c $(INCLUDE) $(AEON_FLAG) $(CC_TVOPTS) $(YOGA_GLOBAL_CFLAGS) $(MALLOC_CFLAGS)

    ifeq ($(OS_TYPE),nos_aeon)
        CC_OPTS0  += -Wno-strict-aliasing
    endif
endif

# config for debug info and optimize
ifeq ($(OS_TYPE),nos_mips)
    #CC_OPTS = $(CC_OPTS0) -O2 -gdwarf-2
    #CC_OPTS = $(CC_OPTS0) -g -O2
    CC_OPTIMIZE = -O2
    ifeq ($(CHIP_FAMILY),MILAN)
        #ifeq ($(MEMORY_SIZE),MEMORY_64MB)
        ifeq ($(BUILD_TARGET),BLOADER_SYSTEM)
            CC_OPTIMIZE = -Os
        endif
    endif
    CC_OPTS = $(CC_OPTS0) -g $(CC_OPTIMIZE)
else
    ifeq ($(VERSION),Debug)
        CC_OPTS  = $(CC_OPTS0) -g -O2 -gdwarf-2
    else
        CC_OPTS  = $(CC_OPTS0) -O2 -gdwarf-2
    endif
endif

#ifeq ($(VERSION),Debug)
#    ifeq ($(OS_TYPE),nos_mips)
#        #CC_OPTS  = $(CC_OPTS0) -g -O0 -gdwarf-2
#    else
#        CC_OPTS  = $(CC_OPTS0) -O0 -ggdb
#    endif
#else
#  #ifeq ($(R2_BACKTRACE),1)
#  #  CC_OPTS  = $(CC_OPTS0) -O2 -gdwarf-2
#  #else
#    CC_OPTS  = $(CC_OPTS0) -O2 -gdwarf-2
#    #CC_OPTS  = $(CC_OPTS0) -O2 -g
#    #CC_OPTS  = $(CC_OPTS0) -Os
#  #endif
#endif

#-------------------------------------------------------------------------------------------------------

ifeq ($(PARA), 1)
    LD_OPTS        +=
else
    #ifneq ($(CROSSCOMPILE),aeon-)
    #LD_OPTS        += -EL
    #endif

    ifeq ($(OS_TYPE),nos_mips)
        # When ld is used for linking (must specify standard library search path or set SEARCH_DIR in linker script)
        LD_OPTS += -g -nostdlib -EL -msoft-float -Wl,--gc-sections -Wl,-static -Wl,-Map,$(AP_MAP)
        LD_LIB += -lm -lc -L$(OS_LIB_DIR) -T$(LOADER)
    else
        LD_OPTS += -EL -nostartfiles $(YOGA_GLOBAL_LDFLAGS) $(AEON_FLAG) -LLIB -W1,--gc-sections
        LDLIB += -lc -lgcc -lm
    endif
endif


# **********************************************
# Rules
# **********************************************
.PHONY : all clean pmsleep lint
.SUFFIXES: .bin .elf .dis .sym .siz

# Project Build

all : $(PROJ)

#---------------------------------------------------------------------------------

$(PROJ): loader setup sboot ap checkbinsize merge debug_info lint
    @echo "[Rule - PROJ_$(PROJ)]"
    @date

#---------------------------------------------------------------------------------

#Note: It's slow to produce .dis file w/o -gdwarf-2 set or original OS source code

ap: $(AP_ELF) $(AP_BIN) $(AP_SYM)
    @echo "[Rule - ap]"
    @echo "[AP_END]"

#---------------------------------------------------------------------------------

ifeq ($(OS_TYPE),nos_mips)
merge: $(MERGE_BIN) SECURE
    @echo "[Rule - merge]"
    @echo "[MERGE]"
else
    @echo "[MERGE BIN]"
ifneq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
merge: $(MERGE_ELF) $(MERGE_BIN)
    @echo "[Rule - merge]"
else
    @echo "[MERGE BIN]"
merge: $(MERGE_BIN)
    @echo "[Rule - merge]"
endif
endif

#-----------------------------------------------------------------------------

ifeq ($(OS_TYPE),nos_mips)
dis: $(AP_DIS)
else
dis: $(AP_DIS) #$(MERGE_DIS)
endif

#====================================================================================

HEAP_START=$$(cat HEAP_START.txt)
HEAP_END=$$(cat HEAP_END.txt)
HEAP_SIZE=$$((($(HEAP_END)-$(HEAP_START))/1024))

ifeq ($(OS_TYPE),nos_aeon)
    HEAP_START_SYMBOL=___heap
    HEAP_END_SYMBOL=___heap_end
    HEAP_START_STRING=                ___heap = .
    HEAP_END_STRING=                ___heap_end = (RAM_START + RAM_SIZE)
else
    HEAP_START_SYMBOL=__heap
    HEAP_END_SYMBOL=__heap_end
    HEAP_START_STRING=                __heap = .
    HEAP_END_STRING=                __heap_end = (RAM_START + RAM_SIZE)
endif

#------------------------------------------------------------------------------------

sboot: $(AP_BIN)
    @echo "[SBOOT START]"
#    @grep '__heap =' $(BINPATH)/AP.map | sed 's\$(HEAP_START_STRING)\\g' | sed 's/^.*0x/0x/g'>HEAP_START.txt;
#    @grep '__heap_end =' $(BINPATH)/AP.map | sed 's\$(HEAP_END_STRING)\\g' | sed 's/^.*0x/0x/g'>HEAP_END.txt;
    @grep '$(HEAP_START_SYMBOL) =' $(BINPATH)/AP.map | sed 's\$(HEAP_START_STRING)\\g' | sed 's/^.*0x/0x/g'>HEAP_START.txt;
    @grep '$(HEAP_END_SYMBOL) =' $(BINPATH)/AP.map | sed 's\$(HEAP_END_STRING)\\g' | sed 's/^.*0x/0x/g'>HEAP_END.txt;
    @echo "HEAP_START= $(HEAP_START)"
    @echo "HEAP_END  = $(HEAP_END)"
    @echo "$(HEAP_SIZE)">HEAP_SIZE.txt
#    @rm HEAP_START.txt
#    @rm HEAP_END.txt
#    @echo "HEAP_SIZE = $(HEAP_SIZE)KB"
    @awk '{if($$1>=100) {print "HEAP_SIZE = "$$1" KB";rm "HEAP_SIZE.txt";}else {print "Error:HEAP_SIZE("$$1"KB) is less than 100 KB";exit 1}}' HEAP_SIZE.txt
    @echo "[SBOOT] $@"
    ifeq ($(BIN_FORMAT),COMPRESS)
#        @$(shell $(LZSSDIR)/lzss.out C 14336 $(BINPATH)/$(AP_NAME).bin $(BINPATH)/$(AP_COMPRESS_NAME).bin;)
        @$(shell $(MSCOMPDIR)/mscompress -c -u 14336 -9 $(BINPATH)/$(AP_NAME).bin > $(BINPATH)/$(AP_COMPRESS_NAME).bin;)
        @$(shell cp $(BINPATH)/$(AP_COMPRESS_NAME).bin $(ROOT)/boot/sboot/bin/$(AP_NAME).bin; cp $(BINPATH)/$(AP_NAME).map $(ROOT)/boot/sboot/bin;)
    else
        ifeq ($(BIN_FORMAT),COMPRESS7)
        @$(shell $(MSCOMPDIR)/mscompress7 e 14336 $(BINPATH)/$(AP_NAME).bin $(BINPATH)/$(AP_COMPRESS_NAME).bin;)
        @$(shell cp $(BINPATH)/$(AP_COMPRESS_NAME).bin $(ROOT)/boot/sboot/bin/$(AP_NAME).bin; cp $(BINPATH)/$(AP_NAME).map $(ROOT)/boot/sboot/bin;)
        else
        @$(shell cp $(BINPATH)/$(AP_NAME).bin $(ROOT)/boot/sboot/bin; cp $(BINPATH)/$(AP_NAME).map $(ROOT)/boot/sboot/bin;)
        endif
    endif
    @(cd $(ROOT)/boot/sboot; $(MAKE) clean;)
    @( mkdir -p $(ROOT)/boot/sboot/out;)

    cp $(ROOT)/boot/sboot/bin/$(BOARD_TYPE_SEL)/$(BD_SBOOT_FILE_NAME) $(ROOT)/boot/sboot/out/sboot.bin;

    ifeq ($(SECURE_BOOT),1)
          @(cp $(ROOT)/boot/sboot/bin/$(BOARD_TYPE_SEL)/sboot_secure.bin $(ROOT)/boot/sboot/out/sboot.bin;)
    endif

    # Copy PM.bin to sboot/out
ifeq ($(OS_TYPE),nos_aeon)
    @(/bin/cp -f $(ROOT)/boot/sboot/bin/$(BOARD_TYPE_SEL)/PM.bin $(ROOT)/boot/sboot/out;)
else ifeq ($(CHIP_FAMILY),MILAN)
    @(/bin/cp -f $(ROOT)/boot/sboot/bin/$(BOARD_TYPE_SEL)/PM.bin $(ROOT)/boot/sboot/out;)
else
    @(/bin/cp -f $(ROOT)/boot/sboot/bin/PM/$(CHIP_FAMILY)/PM.bin $(ROOT)/boot/sboot/out;)
endif
    
#ifeq ($(CHIP_FAMILY),EULER)
#    @(/bin/cp -f $(ROOT)/boot/sboot/bin/$(BOARD_TYPE_SEL)/PM.bin $(ROOT)/boot/sboot/out;)
#else ifeq ($(CHIP_FAMILY),EDEN)
#    @(/bin/cp -f $(ROOT)/boot/sboot/bin/$(BOARD_TYPE_SEL)/PM.bin $(ROOT)/boot/sboot/out;)
#else ifeq ($(CHIP_FAMILY),NASA)
#    @(/bin/cp -f $(ROOT)/boot/sboot/bin/$(BOARD_TYPE_SEL)/PM.bin $(ROOT)/boot/sboot/out;)
#else
#    @(/bin/cp -f $(ROOT)/boot/sboot/bin/PM/$(CHIP_FAMILY)/PM.bin $(ROOT)/boot/sboot/out;)
#endif
########################################################################################
    ifeq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
    $(MAKE) -C $(ROOT)/boot/sboot _FLASH_CHUNK_BASE_ADDRESS=$(BL_SIZE)
    else
    $(MAKE) -C $(ROOT)/boot/sboot
    endif
    @echo "[SBOOT - End]"

#===================================================================================================

ifeq ($(OS_TYPE),nos_mips)
$(AP_ELF) : $(OBJ_S) $(OBJ_C) $(OBJ_B) $(PRANA_LIB) $(DTV_LIB)
    @echo "[Rule - AP_ELF]"
    @echo "[LD]  $@"
    @echo "[LD]  $(LD_LIB)"
    @$(CC) $(LD_OPTS) -o $(AP_ELF) $(OBJ_C) -Wl,--start-group $(BSP_LIB) $(DTV_LIB) $(PRANA_LIB) -Wl,--end-group $(LD_LIB)
    #@$(AR) -r $(ROOT)/core/api/msAPI_Flash.a $(OBJPATH)/core/api/msAPI_Flash.o

else

ifeq ($(R2_BACKTRACE),1)
$(AP_ELF).tmp : $(OBJ_S) $(OBJ_C) $(OBJ_B) $(DTV_LIB)
    @echo "[Rule - AP_ELF.tmp]"
    @$(CC) $(LD_OPTS) -Wl,--start-group $^ -Wl,--end-group -T$(LOADER) -o $@ $(LDLIB)


$(AP_BT_SYM): $(AP_ELF).tmp
    @echo "[Rule - AP_BT_SYM]"
    #$(CC) -v &> gcc_version.txt
    $(NM) -n $< | $(BT_TOOL_TEXT_SYNS) $(TOOL_CHAIN)  SYM_OFF > $@
    #echo "rm $<"
    #@rm $<
    #@rm gcc_version.txt

$(AP_BT_OBJ): $(AP_BT_SYM)
    @echo "[Rule - AP_BT_OBJ]"
    @$(CC) $(CC_OPTS0) -nostdlib -o $@ $< -T$(LOADER)

else

$(AP_BT_OBJ):


endif

$(AP_ELF) : $(OBJ_S) $(OBJ_C) $(OBJ_B) $(DTV_LIB)  $(AP_BT_OBJ)
    @echo "[Rule - AP_ELF]"
    @echo "[LD]  $@"
    @$(CC) $(LD_OPTS) -Wl,-Map,$(AP_MAP) -Wl,--start-group $^ -Wl,--end-group -T$(LOADER) -o $@ $(LDLIB)
    #$(NM) -a -S --size-sort $@ > $(BINPATH)/ap_sym__sort_by_size.txt
    #sort $(BINPATH)/ap_sym__sort_by_size.txt > $(BINPATH)/ap_sym__sort_by_addr.txt
    #@$(AR) -r $(ROOT)/core/api/msAPI_Flash.a $(OBJPATH)/core/api/msAPI_Flash.o
endif
    @$(SIZE) $@

#=====================================================================================================

debug_info:
    @echo "[Rule - debug_info]"
    $(NM) -a -S --size-sort $(AP_ELF) > $(BINPATH)/ap_sym__sort_by_size.txt
    sort $(BINPATH)/ap_sym__sort_by_size.txt > $(BINPATH)/ap_sym__sort_by_addr.txt
    $(READELF) -a $(AP_ELF) > $(BINPATH)/AP_elf.txt

#=====================================================================================================

$(AP_BIN) : $(AP_ELF)
    @echo "[BIN] $@"
    @$(OBJCOPY) -O binary -S -g -x -X -R .sbss -R .bss -R .reginfo $< $@


#=================================================================================================

ifeq ($(AEON_TYPE),aeonR2)
$(AP_OBJ): $(AP_BIN)
    ifeq ($(BIN_FORMAT),COMPRESS)
#        $(shell $(LZSSDIR)/lzss.out C 14336 $(BINPATH)/AP.bin $(BINPATH)/AP_C.bin; cd $(BINPATH); $(OBJCOPY) -I binary -O elf32-littleaeon -B aeon:aeonR2 --prefix-sections=bin AP_C.bin ../$(OBJPATH)/AP.o)
        $(shell $(MSCOMPDIR)/mscompress -c -u 14336 -9 $(BINPATH)/$(AP_NAME).bin > $(BINPATH)/$(AP_COMPRESS_NAME).bin; cd $(BINPATH); $(OBJCOPY) -I binary -O elf32-littleaeon -B aeon:aeonR2 --prefix-sections=bin AP_C.bin ../$(OBJPATH)/AP.o)
    else
        $(shell cd $(BINPATH); $(OBJCOPY) -I binary -O elf32-littleaeon -B aeon:aeonR2 --prefix-sections=bin AP.bin ../$(OBJPATH)/AP.o)
    endif
else
$(AP_OBJ): $(AP_BIN)
    ifeq ($(BIN_FORMAT),COMPRESS)
        $(shell $(LZSSDIR)/lzss.out C 14336 $(BINPATH)/AP.bin $(BINPATH)/AP_C.bin; cd $(BINPATH); $(OBJCOPY) -I binary -O elf32-littleaeon -B aeon --prefix-sections=bin AP_C.bin ../$(OBJPATH)/AP.o)
    else
        $(shell cd $(BINPATH); $(OBJCOPY) -I binary -O elf32-littleaeon -B aeon --prefix-sections=bin AP.bin ../$(OBJPATH)/AP.o)
endif
endif

#=================================================================================================


ifneq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
$(MERGE_ELF): $(BL_OBJ_S) $(BL_OBJ_C) $(BL_OBJ_B) $(AP_OBJ)
    @echo "[LD]  $@"
    @$(CC) $(LD_OPTS) -Wl,-Map,$(MERGE_MAP) $^ -T$(BOOTLOADER) -o $@ $(LDLIB)
    @$(SIZE) $@
endif

#=================================================================================================

BinIDPackCRC = python scripts/AddCRC32.py

SECURE :
ifeq ($(SECURE_BOOT),1)
    ifneq ($(BUILD_TARGET),BLOADER_SYSTEM)
    @echo "[SECURE]"
    @$(shell test/cutter $(BINDIR)/MERGE.bin $(BINDIR)/AP_C.bin secure/tmp_chunk 1> /dev/null;)
    @$(BinIDPackCRC) $(BINDIR)/MERGE.bin
    endif
endif

#=================================================================================================

ifeq ($(OS_TYPE),nos_mips)
$(MERGE_BIN) : $(AP_BIN)
    @echo "[Rule - MERGE_BIN] => $@"
    @echo "[BIN] $@"
    @echo "AP_PACK__FILE_ALIGN=$(AP_PACK__FILE_ALIGN)"
    @echo "BLOADER_PACK__FILE_ALIGN=$(BLOADER_PACK__FILE_ALIGN)"
    $(shell cp $(ROOT)/boot/sboot/out/all.bin $(MERGE_BIN);)
    ifeq ($(BIN_FORMAT),COMPRESS)
        ifeq ($(BUILD_TARGET),BLOADER_SYSTEM)
            $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(BLOADER_PACK__BIN_ALIGN) $(BLOADER_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(APC_BIN) 0 $(OS_TYPE)
        else
          ifeq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
            @cat $(BINPATH)_BLOADER/MERGE.bin $(ROOT)/boot/sboot/out/chunk_header.bin $(APC_BIN) > $@
            @ls -lh $(BINPATH)_BLOADER/MERGE.bin $(BINPATH)/$(AP_COMPRESS_NAME).bin $(MERGE_BIN)
          endif
            $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(AP_PACK__BIN_ALIGN) $(AP_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(APC_BIN) 0 $(OS_TYPE)
        endif
    else
        ifeq ($(BIN_FORMAT),COMPRESS7)
            ifeq ($(BUILD_TARGET),BLOADER_SYSTEM)
                $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(BLOADER_PACK__BIN_ALIGN) $(BLOADER_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(APC_BIN) 1 $(OS_TYPE)
            else
              ifeq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
                @cat $(BINPATH)_BLOADER/MERGE.bin $(ROOT)/boot/sboot/out/chunk_header.bin $(APC_BIN) > $@
                @ls -lh $(BINPATH)_BLOADER/MERGE.bin $(BINPATH)/$(AP_COMPRESS_NAME).bin $(MERGE_BIN)
              endif
                $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(AP_PACK__BIN_ALIGN) $(AP_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(APC_BIN) 1 $(OS_TYPE)
            endif
        else
            ifeq ($(BUILD_TARGET),BLOADER_SYSTEM)
                $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(BLOADER_PACK__BIN_ALIGN) $(BLOADER_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(AP_BIN)
            else
              ifeq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
                @cat $(BINPATH)_BLOADER/MERGE.bin $(ROOT)/boot/sboot/out/chunk_header.bin $(AP_BIN) > $@
                @ls -lh $(BINPATH)_BLOADER/MERGE.bin $(AP_BIN) $(MERGE_BIN)
              endif
                $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(AP_PACK__BIN_ALIGN) $(AP_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(AP_BIN)
            endif
        endif
    endif
#    @scripts/cmac/cmac.exe $(MERGE_BIN)

endif

ifeq ($(OS_TYPE),nos_aeon)
$(MERGE_BIN) : $(AP_BIN)
    @echo "[Rule - MERGE_BIN] => $@"
    @echo "[BIN] $@"
    $(shell cp $(ROOT)/boot/sboot/out/all.bin $(MERGE_BIN);)
    ifeq ($(BIN_FORMAT),COMPRESS)
        ifeq ($(BUILD_TARGET),BLOADER_SYSTEM)
            $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(BLOADER_PACK__BIN_ALIGN) $(BLOADER_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(APC_BIN) 0 $(OS_TYPE)
        else
            ifeq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
                @cat $(BINPATH)_BLOADER/MERGE.bin $(ROOT)/boot/sboot/out/chunk_header.bin $(APC_BIN) > $@
                @ls -lh $(BINPATH)_BLOADER/MERGE.bin $(BINPATH)/$(AP_COMPRESS_NAME).bin $(MERGE_BIN)
            endif
            $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(AP_PACK__BIN_ALIGN) $(AP_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(APC_BIN) 0 $(OS_TYPE)
        endif
    else
        ifeq ($(BIN_FORMAT),COMPRESS7)
            ifeq ($(BUILD_TARGET),BLOADER_SYSTEM)
                $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(BLOADER_PACK__BIN_ALIGN) $(BLOADER_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(APC_BIN) 1 $(OS_TYPE)
            else
              ifeq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
                @cat $(BINPATH)_BLOADER/MERGE.bin $(ROOT)/boot/sboot/out/chunk_header.bin $(APC_BIN) > $@
                @ls -lh $(BINPATH)_BLOADER/MERGE.bin $(BINPATH)/$(AP_COMPRESS_NAME).bin $(MERGE_BIN)
              endif
                $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(AP_PACK__BIN_ALIGN) $(AP_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(APC_BIN) 1 $(OS_TYPE)
            endif
        else
            ifeq ($(BUILD_TARGET),BLOADER_SYSTEM)
                $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(BLOADER_PACK__BIN_ALIGN) $(BLOADER_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(AP_BIN)
            else
              ifeq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
                @cat $(BINPATH)_BLOADER/MERGE.bin $(ROOT)/boot/sboot/out/chunk_header.bin $(AP_BIN) > $@
                @ls -lh $(BINPATH)_BLOADER/MERGE.bin $(AP_BIN) $(MERGE_BIN)
              endif
                $(BinIDPackFiles) -BIGENDIAN -CRC16ENABLE -multiflash $(AP_PACK__BIN_ALIGN) $(AP_PACK__FILE_ALIGN) 0958336900 $@ $(BIN_INFO) $(IMGINFO_OFFSET) $(AP_BIN) $(OS_TYPE)
            endif
        endif
    endif
endif

#=================================================================================================

$(RES_BIN) : $(AP_ELF)
    @echo "[BIN] $@"
    $(BinIDPackResources) -BIGENDIAN -CRC16ENABLE 8 8 0958336900 $< $@ $(BIN_INFO)
    @cp core/bin/s7/audio/out_dvb_t3_d.bin $(BINPATH)/au_d.bin
    @cp core/bin/s7/audio/out_dvb_t3_s.bin $(BINPATH)/au_s.bin
    cp tv-ap/dvb/ui2/logo/Mstar_OSD_Logo_683x384.jpg $(BINPATH)/boot0.jpg
    cp tv-ap/dvb/ui2/logo/mp3.mp3 $(BINPATH)/boot0.mp3
    @./generate_pnl_bin.sh $(BINPATH)/AP.elf $(BINPATH)

#=================================================================================================

$(OBJ_B) $(BL_OBJ_B): $(OBJPATH)/%.o : %.bin
    @echo "[BIN] $@"
    @$(shell cd $(dir $<); $(OBJCOPY) -I binary -O elf32-littleaeon -B aeon --prefix-sections=bin $(notdir $<) $(abspath $@))

#=================================================================================================

$(OBJPATH)/%.o: %.S
    $(call make-depend-compile,$<,$@,$(subst .o,.d,$@))

#=================================================================================================

$(OBJPATH)/%.o: %.c
    $(call make-depend-compile,$<,$@,$(subst .o,.d,$@))

#=================================================================================================

$(OBJPATH)/%.o: %.bin
    @echo "[BIN] $@"
    @$(shell cd $(dir $<); $(OBJCOPY) -I binary -O elf32-littleaeon -B aeon --prefix-sections=bin $(notdir $<) $(abspath $@))

#=================================================================================================
.c.ln:  ;   @echo "[lint]" ;  lint -abhi $*.c
.elf.dis: ; @echo "[Rule - .elf.dis] $@" ; $(OBJDUMP) -d -h -S $< > $@
.elf.sym: ; @echo "[SYM] $@" ; $(NM) -a -S --size-sort $< > $@
.elf.siz: ; @echo "[SIZ] $@" ; $(SIZE) $< > $

#=================================================================================================

ap_dis:
    @echo "[Rule - ap_dis] $(AP_DIS)"
    rm -rf $(AP_DIS)
    $(OBJDUMP) -d -h -s $(AP_ELF) > $(BINPATH)/ap_dis-dhs.txt

#=================================================================================================

LINT_INC=$(subst -I,,$(INC_DIR))
LINT_SRC_C=$(SRC_C)
LINT_DEF=$(subst -D,-d,$(filter -D%,$(CC_OPTS)))

lint:
ifneq ($(DISABLE_LINT),1)
    @(\
    echo "scripts/co-gnu3.lnt"; \
    echo $(LINT_OPT1); \
    echo | $(CPP) -dM | \
    sed -e '/LONG_LONG/d' | \
    sed -e 's/#define \([^ ]*\) "\(..*\)"/-d"\1=(\2)"/' | \
    sed -e 's/#define \([^ ]*\) \(..*\)/-d"\1=\2"/' | \
    sed -e 's/#define /-d/'; \
    for i in $(LINT_DEF); do \
        echo $$i; \
    done; \
    for i in $(LINT_INC); do \
        echo -i\"$$i\"; \
    done; \
    for i in `$(REALPATH) \`echo | $(CPP) -x c -Wp,-v 2>&1 | grep '^ '\``; do \
        echo -i\"$$i\"; \
    done; \
    for i in $(LINT_SRC_C); do \
        echo $$i; \
    done; \
    ) > $(BINPATH)/$(PROJ).lnt
    @$(FLINT) -fff $(BINPATH)/$(PROJ).lnt > $(BINPATH)/LINT.txt; \
     grep -v 'Module:' $(BINPATH)/LINT.txt | grep -v '^$$' ; true
    @echo
    @echo `grep 'Error' $(BINPATH)/LINT.txt | wc -l` LINT Errors
endif

#=================================================================================================
.PHONY: $(OBJPATH)/tv-ap/dvb/ui/MApp_ZUI_ACTmenufunc.o

ifeq ($(OS_TYPE),nos_aeon)
loader:
    @echo "[LOADER]"
    #@$(AR) -r $(ROOT)/core/api/msAPI_Flash.a $(OBJPATH)/core/api/msAPI_Flash.o
ifeq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
    @$(MAKE) PROJ=$(PROJ)_BLOADER
endif
endif
ifeq ($(OS_TYPE),nos_mips)
loader:
ifeq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
    @echo "=================== [Rule - LOADER] =================="
    $(MAKE) PROJ=$(PROJ)_BLOADER
    @echo "=================== [Rule - LOADER] End =================="
endif
endif

#=================================================================================================
# Project Setup
ifeq ($(OS_TYPE),nos_mips)
setup:
    @echo "[Rule - setup] PROJ_$(PROJ)"
    @mkdir -p $(OBJDIR) $(BINDIR);
    @rm -f $(call src-to-obj,$(REBUILD_FILES))
    @cp -f $(ROOT)/project/loader/nos_mips.ld $(LOADER); chmod 744 $(LOADER)
    @$(AWK) '{if ($$2=="BEON_MEM_ADR") print $$3;}' $(MMAP) > $(ROOT)/project/mmap/mips_mem_adr
    @$(AWK) 'BEGIN { \
                getline < "$(ROOT)/project/mmap/mips_mem_adr"; \
                ADR = $$1 \
            } \
            { \
                if ($$1=="RAM_START") gsub($$3, "("ADR"+0x80000000);", $$0); \
                if ($$3=="ORIGIN") gsub($$5, "("ADR"+0x80000000+0x180)", $$0); \
                if ($$1==".prog_img_info") gsub($$2, "("ADR"+0x80000000+0x1100)", $$0); \
                if ($$1==".img_info") gsub($$2, "("ADR"+0x80000000+0x2000)", $$0); \
                if ($$1==".isp_info") gsub($$2, "("ADR"+0x80000000+0x3000)", $$0); \
                print > FILENAME ".tmp"; \
            }' $(LOADER)
            @mv -f $(LOADER).tmp $(LOADER)
    @$(AWK) '{if ($$2=="BEON_MEM_LEN") print $$3;}' $(MMAP) > $(ROOT)/project/mmap/mips_mem_len
    @$(AWK) 'BEGIN { \
                getline < "$(ROOT)/project/mmap/mips_mem_len"; \
                LEN = $$1 \
            } \
            { \
                if ($$1=="RAM_SIZE") gsub($$3, "("LEN");", $$0); \
                if ($$7=="LENGTH") gsub($$9, ""LEN"", $$0); \
                print > FILENAME ".tmp"; \
            }' $(LOADER)
            @mv -f $(LOADER).tmp $(LOADER)
else
ifeq ($(OS_TYPE),nos_aeon)
setup:
    @echo "[SETUP START] - PROJ_$(PROJ)"
    @mkdir -p $(OBJDIR) $(BINDIR);
    @rm -f $(call src-to-obj,$(REBUILD_FILES))
    rm -f $(AP_ELF).tmp
    rm -f $(AP_BT_SYM)
    rm -f $(AP_BT_OBJ)
    cp -f $(ROOT)/project/loader/target.ld $(LOADER); chmod 744 $(LOADER)
    @$(AWK) '{if ($$2=="BEON_MEM_ADR") print $$3;}' $(MMAP) > $(ROOT)/project/mmap/aeon_mem_adr
    @$(AWK) 'BEGIN { \
                getline < "$(ROOT)/project/mmap/aeon_mem_adr"; \
                ADR = $$1 \
            } \
            { \
                if ($$1=="RAM_START") gsub($$3, ""ADR";", $$0); \
                if ($$1=="ram" && $$3=="ORIGIN") gsub($$5, ""ADR",", $$0); \
                if ($$1==".prog_img_info") gsub($$2, "("ADR"+0x1100)", $$0); \
                if ($$1==".img_info") gsub($$2, "("ADR"+0x2000)", $$0); \
                if ($$1==".isp_info") gsub($$2, "("ADR"+0x3000)", $$0); \
                if ($$1==".vectors") gsub($$2, "("ADR")", $$0); \
                print > FILENAME ".tmp"; \
            }' $(LOADER)
            @mv -f $(LOADER).tmp $(LOADER)
    @$(AWK) '{if ($$2=="BEON_MEM_LEN") print $$3;}' $(MMAP) > $(ROOT)/project/mmap/aeon_mem_len
    @$(AWK) 'BEGIN { \
                getline < "$(ROOT)/project/mmap/aeon_mem_len"; \
                LEN = $$1\
            } \
            { \
                if ($$1=="RAM_SIZE") gsub($$3, ""LEN";", $$0); \
                if ($$1=="ram" && $$6=="LENGTH") gsub($$8, ""LEN"", $$0); \
                print > FILENAME ".tmp"; \
            }' $(LOADER)
            @mv -f $(LOADER).tmp $(LOADER)
    @echo "[SETUP END]"
else
setup:
    @mkdir -p $(OBJDIR) $(BINDIR);
    @rm -f $(call src-to-obj,$(REBUILD_FILES))
    @$(AWK) '{if ($$1==".prog_img_info") print "#define IMG_INFO_OFFSET " $$2"+0x10000"}' \
            $(ROOT)/project/loader/target.ld > $(ROOT)/core/middleware/usbupgrade/include/mw_imginfo.h
endif
endif

#=================================================================================================

env:
    @echo CC_OPTS = $(CC_OPTS)
    @echo LD_OPTS = $(LD_OPTS)
    @echo SRC = $(SRC)

#=================================================================================================

# Project API
DOCGEN :
#    doxygen.exe $(ROOT)/project/Doxygen/Venus_3RD_PARTY_DDI_API.doxygen

#=================================================================================================
# Project Clean
clean :
    find $(OBJPATH) -name '*.o' -exec rm -f {} \;
    rm -f $(BINPATH)/$(AP_NAME).* $(BINPATH)/$(MERGE_NAME).* ###$(BINPATH)/$(AP_NAME)_2.* $(BINPATH)/$(MERGE_NAME)_2.*
        ifeq ($(BUILD_TARGET),MAIN_AP_SYSTEM)
        @$(MAKE) PROJ=$(PROJ)_BLOADER clean
        endif

realclean:
    rm -rf Bin_* Obj_*

clean_obj:
    rm -rf Obj_*

#=================================================================================================
checkstack: $(AP_ELF)
ifeq ($(CROSSCOMPILE), aeon-)
    $(OBJDUMP) -d $(AP_ELF) | scripts/checkstack.pl aeon
else
    $(OBJDUMP) -d $(AP_ELF) | scripts/checkstack.pl mips
endif

#=================================================================================================

checkbinsize:
ifeq ($(CHECKBINSIZE),yes)
    @echo "[Rule - checkbinsize]"
    $(CHECK_BIN_DIR)/CheckBinSize_v05.sh  $(CHECK_BIN_DIR)/$(CHECKBINSIZE_PARA)
else
    @echo "[Rule - checkbinsize - skip]"
endif

#=================================================================================================
# Project Dependence

# $(call make-depend-compile,source-file,object-file,depend-file)
define make-depend-compile
    @echo "[CC]  $1"
    @mkdir -p $(dir $2)
    @$(CC) -MM -MF $3 -MP -MT $2 $(CC_OPTS) $1
    @$(CC) $(CC_OPTS) -o $2 -c $1
endef

src-to-obj = $(patsubst %.bin,$(OBJPATH)%.o,\
             $(patsubst %.S,$(OBJPATH)/%.o,\
             $(patsubst %.c,$(OBJPATH)/%.o,$1)))

-include $(OBJ_S:.o=.d) $(OBJ_C:.o=.d)