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