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=lib 7lib_patterns := *.a 8LIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns))) 9 10 11# 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_ 12PIC := _INTR_APIC_ 13CFLAGS = $(GLOBAL_CFLAGS) -D $(PIC) -I $(shell pwd) 14 15export ASFLAGS := --64 16 17LD_LIST := head.o 18OBJ_LIST := head.o 19 20 21kernel_subdirs := common driver process debug filesystem time arch exception mm smp sched syscall 22 23 24 25head.o: head.S 26 gcc -E head.S > head.s # 预处理 27 as $(ASFLAGS) -o head.o head.s 28 29 30main.o: main.c 31# -fno-builtin: 不使用C语言内建函数 32# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture. 33 gcc $(CFLAGS) -c main.c -o main.o 34 35 36all: kernel 37 echo "Linking kernel..." 38 ld -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") -T link.lds 39# 生成kallsyms 40 current_dir=$(pwd) 41 42 @dbg='debug';for x in $$dbg; do \ 43 cd $$x;\ 44 $(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)";\ 45 cd ..;\ 46 done 47 48# 重新链接 49 echo "Re-Linking kernel..." 50 ld -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") -T link.lds 51 echo "Generating kernel ELF file..." 52# 生成内核文件 53 objcopy -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../bin/kernel/kernel.elf 54 echo "Done." 55 56 57kernel: head.o main.o $(OBJ_LIST) 58 59 @list='$(kernel_subdirs)'; for subdir in $$list; do \ 60 echo "make all in $$subdir";\ 61 cd $$subdir;\ 62 $(MAKE) all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)" kernel_root_path="$(shell pwd)";\ 63 cd ..;\ 64 done 65 66 67clean: 68 rm -rf $(GARBAGE) 69 @list='$(kernel_subdirs)'; for subdir in $$list; do \ 70 echo "Clean in dir: $$subdir";\ 71 cd $$subdir && $(MAKE) clean;\ 72 cd .. ;\ 73 done