12813126eSloginSUBDIR_ROOTS := . 22813126eSloginDIRS := . $(shell find $(SUBDIR_ROOTS) -type d) 32813126eSloginGARBAGE_PATTERNS := *.o *.s~ *.s *.S~ *.c~ *.h~ kernel 42813126eSloginGARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS))) 52813126eSlogin 62813126eSloginDIR_LIB=lib 72813126eSloginlib_patterns := *.a 82813126eSloginLIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns))) 92813126eSlogin 102813126eSlogin 112813126eSlogin# 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_ 122813126eSloginPIC := _INTR_APIC_ 132813126eSloginCFLAGS = $(GLOBAL_CFLAGS) -D $(PIC) -I $(shell pwd) 142813126eSlogin 152813126eSloginexport ASFLAGS := --64 162813126eSlogin 172813126eSloginLD_LIST := head.o 182813126eSlogin 192813126eSlogin 202813126eSloginkernel_subdirs := common driver process debug filesystem time arch exception mm smp sched syscall ktest lib ipc 212813126eSlogin 222813126eSlogin 232813126eSlogin 242813126eSloginhead.o: head.S 252813126eSlogin $(CC) -E head.S > _head.s # 预处理 262813126eSlogin as $(ASFLAGS) -o head.o _head.s 272813126eSlogin 282813126eSlogin 292813126eSloginmain.o: main.c 302813126eSlogin# -fno-builtin: 不使用C语言内建函数 312813126eSlogin# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture. 322813126eSlogin $(CC) $(CFLAGS) -c main.c -o main.o 332813126eSlogin 34*82d2e446Sloginkernel_rust: 352813126eSlogin rustup default nightly 362813126eSlogin cargo +nightly build --release --target ./arch/x86_64/x86_64-unknown-none.json 37*82d2e446Sloginall: kernel_rust 38*82d2e446Slogin $(MAKE) kernel || exit 1 392813126eSlogin @echo "Linking kernel..." 402813126eSlogin ld -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T link.lds 412813126eSlogin# 生成kallsyms 422813126eSlogin current_dir=$(pwd) 432813126eSlogin 442813126eSlogin @dbg='debug';for x in $$dbg; do \ 452813126eSlogin cd $$x;\ 462813126eSlogin $(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)";\ 472813126eSlogin cd ..;\ 482813126eSlogin if [ "$$?" != "0" ]; then\ 492813126eSlogin exit $$?;\ 502813126eSlogin fi;\ 512813126eSlogin done 522813126eSlogin 532813126eSlogin 542813126eSlogin# 重新链接 552813126eSlogin @echo "Re-Linking kernel..." 562813126eSlogin ld -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a ./debug/kallsyms.o -T link.lds 572813126eSlogin @echo "Generating kernel ELF file..." 582813126eSlogin# 生成内核文件 592813126eSlogin objcopy -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../../bin/kernel/kernel.elf 602813126eSlogin @echo "Kernel Build Done." 612813126eSlogin 622813126eSloginECHO: 632813126eSlogin @echo "$@" 642813126eSlogin 652813126eSlogin$(kernel_subdirs): ECHO 662813126eSlogin 672813126eSlogin $(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)" kernel_root_path="$(shell pwd)" 682813126eSlogin 692813126eSloginkernel: head.o main.o $(kernel_subdirs) 702813126eSlogin 712813126eSlogin 722813126eSlogin 732813126eSloginclean: 742813126eSlogin cargo clean 752813126eSlogin rm -rf $(GARBAGE) 762813126eSlogin @list='$(kernel_subdirs)'; for subdir in $$list; do \ 772813126eSlogin echo "Clean in dir: $$subdir";\ 782813126eSlogin cd $$subdir && $(MAKE) clean;\ 792813126eSlogin cd .. ;\ 802813126eSlogin done