Skip to content

Commit 9d667bb

Browse files
authored
Merge pull request #5 from guoweikang/copilot/integrate-kconfig-with-cargo-kbuild
Integrate Kconfig with cargo-kbuild for unified build configuration
1 parent c9692db commit 9d667bb

6 files changed

Lines changed: 195 additions & 8 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ Cargo.lock
2323

2424
# kernel config
2525
.config
26+
auto.conf
27+
autoconf.h

Kconfig

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,114 @@
11
mainmenu "X-kernel Configuration"
22

3+
choice
4+
prompt "Target Architecture"
5+
default ARCH_AARCH64
6+
7+
config ARCH_AARCH64
8+
bool "AArch64 (ARM 64-bit)"
9+
10+
config ARCH_RISCV64
11+
bool "RISC-V 64-bit"
12+
13+
config ARCH_X86_64
14+
bool "x86_64"
15+
16+
config ARCH_LOONGARCH64
17+
bool "LoongArch 64-bit"
18+
19+
endchoice
20+
321
menu "Platform Selection"
422

23+
if ARCH_AARCH64
24+
25+
choice
26+
prompt "AArch64 Platform"
27+
default PLATFORM_AARCH64_QEMU_VIRT
28+
29+
config PLATFORM_AARCH64_QEMU_VIRT
30+
bool "QEMU ARM64 Virtual Machine"
31+
32+
config PLATFORM_AARCH64_CROSVM_VIRT
33+
bool "CROSVM ARM64 Virtual Machine"
34+
35+
config PLATFORM_AARCH64_RASPI
36+
bool "Raspberry Pi 4B"
37+
38+
endchoice
39+
40+
endif # ARCH_AARCH64
41+
42+
if ARCH_RISCV64
43+
44+
choice
45+
prompt "RISC-V 64 Platform"
46+
default PLATFORM_RISCV64_QEMU_VIRT
47+
48+
config PLATFORM_RISCV64_QEMU_VIRT
49+
bool "QEMU RISC-V 64 Virtual Machine"
50+
51+
endchoice
52+
53+
endif # ARCH_RISCV64
54+
55+
if ARCH_X86_64
56+
57+
choice
58+
prompt "x86_64 Platform"
59+
default PLATFORM_X86_64_QEMU_VIRT
60+
61+
config PLATFORM_X86_64_QEMU_VIRT
62+
bool "QEMU x86_64 Virtual Machine"
63+
64+
config PLATFORM_X86_CSV
65+
bool "x86 CSV Platform"
66+
67+
endchoice
68+
69+
endif # ARCH_X86_64
70+
71+
if ARCH_LOONGARCH64
72+
73+
choice
74+
prompt "LoongArch64 Platform"
75+
default PLATFORM_LOONGARCH64_QEMU_VIRT
76+
77+
config PLATFORM_LOONGARCH64_QEMU_VIRT
78+
bool "QEMU LoongArch64 Virtual Machine"
79+
80+
endchoice
81+
82+
endif # ARCH_LOONGARCH64
83+
584
source "platforms/Kconfig"
685

786
endmenu
87+
88+
menu "Kernel Features"
89+
90+
config FEAT_SMP
91+
bool "Symmetric Multiprocessing (SMP)"
92+
default y
93+
94+
config FEAT_FP_SIMD
95+
bool "Floating Point and SIMD"
96+
default y
97+
98+
config FEAT_NET
99+
bool "Network Stack"
100+
default y
101+
102+
config FEAT_FS
103+
bool "File System Support"
104+
default y
105+
106+
config FEAT_ALLOC
107+
bool "Dynamic Memory Allocation"
108+
default y
109+
110+
config FEAT_IRQ
111+
bool "Interrupt Support"
112+
default y
113+
114+
endmenu

