os-k/Makefile

397 lines
14 KiB
Makefile

#=----------------------------------------------------------------------------=#
# OS on Kaleid #
# #
# Desc: Project Makefile #
# #
# #
# Copyright © 2018-2021 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 test testnokvm testnosnd test32 debug gdb ddd gdbnokvm dddnokvm installonimage dust clean OS/K run update-cross-cc
.DELETE_ON_ERROR: $(BINDIR)/kaleid
.DEFAULT_GOAL := all
## VARIABLES ----------------------------------------------------------------- #
# Debug
mode ?= debug
ram ?= 4G
cpu ?= core2duo
compile-cross-cc ?= yes
# Cross-cc
CROSS-CC-DIR := $(shell pwd)/cross-cc
LATEST-CROSS-CC := https://git.a-lec.org/os-k-team/cross-cc-builder/-/archive/main/cross-cc-builder-main.tar
LATEST-CROSS-CC-SOURCES := https://git.a-lec.org/os-k-team/cross-cc-builder/-/archive/main/cross-cc-builder-main.tar
export PATH := $(CROSS-CC-DIR)/bin:$(PATH)
NTHREADS := $(shell nproc)
# Programs
ASM=nasm
LD=x86_64-elf-ld
OBJCOPY=x86_64-elf-objcopy
CCNAME=x86_64-elf-gcc
# Flags
ASMFLAGS=-f elf64
LDFLAGS=-melf_x86_64
COPTIM=-O2
CWARNS=-Wall -Wextra -Wno-unused-parameter -Wno-implicit-fallthrough -Werror=implicit-function-declaration -Werror=return-type #-Wpadded
CINCLUDES=-Iinclude -Iinclude/drivers -Iinclude/kernel
CFLAGS1=-nostdlib -ffreestanding -mcmodel=large -std=gnu11 -fstack-protector-strong -fdump-rtl-expand
CFLAGS2= -c -mno-red-zone -mno-mmx -mno-sse -mno-sse2
CFLAGS= $(CFLAGS1) $(CFLAGS2)
CFLAGS_MATHS= $(CFLAGS1) -c -mno-red-zone -mno-mmx
ifeq ($(mode), release)
CFLAGS += -D_NO_DEBUG
CFLAGS_MATHS += -D_NO_DEBUG
dep += dust
endif
ifeq ($(mode), debug)
CFLAGS += -g
CFLAGS_MATHS += -g
endif
# GCC
KCC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CINCLUDES) \
-D_OSK_SOURCE -D_KALEID_KERNEL
KCC_MATHS=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS_MATHS) $(CINCLUDES) \
-D_OSK_SOURCE -D_KALEID_KERNEL
# Folders
MBRDIR=boot/grub
LOADERDIR=boot/loader
KALEIDDIR=kaleid
OBJDIR=build/obj
KOBJDIR=build/obj/kaleid
LOBJDIR=build/obj/boot
BINDIR=build/bin
BUILDDIR=build
vpath %.c $(KALEIDDIR)
# Installation parameters
installdisk ?= $(BINDIR)/disk.img
# Color codes
CL='\033[0;32m'
CL2='\033[1;36m'
CL3='\033[0m'
NC='\033[1;37m'
## SOURCES INSCRIPTION-------------------------------------------------------- #
# Lib C sources + libbuf source
LibCSources = libc/mem.c libc/ctype.c \
libc/rand.c libc/sprintf.c \
libc/errno.c libc/string.c \
libc/strtol.c \
libbuf/bopen.c libbuf/bputc.c libbuf/bscroll.c \
libbuf/bprint.c libbuf/bgetc.c libbuf/bscan.c \
libbuf/bflush.c libbuf/bwrite.c libbuf/bread.c \
libbuf/bmisc.c libbuf/bclose.c \
LibCObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(LibCSources))
LibCDep=$(patsubst %.c,$(KOBJDIR)/%.d,$(LibCSources))
LibCObj += $(KOBJDIR)/libc/atoi.o $(KOBJDIR)/libc/itoa.o
# Kernel sources
KernSources = kernel/ke/cpuid.c kernel/mm/paging.c \
kernel/ke/idt.c kernel/init/init.c \
kernel/init/table.c kernel/io/cursor.c \
kernel/ke/log.c kernel/io/vga.c \
kernel/ke/panic.c kernel/mm/map.c \
kernel/mm/heap.c kernel/mm/malloc.c \
kernel/mm/gdt.c kernel/ps/sched.c \
kernel/init/info.c kernel/init/ssp.c \
kernel/ke/rtc.c kernel/io/keyb.c \
kernel/io/spkr.c kernel/po/shtdwn.c \
kernel/sh/shell.c kernel/sh/shcmds.c \
kernel/sh/musage.c kernel/sh/argv.c \
kernel/ke/pit.c kernel/sh/testcmds.c \
kernel/mm/palloc.c kernel/io/acpi.c \
kernel/io/pci.c
KernObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(KernSources))
KernDep=$(patsubst %.c,$(KOBJDIR)/%.d,$(KernSources))
# Drivers sources
DriverSources = drivers/ata.c
DriverObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(DriverSources))
DriverDep=$(patsubst %.c,$(KOBJDIR)/%.d,$(DriverSources))
-include $(LibCDep)
-include $(KernDep)
-include $(DriverDep)
## MISC MAKEFILE ------------------------------------------------------------- #
ifeq ($(compile-cross-cc), no)
$(CROSS-CC-DIR)/bin/x86_64-elf-gcc:
@echo -e ${CL2}[$@] ${NC} Downloading and extracting the cross-compiler...${CL3}
@wget $(LATEST-CROSS-CC) -O $(CROSS-CC-DIR).tar.xz -q -c --show-progress
@tar -xaf $(CROSS-CC-DIR).tar.xz --skip-old-files
@echo -e ${CL2}[$@] ${CL}Success.${CL3}
endif
ifeq ($(compile-cross-cc), yes)
$(CROSS-CC-DIR)/bin/x86_64-elf-gcc:
@echo -e ${CL2}[$@] ${NC} Downloading and extracting the cross-compiler sources...${CL3}
@wget $(LATEST-CROSS-CC-SOURCES) -O $(CROSS-CC-DIR)-builder.tar -q -c --show-progress
@mkdir -p $(CROSS-CC-DIR)-builder
@tar -xvf $(CROSS-CC-DIR)-builder.tar --skip-old-files -C $(CROSS-CC-DIR)-builder --strip-components 1
cd $(CROSS-CC-DIR)-builder && make
ln -s $(CROSS-CC-DIR)-builder/cross-cc $(CROSS-CC-DIR)
@echo -e ${CL2}[$@] ${CL}Success.${CL3}
endif
./ProjectTree: ./.stylehlp_sh
@cat ./.stylehlp_sh > ./ProjectTree
@echo -e "\n" >> ./ProjectTree
@tree --dirsfirst -I "bin|obj|cross-cc*">> ./ProjectTree
@echo -e ${CL2}[$@] ${CL}Generated.${CL3}
$(KOBJDIR):
@mkdir -p $(KOBJDIR)
wc:
@rm -f build/kaleid*_disasm.asm
@cat $(shell find -name *.[ch]) $(shell find -name *.asm) $(shell find -name *.inc) | wc -l
egypt: CFLAGS := -DNDEBUG $(filter-out -fstack-protector-all,$(CFLAGS))
egypt: dust $(LibCObj) $(KernObj) $(DriverObj)
@find -name '*.expand' -o -name '*.expand' | xargs cat > rtl_exp.out
@egypt < rtl_exp.out | dot -Tps -o osk-graph.ps
@rm -f rtl_exp.out
@evince osk-graph.ps &
## LIB C MAKEFILE ------------------------------------------------------------ #
$(KOBJDIR)/libc/atoi.o: $(KALEIDDIR)/libc/atoi.c | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(KCC) -D_NEED_ATOI $< -o $@.1
@$(KCC) -D_NEED_ATOL $< -o $@.2
@$(KCC) -D_NEED_ATOU $< -o $@.3
@$(KCC) -D_NEED_ATOUL $< -o $@.4
@$(LD) $(LDFLAGS) -r $@.1 $@.2 $@.3 $@.4 -o $@
@rm -f $@.1 $@.2 $@.3 $@.4
@echo -e ${CL2}[$@] ${CL}Compiled.${CL3}
$(KOBJDIR)/libc/itoa.o: $(KALEIDDIR)/libc/itoa.c | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(KCC) -D_NEED_ITOA $< -o $@.1
@$(KCC) -D_NEED_LTOA $< -o $@.2
@$(KCC) -D_NEED_UTOA $< -o $@.3
@$(KCC) -D_NEED_ULTOA $< -o $@.4
@$(LD) $(LDFLAGS) -r $@.1 $@.2 $@.3 $@.4 -o $@
@rm -f $@.1 $@.2 $@.3 $@.4
@echo -e ${CL2}[$@] ${CL}Compiled.${CL3}
$(KOBJDIR)/libc/mem.o: $(KALEIDDIR)/libc/mem.c | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(KCC) -fno-strict-aliasing $< -o $@
@echo -e ${CL2}[$@] ${CL}Compiled.${CL3}
## KERNEL SPECIAL RECIPES MAKEFILE ------------------------------------------- #
$(KOBJDIR)/kernel/ke/idt.o: $(KALEIDDIR)/kernel/ke/idt.c \
$(KALEIDDIR)/kernel/ke/isr.asm | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/ke/isr.asm -o $@.1
@$(KCC) $< -o $@.2
@$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@
@rm -f $@.1 $@.2
@echo -e ${CL2}[$@] ${CL}Compiled.${CL3}
$(KOBJDIR)/kernel/ke/cpuid.o: $(KALEIDDIR)/kernel/ke/cpuid.c \
$(KALEIDDIR)/kernel/ke/cpuf.asm | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/ke/cpuf.asm -o $@.1
@$(KCC_MATHS) $< -o $@.2
@$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@
@rm -f $@.1 $@.2
@echo -e ${CL2}[$@] ${CL}Compiled.${CL3}
$(KOBJDIR)/kernel/ke/shcmds.o: $(KALEIDDIR)/kernel/sh/shcmds.c | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(KCC_MATHS) -MM -MT $(@:%.d=%.o) -MF $@ $<
@echo -e ${CL2}[$@] ${CL}Compiled.${CL3}
$(KOBJDIR)/kernel/mm/paging.o: $(KALEIDDIR)/kernel/mm/paging.c \
$(KALEIDDIR)/kernel/mm/paging.asm | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/mm/paging.asm -o $@.1
@$(KCC) $< -o $@.2
@$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@
@rm -f $@.1 $@.2
@echo -e ${CL2}[$@] ${CL}Compiled.${CL3}
$(KOBJDIR)/kernel/mm/gdt.o: $(KALEIDDIR)/kernel/mm/gdt.c \
$(KALEIDDIR)/kernel/mm/gdt.asm | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/mm/gdt.asm -o $@.1
@$(KCC) $< -o $@.2
@$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@
@rm -f $@.1 $@.2
@echo -e ${CL2}[$@] ${CL}Compiled.${CL3}
## DRIVERS SPECIAL RECIPES MAKEFILE ------------------------------------------ #
$(KOBJDIR)/drivers/ata.o: $(KALEIDDIR)/drivers/ata.c $(KALEIDDIR)/drivers/ata.asm | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(ASM) $(ASMFLAGS) $(KALEIDDIR)/drivers/ata.asm -o $@.1
@$(KCC) $< -o $@.2
@$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@
@rm -f $@.1 $@.2
@echo -e ${CL2}[$@] ${CL}Compiled.${CL3}
## DEPENDENCIES MAKEFILE ----------------------------------------------------- #
$(KOBJDIR)/%.d: %.c $(CROSS-CC-DIR)/bin/x86_64-elf-gcc | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(KCC) -MM -MT $(@:%.d=%.o) -MF $@ $<
@echo -e ${CL2}[$@] ${CL}Dependencies generated.${CL3}
## MAIN MAKEFILE ------------------------------------------------------------- #
$(KOBJDIR)/%.o: %.c | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(KCC) $< -o $@
@echo -e ${CL2}[$@] ${CL}Compiled.${CL3}
$(BINDIR)/kaleid: $(LOBJDIR)/kaleid.x86_64
@echo -e ${CL2}[$@] ${NC}Objcopy...${CL3}
@mkdir -p $(shell dirname $@)
@$(OBJCOPY) -I elf64-x86-64 -O elf32-i386 $(LOBJDIR)/kaleid.x86_64 \
$(BINDIR)/kaleid
@echo -e ${CL2}[$@] ${CL}Success.${CL3}
$(LOBJDIR)/kaleid.x86_64: $(LibCObj) $(KernObj) $(DriverObj) \
$(LOBJDIR)/loader.o $(BUILDDIR)/kernel.ld
@echo -e ${CL2}[$@] ${NC}Linking kernel objects...${CL3}
@mkdir -p $(shell dirname $@)
@$(LD) $(LDFLAGS) -T $(BUILDDIR)/kernel.ld \
$(LOBJDIR)/loader.o $(KernObj) $(DriverObj) $(LibCObj) \
-o $(LOBJDIR)/kaleid.x86_64
@echo -e ${CL2}[$@] ${CL}Success.${CL3}
$(LOBJDIR)/loader.o: $(LOADERDIR)/loader.asm $(LOADERDIR)/*/*.inc
@echo -e ${CL2}[$@] ${NC}Making loader...${CL3}
@mkdir -p $(shell dirname $@)
@$(ASM) $(ASMFLAGS) $(LOADERDIR)/loader.asm -o $(LOBJDIR)/loader.o > /dev/null
@echo -e ${CL2}[$@] ${CL}Success.${CL3}
OS/K: $(dep) ./ProjectTree $(BINDIR)/kaleid
@echo -e ${CL2}[[$@]] ${NC} OS/K successfully made, $(mode) mode.${CL3}
all : $(CROSS-CC-DIR)/bin/x86_64-elf-gcc
@make OS/K -j $(NTHREADS)
## QEMU/DEBUG RELATED
test: all installonimage
@qemu-system-x86_64 -vga std -enable-kvm -machine type=q35 -cpu host -s \
-rtc base=localtime -m $(ram) -hda $(installdisk) -net nic,model=rtl8139 \
-audiodev pa,id=snd0 -machine pcspk-audiodev=snd0 \
-d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &
run: test
testnokvm: all installonimage
@qemu-system-x86_64 -vga std -cpu $(cpu) -machine type=q35 -s \
-audiodev pa,id=snd0 -machine pcspk-audiodev=snd0 \
-rtc base=localtime -m $(ram) -hda $(installdisk) \
-d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &
testnosnd: all installonimage
@qemu-system-x86_64 -vga std -enable-kvm -machine type=q35 -cpu host -s \
-rtc base=localtime -m $(ram) -hda $(installdisk) \
-d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &
test32: all installonimage
@qemu-system-i386 -m $(ram) -hda $(installdisk) -machine type=q35 -d \
cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &
gdb: all installonimage
@setsid qemu-system-x86_64 -m $(ram) -enable-kvm -machine type=q35 -rtc base=localtime \
-hda $(installdisk) -no-reboot -no-shutdown -d \
cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log &
@gdb \
-ex "set arch i386:x86-64:intel" \
-ex "target remote localhost:1234" \
-ex "symbol-file $(BINDIR)/kaleid" \
-ex "break BtStartKern" \
ddd: all installonimage
@setsid qemu-system-x86_64 -m $(ram) -enable-kvm -machine type=q35 -rtc base=localtime \
-hda $(installdisk) -no-reboot -no-shutdown -d \
cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log &
@ddd -n
gdbnokvm: all installonimage
@setsid qemu-system-x86_64 -m $(ram) -machine type=q35 -rtc base=localtime \
-hda $(installdisk) -no-reboot -no-shutdown -d \
cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log &
@gdb \
-ex "set arch i386:x86-64:intel" \
-ex "target remote localhost:1234" \
-ex "symbol-file $(BINDIR)/kaleid" \
-ex "break BtStartKern" \
dddnokvm: all installonimage
@setsid qemu-system-x86_64 -m $(ram) -machine type=q35 -rtc base=localtime \
-hda $(installdisk) -no-reboot -no-shutdown -d \
cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log &
@ddd -n
## HD IMAGE RELATED ---------------------------------------------------------- #
installonimage: $(installdisk) $(MBRDIR)/grub.cfg
@echo -e ${CL2}[$@] ${NC}Installing boot configuration on image...${CL3}
@$(BUILDDIR)/install-os-k.sh $(installdisk) $(MBRDIR)/grub.cfg $(BINDIR)/kaleid
@echo -e ${CL2}[$@] ${CL}Success.${CL3}
$(installdisk): $(BUILDDIR)/create_disk.sh
@echo -e ${CL2}[$@]${NC} Constructing disk image $@...${CL3}
@mkdir -p $(shell dirname $@)
@$(BUILDDIR)/create_disk.sh $(installdisk)
@echo -e ${CL2}[$@]${NC} Constructing disk image...${CL3}
## CLEANUP RELATED ----------------------------------------------------------- #
dust:
@rm -Rf $(OBJDIR)/*
@echo -e ${CL2}[$@] ${CL}Cleaned.${CL3}
clean:
@rm -Rvf ./ProjectTree $(BUILDDIR)/*.log
@rm -Rvf $(BINDIR) $(OBJDIR)
@echo -e ${CL2}[$@] ${CL}Cleaned.${CL3}
update-cross-cc:
@rm -rf cross-cc
@echo -e ${CL2}[$@] ${CL}Ready to update.${CL3}