1SUBDIR_ROOTS := . 2DIRS := . $(shell find $(SUBDIR_ROOTS) -type d) 3GARBAGE_PATTERNS := *.o *.s~ *.s *.S~ *.c~ *.h~ kernel 4GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS))) 5 6DIR_LIB=libs 7lib_patterns := *.a 8LIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns))) 9 10 11# unwind/backtrace related 12UNWIND_ENABLE ?= yes 13CFLAGS_UNWIND = 14LDFLAGS_UNWIND = 15RUSTFLAGS_UNWIND = 16ifeq ($(UNWIND_ENABLE), yes) 17 CFLAGS_UNWIND = -funwind-tables 18ifeq ($(ARCH), x86_64) 19 LDFLAGS_UNWIND = --eh-frame-hdr 20endif 21 RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld 22endif 23 24RUSTFLAGS = $(RUSTFLAGS_UNWIND) 25 26CFLAGS = $(GLOBAL_CFLAGS) -fno-pie $(CFLAGS_UNWIND) -I $(shell pwd) -I $(shell pwd)/include 27 28ifeq ($(ARCH), x86_64) 29 CFLAGS += -I $(shell pwd)/arch/x86_64/include 30else ifeq ($(ARCH), riscv64) 31 CFLAGS += -I $(shell pwd)/arch/riscv64/include -I $(shell pwd)/arch/riscv64/ 32endif 33 34export ASFLAGS := --64 35 36LD_LIST := "" 37 38 39kernel_subdirs := common driver debug exception smp syscall ktest libs time 40 41 42kernel_rust: 43ifeq ($(ARCH), riscv64) 44 RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2023-01-21 $(CARGO_ZBUILD) build --release --target riscv64imac-unknown-none-elf 45else 46 RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2023-01-21 $(CARGO_ZBUILD) build --release --target $(TARGET_JSON) 47endif 48 49all: kernel 50 51# if x86_64 52ifeq ($(ARCH), x86_64) 53 $(MAKE) __link_x86_64_kernel 54else ifeq ($(ARCH), riscv64) 55 $(MAKE) __link_riscv64_kernel 56endif 57 58 @echo "Kernel Build Done." 59 60ECHO: 61 @echo "$@" 62 63$(kernel_subdirs): ECHO 64 65 $(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" kernel_root_path="$(shell pwd)" 66 67kernel: $(kernel_subdirs) kernel_rust 68 69__link_riscv64_kernel: 70 @echo "Linking kernel..." 71 $(LD) -b elf64-littleriscv -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/riscv64imac-unknown-none-elf/release/libdragonos_kernel.a -T arch/riscv64/link.ld --no-relax 72 $(OBJCOPY) -I elf64-littleriscv -O elf64-littleriscv -R ".eh_frame" kernel ../../bin/kernel/kernel.elf 73 @rm kernel 74 $(MAKE) __dragon_stub PAYLOAD_ELF="$(shell pwd)/../../bin/kernel/kernel.elf" 75 76 77 78__link_x86_64_kernel: 79 @echo "Linking kernel..." 80 $(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T arch/x86_64/link.lds --no-relax 81# 生成kallsyms 82 current_dir=$(pwd) 83 84 @dbg='debug';for x in $$dbg; do \ 85 cd $$x;\ 86 $(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\ 87 cd ..;\ 88 done 89 90# 重新链接 91 @echo "Re-Linking kernel..." 92 @echo $(shell find . -name "*.o") 93 $(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a ./debug/kallsyms.o -T arch/x86_64/link.lds --no-relax 94 @echo "Generating kernel ELF file..." 95# 生成内核文件 96ifeq ($(UNWIND_ENABLE), yes) 97 $(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 kernel ../../bin/kernel/kernel.elf 98else 99 $(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".eh_frame" kernel ../../bin/kernel/kernel.elf 100endif 101 rm kernel 102 103__dragon_stub: 104 @echo "Linking dragon_stub..." 105 PAYLOAD_ELF=$(PAYLOAD_ELF) TARGET_SYSROOT=$(ROOT_PATH)/bin/sysroot $(MAKE) -C $(ROOT_PATH)/../DragonStub install -j $(NPROCS) 106 107 108clean: 109 @cargo clean 110 rm -rf $(GARBAGE) 111 @list='$(kernel_subdirs)'; for subdir in $$list; do \ 112 echo "Clean in dir: $$subdir";\ 113 cd $$subdir && $(MAKE) clean;\ 114 cd .. ;\ 115 done 116