2024-01-08 15:27:04 +00:00
|
|
|
# Nuke built-in rules and variables.
|
|
|
|
override MAKEFLAGS += -rR
|
|
|
|
|
|
|
|
# This is the name that our final kernel executable will have.
|
|
|
|
# Change as needed.
|
|
|
|
override KERNEL := kumios
|
|
|
|
|
|
|
|
# Convenience macro to reliably declare user overridable variables.
|
|
|
|
define DEFAULT_VAR =
|
|
|
|
ifeq ($(origin $1),default)
|
|
|
|
override $(1) := $(2)
|
|
|
|
endif
|
|
|
|
ifeq ($(origin $1),undefined)
|
|
|
|
override $(1) := $(2)
|
|
|
|
endif
|
|
|
|
endef
|
|
|
|
|
|
|
|
# It is suggested to use a custom built cross toolchain to build a kernel.
|
|
|
|
# We are using the standard "cc" here, it may work by using
|
|
|
|
# the host system's toolchain, but this is not guaranteed.
|
2024-01-09 15:29:07 +00:00
|
|
|
override DEFAULT_CC := clang
|
2024-01-08 15:27:04 +00:00
|
|
|
$(eval $(call DEFAULT_VAR,CC,$(DEFAULT_CC)))
|
|
|
|
|
|
|
|
# Same thing for "ld" (the linker).
|
|
|
|
override DEFAULT_LD := ld
|
|
|
|
$(eval $(call DEFAULT_VAR,LD,$(DEFAULT_LD)))
|
|
|
|
|
|
|
|
# User controllable C flags.
|
|
|
|
override DEFAULT_CFLAGS := -g -O2 -pipe
|
|
|
|
$(eval $(call DEFAULT_VAR,CFLAGS,$(DEFAULT_CFLAGS)))
|
|
|
|
|
|
|
|
# User controllable C preprocessor flags. We set none by default.
|
|
|
|
override DEFAULT_CPPFLAGS :=
|
|
|
|
$(eval $(call DEFAULT_VAR,CPPFLAGS,$(DEFAULT_CPPFLAGS)))
|
|
|
|
|
|
|
|
# User controllable nasm flags.
|
|
|
|
override DEFAULT_NASMFLAGS := -F dwarf -g
|
|
|
|
$(eval $(call DEFAULT_VAR,NASMFLAGS,$(DEFAULT_NASMFLAGS)))
|
|
|
|
|
|
|
|
# User controllable linker flags. We set none by default.
|
|
|
|
override DEFAULT_LDFLAGS :=
|
|
|
|
$(eval $(call DEFAULT_VAR,LDFLAGS,$(DEFAULT_LDFLAGS)))
|
|
|
|
|
|
|
|
# Internal C flags that should not be changed by the user.
|
|
|
|
override CFLAGS += \
|
|
|
|
-Wall \
|
|
|
|
-Wextra \
|
|
|
|
-std=gnu11 \
|
|
|
|
-ffreestanding \
|
|
|
|
-fno-stack-protector \
|
|
|
|
-fno-stack-check \
|
2024-01-09 15:29:07 +00:00
|
|
|
-fno-tree-vectorize \
|
2024-01-08 15:27:04 +00:00
|
|
|
-fno-lto \
|
|
|
|
-fPIE \
|
|
|
|
-m64 \
|
|
|
|
-march=x86-64 \
|
|
|
|
-mno-80387 \
|
|
|
|
-mno-mmx \
|
2024-01-09 15:29:07 +00:00
|
|
|
-mno-red-zone \
|
|
|
|
-mno-sse \
|
|
|
|
-mno-sse2
|
2024-01-09 09:57:16 +00:00
|
|
|
|
2024-01-08 15:27:04 +00:00
|
|
|
# Internal C preprocessor flags that should not be changed by the user.
|
|
|
|
override CPPFLAGS := \
|
|
|
|
-I src \
|
|
|
|
$(CPPFLAGS) \
|
|
|
|
-MMD \
|
|
|
|
-MP
|
2024-01-08 19:43:35 +00:00
|
|
|
|
2024-01-08 15:27:04 +00:00
|
|
|
# Internal linker flags that should not be changed by the user.
|
|
|
|
override LDFLAGS += \
|
|
|
|
-m elf_x86_64 \
|
|
|
|
-nostdlib \
|
|
|
|
-static \
|
|
|
|
-pie \
|
|
|
|
--no-dynamic-linker \
|
|
|
|
-z text \
|
|
|
|
-z max-page-size=0x1000 \
|
|
|
|
-T linker.ld
|
|
|
|
|
|
|
|
# Internal nasm flags that should not be changed by the user.
|
|
|
|
override NASMFLAGS += \
|
|
|
|
-Wall \
|
|
|
|
-f elf64
|
2024-01-08 19:43:35 +00:00
|
|
|
|
2024-01-08 15:27:04 +00:00
|
|
|
# Use "find" to glob all *.c, *.S, and *.asm files in the tree and obtain the
|
|
|
|
# object and header dependency file names.
|
|
|
|
override CFILES := $(shell cd src && find -L * -type f -name '*.c')
|
|
|
|
override ASFILES := $(shell cd src && find -L * -type f -name '*.S')
|
|
|
|
override NASMFILES := $(shell cd src && find -L * -type f -name '*.asm')
|
|
|
|
override OBJ := $(addprefix obj/,$(CFILES:.c=.c.o) $(ASFILES:.S=.S.o) $(NASMFILES:.asm=.asm.o))
|
|
|
|
override HEADER_DEPS := $(addprefix obj/,$(CFILES:.c=.c.d) $(ASFILES:.S=.S.d))
|
2024-01-08 19:43:35 +00:00
|
|
|
|
2024-01-09 09:57:16 +00:00
|
|
|
## STB
|
|
|
|
|
|
|
|
STB_INCLUDE_PATH = vendor/stb
|
|
|
|
override CFLAGS += -I$(STB_INCLUDE_PATH)
|
|
|
|
|
|
|
|
## End STB
|
|
|
|
|
2024-01-08 15:27:04 +00:00
|
|
|
# Default target.
|
|
|
|
.PHONY: all
|
|
|
|
all: bin/$(KERNEL) iso hdd
|
2024-01-08 19:43:35 +00:00
|
|
|
|
2024-01-08 15:27:04 +00:00
|
|
|
# Link rules for the final kernel executable.
|
2024-01-08 19:43:35 +00:00
|
|
|
bin/$(KERNEL): submodules GNUmakefile linker.ld $(OBJ)
|
2024-01-08 15:27:04 +00:00
|
|
|
mkdir -p "$$(dirname $@)"
|
|
|
|
$(LD) $(OBJ) $(LDFLAGS) -o $@
|
|
|
|
|
|
|
|
# Include header dependencies.
|
|
|
|
-include $(HEADER_DEPS)
|
|
|
|
|
|
|
|
# Compilation rules for *.c files.
|
|
|
|
obj/%.c.o: src/%.c GNUmakefile
|
|
|
|
mkdir -p "$$(dirname $@)"
|
|
|
|
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
|
|
|
|
|
|
|
|
# Compilation rules for *.S files.
|
|
|
|
obj/%.S.o: src/%.S GNUmakefile
|
|
|
|
mkdir -p "$$(dirname $@)"
|
|
|
|
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
|
|
|
|
|
|
|
|
# Compilation rules for *.asm (nasm) files.
|
|
|
|
obj/%.asm.o: src/%.asm GNUmakefile
|
|
|
|
mkdir -p "$$(dirname $@)"
|
|
|
|
nasm $(NASMFLAGS) $< -o $@
|
|
|
|
|
|
|
|
# Remove object files and the final executable.
|
|
|
|
.PHONY: clean
|
|
|
|
clean:
|
2024-01-08 19:43:35 +00:00
|
|
|
rm -rf bin obj iso_root vendor/*
|
2024-01-08 15:27:04 +00:00
|
|
|
|
2024-01-08 19:43:35 +00:00
|
|
|
# Initialize and update git submodules.
|
|
|
|
.PHONY: submodules
|
|
|
|
submodules:
|
|
|
|
git submodule update --init --recursive
|
2024-01-08 15:27:04 +00:00
|
|
|
|
2024-01-08 19:43:35 +00:00
|
|
|
# The `limine` target builds the Limine bootloader.
|
|
|
|
.PHONY: limine
|
|
|
|
limine: submodules
|
|
|
|
$(MAKE) -C vendor/limine
|
2024-01-08 15:27:04 +00:00
|
|
|
|
2024-01-08 19:43:35 +00:00
|
|
|
# The `iso` target builds the bootable ISO image using Limine.
|
|
|
|
.PHONY: iso
|
|
|
|
iso: bin/$(KERNEL) limine
|
2024-01-08 15:27:04 +00:00
|
|
|
# Create the iso_root directory and copy relevant files.
|
|
|
|
mkdir -p iso_root
|
2024-01-08 19:43:35 +00:00
|
|
|
cp -v bin/$(KERNEL) limine.cfg vendor/limine/limine-bios.sys vendor/limine/limine-bios-cd.bin vendor/limine/limine-uefi-cd.bin iso_root/
|
2024-01-08 15:27:04 +00:00
|
|
|
|
|
|
|
# Create the EFI boot directory and copy EFI executables.
|
|
|
|
mkdir -p iso_root/EFI/BOOT
|
2024-01-08 19:43:35 +00:00
|
|
|
cp -v vendor/limine/BOOTX64.EFI iso_root/EFI/BOOT/
|
|
|
|
cp -v vendor/limine/BOOTIA32.EFI iso_root/EFI/BOOT/
|
2024-01-08 15:27:04 +00:00
|
|
|
|
|
|
|
# Create the bootable ISO.
|
|
|
|
xorriso -as mkisofs -b limine-bios-cd.bin -no-emul-boot \
|
|
|
|
-boot-load-size 4 -boot-info-table --efi-boot limine-uefi-cd.bin \
|
|
|
|
-efi-boot-part --efi-boot-image --protective-msdos-label \
|
|
|
|
iso_root -o image.iso
|
|
|
|
|
|
|
|
# Install Limine stage 1 and 2 for legacy BIOS boot.
|
2024-01-08 19:43:35 +00:00
|
|
|
./vendor/limine/limine bios-install image.iso
|
2024-01-08 15:27:04 +00:00
|
|
|
|
|
|
|
mv image.iso bin/$(KERNEL).iso
|
|
|
|
|
|
|
|
@echo "Bootable ISO created as bin/$(KERNEL).iso"
|
|
|
|
|
|
|
|
# The `hdd` target builds the bootable HDD image using Limine.
|
|
|
|
.PHONY: hdd
|
2024-01-08 19:43:35 +00:00
|
|
|
hdd: bin/$(KERNEL) limine
|
2024-01-08 15:27:04 +00:00
|
|
|
# Create empty HDD image file.
|
|
|
|
dd if=/dev/zero bs=1M count=0 seek=64 of=image.hdd
|
|
|
|
|
|
|
|
# Create GPT partition.
|
|
|
|
sgdisk image.hdd -n 1:2048 -t 1:ef00
|
|
|
|
|
|
|
|
# Install Limine BIOS stages.
|
2024-01-08 19:43:35 +00:00
|
|
|
./vendor/limine/limine bios-install image.hdd
|
2024-01-08 15:27:04 +00:00
|
|
|
|
|
|
|
# Format the partition to fat32 filesystem.
|
|
|
|
mformat -i image.hdd@@1M -F ::
|
|
|
|
|
|
|
|
# Create directory structure in HDD image.
|
|
|
|
mmd -i image.hdd@@1M ::/EFI ::/EFI/BOOT
|
|
|
|
|
|
|
|
# Copy kernel and Limine config over to the HDD image.
|
2024-01-08 19:43:35 +00:00
|
|
|
mcopy -i image.hdd@@1M bin/$(KERNEL) limine.cfg vendor/limine/limine-bios.sys ::/
|
|
|
|
mcopy -i image.hdd@@1M vendor/limine/BOOTX64.EFI ::/EFI/BOOT
|
|
|
|
mcopy -i image.hdd@@1M vendor/limine/BOOTIA32.EFI ::/EFI/BOOT
|
2024-01-08 15:27:04 +00:00
|
|
|
|
|
|
|
mv image.hdd bin/$(KERNEL).hdd
|
|
|
|
|
|
|
|
@echo "Bootable HDD image created as bin/$(KERNEL).hdd"
|