# Sivan Toledo, 2008
# Simplified from a WinARM makefile

TARGET = main

SRC  = $(TARGET).c 
ASRC = startup_from_std_firmware.S

# gcc	flags
#  -g*:          generate debugging information
#  -O*:          optimization level
#  -f...:        tuning, see GCC manual and avr-libc documentation
#  -Wall...:     warning level
#  -Wa,...:      tell GCC to pass this to the assembler.
#    -adhlns...: create assembler listing
CFLAGS = -I.
CFLAGS += -mcpu=arm7tdmi
CFLAGS += -gdwarf-2
CFLAGS += $(CDEFS) $(CINCS)
CFLAGS += -Os
CFLAGS += -std=gnu99
#CFLAGS += -Wa,-adhlns=$(subst $(suffix $<),.lst,$<) 
#CFLAGS += -ffunction-sections -fdata-sections

# Assembly flags (to as via gcc)
ASFLAGS = -I. -x assembler-with-cpp
ASFLAGS += -mcpu=arm7tdmi
ASFLAGS += $(ADEFS) 
#ASFLGS += -Wa,-adhlns=$(<:.S=.lst),--gdwarf-2
ASFLAGS += -Wa,--gdwarf-2

# linker flags
LDFLAGS = -lc -lm -lgcc -nostartfiles -Tstartup_from_std_firmware.ld --relocatable

# Define programs and commands.

# Define all object files.
COBJ      = $(SRC:.c=.o) 
AOBJ      = $(ASRC:.S=.o)

# Define all listing files.
LST = $(ASRC:.S=.lst) $(SRC:.c=.lst) 

# Compiler flags to generate dependency files.
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d

# Default target.
all: build size

build: bin
bin: $(TARGET).rxe

elf: $(TARGET).elf
lss: $(TARGET).lss 
sym: $(TARGET).sym

# Display size of file.
ELFSIZE = arm-elf-size -A $(TARGET).elf
size:
	arm-elf-size -A $(TARGET).elf
	ls -l $(TARGET).rxe

# Program the device.
program: $(TARGET).rxe
	NextTool /COM=USB0::0X0694::0X0002::001653019467::RAW -download=$(TARGET).rxe
	#NextTool /COM=usb -download=$(TARGET).rxe
	@echo "Download done."

# Create final output file (.rxe) from ELF output file.
%.rxe: %.elf
	arm-elf-objcopy --strip-debug -O binary $< $@
	ls -l $@

# Create extended listing file from ELF output file.
# testing: option -C
%.lss: %.elf
	arm-elf-objdump -h -S -C -r -R $< > $@

# Create a symbol table from ELF output file.
%.sym: %.elf
	arm-elf-nm -n $< > $@

# Link: create ELF output file from object files.
.SECONDARY : $(TARGET).elf
.PRECIOUS : $(AOBJ) $(COBJ)
%.elf: $(AOBJ) $(COBJ) startup_from_std_firmware.ld
	arm-elf-gcc $(CFLAGS) $(AOBJ) $(COBJ) --output $@ $(LDFLAGS)
	arm-elf-size -A $(TARGET).elf

# Compile: create object files from C source files.
$(COBJ) : %.o : %.c
	arm-elf-gcc -c $(CFLAGS) $(GENDEPFLAGS) $< -o $@ 

# Assemble: create object files from assembler source files.
$(AOBJ) : %.o : %.S
	arm-elf-gcc -c $(ASFLAGS) $< -o $@

# Target: clean project.
REMOVE = rm -f
clean:
	$(REMOVE) $(TARGET).rxe
	$(REMOVE) $(TARGET).elf
	$(REMOVE) $(TARGET).map
	$(REMOVE) $(TARGET).obj
	$(REMOVE) $(TARGET).sym
	$(REMOVE) $(TARGET).lnk
	$(REMOVE) $(TARGET).lss
	$(REMOVE) $(COBJ)
	$(REMOVE) $(AOBJ)
	$(REMOVE) $(LST)
	$(REMOVE) $(SRC:.c=.s)
	$(REMOVE) $(SRC:.c=.d)
	$(REMOVE) $(SRCARM:.c=.s)
	$(REMOVE) $(SRCARM:.c=.d)
	$(REMOVE) $(CPPSRC:.cpp=.s) 
	$(REMOVE) $(CPPSRC:.cpp=.d)
	$(REMOVE) $(CPPSRCARM:.cpp=.s) 
	$(REMOVE) $(CPPSRCARM:.cpp=.d)
	$(REMOVE) .dep/*

# Include the dependency files.
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)

# Listing of phony targets.
.PHONY : all begin finish end sizebefore sizeafter \
build elf hex bin lss sym clean clean_list program

