#################################################
#																     #
#					Caldera Makefile								     #
#					  version 0.0.2a								     #
#					      GNU GPL									     #
#################################################


# Variables

CPPFLAGS = -nostdlib -nostdinc -nostartfiles -nodefaultlibs -fno-rtti -fno-builtin -fno-exceptions -Wall -Werror -Wextra -c -I./kernel/include

CFLAGS = -fno-builtin -nostdlib -nostdinc -nostartfiles -nodefaultlibs -Wall -Werror -Wextra -c -I./kernel/include

DRIVERS =

ASMFLAGS = -f elf

PROJECT_DIRS = kernel/boot kernel/runtime kernel/lib

MINDEPTH = 1

MAXDEPTH = 8

AUX_FILES = Makefile scripts/* ../other/menu.lst drivers

# We let the shell find all the source files,so we don't have to ;)

KERNEL_CPP_SRC = $(shell find $(PROJECT_DIRS) -mindepth $(MINDEPTH) -maxdepth $(MAXDEPTH) -name "*.cpp" )

KERNEL_C_SRC = $(shell find $(PROJECT_DIRS) -mindepth $(MINDEPTH) -maxdepth $(MAXDEPTH) -name "*.c" )

KERNEL_ASM_SRC = $(shell find $(PROJECT_DIRS) -mindepth $(MINDEPTH) -maxdepth $(MAXDEPTH) -name "*.asm" )

# We don't have to locate the C/C++ object files, because g++ puts them in ./
# However, nasm doesn't..

KERNEL_ASM_OBJ = $(patsubst %.asm,%.o, $(KERNEL_ASM_SRC)) 

CALDERA_VERSION = 0.0.3

# Targets

default-target: clean-kernel-bin clean-kernel-image build-kernel build-kernel-image

check:
	@bash scripts/check
	
build-kernel:

	@mkdir -p ../bin
	
	@gcc $(CFLAGS) $(KERNEL_C_SRC)
	
	@g++ $(CPPFLAGS) $(KERNEL_CPP_SRC)
	
	@for file in $(KERNEL_ASM_SRC); do \
		nasm $(ASMFLAGS) $$file ; \
		# For some odd reason, nasm doesn't compile multiple files, \
		# so we have to conform with this ugly loop :( \
	done
	
	@ld -Tscripts/link.ld *.o $(KERNEL_ASM_OBJ) -o ../bin/caldera-$(CALDERA_VERSION)
	@mbchk ../bin/caldera-$(CALDERA_VERSION)

build-drivers:
	
	@cp ../other/menu.lst ./
	@for driver in $(DRIVERS); do \
		make -C drivers -C driver_$$driver; \
		# Run the drivers Makefile \
		diff -Naur ./menu.lst drivers/driver_$$driver > patchFile; \
		patch -b -p0 < patchFile; \
		# Add the  menu entry to menu.lst \
	done

build-kernel-image: build-kernel

	@mkdir -p isofiles/boot/grub
	@mkdir -p ../dist
	@mkdir -p isofiles/boot/modules
	@mkdir -p isofiles/boot/drivers
	@cp /usr/lib/grub/i386-pc/stage2_eltorito isofiles/boot/grub/
	@cp ../bin/caldera-$(CALDERA_VERSION) isofiles/boot/caldera
	@cp ../other/menu.lst isofiles/boot/grub
	
	
	@genisoimage -quiet -A Caldera-$(CALDERA_VERSION) -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o ../dist/caldera-$(CALDERA_VERSION).iso isofiles
	@rm -r isofiles

dist-kernel:
	
	@mkdir -p ../dist
	@tar -uaf ../dist/caldera-$(CALDERA_VERSION)-src.tar.gz $(KERNEL_CPP_SRC) $(KERNEL_C_SRC) $(KERNEL_ASM_SRC) $(AUX_FILES) kernel/include

clean-kernel-bin:

	@rm ../bin/caldera-$(CALDERA_VERSION)
	@rm *.o $(KERNEL_ASM_OBJ)

clean-kernel-image:

	@rm ../dist/caldera-$(CALDERA_VERSION).iso
	
clean-kernel-dist:
	
	@rm ../dist/caldera-$(CALDERA_VERSION)-src.tar.gz

test: build-kernel-image

	@qemu -name "Caldera-$(CALDERA_VERSION)" -cdrom ../dist/caldera-$(CALDERA_VERSION).iso