Merge branch 'config' into kv2

Merge new configurable build system into kv2 working branch.

This new build system can have settings toggleable on-and-off which
includes the ability to not compile things! In the future, this can be
used to make things modular as well

Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
Michael Shaw 2026-04-12 10:38:43 -04:00
commit 27a7f3de75
18 changed files with 254 additions and 97 deletions

2
.config Normal file
View file

@ -0,0 +1,2 @@
# Generated Configuration
CONFIG_TTY=y

1
.source/os2 Symbolic link
View file

@ -0,0 +1 @@
/home/michael/src/os2//home/michael/src/os2

1
.source/scripts Symbolic link
View file

@ -0,0 +1 @@
/home/michael/src/os2/external/kbuild-standalone/kbuild/scripts

136
Makefile
View file

@ -1,11 +1,14 @@
# SPDX-License-Identifier: GPL-2.0-only
ifneq ($(V),1)
Q := @
else
Q :=
endif
ARCH := x86_64
LIMINE_VERSION=v11.2.0
KERNEL := kernel.elf
IMG := boot.iso
QEMUFLAGS := -m 16G
HOST_CC := gcc
HOST_CFLAGS := -O2 -pipe
HOST_LDFLAGS :=
@ -42,115 +45,60 @@ LDFLAGS := \
-static \
-m elf_$(ARCH) \
-z max-page-size=0x1000 \
-T linker.ld \
-no-pie \
KDIRS := mem/ lib/ drivers/ kernel/ fs/ init/
KDIRS := mem/ lib/ drivers/ kernel/ init/
ifeq ($(ARCH),x86_64)
CFLAGS += -Iamd64/include
KDIRS += amd64/
endif
CFILES := $(shell find $(KDIRS) -type f -name '*.c')
ASFILES := $(shell find $(KDIRS) -type f -name '*.S')
OBJ := $(CFILES:.c=.o) $(ASFILES:.S=.o)
HEADER_DEPS := $(CFILES:.c=.d) $(ASFILES:.S=.d)
KOBJ := $(foreach d,$(KDIRS),$(d)/built-in.o)
print-%:
@echo $($(*))
obj-y := $(KDIRS)
.PHONY: default
QEMUFLAGS := -m 16G
export Q
export V
export obj-y
export CC
export LD
export CFLAGS
export LDFLAGS
PHONY: default
default:
@compiledb -n make $(IMG)
@make $(IMG) -j$(shell nproc)
$(Q)compiledb -n make $(KERNEL)
$(Q)make $(KERNEL)
.PHONY: all
all: $(IMG)
@compiledb -n make
all: $(KERNEL)
$(Q)compiledb -n make
$(KERNEL): $(OBJ) linker.ld
@echo " LD $@"
@$(LD) $(OBJ) $(LDFLAGS) -o $@
__build_all:
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _all obj=.
-include $(HEADER_DEPS)
%.o: %.c
@echo " CC $@"
@$(CC) $(CFLAGS) -c $< -o $@
%.o: %.S
@echo " CC $@"
@$(CC) $(CFLAGS) -c $< -o $@
$(KERNEL): __build_all linker.ld
$(Q)echo " LD $@"
$(Q)$(LD) $(KOBJ) $(LDFLAGS) -T linker.ld -o $@
.PHONY: clean
clean:
@echo " CLEAN kernel"
@rm -rf $(KERNEL) $(OBJ) $(HEADER_DEPS) bin
@echo " CLEAN $(IMG)"
@rm -rf $(IMG)
@echo " CLEAN iso_root"
@rm -rf iso_root
@echo " CLEAN os_root"
@rm -rf os_root
$(Q)$(MAKE) -f scripts/Makefile.build _clean obj=.
$(Q)echo " CLEAN ."
$(Q)rm -rf $(IMG)
$(Q)rm -rf iso_root
$(Q)rm -rf os_root
$(q)rm -rf edk2-ovmf
$()rm -rf os_root.img
$(IMG): limine/limine $(KERNEL) limine.conf
rm -rf iso_root
mkdir -p iso_root/boot
cp -v $(KERNEL) iso_root/
mkdir -p iso_root/boot/limine
cp -v limine.conf iso_root/boot/limine/
mkdir -p iso_root/EFI/BOOT
print-%:
$(Q)echo $($(*))
cp -v limine/limine-bios.sys limine/limine-bios-cd.bin limine/limine-uefi-cd.bin iso_root/boot/limine/
cp -v limine/BOOTX64.EFI iso_root/EFI/BOOT/
cp -v limine/BOOTIA32.EFI iso_root/EFI/BOOT/
.PHONY += config
config:
$(Q)scripts/config.sh
mkdir -p os_root/
mke2fs -d os_root -t ext2 os_root.img 512M
xorriso -as mkisofs -R -r -J \
--efi-boot boot/limine/limine-uefi-cd.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
--partition_offset 16 \
-append_partition 2 0x83 os_root.img \
iso_root -o $(IMG)
rm -rf os_root.img
limine/limine:
rm -rf limine
git clone https://codeberg.org/Limine/Limine.git limine --branch=$(LIMINE_VERSION)-binary --depth=1
$(MAKE) -C limine \
CC="$(HOST_CC)" \
CFLAGS="$(HOST_CFLAGS)" \
CPPFLAGS="$(HOST_CPPFLAGS)" \
LDFLAGS="$(HOST_LDFLAGS)" \
LIBS="$(HOST_LIBS)"
.PHONY += run
run: edk2-ovmf $(IMG)
qemu-system-$(ARCH) \
-M q35 \
-drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \
-nographic \
-cdrom $(IMG) \
$(QEMUFLAGS)
.PHONY += debug
debug: edk2-ovmf $(IMG)
qemu-system-$(ARCH) \
-S -s \
-M q35 \
-drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \
-nographic \
-cdrom $(IMG) \
$(QEMUFLAGS)
edk2-ovmf:
curl -L https://github.com/osdev0/edk2-ovmf-nightly/releases/latest/download/edk2-ovmf.tar.gz | gunzip | tar -xf -
.PHONY += distclean
distclean:
make clean
rm -rf limine
-include scripts/Makefile.limine