Makefile

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
# Enable unstable features
3636
export RUSTC_BOOTSTRAP := 1
3737
export DWARF := y
38+
39+
# Include Kconfig extraction (must be before ARCH/PLAT defaults)
40+
-include scripts/make/kconfig.mk
41+
3842
# General options
3943
ARCH ?= aarch64
4044
PLAT ?= $(ARCH)-qemu-virt
@@ -179,7 +183,12 @@ check_config:
179183

180184

181185
menuconfig:
182-
xconf menuconfig
186+
@xconf menuconfig -k Kconfig -s .
187+
@if [ -f .config ]; then \
188+
echo "✅ Configuration saved to .config"; \
189+
else \
190+
echo "ℹ️ No changes saved"; \
191+
fi
183192

184193
rootfs:
185194
@if [ ! -f $(ROOTFS_IMG) ]; then \
@@ -193,10 +202,19 @@ teefs:
193202
$(MAKE) -C tee_apps ARCH=$(ARCH)
194203

195204
defconfig:
196-
$(call defconfig)
205+
@xconf saveconfig -o .config -k Kconfig -s .
206+
@echo "✅ Default configuration saved to .config"
207+
208+
saveconfig:
209+
@xconf saveconfig -o .config -k Kconfig -s .
197210

198-
oldconfig: check_config
199-
$(call oldconfig)
211+
oldconfig:
212+
@if [ ! -f .config ]; then \
213+
echo "$(RED_C)Error$(END_C): .config not found."; \
214+
echo "Please run 'make defconfig' or 'make menuconfig' first."; \
215+
exit 1; \
216+
fi
217+
@xconf oldconfig -c .config -k Kconfig -s .
200218

201219
build: $(OUT_DIR) $(FINAL_IMG)
202220

