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