xref: /DragonOS/kernel/src/Makefile (revision 1603395155fc166de0ac5f80369526e196526ed2)
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_
13
14# unwind/backtrace related
15UNWIND_ENABLE ?= yes
16CFLAGS_UNWIND =
17LDFLAGS_UNWIND =
18RUSTFLAGS_UNWIND =
19ifeq ($(UNWIND_ENABLE), yes)
20    CFLAGS_UNWIND = -funwind-tables
21    LDFLAGS_UNWIND = --eh-frame-hdr
22    RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld
23endif
24
25CFLAGS = $(GLOBAL_CFLAGS) -fno-pie $(CFLAGS_UNWIND) -D $(PIC) -I $(shell pwd) -I $(shell pwd)/include -I $(shell pwd)/arch/x86_64/include
26
27export ASFLAGS := --64
28
29LD_LIST := head.o
30
31
32kernel_subdirs := common driver debug arch exception smp sched syscall ktest libs time
33
34
35head.o: head.S
36	$(CC) -E head.S > _head.s # 预处理
37	$(AS) $(ASFLAGS) -o head.o _head.s
38
39
40main.o: main.c
41# -fno-builtin: 不使用C语言内建函数
42# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture.
43	$(CC) $(CFLAGS) -c main.c  -o main.o
44
45kernel_rust:
46	rustup default nightly
47
48	RUSTFLAGS="$(RUSTFLAGS_UNWIND)" cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json
49all: kernel
50
51	@echo "Linking kernel..."
52	$(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
53# 生成kallsyms
54	current_dir=$(pwd)
55
56	@dbg='debug';for x in $$dbg; do \
57		cd $$x;\
58		$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
59		cd ..;\
60	done
61
62
63# 重新链接
64	@echo "Re-Linking kernel..."
65	@echo $(shell find . -name "*.o")
66	$(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
67	@echo "Generating kernel ELF file..."
68# 生成内核文件
69ifeq ($(UNWIND_ENABLE), yes)
70	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 kernel ../../bin/kernel/kernel.elf
71else
72	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
73endif
74	@echo "Kernel Build Done."
75
76ECHO:
77	@echo "$@"
78
79$(kernel_subdirs): ECHO
80
81	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)" kernel_root_path="$(shell pwd)"
82
83kernel: head.o main.o $(kernel_subdirs) kernel_rust
84
85
86
87clean:
88	cargo clean
89	rm -rf $(GARBAGE)
90	@list='$(kernel_subdirs)'; for subdir in $$list; do \
91		echo "Clean in dir: $$subdir";\
92		cd $$subdir && $(MAKE) clean;\
93		cd .. ;\
94	done
95