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