Skip to content

Commit 6a06827

Browse files
committed
build: 重构项目构建系统并优化内核配置
1 parent b4510f8 commit 6a06827

File tree

4 files changed

+117
-96
lines changed

4 files changed

+117
-96
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ iso/*
1111
kernel/proka-kernel
1212

1313

14-
AGENTS.md
14+
AGENTS.md
15+
16+
assets/initrd.cpio

Makefile

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,85 @@
1-
# Proka Kernel - A kernel for ProkaOS
2-
# Copyright (C) RainSTR Studio 2025, All Rights Reserved.
3-
#
4-
# This is the Makefile, which will build the C and ASM code, in
5-
# order to help us to make a Rust kernel more easily.
6-
#
7-
#
8-
.PHONY: clean debug run iso
9-
# Define some basic variables
10-
BUILD_DIRS = kernel
11-
OBJ_DIR = $(PWD)/target/obj
12-
LDFLAGS = -nostdlib
13-
XORRISOFLAGS = -as mkisofs --efi-boot limine/limine-uefi-cd.bin
14-
QEMU_FLAGS := -bios ./assets/OVMF.fd -cdrom proka-kernel.iso --machine q35 -m 1G
15-
# QEMU_KVM := -enable-kvm -cpu host
16-
# QEMU_reOUT := > ./qemu.log
17-
QEMU_OUT := -serial stdio $(QEMU_reOUT)
18-
# Build the clean codes (easy, just run the Makefile in each dirs)
19-
all:
20-
# Iterate all the BUILD_DIRS.
21-
$(foreach dir, $(BUILD_DIRS), make -C $(dir) OBJ_DIR=$(OBJ_DIR);)
1+
# Proka Kernel - Root Makefile
2+
# Copyright (C) RainSTR Studio 2025-2026, All Rights Reserved.
3+
4+
.DEFAULT_GOAL := all
5+
6+
# Verbosity control
7+
ifeq ($(V),1)
8+
Q :=
9+
else
10+
Q := @
11+
endif
12+
13+
# Core variables
14+
BUILD_DIRS ?= kernel
15+
TARGET_DIR ?= $(CURDIR)/target
16+
OBJ_DIR ?= $(TARGET_DIR)/obj
17+
ISO_DIR ?= $(TARGET_DIR)/iso
18+
ISO_IMAGE ?= proka-kernel.iso
19+
INITRD ?= assets/initrd.cpio
20+
21+
# Build tools & flags
22+
XORRISO ?= xorriso
23+
XORRISOFLAGS ?= -as mkisofs --efi-boot limine/limine-uefi-cd.bin -quiet
24+
QEMU ?= qemu-system-x86_64
25+
QEMU_FLAGS ?= -bios ./assets/OVMF.fd -cdrom $(ISO_IMAGE) --machine q35 -m 1G -enable-kvm
26+
QEMU_OUT ?= -serial stdio
27+
QEMU_EXTRA ?=
28+
29+
# Profile handling (default to release)
30+
PROFILE ?= dev
31+
export PROFILE
32+
33+
.PHONY: all debug clean distclean run rundebug menuconfig iso $(BUILD_DIRS)
34+
35+
# Standard build targets
36+
all: $(BUILD_DIRS)
2237

2338
debug:
24-
# Iterate all the BUILD_DIRS, but use debug.
25-
$(foreach dir, $(BUILD_DIRS), make -C $(dir) OBJ_DIR=$(OBJ_DIR) PROFILE=dev;)
26-
27-
## Build the ISO image
28-
# This code is from TMXQWQ/TKernel2 in github
29-
iso: debug kernel/kernel initrd
30-
mkdir -p iso
31-
cp -r ./assets/rootfs/* ./iso/
32-
cp ./assets/initrd.cpio ./iso/initrd.cpio
33-
rm -f ./assets/initrd.cpio
34-
cp ./kernel/kernel ./iso/kernel
35-
touch ./proka-kernel.iso
36-
xorriso $(XORRISOFLAGS) ./iso -o ./proka-kernel.iso \
37-
2> /dev/null
38-
rm -rf ./iso
39-
@echo "ISO image built: proka-kernel.iso"
40-
41-
initrd:
42-
cd ./assets/initrd && find . -print | cpio -H newc -v -o > ../initrd.cpio && cd ../..
39+
$(Q)$(MAKE) PROFILE=dev all
40+
41+
$(BUILD_DIRS):
42+
@echo "Entering directory: $@"
43+
$(Q)mkdir -p $(OBJ_DIR)
44+
$(Q)$(MAKE) -C $@ OBJ_DIR=$(OBJ_DIR) V=$(V)
45+
46+
# ISO image creation
47+
iso: all $(INITRD)
48+
@echo "Creating ISO image: $(ISO_IMAGE)"
49+
$(Q)mkdir -p $(ISO_DIR)
50+
$(Q)cp -r ./assets/rootfs/* $(ISO_DIR)/
51+
$(Q)cp $(INITRD) $(ISO_DIR)/initrd.cpio
52+
$(Q)cp ./kernel/kernel $(ISO_DIR)/kernel
53+
$(Q)$(XORRISO) $(XORRISOFLAGS) $(ISO_DIR) -o $(ISO_IMAGE)
54+
$(Q)rm -rf $(ISO_DIR)
55+
@echo "ISO build complete."
56+
57+
# Initrd creation
58+
INITRD_SRC := $(shell find assets/initrd -type f 2>/dev/null)
59+
$(INITRD): $(INITRD_SRC)
60+
@echo "Creating initrd: $@"
61+
$(Q)mkdir -p assets
62+
$(Q)cd assets/initrd && find . -print | cpio -H newc -o > ../initrd.cpio 2>/dev/null
4363

64+
# Execution & Debugging
4465
run: iso
45-
qemu-system-x86_64 -enable-kvm $(QEMU_FLAGS) $(QEMU_OUT)
46-
@echo "QEMU started"
66+
$(Q)$(QEMU) $(QEMU_FLAGS) $(QEMU_OUT) $(QEMU_EXTRA)
4767

48-
rundebug: iso
49-
qemu-system-x86_64 -enable-kvm $(QEMU_FLAGS) $(QEMU_OUT) -s -S
68+
rundebug:
69+
$(Q)$(MAKE) QEMU_EXTRA="-s -S" run
5070

5171
menuconfig:
52-
cd ./kernel && cargo anaxa menuconfig
72+
$(Q)$(MAKE) -C kernel menuconfig
5373

74+
# Cleanup
5475
clean:
55-
make -C kernel clean
56-
rm -rf proka-kernel.iso target
76+
@for dir in $(BUILD_DIRS); do \
77+
$(MAKE) -C $$dir clean V=$(V); \
78+
done
79+
$(Q)rm -f $(ISO_IMAGE) $(INITRD)
80+
$(Q)rm -rf $(OBJ_DIR)
81+
@echo "Cleaned."
5782

83+
distclean: clean
84+
$(Q)rm -rf $(TARGET_DIR)
85+
@echo "Full cleanup complete."

kernel/Makefile

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,52 @@
1-
# Nuke built-in rules and variables.
1+
# Proka Kernel - Rust Kernel Makefile
2+
# Copyright (C) RainSTR Studio 2025-2026, All Rights Reserved.
3+
4+
# Disable built-in rules and variables for performance
25
MAKEFLAGS += -rR
36
.SUFFIXES:
47

5-
# This is the name that our final executable will have.
6-
# Change as needed.
7-
override OUTPUT := kernel
8+
# Output binary name
9+
OUTPUT := kernel
10+
# Cargo package name
11+
PKG_NAME := proka-kernel
12+
# Rust target triple
13+
RUST_TARGET := x86_64-unknown-none
14+
15+
# Build profile (dev, release)
16+
PROFILE ?= release
17+
# Map 'dev' to Cargo's 'debug' directory
18+
ifeq ($(PROFILE),dev)
19+
PROFILE_DIR := debug
20+
else
21+
PROFILE_DIR := $(PROFILE)
22+
endif
823

9-
# Convenience macro to reliably declare user overridable variables.
10-
override USER_VARIABLE = $(if $(filter $(origin $(1)),default undefined),$(eval override $(1) := $(2)))
24+
# Verbosity control
25+
ifeq ($(V),1)
26+
Q :=
27+
else
28+
Q := @
29+
endif
1130

12-
# Target architecture to build for. Default to x86_64.
13-
$(call USER_VARIABLE,KARCH,x86_64)
31+
# Binary path relative to kernel directory
32+
BIN_PATH := target/$(RUST_TARGET)/$(PROFILE_DIR)/$(PKG_NAME)
1433

15-
ifeq ($(RUST_TARGET),)
16-
override RUST_TARGET := $(KARCH)-unknown-none
17-
ifeq ($(KARCH),riscv64)
18-
override RUST_TARGET := riscv64gc-unknown-none-elf
19-
endif
20-
endif
34+
.PHONY: all clean distclean
2135

22-
ifeq ($(PROFILE), dev)
23-
override RUST_PROFILE := dev
24-
endif
36+
all: $(OUTPUT)
2537

26-
ifeq ($(PROFILE),)
27-
override RUST_PROFILE := release
28-
endif
38+
$(OUTPUT): $(BIN_PATH)
39+
$(Q)cp $< $@
40+
@echo "Kernel binary ready: $@"
2941

30-
override RUST_PROFILE_SUBDIR := $(RUST_PROFILE)
31-
ifeq ($(RUST_PROFILE),dev)
32-
override RUST_PROFILE_SUBDIR := debug
33-
endif
42+
$(BIN_PATH): .FORCE
43+
@echo "Building kernel in $(PROFILE) mode..."
44+
$(Q)RUSTFLAGS="-C relocation-model=static" cargo anaxa build --no-env --target $(RUST_TARGET) --profile $(PROFILE)
45+
46+
.FORCE:
3447

35-
# Default target.
36-
.PHONY: all
37-
all:
38-
RUSTFLAGS="-C relocation-model=static" cargo anaxa build --no-env --target $(RUST_TARGET) --profile $(RUST_PROFILE)
39-
@# Find the compiled binary and copy it to the current directory as 'kernel'
40-
@BIN_NAME=$$(cd target/$(RUST_TARGET)/$(RUST_PROFILE_SUBDIR) && find . -maxdepth 1 -perm -111 -type f | head -n 1 | sed 's|^\./||'); \
41-
if [ -n "$$BIN_NAME" ]; then \
42-
cp "target/$(RUST_TARGET)/$(RUST_PROFILE_SUBDIR)/$$BIN_NAME" $(OUTPUT); \
43-
echo "Kernel binary ready: $(OUTPUT)"; \
44-
else \
45-
echo "Failed to find kernel binary!"; exit 1; \
46-
fi
47-
48-
# Remove object files and the final executable.
49-
.PHONY: clean
5048
clean:
51-
cargo clean
52-
rm -f $(OUTPUT)
49+
$(Q)cargo clean
50+
$(Q)rm -f $(OUTPUT)
5351

54-
.PHONY: distclean
5552
distclean: clean

kernel/src/main.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121
extern crate proka_kernel;
2222
extern crate alloc;
2323
use proka_kernel::BASE_REVISION;
24-
/* C functions extern area */
25-
extern_safe! {
26-
fn add(a: i32, b: i32) -> i32;
27-
fn sub(a: i32, b: i32) -> i32;
28-
}
29-
3024
/* The Kernel main code */
3125
// The normal one
3226
#[unsafe(no_mangle)]

0 commit comments

Comments
 (0)