Makefile 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 filesystem time
  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. # IPI的代码
  50. ifeq ($(ARCH), __x86_64__)
  51. OBJ_LIST += ipi.o
  52. LD_LIST += arch/x86_64/x86_64_ipi.o
  53. ipi.o: arch/x86_64/x86_64_ipi.c
  54. gcc $(CFLAGS) -c arch/x86_64/x86_64_ipi.c -o arch/x86_64/x86_64_ipi.o
  55. endif
  56. # 驱动程序
  57. # 中断处理芯片的驱动程序
  58. ifeq ($(PIC), _INTR_8259A_)
  59. pic.o: driver/interrupt/8259A/8259A.c
  60. gcc $(CFLAGS) -c driver/interrupt/8259A/8259A.c -o driver/interrupt/pic.o
  61. else
  62. pic.o: driver/interrupt/apic/apic.c
  63. gcc $(CFLAGS) -c driver/interrupt/apic/apic.c -o driver/interrupt/pic.o
  64. endif
  65. multiboot2.o: driver/multiboot2/multiboot2.c
  66. gcc $(CFLAGS) -c driver/multiboot2/multiboot2.c -o driver/multiboot2/multiboot2.o
  67. acpi.o: driver/acpi/acpi.c
  68. gcc $(CFLAGS) -c driver/acpi/acpi.c -o driver/acpi/acpi.o
  69. ps2_keyboard.o: driver/keyboard/ps2_keyboard.c
  70. gcc $(CFLAGS) -c driver/keyboard/ps2_keyboard.c -o driver/keyboard/ps2_keyboard.o
  71. ps2_mouse.o: driver/mouse/ps2_mouse.c
  72. gcc $(CFLAGS) -c driver/mouse/ps2_mouse.c -o driver/mouse/ps2_mouse.o
  73. ata.o: driver/disk/ata.c
  74. gcc $(CFLAGS) -c driver/disk/ata.c -o driver/disk/ata.o
  75. pci.o: driver/pci/pci.c
  76. gcc $(CFLAGS) -c driver/pci/pci.c -o driver/pci/pci.o
  77. ahci.o: driver/disk/ahci/ahci.c
  78. gcc $(CFLAGS) -c driver/disk/ahci/ahci.c -o driver/disk/ahci/ahci.o
  79. rtc.o: driver/timers/rtc/rtc.c
  80. gcc $(CFLAGS) -c driver/timers/rtc/rtc.c -o driver/timers/rtc/rtc.o
  81. HPET.o: driver/timers/HPET/HPET.c
  82. gcc $(CFLAGS) -c driver/timers/HPET/HPET.c -o driver/timers/HPET/HPET.o
  83. OBJ_LIST += uart.o
  84. LD_LIST += driver/uart/uart.o
  85. uart.o: driver/uart/uart.c
  86. gcc $(CFLAGS) -c driver/uart/uart.c -o driver/uart/uart.o
  87. all: kernel
  88. echo "Linking kernel..."
  89. ld -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") -T link.lds
  90. # 生成kallsyms
  91. current_dir=$(pwd)
  92. @dbg='debug';for x in $$dbg; do \
  93. cd $$x;\
  94. $(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)";\
  95. cd ..;\
  96. done
  97. # 重新链接
  98. echo "Re-Linking kernel..."
  99. ld -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") ./debug/kallsyms.o -T link.lds
  100. echo "Generating kernel ELF file..."
  101. # 生成内核文件
  102. objcopy -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../bin/kernel/kernel.elf
  103. echo "Done."
  104. 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 $(OBJ_LIST)
  105. @list='$(kernel_subdirs)'; for subdir in $$list; do \
  106. echo "make all in $$subdir";\
  107. cd $$subdir;\
  108. $(MAKE) all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" kernel_root_path="$(shell pwd)";\
  109. cd ..;\
  110. done
  111. clean:
  112. rm -rf $(GARBAGE)
  113. @list='$(kernel_subdirs)'; for subdir in $$list; do \
  114. echo "Clean in dir: $$subdir";\
  115. cd $$subdir && $(MAKE) clean;\
  116. cd .. ;\
  117. done