export # export all variable to sub-Makefile
CC = mipsel-unknown-elf-gcc
AS = mipsel-unknown-elf-as
LD = mipsel-unknown-elf-ld
DU = mipsel-unknown-elf-objdump

MAP_XML      = mappings/4c_1p_40.xml

SYS_OBJS     = build/sys/vm_handler.o \
               build/sys/sys_handler.o \
               build/sys/giet.o \
               build/sys/switch.o \
               build/sys/common.o \
               build/sys/ctx_handler.o \
               build/sys/drivers.o \
               build/sys/exc_handler.o \
               build/sys/irq_handler.o \
               build/sys/kernel_init.o

BOOT_OBJS    = build/boot/reset.o \
               build/boot/boot_init.o 

DISPLAY_OBJS = build/display/main.o \
               build/libs/stdio.o \
               build/libs/utils.o

ROUTER_OBJS  = build/router/main.o \
               build/libs/mwmr_channel.o \
               build/libs/stdio.o \
               build/libs/utils.o

HELLO_OBJS   = build/hello/main.o \
               build/libs/stdio.o \
               build/libs/utils.o

PGCD_OBJS    = build/pgcd/main.o \
               build/libs/stdio.o \
               build/libs/utils.o

DHRYSTONE_OBJS = build/dhrystone/dhry_1.o \
                 build/dhrystone/dhry_2.o \
                 build/libs/stdio.o \
                 build/libs/utils.o \
                 build/libs/string.o \
                 build/libs/malloc.o


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

INCLUDE = -Iboot -Isys -Ixml -Ilibs -I.

all: map.bin soft.elf

## merge all *.elf and map.bin to soft.elf
soft.elf: build/boot/boot.elf \
          build/sys/sys.elf \
          build/display/display.elf \
          build/router/router.elf \
          build/pgcd/pgcd.elf \
          build/hello/hello.elf \
          build/dhrystone/dhrystone.elf \
          
	$(MAKE) -C memo
	memo/memo.x  map.bin 
	$(DU) -D $@ > build$@.txt

### mapping compilation
map.bin: $(MAP_XML)
	$(MAKE) -C xml
	xml/xml2bin $< .

### system compilation
build/sys/sys.elf: $(SYS_OBJS) sys/sys.ld 
	$(LD) -o $@ -T sys/sys.ld $(SYS_OBJS)
	$(DU) -D $@ > $@.txt

build/sys/%.o: sys/%.c giet_config.h $(MAP_XML)
	$(CC) $(INCLUDE) $(CFLAGS)  -c -o $@ $<

build/sys/%.o: sys/%.s giet_config.h $(MAP_XML)
	$(CC) $(INCLUDE) $(CFLAGS)  -c -o $@ $<

### boot compilation
build/boot/boot.elf: $(BOOT_OBJS) boot/boot.ld
	$(LD) -o $@ -T boot/boot.ld $(BOOT_OBJS)
	$(DU) -D $@ > $@.txt

build/boot/%.o: boot/%.c giet_config.h $(MAP_XML)
	$(CC) $(INCLUDE) $(CFLAGS)  -c -o $@ $<

build/boot/%.o: boot/%.S giet_config.h $(MAP_XML)
	$(CC) $(INCLUDE) $(CFLAGS)  -c -o $@ $<

### display compilation
build/display/display.elf: $(DISPLAY_OBJS) display/display.ld
	$(LD) -o $@ -T display/display.ld $(DISPLAY_OBJS)
	$(DU) -D $@ > $@.txt

build/display/main.o: display/main.c
	$(CC) $(INCLUDE) $(CFLAGS)  -c -o $@ $<

### router compilation
build/router/router.elf: $(ROUTER_OBJS) router/router.ld
	$(LD) -o $@ -T router/router.ld $(ROUTER_OBJS)
	$(DU) -D $@ > $@.txt

build/router/main.o: router/main.c
	$(CC) $(INCLUDE) $(CFLAGS)  -c -o $@ $<

### hello compilation
build/hello/hello.elf: $(HELLO_OBJS) hello/hello.ld
	$(LD) -o $@ -T hello/hello.ld $(HELLO_OBJS)
	$(DU) -D $@ > $@.txt

build/hello/main.o: hello/main.c
	$(CC) $(INCLUDE) $(CFLAGS) -c -o $@ $<

### pgcd compilation
build/pgcd/pgcd.elf: $(PGCD_OBJS) pgcd/pgcd.ld
	$(LD) -o $@ -T pgcd/pgcd.ld $(PGCD_OBJS)
	$(DU) -D $@ > $@.txt

build/pgcd/main.o: pgcd/main.c
	$(CC) $(INCLUDE) $(CFLAGS) -c -o $@ $<

### dhrystone compilation
build/dhrystone/dhrystone.elf: $(DHRYSTONE_OBJS) dhrystone/dhrystone.ld
	$(LD) -o $@ -T dhrystone/dhrystone.ld $(DHRYSTONE_OBJS)
	$(DU) -D $@ > $@.txt

build/dhrystone/dhry_1.o: dhrystone/dhry_1.c
	$(CC) $(INCLUDE) $(CFLAGS)  -c -o $@ $<

build/dhrystone/dhry_2.o: dhrystone/dhry_2.c
	$(CC) $(INCLUDE) $(CFLAGS)  -c -o $@ $<

### libs compilation
build/libs/utils.o: libs/utils.c giet_config.h
	$(CC) $(CFLAGS) $(INCLUDE) -c -o $@ $<

build/libs/stdio.o: libs/stdio.c giet_config.h  
	$(CC) $(CFLAGS) $(INCLUDE) -c -o $@ $<

build/libs/mwmr_channel.o: libs/mwmr_channel.c
	$(CC) $(CFLAGS) $(INCLUDE) -c -o $@ $<

build/libs/malloc.o: libs/malloc.c libs/malloc.h libs/malloc_private.h giet_config.h
	$(CC) $(CFLAGS) $(INCLUDE) -c -o $@ $<

build/libs/string.o: libs/string.c libs/string.h
	$(CC) $(CFLAGS) $(INCLUDE) -c -o $@ $<

### clean 
clean:
	rm -f *.o *.elf *.bin *.txt core  giet_vsegs.ld hard_config.h *~
	$(MAKE) -s clean -C xml
	$(MAKE) -s clean -C memo
	rm -rf build/boot/*
	rm -rf build/sys/*
	rm -rf build/libs/*
	rm -rf build/pgcd/*
	rm -rf build/hello/*
	rm -rf build/display/*
	rm -rf build/router/*
	rm -rf build/dhrystone/*
