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=1 11CFLAGS := -mcmodel=large -fno-builtin -m64 -O0 -I . -fno-stack-protector 12 13ifeq ($(DEBUG), 1) 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 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 63process.o: process/process.c 64 gcc $(CFLAGS) -c process/process.c -o process/process.o 65 66sched.o: sched/sched.c 67 gcc $(CFLAGS) -c sched/sched.c -o sched/sched.o 68 69syscall.o: syscall/syscall.c 70 gcc $(CFLAGS) -c syscall/syscall.c -o syscall/syscall.o 71 72smp.o: smp/smp.c 73 gcc $(CFLAGS) -c smp/smp.c -o smp/smp.o 74 75apu_boot.o: smp/apu_boot.S 76 gcc -E smp/apu_boot.S > smp/apu_boot.s # 预处理 77 as $(ASFLAGS) -o smp/apu_boot.o smp/apu_boot.s 78 79cpu.o: common/cpu.c 80 gcc $(CFLAGS) -c common/cpu.c -o common/cpu.o 81 82softirq.o: exception/softirq.c 83 gcc $(CFLAGS) -c exception/softirq.c -o exception/softirq.o 84 85# IPI的代码 86ifeq ($(ARCH), x86_64) 87OBJ_LIST += ipi.o 88LD_LIST += arch/x86_64/x86_64_ipi.o 89ipi.o: arch/x86_64/x86_64_ipi.c 90 gcc $(CFLAGS) -c arch/x86_64/x86_64_ipi.c -o arch/x86_64/x86_64_ipi.o 91 92endif 93 94# 驱动程序 95# 中断处理芯片的驱动程序 96ifeq ($(PIC), _INTR_8259A_) 97pic.o: driver/interrupt/8259A/8259A.c 98 gcc $(CFLAGS) -c driver/interrupt/8259A/8259A.c -o driver/interrupt/pic.o 99else 100pic.o: driver/interrupt/apic/apic.c 101 gcc $(CFLAGS) -c driver/interrupt/apic/apic.c -o driver/interrupt/pic.o 102endif 103 104multiboot2.o: driver/multiboot2/multiboot2.c 105 gcc $(CFLAGS) -c driver/multiboot2/multiboot2.c -o driver/multiboot2/multiboot2.o 106 107acpi.o: driver/acpi/acpi.c 108 gcc $(CFLAGS) -c driver/acpi/acpi.c -o driver/acpi/acpi.o 109 110ps2_keyboard.o: driver/keyboard/ps2_keyboard.c 111 gcc $(CFLAGS) -c driver/keyboard/ps2_keyboard.c -o driver/keyboard/ps2_keyboard.o 112 113ps2_mouse.o: driver/mouse/ps2_mouse.c 114 gcc $(CFLAGS) -c driver/mouse/ps2_mouse.c -o driver/mouse/ps2_mouse.o 115 116ata.o: driver/disk/ata.c 117 gcc $(CFLAGS) -c driver/disk/ata.c -o driver/disk/ata.o 118 119pci.o: driver/pci/pci.c 120 gcc $(CFLAGS) -c driver/pci/pci.c -o driver/pci/pci.o 121 122ahci.o: driver/disk/ahci/ahci.c 123 gcc $(CFLAGS) -c driver/disk/ahci/ahci.c -o driver/disk/ahci/ahci.o 124 125rtc.o: driver/timers/rtc/rtc.c 126 gcc $(CFLAGS) -c driver/timers/rtc/rtc.c -o driver/timers/rtc/rtc.o 127 128HPET.o: driver/timers/HPET/HPET.c 129 gcc $(CFLAGS) -c driver/timers/HPET/HPET.c -o driver/timers/HPET/HPET.o 130 131timer.o: driver/timers/timer.c 132 gcc $(CFLAGS) -c driver/timers/timer.c -o driver/timers/timer.o 133 134OBJ_LIST += uart.o 135LD_LIST += driver/uart/uart.o 136uart.o: driver/uart/uart.c 137 gcc $(CFLAGS) -c driver/uart/uart.c -o driver/uart/uart.o 138 139 140 141all: kernel 142 objcopy -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../bin/kernel/kernel.elf 143# 144 145kernel: head.o entry.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 $(OBJ_LIST) 146 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 \ 147 common/cpu.o smp/smp.o smp/apu_boot.o exception/softirq.o sched/sched.o \ 148 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 \ 149 $(LD_LIST) \ 150 -T link.lds 151 152 153clean: 154 rm -rf $(GARBAGE)