Makefile 3.4 KB

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