CC=mipsel-unknown-elf-gcc
AS=mipsel-unknown-elf-as
LD=mipsel-unknown-elf-ld
DU=mipsel-unknown-elf-objdump

SYS_OBJS = giet.o \
		switch.o \
		common.o \
		ctx_handler.o \
		drivers.o \
		exc_handler.o \
		irq_handler.o \
		sys_handler.o \
		init.o 		\
		task_init.o 

BOOT_OBJS = reset.o \
		boot_handler.o 

SYS_PATH    = sys
BOOT_PATH   = boot
MAP_PATH    = xml
LIBS_PATH   = libs
BUILD_DIR_NAME	= build

CFLAGS=-Wall -ffreestanding -mno-gpopt -mips32 -g

SYS_INCLUDE = -I$(SYS_PATH) -I$(MAP_PATH) -I$(LIBS_PATH) -I.

BOOT_INCLUDE = -I$(BOOT_PATH) -I$(MAP_PATH) -I$(LIBS_PATH) -I.

#commons between sys and boot are: common.ld , mips_registers.h...

TRASH= /dev/null||true

.PHONY: apps prepare

all: prepare soft.bin
	

## merge all *.bin to soft.bin
soft.bin: boot.bin sys.bin map.bin apps 
	make -C mover
	mover/mover.x -sm map.bin 
	$(DU) -D $@ > $@.txt

## prepare the environement
prepare:
	@mkdir $(BUILD_DIR_NAME) 2>$(TRASH)

## mapping compilation
map.bin: map.xml
	make -C xml 				#compile the parser
	xml/xml2bin map.xml map.bin

## system compilation
sys.bin: $(SYS_OBJS) $(SYS_PATH)/sys.ld
	(cd $(BUILD_DIR_NAME); $(LD) -o ../$@ -T ../$(SYS_PATH)/sys.ld $(SYS_OBJS) )
	(cd $(BUILD_DIR_NAME); $(DU) -D ../$@ > $@.txt)

switch.o: $(SYS_PATH)/switch.s giet_config.h
	$(AS) -g -mips32 -o $(BUILD_DIR_NAME)/$@ $<

giet.o: $(SYS_PATH)/giet.s giet_config.h
	$(AS) -g -mips32 -o $(BUILD_DIR_NAME)/$@ $<

task_init.o: $(SYS_PATH)/task_init.S giet_config.h
	#$(AS) -g -mips32 -o $(BUILD_DIR_NAME)/$@ $<
	$(CC) $(CFLAGS) $(SYS_INCLUDE) -c -o $(BUILD_DIR_NAME)/$@ $<
	$(DU) -D $(BUILD_DIR_NAME)/$@ > $(BUILD_DIR_NAME)/$@.txt

ctx_handler.o: $(SYS_PATH)/ctx_handler.c $(SYS_PATH)/ctx_handler.h giet_config.h
	$(CC) $(CFLAGS) $(SYS_INCLUDE) -c -o $(BUILD_DIR_NAME)/$@ $<

exc_handler.o: $(SYS_PATH)/exc_handler.c $(SYS_PATH)/exc_handler.h giet_config.h
	$(CC) $(CFLAGS) $(SYS_INCLUDE) -c -o $(BUILD_DIR_NAME)/$@ $<

irq_handler.o: $(SYS_PATH)/irq_handler.c $(SYS_PATH)/irq_handler.h giet_config.h
	$(CC) $(CFLAGS) $(SYS_INCLUDE) -c -o $(BUILD_DIR_NAME)/$@ $<

sys_handler.o: $(SYS_PATH)/sys_handler.c $(SYS_PATH)/sys_handler.h giet_config.h
	$(CC) $(CFLAGS) $(SYS_INCLUDE) -c -o $(BUILD_DIR_NAME)/$@ $<

drivers.o: $(SYS_PATH)/drivers.c $(SYS_PATH)/drivers.h giet_config.h
	$(CC) $(CFLAGS) $(SYS_INCLUDE) -c -o $(BUILD_DIR_NAME)/$@ $<

common.o: $(SYS_PATH)/common.c $(SYS_PATH)/common.h giet_config.h
	$(CC) $(CFLAGS) $(SYS_INCLUDE) -c -o $(BUILD_DIR_NAME)/$@ $<

init.o: $(SYS_PATH)/init.c giet_config.h
	$(CC) $(CFLAGS) $(SYS_INCLUDE) -c -o $(BUILD_DIR_NAME)/$@ $<

## boot compilation
boot.bin: $(BOOT_OBJS) $(BOOT_PATH)/boot.ld
	(cd $(BUILD_DIR_NAME); $(LD) -o ../$@ -T ../$(BOOT_PATH)/boot.ld $(BOOT_OBJS) )
	(cd $(BUILD_DIR_NAME); $(DU) -D ../$@ > $@.txt)

reset.o: $(BOOT_PATH)/reset.S giet_config.h
	$(CC) $(CFLAGS) $(BOOT_INCLUDE) -c -o $(BUILD_DIR_NAME)/$@ $<
	$(DU) -D $(BUILD_DIR_NAME)/$@ > $(BUILD_DIR_NAME)/$@.txt

boot_handler.o: $(BOOT_PATH)/boot_handler.c $(BOOT_PATH)/boot_handler.h giet_config.h
	$(CC) $(CFLAGS) $(BOOT_INCLUDE) -c -o $(BUILD_DIR_NAME)/$@ $<

## applications and libs compilation
apps:
	@echo "---------------------------------------------BUILDING APPS------------------------------------------------------"
	make -C apps

### special rules
clean:
	rm -f *.o *.bin *.txt core *~  2>$(TRASH)
	make clean -C xml/ 2>$(TRASH)
	make clean -C mover/ 2>$(TRASH)
	make clean -C apps 2>$(TRASH)
	rm -r $(BUILD_DIR_NAME) 2>$(TRASH)
