+# Written in 2022 by Amélia COUTARD <eliottulio.coutard@gmail.com>
+# 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 <http://creativecommons.org/publicdomain/zero/1.0/>.
+
+# 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
--- /dev/null
+# Written in 2022 by Amélia COUTARD <eliottulio.coutard@gmail.com>
+# 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 <http://creativecommons.org/publicdomain/zero/1.0/>.
+
+# 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)