Skip to content

Commit 2af31ad

Browse files
committed
build(makefile): 增强构建系统功能并改进日志输出
1 parent 8ff7756 commit 2af31ad

File tree

5 files changed

+132
-36
lines changed

5 files changed

+132
-36
lines changed

Makefile

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Proka Kernel - Root Makefile
22
# Copyright (C) RainSTR Studio 2025-2026, All Rights Reserved.
33

4-
.DEFAULT_GOAL := all
4+
.DEFAULT_GOAL := help
5+
6+
# OS detection
7+
UNAME_S := $(shell uname -s)
58

69
# Verbosity control
710
ifeq ($(V),1)
@@ -10,6 +13,20 @@ else
1013
Q := @
1114
endif
1215

16+
# Logging macro
17+
define log_info
18+
@echo "[INFO] $(1)"
19+
endef
20+
define log_success
21+
@echo "[OK] $(1)"
22+
endef
23+
define log_warn
24+
@echo "[WARN] $(1)"
25+
endef
26+
define log_error
27+
@echo "[ERR] $(1)"
28+
endef
29+
1330
# Core variables
1431
BUILD_DIRS ?= kernel
1532
TARGET_DIR ?= $(CURDIR)/target
@@ -22,32 +39,76 @@ INITRD ?= assets/initrd.cpio
2239
XORRISO ?= xorriso
2340
XORRISOFLAGS ?= -as mkisofs --efi-boot limine/limine-uefi-cd.bin -quiet
2441
QEMU ?= qemu-system-x86_64
25-
QEMU_FLAGS ?= -bios ./assets/OVMF.fd -cdrom $(ISO_IMAGE) --machine q35 -m 1G
42+
43+
# Accelerator selection
44+
ifeq ($(UNAME_S),Linux)
45+
QEMU_ACCEL ?= -enable-kvm
46+
else ifeq ($(UNAME_S),Darwin)
47+
QEMU_ACCEL ?= -accel hvf
48+
endif
49+
50+
QEMU_FLAGS ?= -bios ./assets/OVMF.fd -cdrom $(ISO_IMAGE) --machine q35 -m 1G $(QEMU_ACCEL)
2651
QEMU_OUT ?= -serial stdio
2752
QEMU_EXTRA ?=
2853

2954
# Profile handling (default to release)
3055
PROFILE ?= dev
3156
export PROFILE
3257

33-
.PHONY: all debug clean distclean run rundebug menuconfig iso $(BUILD_DIRS) docs-build docs-serve docs-clean
58+
.PHONY: all help debug clean distclean run rundebug menuconfig iso $(BUILD_DIRS) \
59+
docs-build docs-serve docs-clean test clippy fmt check-tools
60+
61+
help:
62+
@echo "$(COLOR_INFO)Proka Kernel Build System$(COLOR_RESET)"
63+
@echo ""
64+
@echo "Usage: make [target] [VARIABLES]"
65+
@echo ""
66+
@echo "Targets:"
67+
@echo " all Build the kernel"
68+
@echo " iso Create a bootable ISO image"
69+
@echo " run Run the kernel in QEMU (with $(QEMU_ACCEL))"
70+
@echo " rundebug Run the kernel in QEMU with GDB stub (-s -S)"
71+
@echo " test Run kernel tests"
72+
@echo " clippy Run clippy lints"
73+
@echo " fmt Format kernel source code"
74+
@echo " menuconfig Open kernel configuration menu"
75+
@echo " clean Remove build artifacts"
76+
@echo " distclean Full cleanup including target directory"
77+
@echo " doc Build documentation"
78+
@echo " check-tools Verify required build tools are installed"
79+
@echo ""
80+
@echo "Variables:"
81+
@echo " V=1 Enable verbose output"
82+
@echo " PROFILE=dev Set build profile (dev/release, default: dev)"
83+
@echo " QEMU_ACCEL= Override QEMU accelerator flags"
84+
@echo ""
85+
@echo "Detected OS: $(UNAME_S)"
86+
87+
# Tool check
88+
check-tools:
89+
$(call log_info,Checking build tools...)
90+
@command -v $(XORRISO) >/dev/null 2>&1 || ( $(call log_error,$(XORRISO) not found); exit 1 )
91+
@command -v $(QEMU) >/dev/null 2>&1 || ( $(call log_error,$(QEMU) not found); exit 1 )
92+
@command -v cargo >/dev/null 2>&1 || ( $(call log_error,cargo not found); exit 1 )
93+
@command -v cpio >/dev/null 2>&1 || ( $(call log_error,cpio not found); exit 1 )
94+
$(call log_success,All tools found.)
3495

