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 11d470019bSLoGin# unwind/backtrace related 12d470019bSLoGinUNWIND_ENABLE ?= yes 13d470019bSLoGinCFLAGS_UNWIND = 14d470019bSLoGinLDFLAGS_UNWIND = 15d470019bSLoGinRUSTFLAGS_UNWIND = 16d470019bSLoGinifeq ($(UNWIND_ENABLE), yes) 17d470019bSLoGin CFLAGS_UNWIND = -funwind-tables 18d470019bSLoGin LDFLAGS_UNWIND = --eh-frame-hdr 19d470019bSLoGin RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld 20d470019bSLoGinendif 21d470019bSLoGin 22*70a4e555SLoGinCFLAGS = $(GLOBAL_CFLAGS) -fno-pie $(CFLAGS_UNWIND) -I $(shell pwd) -I $(shell pwd)/include -I $(shell pwd)/arch/x86_64/include 232813126eSlogin 242813126eSloginexport ASFLAGS := --64 252813126eSlogin 262813126eSloginLD_LIST := head.o 272813126eSlogin 282813126eSlogin 29*70a4e555SLoGinkernel_subdirs := common driver debug arch exception smp syscall ktest libs time 302813126eSlogin 312813126eSlogin 322813126eSloginhead.o: head.S 332813126eSlogin $(CC) -E head.S > _head.s # 预处理 345ed4cd46Sguanjinquan $(AS) $(ASFLAGS) -o head.o _head.s 352813126eSlogin 362813126eSlogin 372813126eSloginmain.o: main.c 382813126eSlogin# -fno-builtin: 不使用C语言内建函数 392813126eSlogin# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture. 402813126eSlogin $(CC) $(CFLAGS) -c main.c -o main.o 412813126eSlogin 4282d2e446Sloginkernel_rust: 43d470019bSLoGin RUSTFLAGS="$(RUSTFLAGS_UNWIND)" cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json 44*70a4e555SLoGin 45fb6c29d0Sloginall: kernel 46fb6c29d0Slogin 472813126eSlogin @echo "Linking kernel..." 48d470019bSLoGin $(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel head.o main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T link.lds --no-relax 492813126eSlogin# 生成kallsyms 502813126eSlogin current_dir=$(pwd) 512813126eSlogin 522813126eSlogin @dbg='debug';for x in $$dbg; do \ 532813126eSlogin cd $$x;\ 54c757940bSYJwu2023 $(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\ 552813126eSlogin cd ..;\ 562813126eSlogin done 572813126eSlogin 582813126eSlogin 592813126eSlogin# 重新链接 602813126eSlogin @echo "Re-Linking kernel..." 61fb6c29d0Slogin @echo $(shell find . -name "*.o") 62d470019bSLoGin $(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -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 --no-relax 632813126eSlogin @echo "Generating kernel ELF file..." 642813126eSlogin# 生成内核文件 65d470019bSLoGinifeq ($(UNWIND_ENABLE), yes) 6640fe15e0SLoGin $(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 kernel ../../bin/kernel/kernel.elf 67d470019bSLoGinelse 68d470019bSLoGin $(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".eh_frame" kernel ../../bin/kernel/kernel.elf 69d470019bSLoGinendif 702813126eSlogin @echo "Kernel Build Done." 712813126eSlogin 722813126eSloginECHO: 732813126eSlogin @echo "$@" 742813126eSlogin 752813126eSlogin$(kernel_subdirs): ECHO 762813126eSlogin 77*70a4e555SLoGin $(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" kernel_root_path="$(shell pwd)" 782813126eSlogin 79fb6c29d0Sloginkernel: head.o main.o $(kernel_subdirs) kernel_rust 802813126eSlogin 812813126eSlogin 822813126eSlogin 832813126eSloginclean: 842813126eSlogin cargo clean 852813126eSlogin rm -rf $(GARBAGE) 862813126eSlogin @list='$(kernel_subdirs)'; for subdir in $$list; do \ 872813126eSlogin echo "Clean in dir: $$subdir";\ 882813126eSlogin cd $$subdir && $(MAKE) clean;\ 892813126eSlogin cd .. ;\ 902813126eSlogin done 91