#
# Makefile fragment for 6.828 kernel.
# This is NOT a complete makefile;
# you must run GNU make in the top-level directory
# where the GNUmakefile is located.
#

OBJDIRS += kern
CLEAN_FILES += kern/kernel kern/bochs.img

#KERN_LDFLAGS = -dc -dp -static -Ttext F0100020 -nostdlib -e start
KERN_LDFLAGS := $(LDFLAGS) -Ttext F0100020 -nostdlib -e start


INIT=init
ENV=env
PMAP=pmap

## Only build files if they exist.
##
## locore.S must be first, so it's the 
## first code in the text segment!!!
##
KERN_SRCFILES :=	kern/locore.S \
			kern/$(INIT).c \
			kern/console.c \
			kern/$(PMAP).c \
			kern/printf.c \
			kern/$(ENV).c \
			kern/kclock.c \
			kern/picirq.c \
			kern/trap.c \
			kern/lab3.S \
			kern/sched.c \
			kern/syscall.c
KERN_SRCFILES := $(foreach file, $(KERN_SRCFILES), \
			$(shell test -f $(file) && echo $(file)))
KERN_OBJFILES := $(patsubst %.c, %.o, \
			$(patsubst %.S, %.o, $(KERN_SRCFILES)))


# Binary program images to embed within the kernel.
KERN_BINFILES := \
	user/buggyhello \
	user/evilhello \
	user/exit \
	user/fairness \
	user/fault \
	user/faultalloc \
	user/faultallocbad \
	user/faultbadhandler \
	user/faultbadstack \
	user/faultdie \
	user/faultevilhandler \
	user/faultevilstack \
	user/faultgoodstack \
	user/hello \
	user/icode \
	user/idle \
	user/init \
	user/pingpong \
	user/pingpong1 \
	user/pingpong2 \
	user/pingpongs \
	user/pipereadeof \
	user/pipewriteeof \
	user/primes \
	user/primespipe \
	user/testfdsharing \
	user/testfsipc \
	user/testkbd \
	user/testpipe \
	user/testpiperace \
	user/testpiperace2 \
	user/testptelibrary \
	user/testshell \
	user/writemotd \
	fs/fs \

KERN_BINFILES := $(foreach file, $(KERN_BINFILES), \
			$(shell test -f $(file).c && echo $(file)))

# How to build the kernel itself
kern/kernel: $(KERN_OBJFILES) $(KERN_BINFILES)
	$(LD) -o $@ $(KERN_LDFLAGS) $(KERN_OBJFILES) $(GCC_LIB) -b binary $(KERN_BINFILES)

# How to build the Bochs disk image
kern/bochs.img: kern/kernel boot/boot
	dd if=/dev/zero of=kern/bochs.img~ count=10000 2>/dev/null
	dd if=boot/boot of=kern/bochs.img~ conv=notrunc 2>/dev/null
	dd if=kern/kernel of=kern/bochs.img~ seek=1 conv=notrunc 2>/dev/null
	mv kern/bochs.img~ kern/bochs.img

all: kern/bochs.img

