xref: /DragonOS/kernel/src/Makefile (revision c757940bd61b0125e037a59eb77565e42470201b)
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
112813126eSlogin# 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_
122813126eSloginPIC := _INTR_APIC_
13cffd7144SloginCFLAGS = $(GLOBAL_CFLAGS) -D $(PIC) -I $(shell pwd) -I $(shell pwd)/include -I $(shell pwd)/arch/x86_64/include
142813126eSlogin
152813126eSloginexport ASFLAGS := --64
162813126eSlogin
172813126eSloginLD_LIST := head.o
182813126eSlogin
192813126eSlogin
20ddb9d917SXshinekernel_subdirs := common driver process debug arch exception smp sched syscall ktest libs ipc time
212813126eSlogin
222813126eSlogin
232813126eSlogin
242813126eSloginhead.o: head.S
252813126eSlogin	$(CC) -E head.S > _head.s # 预处理
265ed4cd46Sguanjinquan	$(AS) $(ASFLAGS) -o head.o _head.s
272813126eSlogin
282813126eSlogin
292813126eSloginmain.o: main.c
302813126eSlogin# -fno-builtin: 不使用C语言内建函数
312813126eSlogin# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture.
322813126eSlogin	$(CC) $(CFLAGS) -c main.c  -o main.o
332813126eSlogin
3482d2e446Sloginkernel_rust:
352813126eSlogin	rustup default nightly
362311e2f3S櫻井桃華	cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json
37fb6c29d0Slogin
38fb6c29d0Sloginall: kernel
39fb6c29d0Slogin
402813126eSlogin	@echo "Linking kernel..."
41e92d0228SLoGin	$(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
422813126eSlogin# 生成kallsyms
432813126eSlogin	current_dir=$(pwd)
442813126eSlogin
452813126eSlogin	@dbg='debug';for x in $$dbg; do \
462813126eSlogin		cd $$x;\
47*c757940bSYJwu2023		$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
482813126eSlogin		cd ..;\
492813126eSlogin	done
502813126eSlogin
512813126eSlogin
522813126eSlogin# 重新链接
532813126eSlogin	@echo "Re-Linking kernel..."
54fb6c29d0Slogin	@echo $(shell find . -name "*.o")
55e92d0228SLoGin	$(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
562813126eSlogin	@echo "Generating kernel ELF file..."
572813126eSlogin# 生成内核文件
5840fe15e0SLoGin	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64  kernel ../../bin/kernel/kernel.elf
5940fe15e0SLoGin# $(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
602813126eSlogin	@echo "Kernel Build Done."
612813126eSlogin
622813126eSloginECHO:
632813126eSlogin	@echo "$@"
642813126eSlogin
652813126eSlogin$(kernel_subdirs): ECHO
662813126eSlogin
672813126eSlogin	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)" kernel_root_path="$(shell pwd)"
682813126eSlogin
69fb6c29d0Sloginkernel: head.o main.o $(kernel_subdirs) kernel_rust
702813126eSlogin
712813126eSlogin
722813126eSlogin
732813126eSloginclean:
742813126eSlogin	cargo clean
752813126eSlogin	rm -rf $(GARBAGE)
762813126eSlogin	@list='$(kernel_subdirs)'; for subdir in $$list; do \
772813126eSlogin		echo "Clean in dir: $$subdir";\
782813126eSlogin		cd $$subdir && $(MAKE) clean;\
792813126eSlogin		cd .. ;\
802813126eSlogin	done