1SUBDIR_ROOTS := . common 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 10CFLAGS := -mcmodel=large -fno-builtin -m64 11ASFLAGS := --64 12 13all: kernel 14 objcopy -I elf64-x86-64 -S -R ".comment" -R ".eh_frame" -O elf64-x86-64 kernel ../bin/kernel/kernel.elf 15# cp kernel ../bin/kernel/kernel.elf 16 17 18kernel: head.o entry.o main.o printk.o trap.o mm.o slab.o irq.o 8259A.o process.o syscall.o multiboot2.o cpu.o 19 ld -b elf64-x86-64 -z muldefs -o kernel head.o exception/entry.o main.o common/printk.o exception/trap.o exception/irq.o mm/mm.o mm/slab.o process/process.o syscall/syscall.o driver/multiboot2/multiboot2.o \ 20 common/cpu.o \ 21 driver/interrupt/8259A/8259A.o \ 22 -T link.lds 23 24head.o: head.S 25 gcc -E head.S > head.s # 预处理 26 as $(ASFLAGS) -o head.o head.s 27#gcc -mcmodel=large -fno-builtin -m64 -c head.S -o head.o 28 29entry.o: exception/entry.S 30 gcc -E exception/entry.S > exception/entry.s 31 as $(ASFLAGS) -o exception/entry.o exception/entry.s 32 33 34 35main.o: main.c 36# -fno-builtin: 不使用C语言内建函数 37# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture. 38 gcc $(CFLAGS) -c main.c -o main.o 39 40 41printk.o: common/printk.c 42 gcc $(CFLAGS) -c common/printk.c -o common/printk.o 43 44trap.o: exception/trap.c 45 gcc $(CFLAGS) -c exception/trap.c -o exception/trap.o 46 47irq.o: exception/irq.c 48 gcc $(CFLAGS) -c exception/irq.c -o exception/irq.o 49 50 51 52mm.o: mm/mm.c 53 gcc $(CFLAGS) -c mm/mm.c -o mm/mm.o 54 55slab.o: mm/slab.c 56 gcc $(CFLAGS) -c mm/slab.c -o mm/slab.o 57 58process.o: process/process.c 59 gcc $(CFLAGS) -c process/process.c -o process/process.o 60syscall.o: syscall/syscall.c 61 gcc $(CFLAGS) -c syscall/syscall.c -o syscall/syscall.o 62 63multiboot2.o: driver/multiboot2/multiboot2.c 64 gcc $(CFLAGS) -c driver/multiboot2/multiboot2.c -o driver/multiboot2/multiboot2.o 65 66cpu.o: common/cpu.c 67 gcc $(CFLAGS) -c common/cpu.c -o common/cpu.o 68 69 70# 驱动程序 718259A.o: driver/interrupt/8259A/8259A.c 72 gcc $(CFLAGS) -c driver/interrupt/8259A/8259A.c -o driver/interrupt/8259A/8259A.o 73clean: 74 rm -rf $(GARBAGE)