Makefile 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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=libs
  6. lib_patterns := *.a
  7. LIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns)))
  8. # unwind/backtrace related
  9. UNWIND_ENABLE ?= yes
  10. CFLAGS_UNWIND =
  11. LDFLAGS_UNWIND =
  12. RUSTFLAGS_UNWIND =
  13. ifeq ($(UNWIND_ENABLE), yes)
  14. CFLAGS_UNWIND = -funwind-tables
  15. LDFLAGS_UNWIND = --eh-frame-hdr
  16. RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld -Cpanic=unwind
  17. endif
  18. RUSTFLAGS += $(RUSTFLAGS_UNWIND)
  19. CFLAGS = $(GLOBAL_CFLAGS) -fno-pie $(CFLAGS_UNWIND) -I $(shell pwd) -I $(shell pwd)/include
  20. ifeq ($(ARCH), x86_64)
  21. CFLAGS += -I $(shell pwd)/arch/x86_64/include
  22. else ifeq ($(ARCH), riscv64)
  23. CFLAGS += -I $(shell pwd)/arch/riscv64/include -I $(shell pwd)/arch/riscv64/
  24. endif
  25. export ASFLAGS := --64
  26. LD_LIST := ""
  27. kernel_subdirs := debug
  28. kernel_rust:
  29. RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2024-11-05 $(CARGO_ZBUILD) build --release --target $(TARGET_JSON)
  30. all: kernel
  31. # if x86_64
  32. ifeq ($(ARCH), x86_64)
  33. $(MAKE) __link_x86_64_kernel
  34. else ifeq ($(ARCH), riscv64)
  35. $(MAKE) __link_riscv64_kernel
  36. endif
  37. @echo "Kernel Build Done."
  38. ECHO:
  39. @echo "$@"
  40. $(kernel_subdirs): ECHO
  41. $(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" kernel_root_path="$(shell pwd)"
  42. kernel: $(kernel_subdirs) kernel_rust
  43. __link_riscv64_kernel:
  44. @echo "Linking kernel..."
  45. $(LD) -b elf64-littleriscv -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/riscv64gc-unknown-none-elf/release/libdragonos_kernel.a -T arch/riscv64/link.ld --no-relax
  46. # 生成kallsyms
  47. current_dir=$(pwd)
  48. @dbg='debug';for x in $$dbg; do \
  49. cd $$x;\
  50. $(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
  51. cd ..;\
  52. done
  53. # 重新链接
  54. @echo "Re-Linking kernel..."
  55. @echo $(shell find . -name "*.o")
  56. $(LD) -b elf64-littleriscv -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/riscv64gc-unknown-none-elf/release/libdragonos_kernel.a ./debug/kallsyms.o -T arch/riscv64/link.ld --no-relax
  57. @echo "Generating kernel ELF file..."
  58. ifeq ($(UNWIND_ENABLE), yes)
  59. $(OBJCOPY) -I elf64-littleriscv -O elf64-littleriscv kernel ../../bin/kernel/kernel.elf
  60. else
  61. $(OBJCOPY) -I elf64-littleriscv -O elf64-littleriscv -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
  62. endif
  63. @rm kernel
  64. $(MAKE) __dragon_stub PAYLOAD_ELF="$(shell pwd)/../../bin/kernel/kernel.elf"
  65. __link_x86_64_kernel:
  66. @echo "Linking kernel..."
  67. $(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T arch/x86_64/link.lds --no-relax
  68. # 生成kallsyms
  69. current_dir=$(pwd)
  70. @dbg='debug';for x in $$dbg; do \
  71. cd $$x;\
  72. $(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
  73. cd ..;\
  74. done
  75. # 重新链接
  76. @echo "Re-Linking kernel..."
  77. @echo $(shell find . -name "*.o")
  78. $(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a ./debug/kallsyms.o -T arch/x86_64/link.lds --no-relax
  79. @echo "Generating kernel ELF file..."
  80. # 生成内核文件
  81. ifeq ($(UNWIND_ENABLE), yes)
  82. $(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 kernel ../../bin/kernel/kernel.elf
  83. else
  84. $(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
  85. endif
  86. rm kernel
  87. __dragon_stub:
  88. @echo "Linking dragon_stub..."
  89. @mkdir -p $(ROOT_PATH)/bin/sysroot
  90. PAYLOAD_ELF=$(PAYLOAD_ELF) TARGET_SYSROOT=$(ROOT_PATH)/bin/sysroot $(MAKE) -C $(ROOT_PATH)/kernel/submodules/DragonStub install -j $(NPROCS)
  91. clean:
  92. @cargo clean
  93. rm -rf $(GARBAGE)
  94. @list='$(kernel_subdirs)'; for subdir in $$list; do \
  95. echo "Clean in dir: $$subdir";\
  96. cd $$subdir && $(MAKE) clean;\
  97. cd .. ;\
  98. done
  99. .PHONY: check
  100. check:
  101. RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2024-11-05 $(CARGO_ZBUILD) check --workspace --message-format=json --target $(TARGET_JSON)