xref: /DragonOS/kernel/src/Makefile (revision 70a4e5550a9fb49b537092287c3ddc36448c5b78)
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 := head.o
27
28
29kernel_subdirs := common driver debug arch exception smp syscall ktest libs time
30
31
32head.o: head.S
33	$(CC) -E head.S > _head.s # 预处理
34	$(AS) $(ASFLAGS) -o head.o _head.s
35
36
37main.o: main.c
38# -fno-builtin: 不使用C语言内建函数
39# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture.
40	$(CC) $(CFLAGS) -c main.c  -o main.o
41
42kernel_rust:
43	RUSTFLAGS="$(RUSTFLAGS_UNWIND)" cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json
44
45all: kernel
46
47	@echo "Linking kernel..."
48	$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel head.o main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T link.lds --no-relax
49# 生成kallsyms
50	current_dir=$(pwd)
51
52	@dbg='debug';for x in $$dbg; do \
53		cd $$x;\
54		$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
55		cd ..;\
56	done
57
58
59# 重新链接
60	@echo "Re-Linking kernel..."
61	@echo $(shell find . -name "*.o")
62	$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel head.o main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a ./debug/kallsyms.o  -T link.lds --no-relax
63	@echo "Generating kernel ELF file..."
64# 生成内核文件
65ifeq ($(UNWIND_ENABLE), yes)
66	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 kernel ../../bin/kernel/kernel.elf
67else
68	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
69endif
70	@echo "Kernel Build Done."
71
72ECHO:
73	@echo "$@"
74
75$(kernel_subdirs): ECHO
76
77	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)"  kernel_root_path="$(shell pwd)"
78
79kernel: head.o main.o $(kernel_subdirs) kernel_rust
80
81
82
83clean:
84	cargo clean
85	rm -rf $(GARBAGE)
86	@list='$(kernel_subdirs)'; for subdir in $$list; do \
87		echo "Clean in dir: $$subdir";\
88		cd $$subdir && $(MAKE) clean;\
89		cd .. ;\
90	done
91