# main Makefile for "PicOs" system building
# stolen from Linux makefile
# needs GNU Make...

.EXPORT_ALL_VARIABLES:

CFLAGS = -Wall -Wstrict-prototypes -fomit-frame-pointer -pipe -O2
# comment next out for 386
CFLAGS := $(CFLAGS) -m486
# LDFLAGS = -oformat zmagic

SVGA_MODE = -DSVGA_MODE=NORMAL_VGA
VPATH	=../lib:lib

AS86	=as86 -0 -a
LD86	=ld86 -0

AS	=as
LD	=ld
CC	=gcc
MAKE	=make
CPP	=$(CC) -E
AR	=ar
STRIP	=strip
SUBDIRS =lib kernel client mm keyboard screen

all : image

.c.s:
	$(CC) $(CFLAGS) -S -o $*.s $<
.s.o:
	$(CC) -D__ASSEMBLY__ -traditional -c -o $*.o $<
#.s.o:
#	$(AS) -c -o $*.o $<
.c.o:
	$(CC) $(CFLAGS) -c -o $*.o $<

lib:
	set -e; $(MAKE) -C lib

tools/build: tools/build.c include/config.h kernel/params.h
	$(CC) -o $@ $<

boot/head.o: boot/head.s

boot/head.s: boot/head.S  
	$(CPP) -traditional $< -o $@

boot/setup: boot/setup.o
	$(LD86) -s -o $@ $<

boot/setup.o: boot/setup.s
	$(AS86) -o $@ $<

boot/setup.s: boot/setup.S include/config.h include/segment.h
	$(CPP) -traditional $(SVGA_MODE) $< -o $@

boot/bootsect: boot/bootsect.o
	$(LD86) -s -o $@ $<

boot/bootsect.o: boot/bootsect.s
	$(AS86) -o $@ $<

boot/bootsect.s: boot/bootsect.S include/config.h
	$(CPP) -traditional $(SVGA_MODE) $< -o $@

Picosubdirs: FORCE
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i; done

System:	boot/head.o kernel/kernel.o -lpico
	$(LD) $(LDFLAGS) -Ttext 1000 -o $@ $^
	nm System | grep -v '\(compiled\)\|\(\.o$$\)\|\( a \)' | \
		sort >System.map
	sync

# The build.info file contains the names of all the files implied
# in the construction of the initial system image.  The order is :
# (hardcoded in tools/build.c) :
# - bootsector (8086 code)
# - setup (8086 code passing to Protected Mode)
# - System (80386 kernel image)
# - one by one all servers loaded at start-up : file stack_size
# The server order has to coincide with the one in include/pid.h

tools/build.info: boot/bootsect boot/setup System Picosubdirs
	echo "boot/bootsect boot/setup System" >tools/build.info
	echo "mm/mm 1024"          >>tools/build.info
	echo "keyboard/scan 1024"  >>tools/build.info
	echo "keyboard/key  1024"  >>tools/build.info
	echo "screen/screen 1024"  >>tools/build.info
	echo "client/client1 1024" >>tools/build.info
	echo "client/client2 1024" >>tools/build.info
	echo "client/cl 1024"      >>tools/build.info

image: Picosubdirs tools/build tools/build.info
	@echo "--------------------------------"
	tools/build tools/build.info \
	  `grep sys_description System.map | cut -c1-8` > image
	sync

#force compilation of all dependents
FORCE:

# the rest are not files but just rules all
.PHONY : clean dep install count backup veryclean

clean:
	rm -f core `find . -name '*.[oas]' -print`
	rm -f `find . -name 'core' -print`
	rm -f image System.map boot/bootsect boot/setup System
	rm -f tools/build tools/build.info boot/*.o tools/*.o
	rm -f tex/*.log
	for i in $(SUBDIRS); do $(MAKE) -C $$i clean; done

veryclean: clean
	rm -f depend `find . -name depend -print`
	rm -f tex/*.dvi tex/*.aux tex/*.toc

install: image
	putit
	@echo "Kernel installed ! Try it - lilo : pico"

count:
	
	@echo -n "as lines       :"
	@cat `find . -name "*.S"` | wc -l	
	@echo -n "c lines        :"
	@cat `find . -name "*.[ch]"` | wc -l
	@echo -n "Makefile lines :"
	@cat `find . -name "[Mm]akefile"` | wc -l
	@echo -n "Doc lines      :"
	@cat tex/*.tex | wc -l
	@echo -n "In total files :"
	@find . -name "*.[cSh]" -o -name Makefile -o -path "./tex/*.tex" | wc -l

dep:
	for i in tools/*.c; do $(CPP) -M $$i; done > depend
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i dep; done

floppy: image
	dd bs=8192 if=image of=/dev/fd0

ifeq (depend,$(wildcard depend))
include depend
endif


