INC := ../include -I../arch/mips/include
LDS := boot.lds
OBJS := boot.o

include ../.config

FLASHBASE ?= 0xbfc00000

LOADBASE ?= 0x98900000

PTR_COUNT_SHIFT ?=2

CROSS_COMPILE ?= mips-linux-

CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld
OBJCPY := $(CROSS_COMPILE)objcopy


GCCINC := $(shell $(CC) -print-file-name=include)
GCCLIB := $(shell dirname `$(CC) -print-libgcc-file-name`)
GENLDSFLAGS := -I$(INC) -DCONFIG_LOAD_BASE=$(LOADBASE) -DPTR_COUNT_SHIFT=$(PTR_COUNT_SHIFT) -ansi -E -P -

CFLAGS := -Os -ffunction-sections -fdata-sections -D__KERNEL__ \
	-DCONFIG_RAMINIT_OFFSET=$(BOOT_SIZE) \
	-DCONFIG_RAMINIT_SIZE=$(RAMINIT_SIZE) \
	-DCONFIG_LOAD_BASE=$(LOADBASE) -I$(INC) -fno-builtin \
	-ffreestanding -nostdinc -isystem $(GCCINC) -pipe -G 0 -mabicalls \
	-fPIC -EL -fno-strict-aliasing -mabi=32 -D__MIPS__ -Wall \
	-Wstrict-prototypes -fno-stack-protector -Wno-format-nonliteral \
	-Wno-format-security -fstack-usage -msoft-float
ASFLAGS := $(CFLAGS) -D__ASSEMBLY__

LDFLAGS := -G 0 -static -n -nostdlib -EL -m elf32ltsmip -T $(LDS) \
	--gc-sections -pie -Bstatic -Ttext $(FLASHBASE) -L $(GCCLIB) -lgcc

OBJFLAGS := --remove-section=.dynsym --gap-fill=0xff -O binary

.PHONY : all
all : $(LDS) sysboot.bin
#$(LDS) : include/$(LDS)
#	$(CC) $(GENLDSFLAGS) < include/$(LDS) > $(LDS)
#.depend :
#	$(CC) $(CFLAGS) -MM $(SRC) > .depend
#-include .depend

boot.o : boot.S
	$(CC) $(ASFLAGS) -c boot.S -o boot.o
boot : $(OBJS) $(LDS)
	$(LD) $(LDFLAGS) $(OBJS) -Map boot.map -o boot
sysboot.bin : boot
	$(OBJCPY) $(OBJFLAGS) boot sysboot.bin

.PHONY : clean
clean :
	rm -f *.o *.su .depend boot.map boot *.bin
	rm -f $(OBJS)
