
BUILD_OUT_IMG_DIR ?= $(ROOT_DIR)/out/$(CONFIG_CHIP)/img
BUILD_OUT_OBJ_DIR ?= $(ROOT_DIR)/out/$(CONFIG_CHIP)/obj



LD_FLAG = -EL -nostdlib -Wl,--gc-sections -mips32r3
C_DEF_FLAG =

C_COMPILE_FLAG = -EL -mips32r3 -mabi=32 -mmt -march=mips32r3 -ffunction-sections -fdata-sections -G0   #mips parameter
C_COMPILE_FLAG += -Wall -Werror -Wno-unused-variable -Wno-unused-function -Wno-pointer-sign -Wno-format -Wno-unused-but-set-variable -Wno-strict-aliasing -Wno-misleading-indentation  #gcc parameter
C_COMPILE_FLAG += -D__FILENAME__='"$(subst $(dir $<),,$<)"' #self def parameter
C_COMPILE_FLAG += -fdebug-prefix-map=$(CONFIG_CODE_PATH)/=./

ifeq ($(CONFIG_HARD_FLOAT),yes)
  C_COMPILE_FLAG += -mhard-float
  LD_FLAG += -mhard-float
  C_DEF_FLAG += -DHV_CONFIG_HARD_FLOAT
else
  C_COMPILE_FLAG += -msoft-float
  LD_FLAG += -msoft-float
endif

ifeq ($(CONFIG_PRINT),yes)
  C_DEF_FLAG += -DHV_CONFIG_ENABLE_PRINT
endif

C_DEF_FLAG += -DHV_CONFIG_CHIP_$(shell echo $(CONFIG_CHIP) | tr '[:lower:]' '[:upper:]')
C_DEF_FLAG += -DHV_CONFIG_ENDIAN_$(shell echo $(CONFIG_ENDIAN) | tr '[:lower:]' '[:upper:]')
C_DEF_FLAG += -DHV_CONFIG_ARCH_$(shell echo $(CONFIG_ARCH) | tr '[:lower:]' '[:upper:]')


USER_CONFIG_FILE := $(ROOT_DIR)/user/$(CONFIG_VENDOR)/$(CONFIG_PROJECT)/property.config

CONFIG_USER_DEFINE := $(shell cat $(USER_CONFIG_FILE) | grep "?=" | awk '{print " -DCONFIG_USER_"$$3}')
C_DEF_FLAG += $(CONFIG_USER_DEFINE)

#ASM_COMPILE_FLAG = -EL -mips32r3 -mabi=32 -mmt -march=mips32r3 -ffunction-sections -fdata-sections -G0
#ASM_COMPILE_FLAG += -Wall -Werror -Wno-unused-variable -Wno-unused-function -Wno-pointer-sign -Wno-format -Wno-unused-but-set-variable -Wno-strict-aliasing -Wno-misleading-indentation

#ifeq ($(CONFIG_HARD_FLOAT),yes)
#  ASM_COMPILE_FLAG += -mhard-float
#else
#  ASM_COMPILE_FLAG += -msoft-float
#endif

LINK_SCRIPT_DIR ?= $(ROOT_DIR)/build/scripts/ldscript/$(CONFIG_CHIP)
FILE_INC_CONFIG += -include $(ROOT_DIR)/user/$(CONFIG_VENDOR)/$(CONFIG_PROJECT)/BoardConfig.h
FILE_INC_CONFIG += -include $(ROOT_DIR)/user/$(CONFIG_VENDOR)/$(CONFIG_PROJECT)/ProjectConfig.h
FILE_INC_CONFIG += -include $(ROOT_DIR)/user/$(CONFIG_VENDOR)/$(CONFIG_PROJECT)/OSD/OsdConfig.h

MAKE=make
CC=mips-mti-elf-gcc
LD=mips-mti-elf-ld
AR=mips-mti-elf-ar
RANLIB=mips-mti-elf-ranlib
OBJCOPY=mips-mti-elf-objcopy
OBJDUMP=mips-mti-elf-objdump
GDB=mips-mti-elf-gdb

ifeq ($(COMPILE_MODE),release)
  C_COMPILE_FLAG += -O2
  #ASM_COMPILE_FLAG += -O0
  C_DEF_FLAG += -DRELEASE_VERSION
  HEADER=@
endif

ifeq ($(COMPILE_MODE),debug)
  C_COMPILE_FLAG += -g -O2
  #ASM_COMPILE_FLAG += -g
  C_DEF_FLAG += -DHV_SCALER_DEBUG_VERSION
  C_DEF_FLAG += -DCOMPILE_USERNAME=\"$(shell whoami)\"
  C_DEF_FLAG += -DCOMPILE_HOSTNAME=\"$(shell hostname)\"
  HEADER=@
endif

ASM_DEF_FLAG = $(C_DEF_FLAG)
ASM_COMPILE_FLAG=$(C_COMPILE_FLAG)

#@if [ $(COMPILE_MODE) = "release" ] ; then echo "$1" ; else echo ""; fi;
define ASM_COMPILE_RULE
 $(HEADER)$(CC) $(INCLUDES) $(ASM_COMPILE_FLAG) $(ASM_DEF_FLAG)  $(FILE_INC_CONFIG) -o $(BUILD_OUT_OBJ_DIR)/$1 -c $2
 @if [ $(COMPILE_MODE) = "release" ] ; then echo "$1" ;  fi;
 @if [ $(COMPILE_MODE) = "debug" ] ; then echo "$1" ;  fi;
endef
define C_COMPILE_RULE
 $(HEADER)$(CC) $(INCLUDES) $(C_COMPILE_FLAG) $(C_DEF_FLAG)   $(FILE_INC_CONFIG) -o $(BUILD_OUT_OBJ_DIR)/$1 -c $2
 @if [ $(COMPILE_MODE) = "release" ] ; then echo "$1" ;  fi;
 @if [ $(COMPILE_MODE) = "debug" ] ; then echo "$1" ;  fi;
endef

%.o : %.S
	$(call ASM_COMPILE_RULE,$@,$<)
%.o : %.c
	$(call C_COMPILE_RULE,$@,$<)



