################################################################################
# Freescale Semiconductor Inc.
# (c) Copyright 2010 Freescale Semiconductor Inc.
# ALL RIGHTS RESERVED.
#############################################################################/*!
#
#	@file Makefile
#
#	@version 1.0.3.0
#
#	@lastmodusr B16958
#
#	@lastmoddate Jun-8-2010
#
#	@brief Makefile for building the AN4147 examples
#
#	Targets:
#		make all NUM:=1..7 	- builds only specified application number
#		make all 		- builds all 7 applications
#		make clean_all 		- deletes all objects and elf files
#	Configuration:
#		COMPILER	- compiler used to build elf files (ghs is default)
#		GHS_DIR 	- installation dir of GreenHills compiler
#		DIAB_DIR 	- installation dir of WindRiver compiler
#
#	@project AN4147
#
#	@author B16958
#
##############################################################################*/
#
# Setup some the default values (compilers, paths)
ifeq ($(GHS_DIR),)
GHS_DIR:=C:/ghs/multi516
endif

ifeq ($(DIAB_DIR),)
DIAB_DIR:=C:/WindRiver/diab/5.6.1.0
endif

ifeq ($(CW_DIR),)
CW_DIR:=C:/Progra~1/Freesc~1/CodeWa~1.3
endif

ifeq ($(COMPILER),)
COMPILER:=ghs
endif

UTIL_FILES:=depend

################################################################################
# Tools
################################################################################
DELETE:=rm -f

################################################################################
# Setup example number to build
################################################################################
ifeq ($(NUM),)
# If the number not specified, recursively build all the elf targets
NUM:=$(words $(NUM_INDEX) x)
  ifneq ($(NUM),7)
  NEXT_RUN:=YES
  endif
endif

################################################################################
# Defining the rules based on the compiler
################################################################################
include $(COMPILER)/rules.mak

################################################################################
# Setup the names of the sources and the headers
################################################################################
COMMON_SRC:= app_main.c sys_init.c 
APPS:=		   \
	sci_app_01 \
	sci_app_02 \
	sci_app_03 \
	sci_app_04 \
	sci_app_05 \
	spi_app_01 \
	spi_app_02
APP_SRC:= $(addsuffix .c, $(APPS)) $(UTIL_FILES)/vector.s 
ifeq ($(COMPILER),cw)
APP_SRC+=$(UTIL_FILES)/startup_cw.s
else
APP_SRC+=$(UTIL_FILES)/startup.c
endif
HEADER_FILES:= $(addsuffix .h, $(APPS)) sys_init.h MPC560xB.h

################################################################################
# Object files and executable files
################################################################################
APP_EXE:= $(addsuffix .elf, $(APPS))
APP_OBJ:= $(addsuffix .o, $(APPS))
COMMON_OBJ:= $(COMMON_SRC:.c=.o) vector.o
ifeq ($(COMPILER),cw)
COMMON_OBJ+=startup_cw.o
else
COMMON_OBJ+=startup.o
endif

OUT_PATH:=out/
APP_EXE   := $(addprefix $(OUT_PATH), $(APP_EXE))
APP_OBJ   := $(addprefix $(OUT_PATH), $(APP_OBJ))
COMMON_OBJ:= $(addprefix $(OUT_PATH), $(COMMON_OBJ))


################################################################################
# Rules
################################################################################
all: $(word $(NUM),$(APP_EXE)) recursion 
vpath %.c $(UTIL_FILES)
vpath %.s $(UTIL_FILES)

$(word $(NUM),$(APP_EXE)): clean_main_obj $(word $(NUM),$(APP_OBJ)) $(COMMON_OBJ)
	$(call LINK_O_FILES,$(NUM))

$(OUT_PATH)%.o : %.c $(HEADER_FILES)
	$(call COMPILE_C_FILE,$(word $(NUM),$(APPS)))

$(OUT_PATH)%.o : %.s
	$(call COMPILE_S_FILE,$(word $(NUM),$(APPS)))

# main.o depends on compiler flags - need to clean it between elf targets
.PHONY: clean_main_obj
clean_main_obj:
	$(DELETE) $(OUT_PATH)app_main.o

.PHONY: clean_all
clean_all:
	$(DELETE) $(COMMON_OBJ) $(APP_OBJ) $(APP_EXE)

################################################################################
# Make recursion for other examples (instead of FOR LOOP)
################################################################################
.PHONY: recursion
recursion:
ifeq ($(NEXT_RUN),YES)
	$(MAKE) all NUM_INDEX:="$(NUM_INDEX) x"
endif
