xref: /DragonOS/Makefile (revision d031d46fd9e9a62e8e975dba76e3bdef027f63b6)
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
46	$(MAKE) -C ./kernel all ARCH=$(ARCH) || (sh -c "echo 内核编译失败" && exit 1)
47
48.PHONY: user
49user:
50	$(MAKE) -C ./user all ARCH=$(ARCH) || (sh -c "echo 用户程序编译失败" && exit 1)
51
52.PHONY: clean
53clean:
54	@list='$(SUBDIRS)'; for subdir in $$list; do \
55		echo "Clean in dir: $$subdir";\
56		cd $$subdir && $(MAKE) clean;\
57		cd .. ;\
58	done
59
60.PHONY: ECHO
61ECHO:
62	@echo "$@"
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
70docs: ECHO
71	bash -c "cd docs && make html && cd .."
72
73clean-docs:
74	bash -c "cd docs && make clean && cd .."
75
76gdb:
77ifeq ($(ARCH), x86_64)
78	rust-gdb -n -x tools/.gdbinit
79else
80	gdb-multiarch -n -x tools/.gdbinit
81endif
82
83# 写入磁盘镜像
84write_diskimage:
85	@echo "write_diskimage arch=$(ARCH)"
86	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 .."
87
88# 写入磁盘镜像(uefi)
89write_diskimage-uefi:
90	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 .."
91# 不编译,直接启动QEMU
92qemu:
93	sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=window && cd .."
94
95# 不编译,直接启动QEMU,不显示图像
96qemu-nographic:
97	sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=nographic && cd .."
98
99# 不编译,直接启动QEMU(UEFI)
100qemu-uefi:
101	sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=window && cd .."
102# 不编译,直接启动QEMU,使用VNC Display作为图像输出
103qemu-vnc:
104	sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=vnc && cd .."
105# 不编译,直接启动QEMU(UEFI),使用VNC Display作为图像输出
106qemu-uefi-vnc:
107	sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=vnc && cd .."
108
109# 编译并写入磁盘镜像
110build:
111	$(MAKE) all -j $(NPROCS)
112	$(MAKE) write_diskimage || exit 1
113
114# 在docker中编译,并写入磁盘镜像
115docker:
116	@echo "使用docker构建"
117	sudo bash tools/build_in_docker.sh || exit 1
118	$(MAKE) write_diskimage || exit 1
119
120# uefi方式启动
121run-uefi:
122	$(MAKE) all -j $(NPROCS)
123	$(MAKE) write_diskimage-uefi || exit 1
124	$(MAKE) qemu-uefi
125
126# 编译并启动QEMU
127run:
128	$(MAKE) all -j $(NPROCS)
129	$(MAKE) write_diskimage || exit 1
130	$(MAKE) qemu
131
132# uefi方式启动,使用VNC Display作为图像输出
133run-uefi-vnc:
134	$(MAKE) all -j $(NPROCS)
135	$(MAKE) write_diskimage-uefi || exit 1
136	$(MAKE) qemu-uefi-vnc
137
138# 编译并启动QEMU,使用VNC Display作为图像输出
139run-vnc:
140	$(MAKE) all -j $(NPROCS)
141	$(MAKE) write_diskimage || exit 1
142	$(MAKE) qemu-vnc
143
144# 在docker中编译,并启动QEMU
145run-docker:
146	@echo "使用docker构建并运行"
147	sudo bash tools/build_in_docker.sh || exit 1
148	$(MAKE) write_diskimage || exit 1
149	$(MAKE) qemu
150
151fmt:
152	@echo "格式化代码"
153	FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C kernel
154	FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C user
155	FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C build-scripts
156
157log-monitor:
158	@echo "启动日志监控"
159	@sh -c "cd tools/debugging/logmonitor && cargo run --release -- --log-dir $(ROOT_PATH)/logs/ --kernel $(ROOT_PATH)/bin/kernel/kernel.elf"
160
161.PHONY: update-submodules
162update-submodules:
163	@echo "更新子模块"
164	@git submodule update --recursive --init
165	@git submodule foreach git pull origin master
166
167.PHONY: update-submodules-by-mirror
168update-submodules-by-mirror:
169	@echo "从镜像更新子模块"
170	@git config --global url."https://git.mirrors.dragonos.org.cn/DragonOS-Community/".insteadOf https://github.com/DragonOS-Community/
171	@$(MAKE) update-submodules
172	@git config --global --unset url."https://git.mirrors.dragonos.org.cn/DragonOS-Community/".insteadOf
173
174help:
175	@echo "编译:"
176	@echo "  make all -j <n>       - 本地编译,不运行,n为要用于编译的CPU核心数"
177	@echo "  make build            - 本地编译,并写入磁盘镜像"
178	@echo "  make docker           - Docker编译,并写入磁盘镜像"
179	@echo ""
180	@echo "编译并运行:"
181	@echo "  make run-docker       - Docker编译,写入磁盘镜像,并在QEMU中运行"
182	@echo "  make run              - 本地编译,写入磁盘镜像,并在QEMU中运行"
183	@echo "  make run-uefi         - 以uefi方式启动运行"
184	@echo ""
185	@echo "运行:"
186	@echo "  make qemu             - 不编译,直接从已有的磁盘镜像启动运行"
187	@echo "  make qemu-uefi        - 不编译,直接从已有的磁盘镜像以UEFI启动运行"
188	@echo ""
189	@echo ""
190	@echo "注: 对于上述的run, run-uefi, qemu, qemu-uefi命令可以在命令后加上-vnc后缀,来通过vnc连接到DragonOS, 默认会在5900端口运行vnc服务器。如:make run-vnc "
191	@echo ""
192	@echo "其他:"
193	@echo "  make clean            - 清理编译产生的文件"
194	@echo "  make fmt              - 格式化代码"
195	@echo "  make log-monitor      - 启动日志监控"
196	@echo "  make docs             - 生成文档"
197	@echo "  make clean-docs       - 清理文档"
198	@echo ""
199	@echo "  make update-submodules - 更新子模块"
200	@echo "  make update-submodules-by-mirror - 从镜像更新子模块"
201
202