# This makefile for use with GNU make, for example in ccsv4/utils/gmake/gmake.exe

.PHONY: clean tags FORCE

# The line below needed for vanilla gmake.	TI gmake seems to always use cmd.exe.
# For normal builds we will use TI gmake, which should be present in the build
# directory.
SHELL = cmd.exe

BIN = ..\Application_Firmware\util

CCDIR = ../Application_Firmware/c2000
CC = "$(CCDIR)\bin\cl2000"
AS = $(CC)
LD = $(CC)
HEX = "$(CCDIR)\bin\hex2000"
AWK = $(BIN)\mawk.exe
SED = $(BIN)\sed.exe
MV = $(BIN)\mv.exe
RM = $(BIN)\rm -rf
MKDIR = $(BIN)\mkdir -p

INCLUDE = \
	 --include_path="$(CCDIR)/include" \
	 --include_path="./DSP2803x_headers" \
	 --include_path="." \
	 --include_path="./inc" \
	 --include_path="./auto" \
	 --include_path="./IQmath/include"

LIBDIR = -i"$(CCDIR)/lib" -i"./lib" -i"./IQMath/lib"

SILICON_VERSION = --silicon_version=28

DEBUG = -g --define="_DEBUG" 

# The large memory model is required for program code in flash.
MEM_MODEL = --large_memory_model --define="LARGE_MODEL" --unified_memory

VERBOSITY = --quiet --diag_warning=225 --issue_remarks --keep_asm

C_OPT = \
	-O2 \
	$(SILICON_VERSION) \
	$(DEBUG) \
	$(INCLUDE) \
	$(MEM_MODEL) \
	$(VERBOSITY)

CFLAGS = $(C_OPT)

ASM_OPT = $(C_OPT) --asm_listing

# Needed to link tables in boot ROM
LD_ROM_OPT = -priority -x

# 17003-D: relocation overflowed
# 10015-D: output file cannot be loaded and run on a target system
# 10063-D: entry-point symbol other than "_c_int00" specified
# 16002-D: build attribute vendor section TI missing in (library)
LD_ERR_OPT = --diag_error=17003-D \
		  --diag_error=10015-D \
		  --diag_suppress=10063-D \
          --diag_suppress=16002-D \
	      --display_error_number

LD_OPT = \
	$(SILICON_VERSION) \
	$(DEBUG) \
	$(MEM_MODEL) \
	$(VERBOSITY) \
	$(LD_ERR_OPT) \
	-z \
	--stack_size=0x1C0 \
	--warn_sections \
	--reread_libs \
	--entry_point=code_start \
	--rom_model \
	$(LD_ROM_OPT)

vpath %.asm asm
vpath %.inc inc
vpath %.c C
vpath %.h C

# Preprocessor dependency generation does not work for assembly, looks like TI doesn't do that,
# so we have to do dependency generation as a separate step.
#
# These rules use vpath to find sources.  Names with spaces are not recommended.
#
# Apparently the backslashes in util\sed.exe and util\mv.exe are needed, forward slashes
# don't work because cmd.exe does not do that.	mv.exe is used instead of move because it
# does work with either forward or backslashes.
obj/%.obj : %.asm
	@$(BIN)/echo -e \n***building $@
		$(AS) $(ASM_OPT) \
		--asm_dependency \
		--obj_directory="./obj" \
		--list_directory="./lst" \
		$<
		@$(SED) -f $(BIN)/deps.sed $(@:%.obj=%.ppa) > tmp.ppa
		@$(MV) tmp.ppa $(@:%.obj=%.ppa)
		@$(AS) $(ASM_OPT) \
		--obj_directory="./obj" \
		--list_directory="./lst" \
		$<

obj/%.obj : %.c
	@$(BIN)/echo -e \n***building $@
	$(CC) $(CFLAGS) \
		--asm_directory="./obj" \
		--preproc_with_compile \
		--preproc_dependency=$(@:%.obj=%.pp) \
		--obj_directory="./obj" \
		--list_directory="./lst" \
		$<

CMD_SRCS = BOOT28034.cmd DSP2803x_Headers_nonBIOS.cmd

C_SRCS = DSP2803x_GlobalVariableDefs.c

C_INCS = DSP2803x_headers/*.h

ASM_SRCS = boot28.asm boot.asm pwm_test.asm

ASM_INCS = regs.inc

ASM_DEPS = $(ASM_SRCS:%.asm=obj/%.ppa)

C_DEPS = $(C_SRCS:%.c=obj/%.pp)

C_ASM = $(C_SRCS:%.c=obj/%.asm)

OBJS =	$(ASM_SRCS:%.asm=obj/%.obj) $(C_SRCS:%.c=obj/%.obj)

# _ml means large memory model.
LIBS = /**/

ALL_DEPS = $(C_DEPS) $(ASM_DEPS)

# The hair below seems to be to allow cleaning without first building the dependency files.

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(ALL_DEPS)),)
-include $(ALL_DEPS)
endif
endif

# Directories that should be created if necessary
DIRS = obj builds lst

$(DIRS):
	$(MKDIR) $@

test.out: $(DIRS) $(OBJS) $(CMD_SRCS)
	@$(BIN)/echo -e \n***building $@
	@echo $(LD_OPT) > link.opt
	@echo $(LIBDIR) >>link.opt
	@echo -m$(@:%.out="%.map") >>link.opt
	@echo $(CMD_SRCS) >>link.opt
	@echo $(OBJS) >> link.opt
	@echo $(LIBS) >> link.opt
	@echo "------link.opt-------"
	type link.opt
	@echo "------link.opt-------"
	$(LD) -@link.opt -o $@

clean:
	-$(RM) obj/*
	-$(RM) lst/*

# Build tags file for emacs.
tags: $(ASM_SRCS) $(ASM_INCS) $(C_SRCS) $(C_INCS) 
	etags --regex="@$(BIN)/cptags.txt" $^
