1ead838bfSfslongjinSUBDIR_ROOTS := . common 2ead838bfSfslongjinDIRS := . $(shell find $(SUBDIR_ROOTS) -type d) 340a551d1SfslongjinGARBAGE_PATTERNS := *.o *.s~ *.s *.S~ *.c~ *.h~ kernel 4ead838bfSfslongjinGARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS))) 5f479f321Sfslongjin 640a551d1SfslongjinDIR_LIB=lib 740a551d1Sfslongjinlib_patterns := *.a 840a551d1SfslongjinLIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns))) 959b4e6f6Sfslongjin 10*4473ff59SfslongjinDEBUG=DEBUG 110e8bf69dSfslongjinCFLAGS := -mcmodel=large -fno-builtin -m64 -O0 -I . -fno-stack-protector 120e8bf69dSfslongjin 13*4473ff59Sfslongjinifeq ($(DEBUG), DEBUG) 140e8bf69dSfslongjinCFLAGS += -g 150e8bf69dSfslongjinendif 160e8bf69dSfslongjin 17279de4c7SfslongjinARCH=x86_64 18574f3232Sfslongjin# 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_ 19574f3232SfslongjinPIC := _INTR_APIC_ 20279de4c7SfslongjinCFLAGS += -D $(PIC) -D $(ARCH) 21574f3232Sfslongjin 2259b4e6f6SfslongjinASFLAGS := --64 2359b4e6f6Sfslongjin 24279de4c7SfslongjinLD_LIST := head.o 25279de4c7SfslongjinOBJ_LIST := head.o 26f479f321Sfslongjin 27279de4c7Sfslongjin 28f479f321Sfslongjin 29f479f321Sfslongjinhead.o: head.S 30f479f321Sfslongjin gcc -E head.S > head.s # 预处理 3159b4e6f6Sfslongjin as $(ASFLAGS) -o head.o head.s 32d9d83335Sfslongjin#gcc -mcmodel=large -fno-builtin -m64 -c head.S -o head.o 33f479f321Sfslongjin 3406cfb1ceSfslongjinentry.o: exception/entry.S 3506cfb1ceSfslongjin gcc -E exception/entry.S > exception/entry.s 3659b4e6f6Sfslongjin as $(ASFLAGS) -o exception/entry.o exception/entry.s 3706cfb1ceSfslongjin 38*4473ff59Sfslongjinprocs.o: process/proc.S 39*4473ff59Sfslongjin gcc -E process/proc.S > process/proc.s 40*4473ff59Sfslongjin as $(ASFLAGS) -o process/procs.o process/proc.s 41*4473ff59Sfslongjin 42eff673edSfslongjin 43eff673edSfslongjin 44ead838bfSfslongjinmain.o: main.c 45ead838bfSfslongjin# -fno-builtin: 不使用C语言内建函数 46ead838bfSfslongjin# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture. 4759b4e6f6Sfslongjin gcc $(CFLAGS) -c main.c -o main.o 48ead838bfSfslongjin 49ead838bfSfslongjin 50ead838bfSfslongjinprintk.o: common/printk.c 5159b4e6f6Sfslongjin gcc $(CFLAGS) -c common/printk.c -o common/printk.o 5206cfb1ceSfslongjin 5306cfb1ceSfslongjintrap.o: exception/trap.c 5459b4e6f6Sfslongjin gcc $(CFLAGS) -c exception/trap.c -o exception/trap.o 5540a551d1Sfslongjin 56eff673edSfslongjinirq.o: exception/irq.c 5759b4e6f6Sfslongjin gcc $(CFLAGS) -c exception/irq.c -o exception/irq.o 58eff673edSfslongjin 59d5eb9e8aSfslongjin 60eff673edSfslongjin 6140a551d1Sfslongjinmm.o: mm/mm.c 6259b4e6f6Sfslongjin gcc $(CFLAGS) -c mm/mm.c -o mm/mm.o 63ead838bfSfslongjin 6436ad7a10Sfslongjinslab.o: mm/slab.c 6536ad7a10Sfslongjin gcc $(CFLAGS) -c mm/slab.c -o mm/slab.o 6636ad7a10Sfslongjin 6767633eb0Sfslongjinprocess.o: process/process.c 6859b4e6f6Sfslongjin gcc $(CFLAGS) -c process/process.c -o process/process.o 6905dc7ac7Sfslongjin 7005dc7ac7Sfslongjinsched.o: sched/sched.c 7105dc7ac7Sfslongjin gcc $(CFLAGS) -c sched/sched.c -o sched/sched.o 7205dc7ac7Sfslongjin 73d3a5048fSfslongjinsyscall.o: syscall/syscall.c 7459b4e6f6Sfslongjin gcc $(CFLAGS) -c syscall/syscall.c -o syscall/syscall.o 7567633eb0Sfslongjin 7660dc9f49Sfslongjinsmp.o: smp/smp.c 7760dc9f49Sfslongjin gcc $(CFLAGS) -c smp/smp.c -o smp/smp.o 780b0cce93Sfslongjin 795197253dSfslongjinapu_boot.o: smp/apu_boot.S 805197253dSfslongjin gcc -E smp/apu_boot.S > smp/apu_boot.s # 预处理 815197253dSfslongjin as $(ASFLAGS) -o smp/apu_boot.o smp/apu_boot.s 825197253dSfslongjin 83ad51f8b6Sfslongjincpu.o: common/cpu.c 84ad51f8b6Sfslongjin gcc $(CFLAGS) -c common/cpu.c -o common/cpu.o 85ad51f8b6Sfslongjin 869828aff9Sfslongjinsoftirq.o: exception/softirq.c 879828aff9Sfslongjin gcc $(CFLAGS) -c exception/softirq.c -o exception/softirq.o 889828aff9Sfslongjin 89979bb355Sfslongjinfat32.o: filesystem/fat32/fat32.c 90979bb355Sfslongjin gcc $(CFLAGS) -c filesystem/fat32/fat32.c -o filesystem/fat32/fat32.o 91979bb355Sfslongjin 929b382dabSfslongjinMBR.o: filesystem/MBR.c 939b382dabSfslongjin gcc $(CFLAGS) -c filesystem/MBR.c -o filesystem/MBR.o 949b382dabSfslongjin 957d3c1b09SfslongjinVFS.o: filesystem/VFS/VFS.c 967d3c1b09Sfslongjin gcc $(CFLAGS) -c filesystem/VFS/VFS.c -o filesystem/VFS/VFS.o 977d3c1b09Sfslongjin 98279de4c7Sfslongjin# IPI的代码 99279de4c7Sfslongjinifeq ($(ARCH), x86_64) 100279de4c7SfslongjinOBJ_LIST += ipi.o 101279de4c7SfslongjinLD_LIST += arch/x86_64/x86_64_ipi.o 102279de4c7Sfslongjinipi.o: arch/x86_64/x86_64_ipi.c 103279de4c7Sfslongjin gcc $(CFLAGS) -c arch/x86_64/x86_64_ipi.c -o arch/x86_64/x86_64_ipi.o 104279de4c7Sfslongjin 105279de4c7Sfslongjinendif 106279de4c7Sfslongjin 107d5eb9e8aSfslongjin# 驱动程序 108574f3232Sfslongjin# 中断处理芯片的驱动程序 109e64be7b4Sfslongjinifeq ($(PIC), _INTR_8259A_) 110574f3232Sfslongjinpic.o: driver/interrupt/8259A/8259A.c 111574f3232Sfslongjin gcc $(CFLAGS) -c driver/interrupt/8259A/8259A.c -o driver/interrupt/pic.o 112e64be7b4Sfslongjinelse 113574f3232Sfslongjinpic.o: driver/interrupt/apic/apic.c 114574f3232Sfslongjin gcc $(CFLAGS) -c driver/interrupt/apic/apic.c -o driver/interrupt/pic.o 115574f3232Sfslongjinendif 116574f3232Sfslongjin 117574f3232Sfslongjinmultiboot2.o: driver/multiboot2/multiboot2.c 118574f3232Sfslongjin gcc $(CFLAGS) -c driver/multiboot2/multiboot2.c -o driver/multiboot2/multiboot2.o 119574f3232Sfslongjin 120e64be7b4Sfslongjinacpi.o: driver/acpi/acpi.c 121e64be7b4Sfslongjin gcc $(CFLAGS) -c driver/acpi/acpi.c -o driver/acpi/acpi.o 122e64be7b4Sfslongjin 12371d6af78Sfslongjinps2_keyboard.o: driver/keyboard/ps2_keyboard.c 12471d6af78Sfslongjin gcc $(CFLAGS) -c driver/keyboard/ps2_keyboard.c -o driver/keyboard/ps2_keyboard.o 125832442afSfslongjin 12671d6af78Sfslongjinps2_mouse.o: driver/mouse/ps2_mouse.c 12771d6af78Sfslongjin gcc $(CFLAGS) -c driver/mouse/ps2_mouse.c -o driver/mouse/ps2_mouse.o 128b3cbd3caSfslongjin 129046bce39Sfslongjinata.o: driver/disk/ata.c 130046bce39Sfslongjin gcc $(CFLAGS) -c driver/disk/ata.c -o driver/disk/ata.o 131046bce39Sfslongjin 13208fa7b53Sfslongjinpci.o: driver/pci/pci.c 13308fa7b53Sfslongjin gcc $(CFLAGS) -c driver/pci/pci.c -o driver/pci/pci.o 13408fa7b53Sfslongjin 135741aa09bSfslongjinahci.o: driver/disk/ahci/ahci.c 136741aa09bSfslongjin gcc $(CFLAGS) -c driver/disk/ahci/ahci.c -o driver/disk/ahci/ahci.o 137741aa09bSfslongjin 138401df84dSfslongjinrtc.o: driver/timers/rtc/rtc.c 139401df84dSfslongjin gcc $(CFLAGS) -c driver/timers/rtc/rtc.c -o driver/timers/rtc/rtc.o 140401df84dSfslongjin 141d887f9a0SfslongjinHPET.o: driver/timers/HPET/HPET.c 142d887f9a0Sfslongjin gcc $(CFLAGS) -c driver/timers/HPET/HPET.c -o driver/timers/HPET/HPET.o 143d887f9a0Sfslongjin 1449828aff9Sfslongjintimer.o: driver/timers/timer.c 1459828aff9Sfslongjin gcc $(CFLAGS) -c driver/timers/timer.c -o driver/timers/timer.o 1469828aff9Sfslongjin 14722359344SfslongjinOBJ_LIST += uart.o 14822359344SfslongjinLD_LIST += driver/uart/uart.o 14922359344Sfslongjinuart.o: driver/uart/uart.c 15022359344Sfslongjin gcc $(CFLAGS) -c driver/uart/uart.c -o driver/uart/uart.o 15122359344Sfslongjin 152b3cbd3caSfslongjin 153279de4c7Sfslongjin 154279de4c7Sfslongjinall: kernel 155279de4c7Sfslongjin objcopy -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../bin/kernel/kernel.elf 156279de4c7Sfslongjin# 157279de4c7Sfslongjin 158*4473ff59Sfslongjinkernel: 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*4473ff59Sfslongjin 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 \ 1607d3c1b09Sfslongjin 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 \ 1619828aff9Sfslongjin 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 \ 162279de4c7Sfslongjin $(LD_LIST) \ 163279de4c7Sfslongjin -T link.lds 164279de4c7Sfslongjin 165279de4c7Sfslongjin 166f479f321Sfslongjinclean: 167ead838bfSfslongjin rm -rf $(GARBAGE)