Makefile 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. SUBDIR_ROOTS := .
  2. DIRS := . $(shell find $(SUBDIR_ROOTS) -type d)
  3. GARBAGE_PATTERNS := *.o *.s~ *.s *.S~ *.c~ *.h~ kernel
  4. GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS)))
  5. DIR_LIB=lib
  6. lib_patterns := *.a
  7. LIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns)))
  8. # 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_
  9. PIC := _INTR_APIC_
  10. CFLAGS = $(GLOBAL_CFLAGS) -D $(PIC) -I $(shell pwd)
  11. ASFLAGS := --64
  12. LD_LIST := head.o
  13. OBJ_LIST := head.o
  14. kernel_subdirs := common driver process debug
  15. head.o: head.S
  16. gcc -E head.S > head.s # 预处理
  17. as $(ASFLAGS) -o head.o head.s
  18. #gcc -mcmodel=large -fno-builtin -m64 -c head.S -o head.o
  19. entry.o: exception/entry.S
  20. gcc -E exception/entry.S > exception/entry.s
  21. as $(ASFLAGS) -o exception/entry.o exception/entry.s
  22. main.o: main.c
  23. # -fno-builtin: 不使用C语言内建函数
  24. # The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture.
  25. gcc $(CFLAGS) -c main.c -o main.o
  26. printk.o: common/printk.c
  27. gcc $(CFLAGS) -c common/printk.c -o common/printk.o
  28. trap.o: exception/trap.c
  29. gcc $(CFLAGS) -c exception/trap.c -o exception/trap.o
  30. irq.o: exception/irq.c
  31. gcc $(CFLAGS) -c exception/irq.c -o exception/irq.o
  32. mm.o: mm/mm.c
  33. gcc $(CFLAGS) -c mm/mm.c -o mm/mm.o
  34. slab.o: mm/slab.c
  35. gcc $(CFLAGS) -c mm/slab.c -o mm/slab.o
  36. sched.o: sched/sched.c
  37. gcc $(CFLAGS) -c sched/sched.c -o sched/sched.o
  38. syscall.o: syscall/syscall.c
  39. gcc $(CFLAGS) -c syscall/syscall.c -o syscall/syscall.o
  40. smp.o: smp/smp.c
  41. gcc $(CFLAGS) -c smp/smp.c -o smp/smp.o
  42. apu_boot.o: smp/apu_boot.S
  43. gcc -E smp/apu_boot.S > smp/apu_boot.s # 预处理
  44. as $(ASFLAGS) -o smp/apu_boot.o smp/apu_boot.s
  45. cpu.o: common/cpu.c
  46. gcc $(CFLAGS) -c common/cpu.c -o common/cpu.o
  47. softirq.o: exception/softirq.c
  48. gcc $(CFLAGS) -c exception/softirq.c -o exception/softirq.o
  49. fat32.o: filesystem/fat32/fat32.c
  50. gcc $(CFLAGS) -c filesystem/fat32/fat32.c -o filesystem/fat32/fat32.o
  51. MBR.o: filesystem/MBR.c
  52. gcc $(CFLAGS) -c filesystem/MBR.c -o filesystem/MBR.o
  53. VFS.o: filesystem/VFS/VFS.c
  54. gcc $(CFLAGS) -c filesystem/VFS/VFS.c -o filesystem/VFS/VFS.o
  55. # IPI的代码
  56. ifeq ($(ARCH), __x86_64__)
  57. OBJ_LIST += ipi.o
  58. LD_LIST += arch/x86_64/x86_64_ipi.o
  59. ipi.o: arch/x86_64/x86_64_ipi.c
  60. gcc $(CFLAGS) -c arch/x86_64/x86_64_ipi.c -o arch/x86_64/x86_64_ipi.o
  61. endif
  62. # 驱动程序
  63. # 中断处理芯片的驱动程序
  64. ifeq ($(PIC), _INTR_8259A_)
  65. pic.o: driver/interrupt/8259A/8259A.c
  66. gcc $(CFLAGS) -c driver/interrupt/8259A/8259A.c -o driver/interrupt/pic.o
  67. else
  68. pic.o: driver/interrupt/apic/apic.c
  69. gcc $(CFLAGS) -c driver/interrupt/apic/apic.c -o driver/interrupt/pic.o
  70. endif
  71. multiboot2.o: driver/multiboot2/multiboot2.c
  72. gcc $(CFLAGS) -c driver/multiboot2/multiboot2.c -o driver/multiboot2/multiboot2.o
  73. acpi.o: driver/acpi/acpi.c
  74. gcc $(CFLAGS) -c driver/acpi/acpi.c -o driver/acpi/acpi.o
  75. ps2_keyboard.o: driver/keyboard/ps2_keyboard.c
  76. gcc $(CFLAGS) -c driver/keyboard/ps2_keyboard.c -o driver/keyboard/ps2_keyboard.o
  77. ps2_mouse.o: driver/mouse/ps2_mouse.c
  78. gcc $(CFLAGS) -c driver/mouse/ps2_mouse.c -o driver/mouse/ps2_mouse.o
  79. ata.o: driver/disk/ata.c
  80. gcc $(CFLAGS) -c driver/disk/ata.c -o driver/disk/ata.o
  81. pci.o: driver/pci/pci.c
  82. gcc $(CFLAGS) -c driver/pci/pci.c -o driver/pci/pci.o
  83. ahci.o: driver/disk/ahci/ahci.c
  84. gcc $(CFLAGS) -c driver/disk/ahci/ahci.c -o driver/disk/ahci/ahci.o
  85. rtc.o: driver/timers/rtc/rtc.c
  86. gcc $(CFLAGS) -c driver/timers/rtc/rtc.c -o driver/timers/rtc/rtc.o
  87. HPET.o: driver/timers/HPET/HPET.c
  88. gcc $(CFLAGS) -c driver/timers/HPET/HPET.c -o driver/timers/HPET/HPET.o
  89. timer.o: driver/timers/timer.c
  90. gcc $(CFLAGS) -c driver/timers/timer.c -o driver/timers/timer.o
  91. OBJ_LIST += uart.o
  92. LD_LIST += driver/uart/uart.o
  93. uart.o: driver/uart/uart.c
  94. gcc $(CFLAGS) -c driver/uart/uart.c -o driver/uart/uart.o
  95. all: kernel
  96. echo "Linking kernel..."
  97. ld -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") -T link.lds
  98. # 生成kallsyms
  99. current_dir=$(pwd)
  100. @dbg='debug';for x in $$dbg; do \
  101. cd $$x;\
  102. $(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)";\
  103. cd ..;\
  104. done
  105. # 重新链接
  106. echo "Re-Linking kernel..."
  107. ld -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") ./debug/kallsyms.o -T link.lds
  108. echo "Generating kernel ELF file..."
  109. # 生成内核文件
  110. objcopy -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../bin/kernel/kernel.elf
  111. echo "Done."
  112. kernel: head.o entry.o main.o printk.o trap.o mm.o slab.o irq.o pic.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)
  113. @list='$(kernel_subdirs)'; for subdir in $$list; do \
  114. echo "make all in $$subdir";\
  115. cd $$subdir;\
  116. $(MAKE) all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" kernel_root_path="$(shell pwd)";\
  117. cd ..;\
  118. done
  119. clean:
  120. rm -rf $(GARBAGE)
  121. @list='$(kernel_subdirs)'; for subdir in $$list; do \
  122. echo "Clean in dir: $$subdir";\
  123. cd $$subdir && $(MAKE) clean;\
  124. cd .. ;\
  125. done