AS = as -Iinclude
LD = ld
CC = gcc
CPP = $(CC) -E -nostdinc -Iinclude

WARNS = -Wall -pedantic -W -Wno-long-long 
OPTS = -nostdlib -nostdinc -fomit-frame-pointer

KOPTS = --oformat binary -N -Ttext 0x8000
MAP = -Map kernel.map

OBJS = entry.o isr.o init.o run.o timer.o exception.o

all: pxeboot.img disk.img

kernel: $(OBJS)
	$(LD) -melf_x86_64 $(KOPTS) -e long_mode $(MAP) -o $@ $(OBJS) $(LIBS)
	@wc -c kernel

load: load.o
	$(LD) --oformat binary -N -Ttext 0x000 -e pm_mode -Map load.map -o $@ load.o

entry.o: entry.s
	$(AS) --64 -o entry.o entry.s

isr.o: isr.s
	$(AS) --64 -o isr.o isr.s

init.o: init.c
	$(CC) -m64 -O2 -c init.c

run.o: run.c
	$(CC) -m64 -O2 -c run.c

timer.o: timer.c
	$(CC) -m64 -O2 -c timer.c

exception.o: exception.c
	$(CC) -m64 -O2 -c exception.c

pxeboot.img: pxeboot kernel
	cat pxeboot kernel > pxeboot.img
	@wc -c pxeboot.img
	cp pxeboot.img /tftpboot/pxeboot

pxeboot: pxeboot.o
	${LD} --oformat binary -N -e start -Ttext 0x7c00 -o pxeboot $<

disk.img: bootsect kernel
	cat bootsect kernel > disk.img
	@wc -c disk.img

bootsect: bootsect.o
	${LD} --oformat binary -N -e start -Ttext 0x7c00 -o bootsect $<

clean:
	rm -f *.o
	rm -f load kernel pxeboot bootsect
	rm -rf *.map
	rm -rf *.img

