##############################################################################
#
#       O       OOOO  OOOOOOO   O       O         ## ## ##
#      O O     O         O      O      O O
#     O   O    O         O      O     O   O      ## ## ##
#    OOOOOOO   O         O      O    OOOOOOO
#   O       O   OOOO     O      O   O       O   ## ## ##
#
#-----------------------------------------------------------------------------
#  HEAD : (c) ACTIA Automotive 2012 : " Any copy and re-use of this
#         file without the written consent of ACTIA is forbidden "
#         --------------------------------------------------------------------
#         Classification :  (-)No   (-)Confident. ACTIA   (-)Confident. Client
#         --------------------------------------------------------------------
#         MAKE : Project make file
##############################################################################
#   Version 		:$Revision: 2451 $
#   Url				:$HeadURL: https://forge2.actia.fr/bgse/8160_actiway/ADU/_Reference/Demos/ADU_GPU_GUI_App/ADU_GPU_Gui.mk $
#   Updated the 	:$Date: 2012-05-03 18:13:40 +0200 (jeu., 03 mai 2012) $
#   By 				:$Author: sgot $
##############################################################################
SHELL=cmd.exe
##############################################################################
# Check (DO NOT MODIFY!)
##############################################################################
ifndef MODE
MODE	= Serial
else
ifneq	($(MODE),Debug)
ifneq	($(MODE),Serial)
MODE	= Serial
endif
endif	
endif


##############################################################################
# Setup you project name
##############################################################################
#Elf name
PROJ_NAME   = App_GUI_App


##############################################################################
# Configuration (DO NOT MODIFY!)
##############################################################################
PLATFORM_PATH	= .

LIB_PATH        = $(PLATFORM_PATH)/Lib
SOURCES_PATH	= $(PLATFORM_PATH)/Sources
OBJ_PATH		= $(PLATFORM_PATH)/Obj/$(MODE)
OUTPUT_PATH		= $(PLATFORM_PATH)/Bin

ifeq	($(MODE),Debug)
TARGET		=$(PROJ_NAME)_d
else
TARGET		=$(PROJ_NAME)
endif		

#Binary memory address
export BIN_ADDR = 0x400000


##############################################################################
# Build option
# Define symbols using -D directive 
##############################################################################
ifeq	($(MODE),Debug)
AS_OPTION		=
C_OPTION		= -DUSE_PROMPT
CPP_OPTION		=
else
AS_OPTION		=
C_OPTION		= 
CPP_OPTION		=
endif


##############################################################################
# Include paths to header files 
##############################################################################
AS_INCLUDES     =

C_INCLUDES      = 	-I$(PLATFORM_PATH)/Include\
					-I$(PLATFORM_PATH)/Include/FreeRTOS\
					-I$(PLATFORM_PATH)/Include/FatFs
					
CPP_INCLUDES    = 	-I$(PLATFORM_PATH)/Include\
					-I$(PLATFORM_PATH)/Include/FreeRTOS\
					-I$(PLATFORM_PATH)/Include/FatFs


##############################################################################
# Define external makefiles
##############################################################################
EXT_MAKE = 	$(PLATFORM_PATH)/_build_settings.mk\
			$(SOURCES_PATH)/APP/_App.mk\
			$(SOURCES_PATH)/CMD/_Cmd.mk\
			$(SOURCES_PATH)/GUI/_GUI.mk


##############################################################################
# Load External makefiles (DO NOT MODIFY!)
##############################################################################	
VPATH = $(SOURCES_PATH)
SOURCES = 
SOURCESCPP = 
SOURCESASM = 
include	$(EXT_MAKE)	
EXT_MAKE += $(PLATFORM_PATH)/Makefile


##############################################################################
# Define libraries
##############################################################################
ifeq	($(MODE),Debug)
LIBRARIES =	libAduGpuBsp_d.a
else
LIBRARIES =	libAduGpuBsp.a
endif

#mandatory for microblaze application building
LIBRARIES +=	libxil.a


###############################################################################
# Sections to include in binary (DO NOT MODIFY!)
###############################################################################
BIN_SECTION	 = 	-j .bsp_ver\
				-j .app_ver\
				-j .text\
				-j .init\
				-j .fini\
				-j .rodata\
				-j .sdata2\
				-j .sbss2\
				-j .data\
				-j .got\
				-j .got1\
				-j .got2\
				-j .ctors\
				-j .dtors\
				-j .eh_frame\
				-j .jcr\
				-j .gcc_except_table\
				-j .sdata\
				-j .sbss\
				-j .tdata


###############################################################################
# Prepare build (DO NOT MODIFY!)
###############################################################################
OBJECTS = $(SOURCES:%.c=$(OBJ_PATH)/%.o)
OBJECTS += $(SOURCESCPP:%.cpp=$(OBJ_PATH)/%.o)
OBJECTS += $(SOURCESASM:%.s=$(OBJ_PATH)/%.o)
TARGET_PATH = $(OUTPUT_PATH)/$(TARGET).elf 

#dependencies
-include $(subst .o,.d,$(OBJECTS))
$(LIBRARIES):


###############################################################################
# Tagrets definition (DO NOT MODIFY!)
###############################################################################
.DEFAULT: info

