xref: /DragonOS/Makefile (revision dcf232f378b36fad754799fc121a70cadc8d5cb3)
1# 导入环境变量
2include env.mk
3
4
5export ROOT_PATH=$(shell pwd)
6
7SUBDIRS = kernel user tools build-scripts
8
9
10
11# todo: 增加参数,判断是否在QEMU中仿真,若是,则启用该环境变量
12# export EMULATOR=__QEMU_EMULATION__
13
14# 计算cpu核心数
15NPROCS:=1
16OS:=$(shell uname -s)
17
18ifeq ($(OS),Linux)
19  NPROCS:=$(shell grep -c ^processor /proc/cpuinfo)
20endif
21ifeq ($(OS),Darwin) # Assume Mac OS X
22  NPROCS:=$(shell system_profiler | awk '/Number Of CPUs/{print $4}{next;}')
23endif
24
25
26
27# 检查是否需要进行fmt --check
28# 解析命令行参数
29FMT_CHECK?=0
30
31ifeq ($(FMT_CHECK), 1)
32	FMT_CHECK=--check
33else
34	FMT_CHECK=
35endif
36
37
38.PHONY: all
39all: kernel user
40
41
42.PHONY: kernel
43kernel:
44	mkdir -p bin/kernel/
45	@if [ -z $$DragonOS_GCC ]; then echo "\033[31m  [错误]尚未安装DragonOS交叉编译器, 请使用tools文件夹下的build_gcc_toolchain.sh脚本安装  \033[0m"; exit 1; fi
46	$(MAKE) -C ./kernel all ARCH=$(ARCH) || (sh -c "echo 内核编译失败" && exit 1)
47
48.PHONY: user
49user:
50
51	@if [ -z $$DragonOS_GCC ]; then echo "\033[31m  [错误]尚未安装DragonOS交叉编译器, 请使用tools文件夹下的build_gcc_toolchain.sh脚本安装  \033[0m"; exit 1; fi
52	$(MAKE) -C ./user all ARCH=$(ARCH) || (sh -c "echo 用户程序编译失败" && exit 1)
53
54.PHONY: clean
55clean:
56	@list='$(SUBDIRS)'; for subdir in $$list; do \
57		echo "Clean in dir: $$subdir";\
58		cd $$subdir && $(MAKE) clean;\
59		cd .. ;\
60	done
61
62.PHONY: ECHO
63ECHO:
64	@echo "$@"
65
66cppcheck-xml:
67	cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) --xml 2> cppcheck.xml
68
69cppcheck:
70	cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS)
71
72docs: ECHO
73	bash -c "cd docs && make html && cd .."
74
75clean-docs:
76	bash -c "cd docs && make clean && cd .."
77
78gdb:
79ifeq ($(ARCH), x86_64)
80	rust-gdb -n -x tools/.gdbinit
81else
82	gdb-multiarch -n -x tools/.gdbinit
83endif
84
85# 写入磁盘镜像
86write_diskimage:
87	@echo "write_diskimage arch=$(ARCH)"
88	bash -c "export ARCH=$(ARCH); cd tools && bash grub_auto_install.sh && sudo ARCH=$(ARCH) bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=legacy && cd .."
89
90# 写入磁盘镜像(uefi)
91write_diskimage-uefi:
92	bash -c "export ARCH=$(ARCH); cd tools && bash grub_auto_install.sh && sudo ARCH=$(ARCH) bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=uefi && cd .."
93# 不编译,直接启动QEMU
94qemu:
95	sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=window && cd .."
96# 不编译,直接启动QEMU(UEFI)
97qemu-uefi:
98	sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=window && cd .."
99# 不编译,直接启动QEMU,使用VNC Display作为图像输出
100qemu-vnc:
101	sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=vnc && cd .."
102# 不编译,直接启动QEMU(UEFI),使用VNC Display作为图像输出
103qemu-uefi-vnc:
104	sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=vnc && cd .."
105
106# 编译并写入磁盘镜像
107build:
108	$(MAKE) all -j $(NPROCS)
109	$(MAKE) write_diskimage || exit 1
110
111# 在docker中编译,并写入磁盘镜像
112docker:
113	@echo "使用docker构建"
114	sudo bash tools/build_in_docker.sh || exit 1
115	$(MAKE) write_diskimage || exit 1
116
117# uefi方式启动
118run-uefi:
119	$(MAKE) all -j $(NPROCS)
120	$(MAKE) write_diskimage-uefi || exit 1
121	$(MAKE) qemu-uefi
122
123# 编译并启动QEMU
124run:
125	$(MAKE) all -j $(NPROCS)
126	$(MAKE) write_diskimage || exit 1
127	$(MAKE) qemu
128
129# uefi方式启动,使用VNC Display作为图像输出
130run-uefi-vnc:
131	$(MAKE) all -j $(NPROCS)
132	$(MAKE) write_diskimage-uefi || exit 1
133	$(MAKE) qemu-uefi-vnc
134
135# 编译并启动QEMU,使用VNC Display作为图像输出
136run-vnc:
137	$(MAKE) all -j $(NPROCS)
138	$(MAKE) write_diskimage || exit 1
139	$(MAKE) qemu-vnc
140
141# 在docker中编译,并启动QEMU
142run-docker:
143	@echo "使用docker构建并运行"
144	sudo bash tools/build_in_docker.sh || exit 1
145	$(MAKE) write_diskimage || exit 1
146	$(MAKE) qemu
147
148fmt:
149	@echo "格式化代码"
150	FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C kernel
151	FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C user
152	FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C build-scripts
153
154log-monitor:
155	@echo "启动日志监控"
156	@sh -c "cd tools/debugging/logmonitor && cargo run --release -- --log-dir $(ROOT_PATH)/logs/ --kernel $(ROOT_PATH)/bin/kernel/kernel.elf"
157
158.PHONY: update-submodules
159update-submodules:
160	@echo "更新子模块"
161	@git submodule update --recursive
162	@git submodule foreach git pull origin master
163
164.PHONY: update-submodules-by-mirror
165update-submodules-by-mirror:
166	@echo "从镜像更新子模块"
167	@git config --global url."https://git.mirrors.dragonos.org.cn/DragonOS-Community/".insteadOf https://github.com/DragonOS-Community/
168	@$(MAKE) update-submodules
169	@git config --global --unset url."https://git.mirrors.dragonos.org.cn/DragonOS-Community/".insteadOf
170
171help:
172	@echo "编译:"
173	@echo "  make all -j <n>       - 本地编译,不运行,n为要用于编译的CPU核心数"
174	@echo "  make build            - 本地编译,并写入磁盘镜像"
175	@echo "  make docker           - Docker编译,并写入磁盘镜像"
176	@echo ""
177	@echo "编译并运行:"
178	@echo "  make run-docker       - Docker编译,写入磁盘镜像,并在QEMU中运行"
179	@echo "  make run              - 本地编译,写入磁盘镜像,并在QEMU中运行"
180	@echo "  make run-uefi         - 以uefi方式启动运行"
181	@echo ""
182	@echo "运行:"
183	@echo "  make qemu             - 不编译,直接从已有的磁盘镜像启动运行"
184	@echo "  make qemu-uefi        - 不编译,直接从已有的磁盘镜像以UEFI启动运行"
185	@echo ""
186	@echo ""
187	@echo "注: 对于上述的run, run-uefi, qemu, qemu-uefi命令可以在命令后加上-vnc后缀,来通过vnc连接到DragonOS, 默认会在5900端口运行vnc服务器。如:make run-vnc "
188	@echo ""
189	@echo "其他:"
190	@echo "  make clean            - 清理编译产生的文件"
191	@echo "  make fmt              - 格式化代码"
192	@echo "  make log-monitor      - 启动日志监控"
193	@echo "  make docs             - 生成文档"
194	@echo "  make clean-docs       - 清理文档"
195	@echo ""
196	@echo "  make update-submodules - 更新子模块"
197	@echo "  make update-submodules-by-mirror - 从镜像更新子模块"
198
199