xref: /DragonOS/kernel/src/Makefile (revision 081428c0d832cde99834cf8f94a0f2a1f41c9704)
1SUBDIR_ROOTS := .
2DIRS := . $(shell find $(SUBDIR_ROOTS) -type d)
3GARBAGE_PATTERNS := *.o *.s~ *.s *.S~ *.c~ *.h~ kernel
4GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS)))
5
6DIR_LIB=libs
7lib_patterns := *.a
8LIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns)))
9
10
11# unwind/backtrace related
12UNWIND_ENABLE ?= yes
13CFLAGS_UNWIND =
14LDFLAGS_UNWIND =
15RUSTFLAGS_UNWIND =
16ifeq ($(UNWIND_ENABLE), yes)
17	CFLAGS_UNWIND = -funwind-tables
18	LDFLAGS_UNWIND = --eh-frame-hdr
19	RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld
20endif
21
22RUSTFLAGS += $(RUSTFLAGS_UNWIND)
23
24CFLAGS = $(GLOBAL_CFLAGS) -fno-pie $(CFLAGS_UNWIND) -I $(shell pwd) -I $(shell pwd)/include
25
26ifeq ($(ARCH), x86_64)
27	CFLAGS +=  -I $(shell pwd)/arch/x86_64/include
28else ifeq ($(ARCH), riscv64)
29	CFLAGS +=  -I $(shell pwd)/arch/riscv64/include -I $(shell pwd)/arch/riscv64/
30endif
31
32export ASFLAGS := --64
33
34LD_LIST := ""
35
36
37kernel_subdirs := debug
38
39
40kernel_rust:
41	RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2024-11-05 $(CARGO_ZBUILD) build --release --target $(TARGET_JSON)
42
43
44all: kernel
45
46# if x86_64
47ifeq ($(ARCH), x86_64)
48	$(MAKE) __link_x86_64_kernel
49else ifeq ($(ARCH), riscv64)
50	$(MAKE) __link_riscv64_kernel
51endif
52
53	@echo "Kernel Build Done."
54
55ECHO:
56	@echo "$@"
57
58$(kernel_subdirs): ECHO
59	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)"  kernel_root_path="$(shell pwd)"
60
61kernel: $(kernel_subdirs) kernel_rust
62
63__link_riscv64_kernel:
64	@echo "Linking kernel..."
65	$(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
66	# 生成kallsyms
67	current_dir=$(pwd)
68
69	@dbg='debug';for x in $$dbg; do \
70		cd $$x;\
71		$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
72		cd ..;\
73	done
74
75# 重新链接
76	@echo "Re-Linking kernel..."
77	@echo $(shell find . -name "*.o")
78	$(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
79	@echo "Generating kernel ELF file..."
80
81ifeq ($(UNWIND_ENABLE), yes)
82	$(OBJCOPY) -I elf64-littleriscv -O elf64-littleriscv kernel ../../bin/kernel/kernel.elf
83else
84	$(OBJCOPY) -I elf64-littleriscv -O elf64-littleriscv -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
85endif
86	@rm kernel
87	$(MAKE) __dragon_stub PAYLOAD_ELF="$(shell pwd)/../../bin/kernel/kernel.elf"
88
89
90
91__link_x86_64_kernel:
92	@echo "Linking kernel..."
93	$(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
94# 生成kallsyms
95	current_dir=$(pwd)
96
97	@dbg='debug';for x in $$dbg; do \
98		cd $$x;\
99		$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
100		cd ..;\
101	done
102
103# 重新链接
104	@echo "Re-Linking kernel..."
105	@echo $(shell find . -name "*.o")
106	$(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
107	@echo "Generating kernel ELF file..."
108# 生成内核文件
109ifeq ($(UNWIND_ENABLE), yes)
110	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 kernel ../../bin/kernel/kernel.elf
111else
112	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
113endif
114	rm kernel
115
116__dragon_stub:
117	@echo "Linking dragon_stub..."
118	@mkdir -p $(ROOT_PATH)/bin/sysroot
119	PAYLOAD_ELF=$(PAYLOAD_ELF) TARGET_SYSROOT=$(ROOT_PATH)/bin/sysroot $(MAKE) -C $(ROOT_PATH)/kernel/submodules/DragonStub install -j $(NPROCS)
120
121
122clean:
123	@cargo clean
124	rm -rf $(GARBAGE)
125	@list='$(kernel_subdirs)'; for subdir in $$list; do \
126		echo "Clean in dir: $$subdir";\
127		cd $$subdir && $(MAKE) clean;\
128		cd .. ;\
129	done
130