5
amd64/Makefile Normal file
View file

@ -0,0 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += arch/
obj-y += drivers/
obj-y += init/
obj-y += kernel/

4
amd64/arch/Makefile Normal file
View file

@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += msr.o
obj-y += page.o
obj-y += reg.o

4
amd64/drivers/Makefile Normal file
View file

@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += apic.o
obj-y += pio.o
obj-y += serial.o

2
amd64/init/Makefile Normal file
View file

@ -0,0 +1,2 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += amd64_init.o

3
amd64/kernel/Makefile Normal file
View file

@ -0,0 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += atomic.o
obj-y += spinlock.o

2
drivers/Makefile Normal file
View file

@ -0,0 +1,2 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += tty/

4
drivers/tty/Makefile Normal file
View file

@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += tty.o
obj-y += vt100.o

3
init/Makefile Normal file
View file

@ -0,0 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += main.o
obj-y += requests.o

2
kernel/Makefile Normal file
View file

@ -0,0 +1,2 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += device.o

8
lib/Makefile Normal file
View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += errno.o
obj-y += math.o
obj-y += printf.o
obj-y += rbtree.o
obj-y += sort.o
obj-y += string.o

5
mem/Makefile Normal file
View file

@ -0,0 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += buddy.o
obj-y += mem.o
obj-y += page.o
obj-y += slab.o

46
scripts/Makefile.build Normal file
View file

@ -0,0 +1,46 @@
# SPDX-License-Identifier: GPL-2.0-only
#
# Master makefile build script for object files in the kernel
#
# Important to remember that this is run at the project-root-level,
# all of it, as it is invoked with $(MAKE) -f
include .config
export
obj-y :=
subdir-y :=
include $(obj)/Makefile
subdir-y :=
real-objs-y :=
subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
real-objs-y := $(addprefix $(obj)/, $(filter-out %/, $(obj-y)))
deps := $(real-objs-y:.o=.d)
-include $(deps)
_all: $(obj)/built-in.o
$(obj)/%.o: $(obj)/%.c
$(Q)echo " CC $@"
$(Q)$(CC) $(CFLAGS) -c $< -o $@
$(obj)/%.o: $(obj)/%.S
$(Q)echo " CC $@"
$(Q)$(CC) $(CFLAGS) -c $< -o $@
$(obj)/built-in.o: $(real-objs-y) $(subdir-y)
$(Q)echo " LD $@"
$(Q)$(LD) -r -o $@ $(real-objs-y) $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o)
.PHONY: _clean
_clean: $(subdir-y)
$(Q)echo " CLEAN $(obj)"
@rm -rf $(obj)/built-in.o $(real-objs-y) $(deps)
.PHONY: $(subdir-y)
$(subdir-y):
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build $(MAKECMDGOALS) obj=$(obj)/$@

