xref: /DragonOS/kernel/src/Makefile (revision 4fda81ce81939d83b74c8042d6fb4223deff3685)
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
20endif
21    RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld
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
32endif
33
34export ASFLAGS := --64
35
36LD_LIST := ""
37
38
39kernel_subdirs := common driver debug exception smp syscall ktest libs time
40
41
42kernel_rust:
43ifeq ($(ARCH), riscv64)
44	RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2023-01-21 $(CARGO_ZBUILD) build --release --target riscv64imac-unknown-none-elf
45else
46	RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2023-01-21 $(CARGO_ZBUILD) build --release --target $(TARGET_JSON)
47endif
48
49all: kernel
50
51# if x86_64
52ifeq ($(ARCH), x86_64)
53	$(MAKE) __link_x86_64_kernel
54else ifeq ($(ARCH), riscv64)
55	$(MAKE) __link_riscv64_kernel
56endif
57
58	@echo "Kernel Build Done."
59
60ECHO:
61	@echo "$@"
62
63$(kernel_subdirs): ECHO
64
65	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)"  kernel_root_path="$(shell pwd)"
66
67kernel: $(kernel_subdirs) kernel_rust
68
69__link_riscv64_kernel:
70	@echo "Linking kernel..."
71	$(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
72	$(OBJCOPY) -I elf64-littleriscv -O elf64-littleriscv -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
73	rm kernel
74
75# 构建uboot的scr文件
76	@echo "Generating kernel scr file..."
77	@mkdir -p $(ROOT_PATH)/bin/sysroot/boot/
78	@mkimage -A riscv -T script -d arch/riscv64/boot/bootscript.cmd $(ROOT_PATH)/bin/sysroot/boot/boot.scr
79
80
81__link_x86_64_kernel:
82	@echo "Linking kernel..."
83	$(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
84# 生成kallsyms
85	current_dir=$(pwd)
86
87	@dbg='debug';for x in $$dbg; do \
88		cd $$x;\
89		$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
90		cd ..;\
91	done
92
93
94# 重新链接
95	@echo "Re-Linking kernel..."
96	@echo $(shell find . -name "*.o")
97	$(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
98	@echo "Generating kernel ELF file..."
99# 生成内核文件
100ifeq ($(UNWIND_ENABLE), yes)
101	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 kernel ../../bin/kernel/kernel.elf
102else
103	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
104endif
105	rm kernel
106
107clean:
108	@cargo clean
109	rm -rf $(GARBAGE)
110	@list='$(kernel_subdirs)'; for subdir in $$list; do \
111		echo "Clean in dir: $$subdir";\
112		cd $$subdir && $(MAKE) clean;\
113		cd .. ;\
114	done
115