xref: /DragonOS/kernel/src/Makefile (revision cc5feaf67b914ecf701abcba70c01da149755491)
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
22CFLAGS = $(GLOBAL_CFLAGS) -fno-pie $(CFLAGS_UNWIND) -I $(shell pwd) -I $(shell pwd)/include -I $(shell pwd)/arch/x86_64/include
23
24export ASFLAGS := --64
25
26LD_LIST := ""
27
28
29kernel_subdirs := common driver debug exception smp syscall ktest libs time
30
31
32kernel_rust:
33	RUSTFLAGS="$(RUSTFLAGS_UNWIND)" cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json
34
35all: kernel
36
37	@echo "Linking kernel..."
38	$(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 link.lds --no-relax
39# 生成kallsyms
40	current_dir=$(pwd)
41
42	@dbg='debug';for x in $$dbg; do \
43		cd $$x;\
44		$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
45		cd ..;\
46	done
47
48
49# 重新链接
50	@echo "Re-Linking kernel..."
51	@echo $(shell find . -name "*.o")
52	$(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 link.lds --no-relax
53	@echo "Generating kernel ELF file..."
54# 生成内核文件
55ifeq ($(UNWIND_ENABLE), yes)
56	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 kernel ../../bin/kernel/kernel.elf
57else
58	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
59endif
60	@echo "Kernel Build Done."
61
62ECHO:
63	@echo "$@"
64
65$(kernel_subdirs): ECHO
66
67	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)"  kernel_root_path="$(shell pwd)"
68
69kernel: $(kernel_subdirs) kernel_rust
70
71
72
73clean:
74	cargo clean
75	rm -rf $(GARBAGE)
76	@list='$(kernel_subdirs)'; for subdir in $$list; do \
77		echo "Clean in dir: $$subdir";\
78		cd $$subdir && $(MAKE) clean;\
79		cd .. ;\
80	done
81