62
scripts/Makefile.limine Normal file
View file

@ -0,0 +1,62 @@
# SPDX-License-Identifier: GPL-2.0-only
# Makefile stuff for all the booting and bootloader related stuff
$(IMG): limine/limine $(KERNEL) limine.conf
rm -rf iso_root
mkdir -p iso_root/boot
cp -v $(KERNEL) iso_root/
mkdir -p iso_root/boot/limine
cp -v limine.conf iso_root/boot/limine/
mkdir -p iso_root/EFI/BOOT
cp -v limine/limine-bios.sys limine/limine-bios-cd.bin limine/limine-uefi-cd.bin iso_root/boot/limine/
cp -v limine/BOOTX64.EFI iso_root/EFI/BOOT/
cp -v limine/BOOTIA32.EFI iso_root/EFI/BOOT/
mkdir -p os_root/
mke2fs -d os_root -t ext2 os_root.img 512M
xorriso -as mkisofs -R -r -J \
--efi-boot boot/limine/limine-uefi-cd.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
--partition_offset 16 \
-append_partition 2 0x83 os_root.img \
iso_root -o $(IMG)
rm -rf os_root.img
limine/limine:
rm -rf limine
git clone https://codeberg.org/Limine/Limine.git limine --branch=$(LIMINE_VERSION)-binary --depth=1
$(MAKE) -C limine \
CC="$(HOST_CC)" \
CFLAGS="$(HOST_CFLAGS)" \
CPPFLAGS="$(HOST_CPPFLAGS)" \
LDFLAGS="$(HOST_LDFLAGS)" \
LIBS="$(HOST_LIBS)"
.PHONY += run
run: edk2-ovmf $(IMG)
qemu-system-$(ARCH) \
-M q35 \
-drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \
-nographic \
-cdrom $(IMG) \
$(QEMUFLAGS)
.PHONY += debug
debug: edk2-ovmf $(IMG)
qemu-system-$(ARCH) \
-S -s \
-M q35 \
-drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \
-nographic \
-cdrom $(IMG) \
$(QEMUFLAGS)
edk2-ovmf:
curl -L https://github.com/osdev0/edk2-ovmf-nightly/releases/latest/download/edk2-ovmf.tar.gz | gunzip | tar -xf -
.PHONY += distclean
distclean:
make clean
rm -rf limine

55
scripts/config.sh Executable file
View file

@ -0,0 +1,55 @@
#!/bin/bash
CONFIG_FILE=".config"
# --- The Engine ---
run_menuconfig() {
local TITLE="$1"
shift
local OPTIONS=("$@") # Array of "KEY" "DESCRIPTION"
local DIALOG_ARGS=()
# 1. Build the dialog arguments dynamically
for ((i=0; i<${#OPTIONS[@]}; i+=2)); do
KEY="${OPTIONS[i]}"
DESC="${OPTIONS[i+1]}"
# Check current state in .config
STATE="OFF"
if grep -q "^CONFIG_${KEY}=y" "$CONFIG_FILE" 2>/dev/null; then
STATE="ON"
fi
DIALOG_ARGS+=("$KEY" "$DESC" "$STATE")
done
# 2. Show the menu
CHOICES=$(dialog --stdout --backtitle "Project Setup" \
--title "$TITLE" \
--checklist "Use SPACE to toggle, ENTER to save:" 15 70 10 \
"${DIALOG_ARGS[@]}")
# 3. Save if user didn't cancel
if [ $? -eq 0 ]; then
echo "# Generated Configuration" > "$CONFIG_FILE"
for OPTION in $CHOICES; do
CLEAN=$(echo "$OPTION" | tr -d '"')
echo "CONFIG_${CLEAN}=y" >> "$CONFIG_FILE"
done
# Add "is not set" for everything else to be professional
for ((i=0; i<${#OPTIONS[@]}; i+=2)); do
KEY="${OPTIONS[i]}"
if ! grep -q "CONFIG_${KEY}=y" "$CONFIG_FILE"; then
echo "# CONFIG_${KEY} is not set" >> "$CONFIG_FILE"
fi
done
fi
}
# --- How you use it ---
# Format: "VARIABLE_NAME" "Human Readable Description"
MY_FEATURES=(
"TEST_FEATURE" "TEST_FEATURE"
)
run_menuconfig "System Features" "${MY_FEATURES[@]}"