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 6fb6c29d0SloginDIR_LIB=libs 72813126eSloginlib_patterns := *.a 82813126eSloginLIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns))) 92813126eSlogin 102813126eSlogin 112813126eSlogin# 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_ 122813126eSloginPIC := _INTR_APIC_ 13cffd7144SloginCFLAGS = $(GLOBAL_CFLAGS) -D $(PIC) -I $(shell pwd) -I $(shell pwd)/include -I $(shell pwd)/arch/x86_64/include 142813126eSlogin 152813126eSloginexport ASFLAGS := --64 162813126eSlogin 172813126eSloginLD_LIST := head.o 182813126eSlogin 192813126eSlogin 20fb6c29d0Sloginkernel_subdirs := common driver process debug filesystem time arch exception mm smp sched syscall ktest libs ipc 212813126eSlogin 222813126eSlogin 232813126eSlogin 242813126eSloginhead.o: head.S 252813126eSlogin $(CC) -E head.S > _head.s # 预处理 26*5ed4cd46Sguanjinquan $(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 3482d2e446Sloginkernel_rust: 352813126eSlogin rustup default nightly 362813126eSlogin cargo +nightly build --release --target ./arch/x86_64/x86_64-unknown-none.json 37fb6c29d0Slogin 38fb6c29d0Sloginall: kernel 39fb6c29d0Slogin 402813126eSlogin @echo "Linking kernel..." 41*5ed4cd46Sguanjinquan $(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 422813126eSlogin# 生成kallsyms 432813126eSlogin current_dir=$(pwd) 442813126eSlogin 452813126eSlogin @dbg='debug';for x in $$dbg; do \ 462813126eSlogin cd $$x;\ 472813126eSlogin $(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)";\ 482813126eSlogin cd ..;\ 492813126eSlogin if [ "$$?" != "0" ]; then\ 502813126eSlogin exit $$?;\ 512813126eSlogin fi;\ 522813126eSlogin done 532813126eSlogin 542813126eSlogin 552813126eSlogin# 重新链接 562813126eSlogin @echo "Re-Linking kernel..." 57fb6c29d0Slogin @echo $(shell find . -name "*.o") 58*5ed4cd46Sguanjinquan $(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 592813126eSlogin @echo "Generating kernel ELF file..." 602813126eSlogin# 生成内核文件 61*5ed4cd46Sguanjinquan $(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../../bin/kernel/kernel.elf 622813126eSlogin @echo "Kernel Build Done." 632813126eSlogin 642813126eSloginECHO: 652813126eSlogin @echo "$@" 662813126eSlogin 672813126eSlogin$(kernel_subdirs): ECHO 682813126eSlogin 692813126eSlogin $(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)" kernel_root_path="$(shell pwd)" 702813126eSlogin 71fb6c29d0Sloginkernel: head.o main.o $(kernel_subdirs) kernel_rust 722813126eSlogin 732813126eSlogin 742813126eSlogin 752813126eSloginclean: 762813126eSlogin cargo clean 772813126eSlogin rm -rf $(GARBAGE) 782813126eSlogin @list='$(kernel_subdirs)'; for subdir in $$list; do \ 792813126eSlogin echo "Clean in dir: $$subdir";\ 802813126eSlogin cd $$subdir && $(MAKE) clean;\ 812813126eSlogin cd .. ;\ 822813126eSlogin done