#===============================#
#=   CONFIGURATION OPTIONS     =#
#===============================#

MAKEFLAGS += --no-print-directory

# Change this to build for a different arch
ARCH = i386

#Change to N to produce a smaller kernel if debug functionality is not required
CONFIG_ALLSYMS = N

#Make sure all makefiles can see everything
export ARCH

# All directories that are to be built or cleaned.
SUBDIRS = arch/$(ARCH) devices fs kernel lib memory net video user

all: cd	
	mkdir -p CdRoot CdRoot/Applications CdRoot/Mount CdRoot/System/Devices CdRoot/System/Include CdRoot/System/Runtime CdRoot/System/Runtime/C
	make -C user install
	mkisofs -o cd.iso -b Boot/isoboot -no-emul-boot -c Boot/boot.cat -boot-load-size 4 -boot-info-table -iso-level 3 -l CdRoot

floppy: kern
	
ifneq ($(CONFIG_ALLSYMS),Y)
	strip -s kern
endif

cd: kern
	mkdir -p CdRoot/Boot
	cp arch/$(ARCH)/boot/iso/isoboot CdRoot/Boot/isoboot
	cp arch/$(ARCH)/boot/setup CdRoot/loader.bin
	cp kern CdRoot/kern.bin
	cp about.txt CdRoot/readme.txt
	
	nm kern | sort -g > kernel.txt

ifneq ($(CONFIG_ALLSYMS),Y)
	strip -s kern
endif

	mkisofs -o cd.iso -b Boot/isoboot -no-emul-boot -c Boot/boot.cat -boot-load-size 4 -boot-info-table -iso-level 3 -l CdRoot

kern: subdirs
	ld -M -T link.ld arch/$(ARCH)/boot/*.o arch/$(ARCH)/lib/*.o arch/$(ARCH)/kernel/*.o arch/$(ARCH)/mm/*.o devices/*.o devices/net/*.o devices/input/*.o devices/storage/*.o devices/storage/ata/*.o devices/pci/*.o kernel/*.o net/*.o net/inet/*.o video/*.o fs/*.o fs/isofs/*.o fs/vfs/*.o fs/fat/*.o fs/ext3/*.o lib/*.o memory/*.o -o kern > kernel.txt

subdirs:
	for dir in $(SUBDIRS); do \
		$(MAKE) -C $$dir || exit 1; \
	done;

$(SUBDIRS):
	$(MAKE) -C $@

.PHONY: subdirs $(SUBDIRS)

clean:
	make -C arch/$(ARCH) clean
	make -C devices clean
	make -C fs clean
	make -C kernel clean
	make -C lib clean
	make -C net clean
	make -C video clean
	make -C memory clean
	make -C user clean
	rm -f cd.iso kern out.txt kernel.txt
	rm -rf CdRoot

help:
	@echo =====================
	@echo = Whitix build help =
	@echo =====================
	@echo -
	@echo "all - build the kernel and userspace"
	@echo "floppy - build the kernel and construct a floppy image"
	@echo "clean - remove all intermediate files"
	@echo "See the readme file for more help"

# Phony targets.
.PHONY: clean help cd
