#===============================#
#=   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 = Y

#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

DIRMODULES = CdRoot/System/Modules CdRoot/System/Modules/Core CdRoot/System/Modules/Input CdRoot/System/Modules/Network CdRoot/System/Modules/Storage CdRoot/System/Modules/Filesystems CdRoot/System/Modules/Video
DIRLIST = CdRoot CdRoot/Applications CdRoot/Mount CdRoot/System/Devices CdRoot/System/Include CdRoot/System/Runtime CdRoot/System/Runtime/C CdRoot/System/Startup/

all: cd	
	mkdir -p $(DIRLIST)
	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/Boot/KeLoader
	cp kern CdRoot/Boot/Kernel
	cp Boot.modules CdRoot/Boot/Boot.modules
	mv CdRoot/System/Modules/Filesystems/cdfs.sys CdRoot/System/Modules/Core/cdfs.sys
	mv CdRoot/System/Modules/Storage/ata_ide.sys CdRoot/System/Modules/Core/ata_ide.sys
	mv CdRoot/System/Modules/Video/console.sys CdRoot/System/Modules/Core/
	cp about.txt CdRoot/readme.txt
	
	nm kern | sort -u > kernel.txt

ifneq ($(CONFIG_ALLSYMS),Y)
	strip -s kern -R .comment
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 kernel/*.o lib/*.o memory/*.o video/*.o -o kern > kernel.txt
	mkdir -p $(DIRMODULES)
	make -C net modules_install
	make -C fs modules_install
	make -C devices modules_install
	make -C memory modules_install
	make -C video modules_install
	make -C kernel modules_install

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