3596
# Documentation targets
3697
doc:
37-
@echo "Building guide (mdBook)..."
98+
$(call log_info,Building guide (mdBook)...)
3899
$(Q)mdbook build
39-
@echo "Building API documentation (rustdoc)..."
100+
$(call log_info,Building API documentation (rustdoc)...)
40101
$(Q)cd kernel && cargo doc --no-deps
41102
$(Q)rm -rf book/api
42103
$(Q)cp -r kernel/target/x86_64-unknown-none/doc book/api
43-
@echo "Documentation built in book/"
104+
$(call log_success,Documentation built in book/)
44105

45106
docs-serve:
46-
@echo "Serving documentation..."
107+
$(call log_info,Serving documentation...)
47108
$(Q)mdbook serve --hostname 0.0.0.0
48109

49110
docs-clean:
50-
@echo "Cleaning documentation..."
111+
$(call log_info,Cleaning documentation...)
51112
$(Q)mdbook clean
52113
$(Q)rm -rf book
53114
$(Q)cd kernel && cargo clean --doc
@@ -59,25 +120,26 @@ debug:
59120
$(Q)$(MAKE) PROFILE=dev all
60121

61122
$(BUILD_DIRS):
62-
@echo "Entering directory: $@"
123+
$(call log_info,Entering directory: $@)
63124
$(Q)mkdir -p $(OBJ_DIR)
64125
$(Q)$(MAKE) -C $@ OBJ_DIR=$(OBJ_DIR) V=$(V)
65126

