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 15ASFLAGS := --64 16 17LD_LIST := head.o 18OBJ_LIST := head.o 19 20 21kernel_subdirs := common driver process 22 23 24 25 26 27head.o: head.S 28 gcc -E head.S > head.s # 预处理 29 as $(ASFLAGS) -o head.o head.s 30#gcc -mcmodel=large -fno-builtin -m64 -c head.S -o head.o 31 32entry.o: exception/entry.S 33 gcc -E exception/entry.S > exception/entry.s 34 as $(ASFLAGS) -o exception/entry.o exception/entry.s 35 36 37 38 39 40main.o: main.c 41# -fno-builtin: 不使用C语言内建函数 42# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture. 43 gcc $(CFLAGS) -c main.c -o main.o 44 45 46printk.o: common/printk.c 47 gcc $(CFLAGS) -c common/printk.c -o common/printk.o 48 49trap.o: exception/trap.c 50 gcc $(CFLAGS) -c exception/trap.c -o exception/trap.o 51 52irq.o: exception/irq.c 53 gcc $(CFLAGS) -c exception/irq.c -o exception/irq.o 54 55 56 57mm.o: mm/mm.c 58 gcc $(CFLAGS) -c mm/mm.c -o mm/mm.o 59 60slab.o: mm/slab.c 61 gcc $(CFLAGS) -c mm/slab.c -o mm/slab.o 62 63 64sched.o: sched/sched.c 65 gcc $(CFLAGS) -c sched/sched.c -o sched/sched.o 66 67syscall.o: syscall/syscall.c 68 gcc $(CFLAGS) -c syscall/syscall.c -o syscall/syscall.o 69 70smp.o: smp/smp.c 71 gcc $(CFLAGS) -c smp/smp.c -o smp/smp.o 72 73apu_boot.o: smp/apu_boot.S 74 gcc -E smp/apu_boot.S > smp/apu_boot.s # 预处理 75 as $(ASFLAGS) -o smp/apu_boot.o smp/apu_boot.s 76 77cpu.o: common/cpu.c 78 gcc $(CFLAGS) -c common/cpu.c -o common/cpu.o 79 80softirq.o: exception/softirq.c 81 gcc $(CFLAGS) -c exception/softirq.c -o exception/softirq.o 82 83fat32.o: filesystem/fat32/fat32.c 84 gcc $(CFLAGS) -c filesystem/fat32/fat32.c -o filesystem/fat32/fat32.o 85 86MBR.o: filesystem/MBR.c 87 gcc $(CFLAGS) -c filesystem/MBR.c -o filesystem/MBR.o 88 89VFS.o: filesystem/VFS/VFS.c 90 gcc $(CFLAGS) -c filesystem/VFS/VFS.c -o filesystem/VFS/VFS.o 91 92# IPI的代码 93ifeq ($(ARCH), __x86_64__) 94OBJ_LIST += ipi.o 95LD_LIST += arch/x86_64/x86_64_ipi.o 96ipi.o: arch/x86_64/x86_64_ipi.c 97 gcc $(CFLAGS) -c arch/x86_64/x86_64_ipi.c -o arch/x86_64/x86_64_ipi.o 98 99endif 100 101# 驱动程序 102# 中断处理芯片的驱动程序 103ifeq ($(PIC), _INTR_8259A_) 104pic.o: driver/interrupt/8259A/8259A.c 105 gcc $(CFLAGS) -c driver/interrupt/8259A/8259A.c -o driver/interrupt/pic.o 106else 107pic.o: driver/interrupt/apic/apic.c 108 gcc $(CFLAGS) -c driver/interrupt/apic/apic.c -o driver/interrupt/pic.o 109endif 110 111multiboot2.o: driver/multiboot2/multiboot2.c 112 gcc $(CFLAGS) -c driver/multiboot2/multiboot2.c -o driver/multiboot2/multiboot2.o 113 114acpi.o: driver/acpi/acpi.c 115 gcc $(CFLAGS) -c driver/acpi/acpi.c -o driver/acpi/acpi.o 116 117ps2_keyboard.o: driver/keyboard/ps2_keyboard.c 118 gcc $(CFLAGS) -c driver/keyboard/ps2_keyboard.c -o driver/keyboard/ps2_keyboard.o 119 120ps2_mouse.o: driver/mouse/ps2_mouse.c 121 gcc $(CFLAGS) -c driver/mouse/ps2_mouse.c -o driver/mouse/ps2_mouse.o 122 123ata.o: driver/disk/ata.c 124 gcc $(CFLAGS) -c driver/disk/ata.c -o driver/disk/ata.o 125 126pci.o: driver/pci/pci.c 127 gcc $(CFLAGS) -c driver/pci/pci.c -o driver/pci/pci.o 128 129ahci.o: driver/disk/ahci/ahci.c 130 gcc $(CFLAGS) -c driver/disk/ahci/ahci.c -o driver/disk/ahci/ahci.o 131 132rtc.o: driver/timers/rtc/rtc.c 133 gcc $(CFLAGS) -c driver/timers/rtc/rtc.c -o driver/timers/rtc/rtc.o 134 135HPET.o: driver/timers/HPET/HPET.c 136 gcc $(CFLAGS) -c driver/timers/HPET/HPET.c -o driver/timers/HPET/HPET.o 137 138timer.o: driver/timers/timer.c 139 gcc $(CFLAGS) -c driver/timers/timer.c -o driver/timers/timer.o 140 141OBJ_LIST += uart.o 142LD_LIST += driver/uart/uart.o 143uart.o: driver/uart/uart.c 144 gcc $(CFLAGS) -c driver/uart/uart.c -o driver/uart/uart.o 145 146 147all: kernel 148 149 ld -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") -T link.lds 150 objcopy -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../bin/kernel/kernel.elf 151 152kernel: head.o entry.o main.o printk.o trap.o mm.o slab.o irq.o pic.o sched.o syscall.o multiboot2.o cpu.o acpi.o ps2_keyboard.o ps2_mouse.o ata.o pci.o ahci.o smp.o apu_boot.o rtc.o HPET.o softirq.o timer.o fat32.o MBR.o VFS.o $(OBJ_LIST) 153 154 @list='$(kernel_subdirs)'; for subdir in $$list; do \ 155 echo "make all in $$subdir";\ 156 cd $$subdir;\ 157 $(MAKE) all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)";\ 158 cd ..;\ 159 done 160 161 162 163 164clean: 165 rm -rf $(GARBAGE)