###############################################################################
#
# MODULE:   Makefile
#
# DESCRIPTION: Makefile for the Remote
# 
###############################################################################
#
# This software is owned by NXP B.V. and/or its supplier and is protected
# under applicable copyright laws. All rights are reserved. We grant You,
# and any third parties, a license to use this software solely and
# exclusively on NXP products [NXP Microcontrollers such as JN5148, JN5142, 
# JN5139]. You, and any third parties must reproduce the copyright and 
# warranty notice and any other legend of ownership on each copy or partial 
# copy of the software.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Copyright NXP B.V. 2012. All rights reserved
#
###############################################################################
# Subversion variables
# $HeadURL: https://www.collabnet.nxp.com/svn/lprf_apps/Application_Notes/JN-AN-1158-ZigBee-RF4CE-Demonstration/Tags/Release_2v2-Public/Remote/Build/Makefile $
# $Revision: 10455 $
# $LastChangedBy: nxp46755 $
# $LastChangedDate: 2013-03-19 10:02:59 +0000 (Tue, 19 Mar 2013) $
# $Id: Makefile 10455 2013-03-19 10:02:59Z nxp46755 $ 
#
###############################################################################

# Application target name
TARGET = Remote
REDUCED_MAC_LIB_SUFFIX = ZIGBEE_

##############################################################################
#User definable make parameters that may be overwritten from the command line

# Default target device is JN5168
JENNIC_CHIP ?= JN5168

# Default development kit target hardware
EK ?= EK001
CFLAGS += -D$(EK)

##############################################################################
# Default DK4 development kit target hardware

ifeq ($(EK),EK001)
JENNIC_PCB ?= DEVKIT4
else
JENNIC_PCB ?= DEVKIT3
endif 

###############################################################################
# Select the network stack (e.g. MAC, ZBPro, RF4CE)

JENNIC_STACK ?= RF4CE

###############################################################################
# Stack specific options


###############################################################################
# Debug options - define DEBUG to enable
# DEBUG ?=HW
#
# Define which UART to use for HW debug
# DEBUG_PORT ?= UART1

###############################################################################
# Define TRACE to use with DBG module
TRACE ?=0

ifeq ($(TRACE), 1)
CFLAGS  += -DDBG_ENABLE
#APPLIBS += DBG
$(info Building trace version ...)
# Enable any debug output here:
#CFLAGS  += -DTRACE_APP=1
endif

STACK_MEASURE ?=0
ifeq ($(STACK_MEASURE),1)
CFLAGS  += -DSTACK_MEASURE
endif

CFLAGS  += -Os
CFLAGS  += -Wno-unreachable-code

###############################################################################
# Path definitions

# Use if application directory contains multiple targets
SDK_BASE_DIR   = $(abspath ../../../..)
APP_BASE_DIR   = $(abspath ../..)
SDK_LIB_DIR    = $(SDK_BASE_DIR)/Components
UTIL_SRC_DIR   = $(SDK_LIB_DIR)/Utilities/Source
APP_BLD_DIR    = $(APP_BASE_DIR)/$(TARGET)/Build
APP_SRC_DIR    = $(APP_BASE_DIR)/$(TARGET)/Source
APP_CMN_DIR    = $(APP_BASE_DIR)/Common/Source
##############################################################################
# Application source files

# Note: Path to source file is found using vpath below, so only .c filename is required
APPSRC  = Remote.c
APPSRC += ZID.c
APPSRC += ZRC.c
ifeq ($(JENNIC_PCB), DEVKIT4)
APPSRC += app_buttons.c
endif
ifeq ($(TRACE), 1)
APPSRC += Printf.c
endif
ifeq ($(STACK_MEASURE),1)
APPSRC += StackMeasure.c
endif

###############################################################################
# Standard Application header search paths

#INCFLAGS += $(addsuffix /Include,$(addprefix -I$(SDK_LIB_DIR)/,$(APPLIBS)))
INCFLAGS += -I$(APP_SRC_DIR)
INCFLAGS += -I$(APP_SRC_DIR)/..
INCFLAGS += -I$(APP_CMN_DIR)
#INCFLAGS += -I$(APP_COMMON_SRC)

# Application specific include files
INCFLAGS += -I$(APP_BASE_DIR)/Common/Source/

