From b7737793e390aef44bc7fd1d91e2c71559bfc239 Mon Sep 17 00:00:00 2001 From: Amelia Coutard Date: Fri, 14 Oct 2022 01:17:24 +0200 Subject: [PATCH] Restructured build system, to allow for easier adding of separate programs later --- .gitignore | 4 +- Makefile | 61 +++++++++++------------------- linker.ld => kernel/linker.ld | 0 kernel/module.mk | 55 +++++++++++++++++++++++++++ {src => kernel/src}/boot.S | 0 {src => kernel/src}/crti.S | 0 {src => kernel/src}/crtn.S | 0 {src => kernel/src}/interrupts.S | 0 {src => kernel/src}/interrupts.cpp | 0 {src => kernel/src}/interrupts.hpp | 0 {src => kernel/src}/kernel.cpp | 0 {src => kernel/src}/multiboot2.hpp | 0 {src => kernel/src}/paging.cpp | 0 {src => kernel/src}/paging.hpp | 0 {src => kernel/src}/phys_ptr.hpp | 0 {src => kernel/src}/ring3.S | 0 {src => kernel/src}/ring3.cpp | 0 {src => kernel/src}/ring3.hpp | 0 {src => kernel/src}/serial.cpp | 0 {src => kernel/src}/serial.hpp | 0 {src => kernel/src}/utils.cpp | 0 {src => kernel/src}/utils.hpp | 0 22 files changed, 79 insertions(+), 41 deletions(-) rename linker.ld => kernel/linker.ld (100%) create mode 100644 kernel/module.mk rename {src => kernel/src}/boot.S (100%) rename {src => kernel/src}/crti.S (100%) rename {src => kernel/src}/crtn.S (100%) rename {src => kernel/src}/interrupts.S (100%) rename {src => kernel/src}/interrupts.cpp (100%) rename {src => kernel/src}/interrupts.hpp (100%) rename {src => kernel/src}/kernel.cpp (100%) rename {src => kernel/src}/multiboot2.hpp (100%) rename {src => kernel/src}/paging.cpp (100%) rename {src => kernel/src}/paging.hpp (100%) rename {src => kernel/src}/phys_ptr.hpp (100%) rename {src => kernel/src}/ring3.S (100%) rename {src => kernel/src}/ring3.cpp (100%) rename {src => kernel/src}/ring3.hpp (100%) rename {src => kernel/src}/serial.cpp (100%) rename {src => kernel/src}/serial.hpp (100%) rename {src => kernel/src}/utils.cpp (100%) rename {src => kernel/src}/utils.hpp (100%) diff --git a/.gitignore b/.gitignore index a701a8f..60ebeb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ out -deps +dep isodir -doxygen +amycros.iso diff --git a/Makefile b/Makefile index 63b12d6..5b2df7f 100644 --- a/Makefile +++ b/Makefile @@ -1,50 +1,33 @@ +# Written in 2022 by Amélia COUTARD +# To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this file to the public domain worldwide. This file is distributed without any warranty. +# You should have received a copy of the CC0 Public Domain Dedication along with this file. If not, see . + +# So, about the license. I don't think I have a copyright on this file's code, but, +# since I gave it the CC0 license in case I do, that doesn't matter either way. + .PHONY: build qemu clean -OUT_DIR ?= out/ -SRC_DIR ?= src/ -DEPS_DIR ?= deps/ +build: # Build as default target CXX := x86_64-elf-g++ CXXFLAGS ?= -O2 CXXFLAGS := $(CXXFLAGS) -Wall -Wextra -Werror -std=c++20 \ - -ffreestanding -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -fno-exceptions -fno-rtti \ - -isystem /usr/include -isystem /usr/include/c++/12.2.0 -isystem /usr/include/c++/12.2.0/x86_64-pc-linux-gnu - + -mno-mmx -mno-sse -mno-sse2 -fno-exceptions -fno-rtti -ffreestanding LDFLAGS ?= -O2 -LDFLAGS := $(LDFLAGS) -Wall -Wextra -Werror -std=c++20 \ - -ffreestanding -T linker.ld -z max-page-size=0x1000 \ - -mno-red-zone -mcmodel=kernel # Normally has no effect, but is used to change the multilib libgcc, to use mcmodel=kernel and be able to use global constructors and destructors. -LDLIBS := $(LDLIBS) -nostdlib -lgcc - -CPPOBJS := $(patsubst $(SRC_DIR)%,$(OUT_DIR)%.o,$(shell find $(SRC_DIR) -name '*.cpp')) -ASMOBJS := $(patsubst $(SRC_DIR)%,$(OUT_DIR)%.o,$(shell find $(SRC_DIR) -name '*.S' -and -not -name 'crti.S' -and -not -name 'crtn.S')) -OBJECTS := $(CPPOBJS) $(ASMOBJS) -CRTI_OBJ := $(OUT_DIR)crti.S.o -CRTBEGIN_OBJ := $(shell $(CXX) $(LDFLAGS) -print-file-name=crtbegin.o) -CRTEND_OBJ := $(shell $(CXX) $(LDFLAGS) -print-file-name=crtend.o) -CRTN_OBJ := $(OUT_DIR)crtn.S.o - -build: $(OUT_DIR)amycros.iso -qemu: build - qemu-system-x86_64 -cdrom $(OUT_DIR)amycros.iso -serial stdio -cpu qemu64,pdpe1gb -no-reboot $(QEMUFLAGS) +LDFLAGS := $(LDFLAGS) -Wall -Wextra -Werror -std=c++20 -ffreestanding -$(OUT_DIR)amycros.iso: $(OUT_DIR)kernel.elf64 grub.cfg - mkdir -p isodir/boot/grub - cp $(OUT_DIR)kernel.elf64 isodir/boot - cp grub.cfg isodir/boot/grub - grub-mkrescue -o "$@" isodir +TO_ISO := +TO_CLEAN := -clean: - -rm -rf $(DEPS_DIR) $(OUT_DIR) isodir +include */module.mk +build: amycros.iso +qemu: build + qemu-system-x86_64 -cdrom amycros.iso -serial stdio -cpu qemu64,pdpe1gb -no-reboot $(QEMUFLAGS) +clean: + -rm -rf $(TO_CLEAN) isodir amycros.iso -$(OUT_DIR)kernel.elf64: $(OBJECTS) $(CRTI_OBJ) $(CRTBEGIN_OBJ) $(CRTEND_OBJ) $(CRTN_OBJ) linker.ld - mkdir -p $(OUT_DIR) - $(CXX) $(LDFLAGS) -o "$@" $(CRTI_OBJ) $(CRTBEGIN_OBJ) $(OBJECTS) $(CRTEND_OBJ) $(CRTN_OBJ) $(LDLIBS) - -$(OUT_DIR)%.o: $(SRC_DIR)% - mkdir -p $(@D) - mkdir -p $(dir $(DEPS_DIR)$*) - $(CXX) $(CXXFLAGS) -c "$<" -MMD -MT "$@" -MF "$(DEPS_DIR)$*.d" -o "$@" - --include deps/*.d +amycros.iso: $(TO_ISO) grub.cfg + mkdir -p isodir/boot/grub + install -m 644 grub.cfg isodir/boot/grub/grub.cfg + grub-mkrescue -o "$@" isodir diff --git a/linker.ld b/kernel/linker.ld similarity index 100% rename from linker.ld rename to kernel/linker.ld diff --git a/kernel/module.mk b/kernel/module.mk new file mode 100644 index 0000000..eefe87c --- /dev/null +++ b/kernel/module.mk @@ -0,0 +1,55 @@ +# Written in 2022 by Amélia COUTARD +# To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this file to the public domain worldwide. This file is distributed without any warranty. +# You should have received a copy of the CC0 Public Domain Dedication along with this file. If not, see . + +# So, about the license. I don't think I have a copyright on this file's code, but, +# since I gave it the CC0 license in case I do, that doesn't matter either way. + +SRC_DIR := kernel/src/ +OUT_DIR := kernel/out/ +DEP_DIR := kernel/dep/ +EXEC_NAME := kernel.elf64 + +TO_ISO += isodir/boot/$(EXEC_NAME) +TO_CLEAN += $(OUT_DIR) $(DEP_DIR) + +OLD_CXXFLAGS := $(CXXFLAGS) +OLD_LDFLAGS := $(LDFLAGS) +CXXFLAGS := $(OLD_CXXFLAGS) -mcmodel=kernel -mno-red-zone \ + -isystem /usr/include -isystem /usr/include/c++/12.2.0 \ + -isystem /usr/include/c++/12.2.0/x86_64-pc-linux-gnu + # The previous two lines are a dirty hack. +LDFLAGS := $(OLD_LDFLAGS) -T kernel/linker.ld -z max-page-size=0x1000 \ + -mno-red-zone -mcmodel=kernel + +CPPOBJS := $(patsubst $(SRC_DIR)%,$(OUT_DIR)%.o,$(shell find $(SRC_DIR) -name '*.cpp')) +ASMOBJS := $(patsubst $(SRC_DIR)%,$(OUT_DIR)%.o,$(shell find $(SRC_DIR) -name '*.S' | grep -v crt)) +CRTI_OBJ := $(OUT_DIR)crti.S.o +CRTBEGIN_OBJ := $(shell $(CXX) $(LDFLAGS) -print-file-name=crtbegin.o) +CRTEND_OBJ := $(shell $(CXX) $(LDFLAGS) -print-file-name=crtend.o) +CRTN_OBJ := $(OUT_DIR)crtn.S.o +OBJS := $(CRTI_OBJ) $(CRTBEGIN_OBJ) $(CPPOBJS) $(ASMOBJS) $(CRTEND_OBJ) $(CRTN_OBJ) + +isodir/boot/$(EXEC_NAME): $(OUT_DIR)$(EXEC_NAME) + mkdir -p "$(@D)" + install -m 644 "$<" "$@" + +$(OUT_DIR)$(EXEC_NAME): OBJS := $(OBJS) +$(OUT_DIR)$(EXEC_NAME): LDLIBS := -nostdlib -lgcc +$(OUT_DIR)$(EXEC_NAME): LDFLAGS := $(LDFLAGS) +$(OUT_DIR)$(EXEC_NAME): $(OBJS) kernel/linker.ld + mkdir -p "$(@D)" + $(CXX) $(LDFLAGS) -o "$@" $(OBJS) $(LDLIBS) + + +$(OUT_DIR)%.o: DEP_DIR := $(DEP_DIR) +$(OUT_DIR)%.o: CXXFLAGS := $(CXXFLAGS) +$(OUT_DIR)%.o: $(SRC_DIR)% + mkdir -p $(@D) + mkdir -p $(dir $(DEP_DIR)$*) + $(CXX) $(CXXFLAGS) -c "$<" -MMD -MT "$@" -MF "$(DEP_DIR)$*.d" -o "$@" + +-include $(DEPS_DIR)*.d + +CXXFLAGS := $(OLD_CXXFLAGS) +LDFLAGS := $(OLD_LDFLAGS) diff --git a/src/boot.S b/kernel/src/boot.S similarity index 100% rename from src/boot.S rename to kernel/src/boot.S diff --git a/src/crti.S b/kernel/src/crti.S similarity index 100% rename from src/crti.S rename to kernel/src/crti.S diff --git a/src/crtn.S b/kernel/src/crtn.S similarity index 100% rename from src/crtn.S rename to kernel/src/crtn.S diff --git a/src/interrupts.S b/kernel/src/interrupts.S similarity index 100% rename from src/interrupts.S rename to kernel/src/interrupts.S diff --git a/src/interrupts.cpp b/kernel/src/interrupts.cpp similarity index 100% rename from src/interrupts.cpp rename to kernel/src/interrupts.cpp diff --git a/src/interrupts.hpp b/kernel/src/interrupts.hpp similarity index 100% rename from src/interrupts.hpp rename to kernel/src/interrupts.hpp diff --git a/src/kernel.cpp b/kernel/src/kernel.cpp similarity index 100% rename from src/kernel.cpp rename to kernel/src/kernel.cpp diff --git a/src/multiboot2.hpp b/kernel/src/multiboot2.hpp similarity index 100% rename from src/multiboot2.hpp rename to kernel/src/multiboot2.hpp diff --git a/src/paging.cpp b/kernel/src/paging.cpp similarity index 100% rename from src/paging.cpp rename to kernel/src/paging.cpp diff --git a/src/paging.hpp b/kernel/src/paging.hpp similarity index 100% rename from src/paging.hpp rename to kernel/src/paging.hpp diff --git a/src/phys_ptr.hpp b/kernel/src/phys_ptr.hpp similarity index 100% rename from src/phys_ptr.hpp rename to kernel/src/phys_ptr.hpp diff --git a/src/ring3.S b/kernel/src/ring3.S similarity index 100% rename from src/ring3.S rename to kernel/src/ring3.S diff --git a/src/ring3.cpp b/kernel/src/ring3.cpp similarity index 100% rename from src/ring3.cpp rename to kernel/src/ring3.cpp diff --git a/src/ring3.hpp b/kernel/src/ring3.hpp similarity index 100% rename from src/ring3.hpp rename to kernel/src/ring3.hpp diff --git a/src/serial.cpp b/kernel/src/serial.cpp similarity index 100% rename from src/serial.cpp rename to kernel/src/serial.cpp diff --git a/src/serial.hpp b/kernel/src/serial.hpp similarity index 100% rename from src/serial.hpp rename to kernel/src/serial.hpp diff --git a/src/utils.cpp b/kernel/src/utils.cpp similarity index 100% rename from src/utils.cpp rename to kernel/src/utils.cpp diff --git a/src/utils.hpp b/kernel/src/utils.hpp similarity index 100% rename from src/utils.hpp rename to kernel/src/utils.hpp -- 2.47.0