#
#  Copyright (c) 2013-2014, Texas Instruments Incorporated
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#  *  Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#
#  *  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  *  Neither the name of Texas Instruments Incorporated nor the names of
#     its contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
#
#  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 OWNER 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.
#
#  ======== makefile ========
#  GNUmake-based makefile
#

EXBASE = ..
include $(EXBASE)/products.mak

# Configuro settings.

#  CONFIGPKG is the base name of the configuration script (local.cfg)
CONFIGPKG = local
CONFIG_BLD := ./_config.bld

# Name the xdc target and platform
XDCTARGET = ti.targets.elf.C64P

XDCPLATFORM = ti.platforms.evm6472:dsp

#
#  TODO:  We shouldn't have to set this
#
FC_REPO         ?= $(FC_INSTALL_DIR)/packages
EXAMPLES_ROOTDIR = $(FC_INSTALL_DIR)/examples

XDC_PATH := $(XDC_INSTALL_DIR)/packages;$(FC_REPO);$(BIOS_INSTALL_DIR)/packages;$(XDAIS_INSTALL_DIR)/packages;$(EDMA3_LLD_INSTALL_DIR)/packages;$(EXAMPLES_ROOTDIR);$(XDC_PATH)

REPO_PATH = $(subst ;, ,$(XDC_PATH))

# The path to the configuro utility
CONFIGURO = XDCPATH="$(XDC_PATH)" $(XDC_INSTALL_DIR)/xs xdc.tools.configuro

COMPILER_FILE = $(CONFIGPKG)/compiler.opt
LINKER_FILE = $(CONFIGPKG)/linker.cmd

#PROFILE = release
PROFILE = debug

#
#  Location of codec packages and codec libraries to link into the server
#

CODECS_REPO = ../codec/
CODEC_LIBS = -l"../codec/lib/$(PROFILE)/universal_dma_ti_ecpy.ae64P"
CODEC_LINKER_FILE = -l"../codec/local_$(PROFILE)_e64P.cmd"

PROGRAM = bin/$(PROFILE)/universal_dma_example_dsp.xe64P

CCPROFILE.release = -O2
CCPROFILE.debug = -g -D_DEBUG_=1 --symdebug:dwarf



CPPFLAGS = -Dxdc_target_name__=C64P \
    -Dxdc_target_types__=ti/targets/elf/std.h $(addprefix -I,$(CODECS_REPO))

# Augment the standard $(CPPFLAGS) variable, adding the
# Configuro-generated compiler file.
CPPFLAGS +=  $(shell cat $(COMPILER_FILE))

LDFLAGS = $(LNKPROFILE.$(PROFILE)) -w -q -u _c_int00 -c  \
	$(addprefix -I,$(REPO_PATH))

LDLIBS = -l $(ti.targets.elf.C64P)/lib/rts64plus_elf.lib

LNKPROFILE.debug = -q --compress_dwarf
LNKPROFILE.release = -q --compress_dwarf

#
#  ======== toolchain macros ========
#
#
# ti.targets.elf.C64P is the location of your C64P CGtools.
#  Check to make sure this variable is set.
#
ifeq (,$(wildcard $(ti.targets.elf.C64P)))
    $(error ti.targets.elf.C64P  is set to "$(ti.targets.elf.C64P)" which is invalid. Please edit the products.mak file and set this variable to the cgtools location)
endif

CC = $(ti.targets.elf.C64P)/bin/cl6x
LNK = $(ti.targets.elf.C64P)/bin/lnk6x

#
#  Source files.
#

APP.srcs =               \
	main_BIOS.c      \
	app.c

APP.objs = $(addprefix bin/$(PROFILE)/,$(patsubst %.c,%.oe64P,$(APP.srcs)))

#
#  This linker command file is located in $(FC_REPO)/ti/sdo/fc/utils/rtcfg/bios
#  The vpath variable is set to this directory so that make will find it. If
#  desired, this linker command file can be overridden, by copying it to this
#  directory and modifying it as needed.
#
LOCAL_LINKER_FILE = local_$(PROFILE)_e64P.cmd
#
#  Set the vpath variable for make to locate .c and .cmd files not in this
#  directory.
#

#
#  ======== make commands ========
#
all: $(CONFIGPKG)/linker.cmd
	$(MAKE) $(PROGRAM)


# Link
$(PROGRAM):  $(APP.objs) $(LINKER_FILE) $(LOCAL_LINKER_FILE)
	@$(ECHO) "#"
	@$(ECHO) "# Making $@ ..."
	$(LNK) -o $@ $(LDFLAGS) $^ $(LDLIBS) $(CODEC_LIBS) $(CODEC_LINKER_FILE) -m $@.map


# Compile
bin/$(PROFILE)/%.oe64P : %.c $(LINKER_FILE)
	@$(ECHO) "#"
	@$(ECHO) "# Making $@ ..."
	$(CC) -c $(CPPFLAGS) $(CCPROFILE.$(PROFILE)) $(CFLAGS)  -eo.oe64P -fr=bin/$(PROFILE) $<


# The generated linker and compiler files depend on the generated
# config package
$(LINKER_FILE) $(COMPILER_FILE) : $(CONFIGPKG).cfg

# The generated config package depends on the 2 config scripts
$(CONFIGPKG)/linker.cmd : $(CONFIGPKG).cfg $(CONFIGBLD)
	$(CONFIGURO) -c $(ti.targets.elf.C64P) -o $(CONFIGPKG) -t $(XDCTARGET) \
	-p $(XDCPLATFORM) -b $(CONFIG_BLD) -r debug $(CONFIGPKG).cfg


install:
	@$(ECHO) "#"
	@$(ECHO) "# Making $@ ..."
	@$(MKDIR) $(INSTALL_DIR)
	$(CP) $(PROGRAM) $(INSTALL_DIR)


#
#  ======== clean ========
#
clean::
	@$(ECHO) "# Removing $(CONFIGPKG) directory..."
	$(RMDIR) $(CONFIGPKG)
	@$(ECHO) "# Removing bin directory..."
	$(RMDIR) bin


#  ======== standard macros ========
ifneq (,$(wildcard $(XDC_INSTALL_DIR)/bin/echo.exe))
    # use these on Windows
    CP      = $(XDC_INSTALL_DIR)/bin/cp
    ECHO    = $(XDC_INSTALL_DIR)/bin/echo
    MKDIR   = $(XDC_INSTALL_DIR)/bin/mkdir -p
    RM      = $(XDC_INSTALL_DIR)/bin/rm -f
    RMDIR   = $(XDC_INSTALL_DIR)/bin/rm -rf
else
    # use these on Linux
    CP      = cp
    ECHO    = echo
    MKDIR   = mkdir -p
    RM      = rm -f
    RMDIR   = rm -rf
endif

#  ======== install validation ========
ifeq (install,$(MAKECMDGOALS))
ifeq (,$(INSTALL_DIR))
$(error must specify INSTALL_DIR)
endif
endif


#  ======== create output directories ========
ifneq (clean,$(MAKECMDGOALS))
ifneq (,$(PROFILE))
ifeq (,$(wildcard bin/$(PROFILE)))
    $(shell $(MKDIR) -p bin/$(PROFILE))
endif
endif
endif
