#
# Makefile for cnix.

AS	= as
LD	= ld
OBJDUMP = objdump
LDFLAGS	= -m elf_i386 -Ttext 0x100000 -e on_journey
CC	= gcc $(RAMDISK)
CFLAGS	= -fstrength-reduce -fomit-frame-pointer \
	 -finline-functions -nostdinc -I../include
CPP	= cpp -nostdinc -Iinclude

ARCHIVES = driver/driver.o mm/mm.o fs/fs.o kernel/kernel.o shell/shell.o
LIBS	= lib/lib.a

all:	Image

Image: boot/boot tools/system tools/build
	objcopy -O binary -R .note -R .comment tools/system tools/kernel
	tools/build boot/boot tools/kernel > Image
	rm tools/kernel -f
	sync

disk:	Image
	dd bs=8192 if=Image of=/dev/fd0

tools/build: tools/build.c
	$(CC) $(CFLAGS) \
	-o tools/build tools/build.c

tools/system:	boot/head.o init/main.o \
	$(ARCHIVES) $(LIBS)
	$(LD) $(LDFLAGS) boot/head.o init/main.o \
	$(ARCHIVES) \
	$(LIBS) \
	-o tools/system  
	nm tools/system | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aU] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)'| sort > System.map
	$(OBJDUMP) -S -D tools/system >System.S

init/main.o:
	(cd init;make)

boot/boot:	boot/boot.o tools/system
	$(LD) -Ttext 0x0 -s --oformat binary -o $@ boot/boot.o
boot/boot.o:    boot/boot.s
	$(AS) -o $@ $<
boot/boot.s:	boot/boot.S
	$(CC) $(CFLAGS) -S $<>$@

boot/head.o:    boot/head.s
	$(AS) -o $@ $<
boot/head.s:	boot/head.S
	$(CC) $(CFLAGS) -S $<>$@

driver/driver.o:
	(cd driver;make)

shell/shell.o:
	(cd shell;make)
# need to be modified
mm/mm.o:
	(cd mm;make)
fs/fs.o:
	(cd fs;make)

kernel/kernel.o:
	(cd kernel;make)

lib/lib.a:
	(cd lib;make)

clean:
	rm -f Image System.map tmp_make boot/boot boot/*.s core
	rm -f tools/system System.asm System.S
	rm -f lib/*.a
	find -name "*.o"    | xargs rm -f
	find -name "*~"     | xargs rm -f
	find -name ".*.swp" | xargs rm -f