INCFLAGS += -I$(SDK_LIB_DIR)/AppQueueApi/Include
INCFLAGS += -I$(SDK_LIB_DIR)/NXPLogo/Include
INCFLAGS += -I$(SDK_LIB_DIR)/Utilities/Include
INCFLAGS += -I$(SDK_LIB_DIR)/RF4CE/Include


###############################################################################
# Application libraries
# Specify additional Component libraries

#APPLIBS += 

###############################################################################

# You should not need to edit below this line

###############################################################################
###############################################################################
# Configure for the selected chip or chip family

include $(SDK_BASE_DIR)/Chip/Common/Build/config.mk
include $(SDK_BASE_DIR)/Platform/Common/Build/Config.mk
include $(SDK_BASE_DIR)/Stack/Common/Build/config.mk

###############################################################################

TEMP = $(APPSRC:.c=.o)
APPOBJS = $(TEMP:.S=.o)

###############################################################################
# Application dynamic dependencies

APPLIBS += RF4CE
APPDEPS = $(APPOBJS:.o=.d)

LDLIBS += PDM_EEPROM_JN516x
LINKCMD = AppBuildRF4CE_$(JENNIC_CHIP).ld
###############################################################################
# Linker

# Add application libraries before chip specific libraries to linker so
# symbols are resolved correctly (i.e. ordering is significant for GCC)

APPLDLIBS := $(foreach lib,$(APPLIBS),$(if $(wildcard $(addprefix $(COMPONENTS_BASE_DIR)/Library/lib,$(addsuffix _$(TEMPCHIP).a,$(lib)))),$(addsuffix _$(TEMPCHIP),$(lib)),$(addsuffix _$(JENNIC_CHIP_FAMILY),$(lib))))
LDLIBS := $(APPLDLIBS) $(LDLIBS)

###############################################################################
# Dependency rules

.PHONY: all clean
# Path to directories containing application source 
vpath % $(SDK_BASE_DIR):$(APP_BASE_DIR):$(SDK_LIB_DIR):$(APP_BLD_DIR):$(APP_SRC_DIR):$(APP_SRC_DIR)/..:$(APP_CMN_DIR)


all: $(TARGET)_$(JENNIC_CHIP)_$(EK).bin

-include $(APPDEPS)
%.d:
	rm -f $*.o

%.o: %.S
	$(info Assembling $< ...)
	$(CC) -c -o $(subst Source,Build,$@) $(CFLAGS) $(INCFLAGS) $< -MD -MF $(APP_BLD_DIR)/$*.d -MP
	@echo

%.o: %.c 
	$(info Compiling $< ...)
	$(CC) -c -o $(subst Source,Build,$@) $(CFLAGS) $(INCFLAGS) $< -MD -MF $(APP_BLD_DIR)/$*.d -MP
	@echo

$(TARGET)_$(JENNIC_CHIP)_$(EK).elf: $(APPOBJS) $(addsuffix _$(JENNIC_CHIP_FAMILY).a,$(addprefix ../../../Components/Library/lib,$(APPLIBS))) 
	$(info Linking $@ ...)
	$(CC) -Wl,--gc-sections -Wl,-u_AppColdStart -Wl,-u_AppWarmStart $(LDFLAGS) -T$(LINKCMD) -o$@  -Wl,--start-group $(APPOBJS) $(addprefix -l,$(LDLIBS)) -Wl,--end-group -Wl,-Map,$(TARGET)_$(JENNIC_CHIP)_$(EK).map 
	@echo

$(TARGET)_$(JENNIC_CHIP)_$(EK).bin: $(TARGET)_$(JENNIC_CHIP)_$(EK).elf 
	$(info Generating binary ...)
	$(OBJCOPY) -S -O binary "$<" "$@"
	$(OBJDUMP) -d -s -S $(TARGET)_$(JENNIC_CHIP)_$(EK).elf > $(TARGET)_$(JENNIC_CHIP)_$(EK).dump
	$(SIZE) $(TARGET)_$(JENNIC_CHIP)_$(EK).elf
#########################################################################

clean:
	rm -f $(APPOBJS) $(APPDEPS) $(TARGET)_$(JENNIC_CHIP)_$(EK).bin $(TARGET)_$(JENNIC_CHIP)_$(EK).elf $(TARGET)_$(JENNIC_CHIP)_$(EK).map $(TARGET)_$(JENNIC_CHIP)_$(EK).dump

#########################################################################
