xref: /DragonOS/kernel/src/Makefile (revision 081428c0d832cde99834cf8f94a0f2a1f41c9704)
12813126eSloginSUBDIR_ROOTS := .
22813126eSloginDIRS := . $(shell find $(SUBDIR_ROOTS) -type d)
32813126eSloginGARBAGE_PATTERNS := *.o *.s~ *.s *.S~ *.c~ *.h~ kernel
42813126eSloginGARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS)))
52813126eSlogin
6fb6c29d0SloginDIR_LIB=libs
72813126eSloginlib_patterns := *.a
82813126eSloginLIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns)))
92813126eSlogin
102813126eSlogin
11d470019bSLoGin# unwind/backtrace related
12d470019bSLoGinUNWIND_ENABLE ?= yes
13d470019bSLoGinCFLAGS_UNWIND =
14d470019bSLoGinLDFLAGS_UNWIND =
15d470019bSLoGinRUSTFLAGS_UNWIND =
16d470019bSLoGinifeq ($(UNWIND_ENABLE), yes)
17d470019bSLoGin	CFLAGS_UNWIND = -funwind-tables
18d470019bSLoGin	LDFLAGS_UNWIND = --eh-frame-hdr
19d470019bSLoGin	RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld
20d470019bSLoGinendif
21d470019bSLoGin
22bd70d2d1SLoGinRUSTFLAGS += $(RUSTFLAGS_UNWIND)
234fda81ceSLoGin
244fda81ceSLoGinCFLAGS = $(GLOBAL_CFLAGS) -fno-pie $(CFLAGS_UNWIND) -I $(shell pwd) -I $(shell pwd)/include
254fda81ceSLoGin
264fda81ceSLoGinifeq ($(ARCH), x86_64)
274fda81ceSLoGin	CFLAGS +=  -I $(shell pwd)/arch/x86_64/include
284fda81ceSLoGinelse ifeq ($(ARCH), riscv64)
2901090de7SLoGin	CFLAGS +=  -I $(shell pwd)/arch/riscv64/include -I $(shell pwd)/arch/riscv64/
304fda81ceSLoGinendif
312813126eSlogin
322813126eSloginexport ASFLAGS := --64
332813126eSlogin
3411f78b73SLoGinLD_LIST := ""
352813126eSlogin
362813126eSlogin
37415e46eaSLoGinkernel_subdirs := debug
382813126eSlogin
392813126eSlogin
4082d2e446Sloginkernel_rust:
417c28051eSlinfeng	RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2024-11-05 $(CARGO_ZBUILD) build --release --target $(TARGET_JSON)
421a72a751SLoGin
4370a4e555SLoGin
44fb6c29d0Sloginall: kernel
45fb6c29d0Slogin
464fda81ceSLoGin# if x86_64
474fda81ceSLoGinifeq ($(ARCH), x86_64)
484fda81ceSLoGin	$(MAKE) __link_x86_64_kernel
494fda81ceSLoGinelse ifeq ($(ARCH), riscv64)
504fda81ceSLoGin	$(MAKE) __link_riscv64_kernel
514fda81ceSLoGinendif
524fda81ceSLoGin
534fda81ceSLoGin	@echo "Kernel Build Done."
544fda81ceSLoGin
554fda81ceSLoGinECHO:
564fda81ceSLoGin	@echo "$@"
574fda81ceSLoGin
584fda81ceSLoGin$(kernel_subdirs): ECHO
594fda81ceSLoGin	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)"  kernel_root_path="$(shell pwd)"
604fda81ceSLoGin
614fda81ceSLoGinkernel: $(kernel_subdirs) kernel_rust
624fda81ceSLoGin
634fda81ceSLoGin__link_riscv64_kernel:
642813126eSlogin	@echo "Linking kernel..."
6523ef2b33SLoGin	$(LD) -b elf64-littleriscv -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/riscv64gc-unknown-none-elf/release/libdragonos_kernel.a -T arch/riscv64/link.ld --no-relax
66*081428c0Slinfeng	# 生成kallsyms
67*081428c0Slinfeng	current_dir=$(pwd)
68*081428c0Slinfeng
69*081428c0Slinfeng	@dbg='debug';for x in $$dbg; do \
70*081428c0Slinfeng		cd $$x;\
71*081428c0Slinfeng		$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
72*081428c0Slinfeng		cd ..;\
73*081428c0Slinfeng	done
74*081428c0Slinfeng
75*081428c0Slinfeng# 重新链接
76*081428c0Slinfeng	@echo "Re-Linking kernel..."
77*081428c0Slinfeng	@echo $(shell find . -name "*.o")
78*081428c0Slinfeng	$(LD) -b elf64-littleriscv -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/riscv64gc-unknown-none-elf/release/libdragonos_kernel.a ./debug/kallsyms.o  -T arch/riscv64/link.ld --no-relax
79*081428c0Slinfeng	@echo "Generating kernel ELF file..."
80*081428c0Slinfeng
81*081428c0Slinfengifeq ($(UNWIND_ENABLE), yes)
82*081428c0Slinfeng	$(OBJCOPY) -I elf64-littleriscv -O elf64-littleriscv kernel ../../bin/kernel/kernel.elf
83*081428c0Slinfengelse
844fda81ceSLoGin	$(OBJCOPY) -I elf64-littleriscv -O elf64-littleriscv -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
85*081428c0Slinfengendif
8601090de7SLoGin	@rm kernel
8701090de7SLoGin	$(MAKE) __dragon_stub PAYLOAD_ELF="$(shell pwd)/../../bin/kernel/kernel.elf"
884fda81ceSLoGin
894fda81ceSLoGin
904fda81ceSLoGin
914fda81ceSLoGin__link_x86_64_kernel:
924fda81ceSLoGin	@echo "Linking kernel..."
934fda81ceSLoGin	$(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
942813126eSlogin# 生成kallsyms
952813126eSlogin	current_dir=$(pwd)
962813126eSlogin
972813126eSlogin	@dbg='debug';for x in $$dbg; do \
982813126eSlogin		cd $$x;\
99c757940bSYJwu2023		$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
1002813126eSlogin		cd ..;\
1012813126eSlogin	done
1022813126eSlogin
1032813126eSlogin# 重新链接
1042813126eSlogin	@echo "Re-Linking kernel..."
105fb6c29d0Slogin	@echo $(shell find . -name "*.o")
1064fda81ceSLoGin	$(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
1072813126eSlogin	@echo "Generating kernel ELF file..."
1082813126eSlogin# 生成内核文件
109d470019bSLoGinifeq ($(UNWIND_ENABLE), yes)
11040fe15e0SLoGin	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 kernel ../../bin/kernel/kernel.elf
111d470019bSLoGinelse
112d470019bSLoGin	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
113d470019bSLoGinendif
1144fda81ceSLoGin	rm kernel
1152813126eSlogin
11601090de7SLoGin__dragon_stub:
11701090de7SLoGin	@echo "Linking dragon_stub..."
118af354310SLoGin	@mkdir -p $(ROOT_PATH)/bin/sysroot
119af354310SLoGin	PAYLOAD_ELF=$(PAYLOAD_ELF) TARGET_SYSROOT=$(ROOT_PATH)/bin/sysroot $(MAKE) -C $(ROOT_PATH)/kernel/submodules/DragonStub install -j $(NPROCS)
12001090de7SLoGin
12101090de7SLoGin
1222813126eSloginclean:
1234fda81ceSLoGin	@cargo clean
1242813126eSlogin	rm -rf $(GARBAGE)
1252813126eSlogin	@list='$(kernel_subdirs)'; for subdir in $$list; do \
1262813126eSlogin		echo "Clean in dir: $$subdir";\
1272813126eSlogin		cd $$subdir && $(MAKE) clean;\
1282813126eSlogin		cd .. ;\
1292813126eSlogin	done
130