info:
	@echo ---- Actia Actiway applications makefile ----
	@echo make makefile.mk MODE=[Mode] [Target]
	@echo Mode list:
	@echo  - 'Serial' generates executable for production
	@echo  - 'Debug'  generates executable for debug
	@echo Target list:
	@echo  - Use 'conf' to show the configuration
	@echo  - Use 'clean' to remove old object files
	@echo  - Use 'build' to update and link the executable
	
conf:
	@echo "----- Configuration ----------"
	@echo Mode       : $(MODE)
	@echo Tool chain : $(TOOLCHAIN)
	@echo ASM options : $(AS_FLAGS)
	@echo C options : $(CC_FLAGS)
	@echo CPP options : $(CCPP_FLAGS)
	@echo Link options : $(LINKER_FLAGS)
	@echo Sources    : $(SOURCES)
	@echo Objects    : $(OBJECTS)
	@echo Target     : $(TARGET)
	@echo Path       : $(VPATH)

$(OBJ_PATH):
	@$(MD) $(subst /,\,$(OBJ_PATH))

$(OUTPUT_PATH):
	@$(MD) $(subst /,\,$(OUTPUT_PATH))

clean:
	@echo "----- Clean ------------------"
	@$(RM) $(subst /,\,$(OUTPUT_PATH)/$(TARGET).*) /S /Q
	@$(RM) $(subst /,\,$(OBJ_PATH)/*.*) /S /Q

link: $(OUTPUT_PATH) $(OBJECTS) $(LIBRARIES)
	@echo ------------ Link ------------
	@$(CPP) $(LINKER_FILE) -L$(LIB_PATH) $(LINKER_FLAGS) -o"$(OUTPUT_PATH)/$(TARGET).elf" $(OBJECTS) $(subst lib,-l,$(subst .a,,$(LIBRARIES)))
	@$(OBJCPY) $(OUTPUT_PATH)/$(TARGET).elf $(OUTPUT_PATH)/$(TARGET).bin -O binary $(BIN_SECTION) -S
	$(SZ) $(OUTPUT_PATH)/$(TARGET).elf
ifeq ($(MODE),Serial)
	@$(RM) $(subst /,\,$(OUTPUT_PATH)/$(TARGET).elf)
endif
	
build: $(OBJ_PATH) link
ifeq	($(MODE),Serial)
	@echo --------- Conversion ---------     
	@$(SETH) -c -s $(OUTPUT_PATH)/$(TARGET).bin
	@$(LZMA) e $(OUTPUT_PATH)/$(TARGET).bin $(OUTPUT_PATH)/temp -lc0
	@$(SETH) -s $(OUTPUT_PATH)/temp
	@$(OBJCPY) -I binary -O srec --srec-forceS3 --change-addresses 0x20000000 $(OUTPUT_PATH)/temp $(OUTPUT_PATH)/$(PROJ_NAME).MHX
#	@$(OBJCPY) -I binary -O srec --srec-forceS3 $(OUTPUT_PATH)/temp $(OUTPUT_PATH)/$(PROJ_NAME).mhx
	@$(RM) $(subst /,\,$(OUTPUT_PATH)/temp)
	@echo --
	@echo Downloadable application is : $(OUTPUT_PATH)/$(PROJ_NAME).mhx
	@echo --
		
#	@$(BS) $(OUTPUT_PATH)/$(TARGET).bin  $(OUTPUT_PATH)/temp -w 2
#	@$(OBJCPY) -I binary -O ihex --change-addresses=$(BIN_ADDR) $(OUTPUT_PATH)/temp $(OUTPUT_PATH)/temp2
#	@$(OBJCPY) -I ihex -O ihex --set-start= $(OUTPUT_PATH)/temp2 $(OUTPUT_PATH)/$(PROJ_NAME).mcs
#	@$(RM) $(subst /,\,$(OUTPUT_PATH)/temp)
#	@$(RM) $(subst /,\,$(OUTPUT_PATH)/temp2)
#	@echo iMPACT download binary generated : $(PROJ_NAME).mcs

endif
	@echo ------------ End -------------

	
##############################################################################
# Implicit rules (DO NOT MODIFY!)
##############################################################################	
$(OBJ_PATH)/%.o: %.s $(EXT_MAKE)
	@$(AS) $(AS_OPTION) $(CAS_FLAGS) $(AS_INCLUDES) -o $@ $<	
	@echo $@ Assembled
	
$(OBJ_PATH)/%.o: %.c $(EXT_MAKE)
	@$(CC) $(C_OPTION) $(CC_FLAGS) $(C_INCLUDES) -MMD -MP -MF$(subst .o,.d,$@) -MT$(subst .o,.d,$@) -o $@ $(subst ./,,$<)
	@echo $@ Compiled
	
$(OBJ_PATH)/%.o: %.cpp $(EXT_MAKE)
	@$(CPP) $(CPP_OPTION) $(CCPP_FLAGS) $(CPP_INCLUDES) -MMD -MP -MF$(subst .o,.d,$@) -MT$(subst .o,.d,$@) -o $@ $(subst ./,,$<)
	@echo $@ Compiled
	