LD=ld
CC=/usr/bin/gcc-4.4
OBJDUMP=objdump
CFLAGS=-O0 -m32
ASM=nasm
ASMFLAGS=-f elf
TIMEOUT=10

all: build

build:	asm compile linking objdump mtools

asm:
	$(ASM) $(ASMFLAGS) -o start.o start.asm

compile:
	$(CC) $(CFLAGS) -fno-stack-protector -c -o printf.o printf.c
	$(CC) $(CFLAGS) -fno-stack-protector -std=c99 -g -O0 -nostdlib -nostdinc -fno-builtin -I./include -c -o ulix.o -aux-info ulix.aux ulix.c

linking:
	$(LD) $(LDFLAGS) -T ulix.ld -o ulix.bin *.o 

mtools:
	mcopy -o -i ulixboot.img  ulix.bin ::

objdump:
	$(OBJDUMP) -M intel -D ulix.bin > ulix.dump
	cat ulix.dump | grep -e '^[^ ]* <' | sed -e 's/<//' -e 's/>://' > ulix.sym

clean:
	rm -f ./*.o ./*.c ./*.h ./*.pre ./ulix.bin ./ulix.aux ./ulix.ce 
	rm -f ./ulix.dump* ./*asm ./*.objdump ./*sym

run: 
	#qemu -m 64 -boot a -fda ulixboot.img -d cpu_reset -s -serial mon:stdio | tee ulix.output
	rm -f ulix.output
	echo "Starting qemu, going to kill it in $(TIMEOUT) seconds."
	( qemu -m 64 -boot a -fda ulixboot.img -d cpu_reset -s -serial mon:stdio > ulix.output ) &
	tail -f ulix.output &
	sleep $(TIMEOUT) && killall qemu
	echo "qemu killed after $(TIMEOUT) seconds (see Makefile)"


