#=----------------------------------------------------------------------------=#
#                            GNU GPL OS/K                                      #
#                                                                              #
#   Desc:   Project Makefile                                                   #
#                                                                              #
#                                                                              #
#   Copyright © 2018-2019 The OS/K Team                                        #
#                                                                              #
#   This file is part of OS/K.                                                 #
#                                                                              #
#   OS/K is free software: you can redistribute it and/or modify               #
#   it under the terms of the GNU General Public License as published by       #
#   the Free Software Foundation, either version 3 of the License, or          #
#   any later version.                                                         #
#                                                                              #
#   OS/K is distributed in the hope that it will be useful,                    #
#   but WITHOUT ANY WARRANTY# without even the implied warranty of             #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
#   GNU General Public License for more details.                               #
#                                                                              #
#   You should have received a copy of the GNU General Public License          #
#   along with OS/K.  If not, see <https://www.gnu.org/licenses/>.             #
#=----------------------------------------------------------------------------=#

.PHONY: all

#Programs
ASM=nasm
LD=ld
ASMFLAGS=-f elf64
LDFLAGS= -melf_x86_64

#Folders
MBRDIR=boot/grub
LOADERDIR=boot/loader
OBJDIR=build/obj
BINDIR=build/bin

# Object to link (temp)
l_objects=./build/obj/kaleid/crtlib/memory.o ./build/obj/kaleid/crtlib/rand.o ./build/obj/kaleid/crtlib/string.o ./build/obj/kaleid/crtlib/ultoa.o ./build/obj/kaleid/crtlib/strtol.o ./build/obj/kaleid/crtlib/utoa.o ./build/obj/kaleid/crtlib/status.o ./build/obj/kaleid/crtlib/atoul.o ./build/obj/kaleid/crtlib/atol.o ./build/obj/kaleid/crtlib/itoa.o ./build/obj/kaleid/crtlib/ltoa.o ./build/obj/kaleid/crtlib/atou.o ./build/obj/kaleid/crtlib/arith.o ./build/obj/kaleid/crtlib/atoi.o ./build/obj/kaleid/extras/prog.o ./build/obj/kaleid/extras/argv.o ./build/obj/kaleid/kernel/init/table.o ./build/obj/kaleid/kernel/init/init.o ./build/obj/kaleid/kernel/io/vga.o ./build/obj/kaleid/kernel/io/cursor.o ./build/obj/kaleid/kernel/io/term.o ./build/obj/kaleid/kernel/ke/panic.o ./build/obj/boot/loader.o

#Color codes
CL='\033[0;32m'
CL2='\033[1;36m'
CL3='\033[0m'
NC='\033[1;37m'

kernel:
	cpp ./Makefile.in > build/Makefile.out
	python build/idttool.py
	make kernel -f build/Makefile.out.2
	rm build/Makefile.out build/Makefile.out.2

kernel-asm:
	cpp -D_TO_ASM ./Makefile.in > build/Makefile.out
	python build/idttool.py
	make kernel -f build/Makefile.out.2
	rm build/Makefile.out build/Makefile.out.2

tests:
	cpp -D_TESTS ./Makefile.in > build/Makefile.out
	python build/idttool.py
	make tests -f build/Makefile.out.2
	rm build/Makefile.out build/Makefile.out.2

boot.mbr: $(BINDIR)/disk.img $(MBRDIR)/grub.cfg
	@mkdir -p $(BINDIR)/disk
	@echo ${CL2}[boot.mbr]${NC} Installing MBR on image...${CL3}
	@$(MBRDIR)/grub-install.sh $(BINDIR)/disk.img $(BINDIR)/disk $(MBRDIR)/grub.cfg
	@echo ${CL2}[boot.mbr]${CL} OK${CL3}
	@rmdir $(BINDIR)/disk

boot.loader.asm: $(LOADERDIR)/loader.asm
	@echo ${CL2}[boot.loader.asm]${NC} Making loader...${CL3}
	@$(ASM) $(ASMFLAGS) $(LOADERDIR)/loader.asm -o $(OBJDIR)/boot/loader.o > /dev/null
	@echo ${CL2}[boot.loader.asm]${CL} OK${CL3}

loader: boot.loader.asm link copykernel

copykernel:
	@mkdir -p $(BINDIR)/disk
	@echo ${CL2}[disk]${NC} Integrating kernel...${CL3}
	@$(MBRDIR)/mount.sh $(BINDIR)/disk.img $(BINDIR)/disk
	@cp $(BINDIR)/kaleid $(BINDIR)/disk/boot/kaleid
	@$(MBRDIR)/umount.sh $(BINDIR)/disk
	@echo ${CL2}[disk]${CL} OK${CL3}
	@rmdir $(BINDIR)/disk

make_disk:
	@echo ${CL2}[make_disk]${NC} Constructing disk image...${CL3}
	@$(MBRDIR)/create_disk.sh $(BINDIR)/disk.img
	@echo ${CL2}[make_disk]${CL} OK${CL3}

kaleid: kernel loader

test: kaleid
	@qemu-system-x86_64 -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -enable-kvm 2> qemu.log &
	@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > loader_disasm64.asm
	@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > loader_disasm32.asm


test32: kaleid
	@qemu-system-i386 -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -enable-kvm 2> qemu.log &
	@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > loader_disasm64.asm
	@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > loader_disasm32.asm

debug: kaleid
	@qemu-system-x86_64 -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -s -S -enable-kvm 2> qemu.log &
	@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > loader_disasm64.asm
	@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > loader_disasm32.asm

boot: make_disk boot.mbr
	@echo ${CL2}[[boot]]${CL} Terminated without error.${CL3}

all: boot kaleid
	@echo ${CL2}[[all]]${CL} Terminated without error.${CL3}

link:
	@$(LD) $(LDFLAGS) -T build/kernel.ld $(l_objects) -o $(OBJDIR)/boot/kaleid.x86_64
	@x86_64-elf-objcopy -I elf64-x86-64 -O elf32-i386 $(OBJDIR)/boot/kaleid.x86_64 $(BINDIR)/kaleid

clean:
	@rm -Rf $(BINDIR)/*
	@rm -Rf $(OBJDIR)/*/*/*/*.o