xref: /DragonOS/Makefile (revision 36fd013004ee0bd5fc7cfb452ba22531a83a859c)
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 --display=window && cd .."
83# 不编译,直接启动QEMU(UEFI)
84qemu-uefi:
85	sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=window && cd .."
86# 不编译,直接启动QEMU,使用VNC Display作为图像输出
87qemu-vnc:
88	sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=vnc && cd .."
89# 不编译,直接启动QEMU(UEFI),使用VNC Display作为图像输出
90qemu-uefi-vnc:
91	sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=vnc && cd .."
92
93# 编译并写入磁盘镜像
94build:
95	$(MAKE) all -j $(NPROCS)
96	$(MAKE) write_diskimage || exit 1
97
98# 在docker中编译,并写入磁盘镜像
99docker:
100	@echo "使用docker构建"
101	sudo bash tools/build_in_docker.sh || exit 1
102	$(MAKE) write_diskimage || exit 1
103
104# uefi方式启动
105run-uefi:
106	$(MAKE) all -j $(NPROCS)
107	$(MAKE) write_diskimage-uefi || exit 1
108	$(MAKE) qemu-uefi
109
110# 编译并启动QEMU
111run:
112	$(MAKE) all -j $(NPROCS)
113	$(MAKE) write_diskimage || exit 1
114	$(MAKE) qemu
115
116# uefi方式启动,使用VNC Display作为图像输出
117run-uefi-vnc:
118	$(MAKE) all -j $(NPROCS)
119	$(MAKE) write_diskimage-uefi || exit 1
120	$(MAKE) qemu-uefi-vnc
121
122# 编译并启动QEMU,使用VNC Display作为图像输出
123run-vnc:
124	$(MAKE) all -j $(NPROCS)
125	$(MAKE) write_diskimage || exit 1
126	$(MAKE) qemu-vnc
127
128# 在docker中编译,并启动QEMU
129run-docker:
130	@echo "使用docker构建并运行"
131	sudo bash tools/build_in_docker.sh || exit 1
132	$(MAKE) write_diskimage || exit 1
133	$(MAKE) qemu
134
135help:
136	@echo "编译:"
137	@echo "  make all -j <n>       - 本地编译,不运行,n为要用于编译的CPU核心数"
138	@echo "  make build            - 本地编译,并写入磁盘镜像"
139	@echo "  make docker           - Docker编译,并写入磁盘镜像"
140	@echo ""
141	@echo "编译并运行:"
142	@echo "  make run-docker       - Docker编译,写入磁盘镜像,并在QEMU中运行"
143	@echo "  make run              - 本地编译,写入磁盘镜像,并在QEMU中运行"
144	@echo "  make run-uefi         - 以uefi方式启动运行"
145	@echo ""
146	@echo "运行:"
147	@echo "  make qemu             - 不编译,直接从已有的磁盘镜像启动运行"
148	@echo "  make qemu-uefi        - 不编译,直接从已有的磁盘镜像以UEFI启动运行"
149	@echo ""
150	@echo ""
151	@echo "注: 对于上述的run, run-uefi, qemu, qemu-uefi命令可以在命令后加上-vnc后缀,来通过vnc连接到DragonOS, 默认会在5900端口运行vnc服务器。如:make run-vnc "
152