OBJS = boot/bootsect 
KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/graphic.o kernel/vsprintf.o font.o 
KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/graphic.c kernel/vsprintf.c 

all: Makefile
	make -C tools/
	make -C boot/
	make -C init/
	make -C modules/
	make -C kernel/ 
	make xnix.img
	make vmxnix


font.o: tools/bin2s font.psf 
	tools/bin2s font font.psf > font.asm
	nasm font.asm -o font.o -f elf64


kbinary: $(KSRCS) $(KOBJS) boot/bootsect Makefile
	ld -b elf64-x86-64 -o kernel.o $(KOBJS) -T kernel.lds
	objcopy -I elf64-x86-64 -S -R ".comment" -R ".eh_frame" -O binary kernel.o kbinary

vmxnix: $(KSRCS) $(KOBJS) Makefile 
	ld -b elf64-x86-64 $(KOBJS) -e init -T kernel.lds -o vmxnix


xnix.img: boot/bootsect $(KSRCS) $(KOBJS) Makefile kbinary
	dd if=/dev/zero of=xnix.img bs=1024 count=1440
	dd if=boot/bootsect of=xnix.img conv=notrunc seek=0
	dd if=kbinary of=xnix.img conv=notrunc bs=512 seek=2


lines: 
	wc -l `find ./ -name *.asm -o -name *.c -o -name *.h -o -name *.S -o -name *.s`


clear: 
	make -C boot/ clear
	make -C init/ clear
	make -C kernel/ clear
	make -C tools/ clear
	rm boot/bootsect
	rm xnix.img kbinary kernel.o vmxnix font.o font.asm 
	make -C modules/ clear

