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 11 12# 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_ 13PIC := _INTR_APIC_ 14CFLAGS += -D $(PIC) 15 16ASFLAGS := --64 17 18all: kernel 19 objcopy -I elf64-x86-64 -S -R ".comment" -R ".eh_frame" -O elf64-x86-64 kernel ../bin/kernel/kernel.elf 20 21 22kernel: 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 23 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 \ 24 common/cpu.o \ 25 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 \ 26 -T link.lds 27 28head.o: head.S 29 gcc -E head.S > head.s # 预处理 30 as $(ASFLAGS) -o head.o head.s 31#gcc -mcmodel=large -fno-builtin -m64 -c head.S -o head.o 32 33entry.o: exception/entry.S 34 gcc -E exception/entry.S > exception/entry.s 35 as $(ASFLAGS) -o exception/entry.o exception/entry.s 36 37 38 39main.o: main.c 40# -fno-builtin: 不使用C语言内建函数 41# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture. 42 gcc $(CFLAGS) -c main.c -o main.o 43 44 45printk.o: common/printk.c 46 gcc $(CFLAGS) -c common/printk.c -o common/printk.o 47 48trap.o: exception/trap.c 49 gcc $(CFLAGS) -c exception/trap.c -o exception/trap.o 50 51irq.o: exception/irq.c 52 gcc $(CFLAGS) -c exception/irq.c -o exception/irq.o 53 54 55 56mm.o: mm/mm.c 57 gcc $(CFLAGS) -c mm/mm.c -o mm/mm.o 58 59slab.o: mm/slab.c 60 gcc $(CFLAGS) -c mm/slab.c -o mm/slab.o 61 62process.o: process/process.c 63 gcc $(CFLAGS) -c process/process.c -o process/process.o 64syscall.o: syscall/syscall.c 65 gcc $(CFLAGS) -c syscall/syscall.c -o syscall/syscall.o 66 67 68 69cpu.o: common/cpu.c 70 gcc $(CFLAGS) -c common/cpu.c -o common/cpu.o 71 72# 驱动程序 73# 中断处理芯片的驱动程序 74ifeq ($(PIC), _INTR_8259A_) 75pic.o: driver/interrupt/8259A/8259A.c 76 gcc $(CFLAGS) -c driver/interrupt/8259A/8259A.c -o driver/interrupt/pic.o 77else 78pic.o: driver/interrupt/apic/apic.c 79 gcc $(CFLAGS) -c driver/interrupt/apic/apic.c -o driver/interrupt/pic.o 80endif 81 82multiboot2.o: driver/multiboot2/multiboot2.c 83 gcc $(CFLAGS) -c driver/multiboot2/multiboot2.c -o driver/multiboot2/multiboot2.o 84 85acpi.o: driver/acpi/acpi.c 86 gcc $(CFLAGS) -c driver/acpi/acpi.c -o driver/acpi/acpi.o 87 88ps2_keyboard.o: driver/keyboard/ps2_keyboard.c 89 gcc $(CFLAGS) -c driver/keyboard/ps2_keyboard.c -o driver/keyboard/ps2_keyboard.o 90 91ps2_mouse.o: driver/mouse/ps2_mouse.c 92 gcc $(CFLAGS) -c driver/mouse/ps2_mouse.c -o driver/mouse/ps2_mouse.o 93 94ata.o: driver/disk/ata.c 95 gcc $(CFLAGS) -c driver/disk/ata.c -o driver/disk/ata.o 96 97pci.o: driver/pci/pci.c 98 gcc $(CFLAGS) -c driver/pci/pci.c -o driver/pci/pci.o 99 100 101clean: 102 rm -rf $(GARBAGE)