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