#############################################################################
#
# Makefile for building the Mark III Mini Sumo program
#
#############################################################################

MPLAB       = /c/Program\ Files/MPLab\ IDE/MCHIP_Tools
MPASM       = $(MPLAB)/mpasmwin.exe
MPLINK      = $(MPLAB)/mplink.exe
CC5X_DIR    = c:/cc5x
CC5X        = $(CC5X_DIR)/cc5x.exe
PIC_LOADER  = /MyDocuments/Robotics/MyBots/Mark-III/PIC-Loader/PicLdrCmd.exe
CPP         = ../Tools/cpp.exe

#
# Run make with verbose=1 to get verbose output
#
ifeq ($(verbose),)
export verbose = 0
else
export verbose = 1
endif

ifeq ($(verbose),0)
MAKEFLAGS += -s
endif

all : MiniSumo

.PHONY: MiniSumo

SOURCE_FILES = \
    a2d.c       \
    AvoidEdg.c  \
    MiniSumo.c  \
    DebugKey.c  \
    Delay.c     \
    Init.c      \
    LineF.c     \
    Motor.c     \
    Puts.c      \
    Sensor.c    \

x = \
    AvoidTil.c  \
    Tilt.c

OBJ_DIR     = obj
OBJ_FILES   = $(addprefix $(OBJ_DIR)/,$(SOURCE_FILES:.c=.o))
ASM_FILES   = $(addprefix $(OBJ_DIR)/,$(SOURCE_FILES:.c=.asm))
DEP_FILES   = $(addprefix $(OBJ_DIR)/,$(SOURCE_FILES:.c=.d))

MiniSumo : MiniSumo.hex

d MiniSumo.dld : MiniSumo.hex
    @echo "Downloading $< ..."
    $(PIC_LOADER) $<

MiniSumo.hex : $(OBJ_FILES)
    @echo "Linking $@ ..."
    $(MPLINK) /aINHX8M MiniSumo.lkr $(OBJ_FILES) /m$(@:.hex=.map) /o$@

$(OBJ_DIR)/%.asm : %.c
    @echo "Compiling $< ..."
    $(CC5X) -I$(CC5X_DIR) -r -a -S -O$(@D) $<

$(OBJ_DIR)/%.o : $(OBJ_DIR)/%.asm
    @echo "Assembling $< ..."
    cd $(OBJ_DIR); $(MPASM) /o /q $(<F)
   
clean:
    @echo "Removing stuff from $(OBJ_DIR) ..."
    rm -f $(OBJ_DIR)/*
    @echo "Removing linked output ..."
    rm -f MiniSumo.hex MiniSumo.cod MiniSumo.lst

#
# Dependency file generation
#

$(OBJ_DIR)/%.d : %.c
    @echo "Creating Dependency file for $< ..."
    set -e; $(CPP) -M $< | sed 's=$(*F).o[ :]*=$(@:.d=.o) $@ : =g' > $@; [ -s $@ ] || ( rm -f $@; exit 1 )

ifneq ($(DEP_FILES),)
ifeq ($(strip $(filter clean% exec print-%, $(MAKECMDGOALS))),)
-include $(DEP_FILES)
endif
endif

