xref: /DragonOS/kernel/src/Makefile (revision 40fe15e0953f989ccfeb74826d61621d43dea6bb)
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
20*40fe15e0SLoGinkernel_subdirs := common driver process debug arch exception smp sched syscall ktest libs ipc io 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..."
415ed4cd46Sguanjinquan	$(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;\
472813126eSlogin		$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)";\
482813126eSlogin		cd ..;\
492813126eSlogin		if [ "$$?" != "0" ]; then\
502813126eSlogin            exit $$?;\
512813126eSlogin        fi;\
522813126eSlogin	done
532813126eSlogin
542813126eSlogin
552813126eSlogin# 重新链接
562813126eSlogin	@echo "Re-Linking kernel..."
57fb6c29d0Slogin	@echo $(shell find . -name "*.o")
585ed4cd46Sguanjinquan	$(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
592813126eSlogin	@echo "Generating kernel ELF file..."
602813126eSlogin# 生成内核文件
61*40fe15e0SLoGin	$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64  kernel ../../bin/kernel/kernel.elf
62*40fe15e0SLoGin# $(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
632813126eSlogin	@echo "Kernel Build Done."
642813126eSlogin
652813126eSloginECHO:
662813126eSlogin	@echo "$@"
672813126eSlogin
682813126eSlogin$(kernel_subdirs): ECHO
692813126eSlogin
702813126eSlogin	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)" kernel_root_path="$(shell pwd)"
712813126eSlogin
72fb6c29d0Sloginkernel: head.o main.o $(kernel_subdirs) kernel_rust
732813126eSlogin
742813126eSlogin
752813126eSlogin
762813126eSloginclean:
772813126eSlogin	cargo clean
782813126eSlogin	rm -rf $(GARBAGE)
792813126eSlogin	@list='$(kernel_subdirs)'; for subdir in $$list; do \
802813126eSlogin		echo "Clean in dir: $$subdir";\
812813126eSlogin		cd $$subdir && $(MAKE) clean;\
822813126eSlogin		cd .. ;\
832813126eSlogin	done