os-k/Makefile

201 lines
8.1 KiB
Makefile

#=----------------------------------------------------------------------------=#
# 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/>. #
#=----------------------------------------------------------------------------=#
## VARIABLES ----------------------------------------------------------------- #
#Programs
ASM=nasm
LD=ld
CCNAME=x86_64-elf-gcc
ASMFLAGS=-f elf64
LDFLAGS=-melf_x86_64
COPTIM=-O2
CWARNS=-Wall -Wextra # -Werror=implicit-function-declaration
CINCLUDES=-Ikaleid/include
CFLAGS1=-nostdlib -ffreestanding -mcmodel=large # -std=gnu11
CFLAGS2= -c -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -fno-strict-aliasing
CFLAGS=$(CFLAGS1) $(CFLAGS2) -DNDEBUG
KCC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CINCLUDES) -D_OSK_SOURCE -D_KALEID_KERNEL
#Folders
MBRDIR=boot/grub
LOADERDIR=boot/loader
KERNELDIR=kaleid
OBJDIR=build/obj
BINDIR=build/bin
#Color codes
CL='\033[0;32m'
CL2='\033[1;36m'
CL3='\033[0m'
NC='\033[1;37m'
.PHONY: all
all : OS/K
## KALEID MAKEFILE ----------------------------------------------------------- #
# Common objects
kal_com_obj= $(OBJDIR)/kaleid/atoi.o $(OBJDIR)/kaleid/ctype.o \
$(OBJDIR)/kaleid/itoa.o $(OBJDIR)/kaleid/memory.o \
$(OBJDIR)/kaleid/rand.o $(OBJDIR)/kaleid/sprintf.o \
$(OBJDIR)/kaleid/status.o $(OBJDIR)/kaleid/string.o \
$(OBJDIR)/kaleid/strtol.o $(OBJDIR)/kaleid/argv.o \
$(OBJDIR)/kaleid/prog.o $(OBJDIR)/kaleid/atol.o \
$(OBJDIR)/kaleid/atou.o $(OBJDIR)/kaleid/atoul.o \
$(OBJDIR)/kaleid/utoa.o $(OBJDIR)/kaleid/ltoa.o \
$(OBJDIR)/kaleid/ultoa.o
$(OBJDIR)/kaleid/atoi.o: $(KERNELDIR)/crtlib/atoi.c
@$(KCC) -D_NEED_ATOI $< -o $@
$(OBJDIR)/kaleid/atol.o: $(KERNELDIR)/crtlib/atoi.c
@$(KCC) -D_NEED_ATOL $< -o $@
$(OBJDIR)/kaleid/atou.o: $(KERNELDIR)/crtlib/atoi.c
@$(KCC) -D_NEED_ATOU $< -o $@
$(OBJDIR)/kaleid/atoul.o: $(KERNELDIR)/crtlib/atoi.c
@$(KCC) -D_NEED_ATOUL $< -o $@
$(OBJDIR)/kaleid/ctype.o: $(KERNELDIR)/crtlib/ctype.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/itoa.o: $(KERNELDIR)/crtlib/itoa.c
@$(KCC) -D_NEED_ITOA $< -o $@
$(OBJDIR)/kaleid/ltoa.o: $(KERNELDIR)/crtlib/itoa.c
@$(KCC) -D_NEED_LTOA $< -o $@
$(OBJDIR)/kaleid/utoa.o: $(KERNELDIR)/crtlib/itoa.c
@$(KCC) -D_NEED_UTOA $< -o $@
$(OBJDIR)/kaleid/ultoa.o: $(KERNELDIR)/crtlib/itoa.c
@$(KCC) -D_NEED_ULTOA $< -o $@
$(OBJDIR)/kaleid/memory.o: $(KERNELDIR)/crtlib/memory.c
@$(KCC) -fno-strict-aliasing $< -o $@
$(OBJDIR)/kaleid/rand.o: $(KERNELDIR)/crtlib/rand.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/sprintf.o: $(KERNELDIR)/crtlib/sprintf.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/status.o: $(KERNELDIR)/crtlib/status.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/string.o: $(KERNELDIR)/crtlib/string.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/strtol.o: $(KERNELDIR)/crtlib/strtol.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/argv.o: $(KERNELDIR)/extras/argv.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/prog.o: $(KERNELDIR)/extras/prog.c
@$(KCC) $< -o $@
# Kernel objects
kal_kern_obj= $(OBJDIR)/kaleid/kernel/cpuid.o $(OBJDIR)/kaleid/kernel/init.o \
$(OBJDIR)/kaleid/kernel/table.o $(OBJDIR)/kaleid/kernel/cursor.o \
$(OBJDIR)/kaleid/kernel/term.o $(OBJDIR)/kaleid/kernel/vga.o \
$(OBJDIR)/kaleid/kernel/panic.o
$(OBJDIR)/kaleid/kernel/cpuid.o: $(KERNELDIR)/kernel/cpu/cpuid.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/kernel/init.o: $(KERNELDIR)/kernel/init/init.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/kernel/table.o: $(KERNELDIR)/kernel/init/table.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/kernel/cursor.o: $(KERNELDIR)/kernel/io/cursor.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/kernel/term.o: $(KERNELDIR)/kernel/io/term.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/kernel/vga.o: $(KERNELDIR)/kernel/io/vga.c
@$(KCC) $< -o $@
$(OBJDIR)/kaleid/kernel/panic.o: $(KERNELDIR)/kernel/ke/panic.c
@$(KCC) $< -o $@
## MAIN MAKEFILE ------------------------------------------------------------- #
.PHONY: test
test: all
@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
.PHONY: test32
test32: all
@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
.PHONY: debug
debug: all
@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
.PHONY:OS/K
OS/K: $(BINDIR)/kaleid $(BINDIR)/disk.img
@mkdir -p $(BINDIR)/disk
@echo ${CL2}[[$@]] ${NC}Integrating kernel...${CL3}
-@$(MBRDIR)/umount.sh $(BINDIR)/disk
@$(MBRDIR)/mount.sh $(BINDIR)/disk.img $(BINDIR)/disk
@cp $(BINDIR)/kaleid $(BINDIR)/disk/boot/kaleid
@$(MBRDIR)/umount.sh $(BINDIR)/disk
@echo ${CL2}[[$@]] ${CL}Success.${CL3}
@rmdir $(BINDIR)/disk
.PHONY: install_mbr
install_mbr: $(BINDIR)/disk.img $(MBRDIR)/grub.cfg
@mkdir -p $(BINDIR)/disk
@echo ${CL2}[$@] ${NC}Installing MBR on image...${CL3}
-@$(MBRDIR)/umount.sh $(BINDIR)/disk
@$(MBRDIR)/grub-install.sh $(BINDIR)/disk.img $(BINDIR)/disk $(MBRDIR)/grub.cfg
@tail -1 grub.log | head -1 | grep "Installation terminée, sans erreur."
@rmdir $(BINDIR)/disk
@echo ${CL2}[$@] ${CL}Success.${CL3}
.PHONY: clean
clean:
-@$(MBRDIR)/umount.sh $(BINDIR)/disk
@rm -Rvf $(BINDIR)/*.*
@rm -Rvf $(OBJDIR)/*.o
@rm -Rvf $(OBJDIR)/*/*.o
@rm -Rvf $(OBJDIR)/*/*/*.o
$(BINDIR)/kaleid: $(OBJDIR)/boot/kaleid.x86_64
@echo ${CL2}[$@] ${NC}Objcopy...${CL3}
@x86_64-elf-objcopy -I elf64-x86-64 -O elf32-i386 $(OBJDIR)/boot/kaleid.x86_64 $(BINDIR)/kaleid
@echo ${CL2}[$@] ${CL}Success.${CL3}
$(OBJDIR)/boot/kaleid.x86_64: $(kal_kern_obj) $(kal_com_obj) $(OBJDIR)/boot/loader.o
@echo ${CL2}[$@] ${NC}Linking kernel objects...${CL3}
@$(LD) $(LDFLAGS) -T build/kernel.ld \
$(OBJDIR)/boot/loader.o \
$(OBJDIR)/kaleid/*.o \
$(OBJDIR)/kaleid/kernel/*.o \
-o $(OBJDIR)/boot/kaleid.x86_64
@echo ${CL2}[$@] ${CL}Success.${CL3}
$(OBJDIR)/boot/loader.o: $(LOADERDIR)/loader.asm
@echo ${CL2}[$@] ${NC}Making loader...${CL3}
@$(ASM) $(ASMFLAGS) $(LOADERDIR)/loader.asm -o $(OBJDIR)/boot/loader.o > /dev/null
@echo ${CL2}[$@] ${CL}Success.${CL3}
$(BINDIR)/disk.img: $(MBRDIR)/create_disk.sh
@echo ${CL2}[$@]${NC} Constructing disk image...${CL3}
-@$(MBRDIR)/umount.sh $(BINDIR)/disk
@$(MBRDIR)/create_disk.sh $(BINDIR)/disk.img
@make install_mbr
@echo ${CL2}[$@]${NC} Constructing disk image...${CL3}