@@ -248,11 +266,16 @@ endif
248266
clean: clean_c
249267
rm -rf $(APP)/*.bin $(APP)/*.elf $(OUT_CONFIG)
250268
cargo clean
269+
@rm -f target/kbuild/config.rs .cargo/config.toml
270+
271+
distclean: clean
272+
@rm -f .config auto.conf autoconf.h
273+
@echo "✅ Removed all configuration files"
251274

252275
clean_c::
253276
rm -rf $(app-objs)
254277

255-
.PHONY: all check_config defconfig oldconfig menuconfig \
278+
.PHONY: all check_config defconfig oldconfig menuconfig saveconfig \
256279
build disasm run justrun debug \
257280
clippy doc doc_check_missing fmt fmt_c unittest unittest_no_fail_fast \
258-
disk_img clean clean_c
281+
disk_img clean distclean clean_c

scripts/make/build.mk

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,18 @@ else
4242
RUSTFLAGS += --check-cfg cfg(unittest)
4343
endif
4444

45-
_cargo_build: oldconfig
45+
# Build requires platform configuration ($(OUT_CONFIG))
46+
# oldconfig generates .platconfig.toml from platform-specific TOML files
47+
# This is separate from the xconf Kconfig system (.config)
48+
_cargo_build: $(OUT_CONFIG)
4649
@printf " $(GREEN_C)Building$(END_C) App: $(APP_NAME), Arch: $(ARCH), Platform: $(PLAT_NAME)\n"
4750
$(call cargo_build,$(APP),$(KFEAT) $(APP_FEAT))
4851
@cp $(rust_elf) $(OUT_ELF)
4952

53+
# Generate .platconfig.toml if it doesn't exist
54+
$(OUT_CONFIG):
55+
$(call oldconfig)
56+
5057
$(OUT_DIR):
5158
$(call run_cmd,mkdir,-p $@)
5259

scripts/make/cargo.mk

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ ifeq ($(MAKECMDGOALS), doc_check_missing)
2424
RUSTDOCFLAGS += -D missing-docs
2525
endif
2626

27+
# Select cargo command: use cargo-kbuild if .config exists, otherwise use cargo
28+
CARGO_CMD := $(if $(wildcard .config),cargo-kbuild,cargo)
29+
CARGO_KCONFIG_ARG := $(if $(wildcard .config),--kconfig .config,)
30+
2731
define cargo_build
28-
$(call run_cmd,cargo -C $(1) build,$(build_args) --features "$(strip $(2))")
32+
$(call run_cmd,$(CARGO_CMD) -C $(1) build $(CARGO_KCONFIG_ARG),$(build_args) --features "$(strip $(2))")
2933
endef
3034

3135
clippy_args := -A unsafe_op_in_unsafe_fn

scripts/make/kconfig.mk

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Extract ARCH and PLAT from .config if it exists
2+
3+
ifneq ($(wildcard .config),)
4+
# Read .config once and extract ARCH and PLAT in a single pass
5+
CONFIG_VALUES := $(shell awk '/^(CONFIG_)?ARCH_[A-Z0-9_]+=y/ { print $$0 } /^(CONFIG_)?PLATFORM_[A-Z0-9_]+=y/ { print $$0 }' .config)
6+
7+
# Parse architecture
8+
ifeq ($(findstring ARCH_AARCH64=y,$(CONFIG_VALUES)),ARCH_AARCH64=y)
9+
ARCH_FROM_CONFIG := aarch64
10+
else ifeq ($(findstring ARCH_RISCV64=y,$(CONFIG_VALUES)),ARCH_RISCV64=y)
11+
ARCH_FROM_CONFIG := riscv64
12+
else ifeq ($(findstring ARCH_X86_64=y,$(CONFIG_VALUES)),ARCH_X86_64=y)
13+
ARCH_FROM_CONFIG := x86_64
14+
else ifeq ($(findstring ARCH_LOONGARCH64=y,$(CONFIG_VALUES)),ARCH_LOONGARCH64=y)
15+
ARCH_FROM_CONFIG := loongarch64
16+
endif
17+
18+
# Parse platform
19+
ifeq ($(findstring PLATFORM_AARCH64_QEMU_VIRT=y,$(CONFIG_VALUES)),PLATFORM_AARCH64_QEMU_VIRT=y)
20+
PLAT_FROM_CONFIG := aarch64-qemu-virt
21+
else ifeq ($(findstring PLATFORM_AARCH64_CROSVM_VIRT=y,$(CONFIG_VALUES)),PLATFORM_AARCH64_CROSVM_VIRT=y)
22+
PLAT_FROM_CONFIG := aarch64-crosvm-virt
23+
else ifeq ($(findstring PLATFORM_AARCH64_RASPI=y,$(CONFIG_VALUES)),PLATFORM_AARCH64_RASPI=y)
24+
PLAT_FROM_CONFIG := aarch64-raspi
25+
else ifeq ($(findstring PLATFORM_RISCV64_QEMU_VIRT=y,$(CONFIG_VALUES)),PLATFORM_RISCV64_QEMU_VIRT=y)
26+
PLAT_FROM_CONFIG := riscv64-qemu-virt
27+
else ifeq ($(findstring PLATFORM_X86_64_QEMU_VIRT=y,$(CONFIG_VALUES)),PLATFORM_X86_64_QEMU_VIRT=y)
28+
PLAT_FROM_CONFIG := x86_64-qemu-virt
29+
else ifeq ($(findstring PLATFORM_X86_CSV=y,$(CONFIG_VALUES)),PLATFORM_X86_CSV=y)
30+
PLAT_FROM_CONFIG := x86-csv
31+
else ifeq ($(findstring PLATFORM_LOONGARCH64_QEMU_VIRT=y,$(CONFIG_VALUES)),PLATFORM_LOONGARCH64_QEMU_VIRT=y)
32+
PLAT_FROM_CONFIG := loongarch64-qemu-virt
33+
endif
34+
35+
# Use config values as defaults, but allow command line override
36+
ARCH ?= $(ARCH_FROM_CONFIG)
37+
PLAT ?= $(PLAT_FROM_CONFIG)
38+
endif
39+
40+
# Final defaults if not set from config or command line
41+
ARCH ?= aarch64
42+
PLAT ?= $(ARCH)-qemu-virt
43+
44+
export ARCH PLAT

0 commit comments

Comments
 (0)