xref: /DragonOS/kernel/src/Makefile (revision ae5ede03bebe5c4b593ad7a350f0945f1367be7c)
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# 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_
12PIC := _INTR_APIC_
13CFLAGS = $(GLOBAL_CFLAGS) -fno-pie -D $(PIC) -I $(shell pwd) -I $(shell pwd)/include -I $(shell pwd)/arch/x86_64/include
14
15export ASFLAGS := --64
16
17LD_LIST := head.o
18
19
20kernel_subdirs := common driver debug arch exception smp sched syscall ktest libs time
21
22
23head.o: head.S
24	$(CC) -E head.S > _head.s # 预处理
25	$(AS) $(ASFLAGS) -o head.o _head.s
26
27
28main.o: main.c
29# -fno-builtin: 不使用C语言内建函数
30# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture.
31	$(CC) $(CFLAGS) -c main.c  -o main.o
32
33kernel_rust:
34	rustup default nightly
35	cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json
36
37all: kernel
38
39	@echo "Linking kernel..."
40	$(LD) -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T link.lds --no-relax
41# 生成kallsyms
42	current_dir=$(pwd)
43
44	@dbg='debug';for x in $$dbg; do \
45		cd $$x;\
46		$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
47		cd ..;\
48	done
49
50
51# 重新链接
52	@echo "Re-Linking kernel..."
53	@echo $(shell find . -name "*.o")
54	$(LD) -b elf64-x86-64 -z muldefs -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
55	@echo "Generating kernel ELF file..."
56# 生成内核文件
57	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64  kernel ../../bin/kernel/kernel.elf
58# $(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
59	@echo "Kernel Build Done."
60
61ECHO:
62	@echo "$@"
63
64$(kernel_subdirs): ECHO
65
66	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)" kernel_root_path="$(shell pwd)"
67
68kernel: head.o main.o $(kernel_subdirs) kernel_rust
69
70
71
72clean:
73	cargo clean
74	rm -rf $(GARBAGE)
75	@list='$(kernel_subdirs)'; for subdir in $$list; do \
76		echo "Clean in dir: $$subdir";\
77		cd $$subdir && $(MAKE) clean;\
78		cd .. ;\
79	done