66127
# ISO image creation
67-
iso: all $(INITRD)
68-
@echo "Creating ISO image: $(ISO_IMAGE)"
128+
ROOTFS_SRC := $(shell find assets/rootfs -type f 2>/dev/null)
129+
iso: all $(INITRD) $(ROOTFS_SRC)
130+
$(call log_info,Creating ISO image: $(ISO_IMAGE))
69131
$(Q)mkdir -p $(ISO_DIR)
70132
$(Q)cp -r ./assets/rootfs/* $(ISO_DIR)/
71133
$(Q)cp $(INITRD) $(ISO_DIR)/initrd.cpio
72134
$(Q)cp ./kernel/kernel $(ISO_DIR)/kernel
73135
$(Q)$(XORRISO) $(XORRISOFLAGS) $(ISO_DIR) -o $(ISO_IMAGE)
74136
$(Q)rm -rf $(ISO_DIR)
75-
@echo "ISO build complete."
137+
$(call log_success,ISO build complete.)
76138

77139
# Initrd creation
78140
INITRD_SRC := $(shell find assets/initrd -type f 2>/dev/null)
79141
$(INITRD): $(INITRD_SRC)
80-
@echo "Creating initrd: $@"
142+
$(call log_info,Creating initrd: $@)
81143
$(Q)mkdir -p assets
82144
$(Q)cd assets/initrd && find . -print | cpio -H newc -o > ../initrd.cpio 2>/dev/null
83145

@@ -88,6 +150,14 @@ run: iso
88150
rundebug:
89151
$(Q)$(MAKE) QEMU_EXTRA="-s -S" run
90152

153+
test:
154+
$(call log_info,Running kernel tests...)
155+
$(Q)$(MAKE) -C kernel test
156+
157+
clippy:
158+
$(call log_info,Running clippy...)
159+
$(Q)$(MAKE) -C kernel clippy
160+
91161
menuconfig:
92162
$(Q)$(MAKE) -C kernel menuconfig
93163

@@ -101,8 +171,9 @@ clean: docs-clean
101171
done
102172
$(Q)rm -f $(ISO_IMAGE) $(INITRD)
103173
$(Q)rm -rf $(OBJ_DIR)
104-
@echo "Cleaned."
174+
$(call log_success,Cleaned.)
105175

106176
distclean: clean
107177
$(Q)rm -rf $(TARGET_DIR)
108-
@echo "Full cleanup complete."
178+
$(call log_success,Full cleanup complete.)
179+

kernel/Makefile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ MAKEFLAGS += -rR
66
.SUFFIXES:
77

88
# Output binary name
9-
OUTPUT := kernel
9+
OUTPUT := kernel
1010
# Cargo package name
11-
PKG_NAME := proka-kernel
11+
PKG_NAME := proka-kernel
1212
# Rust target triple
1313
RUST_TARGET := x86_64-unknown-none
1414

1515
# Build profile (dev, release)
16-
PROFILE ?= release
16+
PROFILE ?= release
1717
# Map 'dev' to Cargo's 'debug' directory
1818
ifeq ($(PROFILE),dev)
1919
PROFILE_DIR := debug
@@ -28,10 +28,13 @@ else
2828
Q := @
2929
endif
3030

31+
# Rust compilation flags
32+
RUSTFLAGS ?= -C relocation-model=static
33+
3134
# Binary path relative to kernel directory
3235
BIN_PATH := target/$(RUST_TARGET)/$(PROFILE_DIR)/$(PKG_NAME)
3336

34-
.PHONY: all clean distclean
37+
.PHONY: all clean distclean menuconfig fmt clippy test
3538

3639
all: $(OUTPUT)
3740

@@ -41,7 +44,13 @@ $(OUTPUT): $(BIN_PATH)
4144

4245
$(BIN_PATH): .FORCE
4346
@echo "Building kernel in $(PROFILE) mode..."
44-
$(Q)RUSTFLAGS="-C relocation-model=static" cargo anaxa build --no-env --target $(RUST_TARGET) --profile $(PROFILE)
47+
$(Q)RUSTFLAGS="$(RUSTFLAGS)" cargo anaxa build --no-env --target $(RUST_TARGET) --profile $(PROFILE)
48+
49+
clippy:
50+
$(Q)RUSTFLAGS="$(RUSTFLAGS)" cargo clippy --target $(RUST_TARGET) --all-features
51+
52+
test:
53+
$(Q)RUSTFLAGS="$(RUSTFLAGS)" cargo test --lib --target $(RUST_TARGET)
4554

4655
.FORCE:
4756

kernel/src/memory/Kconfig.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title = "Memory Manager"
33
[[config]]
44
name = "KERNEL_INIT_HEAP_SIZE"
55
type = "int"
6-
default = 8388608
6+
default = 65536
77
desc = "The size of the init heap memory (Unit: Bytes)."
88
help = "Default init heap size for the kernel."
99
rust_type = "u64"
@@ -19,7 +19,7 @@ rust_type = "usize"
1919
[[config]]
2020
name = "OOM_EXPAND_SIZE"
2121
type = "int"
22-
default = 4096
22+
default = 1048576 # 1MB
2323
desc = "The size of expand when OOM (Unit: Bytes)."
2424
help = "The size of expand when OOM. Maximum expansion is 4MB."
2525
rust_type = "u64"

kernel/src/memory/heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn init_heap(
9292
) -> Result<(), MapToError<Size4KiB>> {
9393
let heap_start = VirtAddr::new(HEAP_START as u64);
9494
// Map initial 64KB for boot-strapping VMM
95-
let initial_size = 64 * 1024;
95+
let initial_size = crate::config::KERNEL_INIT_HEAP_SIZE;
9696
let heap_end = heap_start + initial_size;
9797

9898
let page_range = {

tests/Makefile

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
1-
# Proka Kernel - A kernel for ProkaOS
1+
# Proka Kernel - Test Makefile
22
# Copyright (C) RainSTR Studio 2025, All Rights Reserved.
3-
#
4-
# This is the Makefile, which will build the C code
5-
# only and link with ASM code.
63

7-
# The ASM source
8-
C_CODE = $(wildcard *.c)
9-
CFLAGS = -std=gnu99 -ffreestanding -O0 -Wall -Wextra -fPIE -fno-stack-protector -lgcc
10-
C_MODE_FLAGS := $(if $(DEBUG),-g)
4+
# Build configuration
5+
CC := gcc
6+
CFLAGS := -std=gnu99 -ffreestanding -O0 -Wall -Wextra -fPIE -fno-stack-protector
7+
LDFLAGS := -lgcc
118

12-
# The object file
13-
OBJ_FILES = $(patsubst %.c,$(OBJ_DIR)/%.o,$(C_CODE))
9+
# Debug mode
10+
ifeq ($(DEBUG),1)
11+
CFLAGS += -g
12+
endif
1413

15-
# Build
16-
all: $(OBJ_FILES)
14+
# Source and objects
15+
SRCS := $(wildcard *.c)
16+
OBJS := $(patsubst %.c,$(OBJ_DIR)/%.o,$(SRCS))
17+
DEPS := $(OBJS:.o=.d)
1718

19+
# Ensure OBJ_DIR exists
20+
$(shell mkdir -p $(OBJ_DIR))
21+
22+
.PHONY: all clean
23+
24+
all: $(OBJS)
25+
26+
# Include dependency files
27+
-include $(DEPS)
28+
29+
# Compile C source to object
1830
$(OBJ_DIR)/%.o: %.c
19-
gcc -c $(CFLAGS) $(C_MODE_FLAGS) $< -o $@
31+
@echo " CC $<"
32+
$(Q)$(CC) -MMD -MP -c $(CFLAGS) $< -o $@
33+
34+
clean:
35+
$(Q)rm -f $(OBJS) $(DEPS)

0 commit comments

Comments
 (0)