1SUBDIRS = kernel user
2
3# ifndef $(EMULATOR)
4ifeq ($(EMULATOR), )
5export EMULATOR=__NO_EMULATION__
6endif
7# todo: 增加参数,判断是否在QEMU中仿真,若是,则启用该环境变量
8# export EMULATOR=__QEMU_EMULATION__
9
10# 计算cpu核心数
11NPROCS:=1
12OS:=$(shell uname -s)
13
14ifeq ($(OS),Linux)
15  NPROCS:=$(shell grep -c ^processor /proc/cpuinfo)
16endif
17ifeq ($(OS),Darwin) # Assume Mac OS X
18  NPROCS:=$(shell system_profiler | awk '/Number Of CPUs/{print $4}{next;}')
19endif
20
21export ARCH=__x86_64__
22export ROOT_PATH=$(shell pwd)
23
24export DEBUG=DEBUG
25export GLOBAL_CFLAGS := -mcmodel=large -fno-builtin -m64  -fno-stack-protector -D $(ARCH) -D $(EMULATOR) -O1
26
27ifeq ($(DEBUG), DEBUG)
28GLOBAL_CFLAGS += -g
29endif
30
31
32export CC=$(DragonOS_GCC)/x86_64-elf-gcc
33export LD=ld
34export AS=$(DragonOS_GCC)/x86_64-elf-as
35export NM=$(DragonOS_GCC)/x86_64-elf-nm
36export AR=$(DragonOS_GCC)/x86_64-elf-ar
37export OBJCOPY=$(DragonOS_GCC)/x86_64-elf-objcopy
38
39
40.PHONY: all
41all: kernel user
42
43
44.PHONY: kernel
45kernel:
46	mkdir -p bin/kernel/
47	@if [ -z $$DragonOS_GCC ]; then echo "\033[31m  [错误]尚未安装DragonOS交叉编译器, 请使用tools文件夹下的build_gcc_toolchain.sh脚本安装  \033[0m"; exit 1; fi
48	$(MAKE) -C ./kernel all || (sh -c "echo 内核编译失败" && exit 1)
49
50.PHONY: user
51user:
52
53	@if [ -z $$DragonOS_GCC ]; then echo "\033[31m  [错误]尚未安装DragonOS交叉编译器, 请使用tools文件夹下的build_gcc_toolchain.sh脚本安装  \033[0m"; exit 1; fi
54	$(MAKE) -C ./user all || (sh -c "echo 用户程序编译失败" && exit 1)
55
56.PHONY: clean
57clean:
58	@list='$(SUBDIRS)'; for subdir in $$list; do \
59		echo "Clean in dir: $$subdir";\
60		cd $$subdir && $(MAKE) clean;\
61		cd .. ;\
62	done
63
64cppcheck-xml:
65	cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) --xml 2> cppcheck.xml
66
67cppcheck:
68	cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS)
69
70gdb:
71	gdb -n -x tools/.gdbinit
72
73# 写入磁盘镜像
74write_diskimage:
75	bash -c "cd tools && bash grub_auto_install.sh && sudo bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=legacy && cd .."
76
77# 写入磁盘镜像(uefi)
78write_diskimage-uefi:
79	bash -c "cd tools && bash grub_auto_install.sh && sudo bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=uefi && cd .."
80# 不编译,直接启动QEMU
81qemu:
82	sh -c "cd tools && bash run-qemu.sh --bios=legacy && cd .."
83# 不编译,直接启动QEMU(UEFI)
84qemu-uefi:
85	sh -c "cd tools && bash run-qemu.sh --bios=uefi && cd .."
86
87# 编译并写入磁盘镜像
88build:
89	$(MAKE) all -j $(NPROCS)
90	$(MAKE) write_diskimage || exit 1
91
92# 在docker中编译,并写入磁盘镜像
93docker:
94	@echo "使用docker构建"
95	sudo bash tools/build_in_docker.sh || exit 1
96	$(MAKE) write_diskimage || exit 1
97
98# uefi方式启动
99run-uefi:
100	$(MAKE) all -j $(NPROCS)
101	$(MAKE) write_diskimage-uefi || exit 1
102	$(MAKE) qemu-uefi
103
104# 编译并启动QEMU
105run:
106	$(MAKE) all -j $(NPROCS)
107	$(MAKE) write_diskimage || exit 1
108	$(MAKE) qemu
109
110# 在docker中编译,并启动QEMU
111run-docker:
112	@echo "使用docker构建并运行"
113	sudo bash tools/build_in_docker.sh || exit 1
114	$(MAKE) write_diskimage || exit 1
115	$(MAKE) qemu
116