xref: /DragonOS/Makefile (revision c75089286e9d49cef8d039446bf570c1bd4d2550)
1e26ca418SLoGinSUBDIRS = kernel user tools build-scripts
20aec6827Sfslongjin
3416a802bSfslongjin# ifndef $(EMULATOR)
4416a802bSfslongjinifeq ($(EMULATOR), )
5416a802bSfslongjinexport EMULATOR=__NO_EMULATION__
6416a802bSfslongjinendif
72813126eSlogin# todo: 增加参数,判断是否在QEMU中仿真,若是,则启用该环境变量
82813126eSlogin# export EMULATOR=__QEMU_EMULATION__
90aec6827Sfslongjin
10d328bfceSlogin# 计算cpu核心数
11d328bfceSloginNPROCS:=1
12d328bfceSloginOS:=$(shell uname -s)
13d328bfceSlogin
14d328bfceSloginifeq ($(OS),Linux)
15d328bfceSlogin  NPROCS:=$(shell grep -c ^processor /proc/cpuinfo)
16d328bfceSloginendif
17d328bfceSloginifeq ($(OS),Darwin) # Assume Mac OS X
18d328bfceSlogin  NPROCS:=$(shell system_profiler | awk '/Number Of CPUs/{print $4}{next;}')
19d328bfceSloginendif
200aec6827Sfslongjin
21*c7508928SLoGin# if arch not defined, set it to x86_64
22*c7508928SLoGinexport ARCH?=x86_64
23*c7508928SLoGin
24*c7508928SLoGinCFLAGS_DEFINE_ARCH="__$(ARCH)__"
25*c7508928SLoGin
26*c7508928SLoGin
270aec6827Sfslongjinexport ROOT_PATH=$(shell pwd)
280aec6827Sfslongjin
290aec6827Sfslongjinexport DEBUG=DEBUG
30*c7508928SLoGinexport GLOBAL_CFLAGS := -mcmodel=large -fno-builtin -m64  -fno-stack-protector -D $(CFLAGS_DEFINE_ARCH) -D $(EMULATOR) -O1
310aec6827Sfslongjin
320aec6827Sfslongjinifeq ($(DEBUG), DEBUG)
330aec6827SfslongjinGLOBAL_CFLAGS += -g
340aec6827Sfslongjinendif
35f479f321Sfslongjin
365ed4cd46Sguanjinquan
375ed4cd46Sguanjinquanexport CC=$(DragonOS_GCC)/x86_64-elf-gcc
385ed4cd46Sguanjinquanexport LD=ld
395ed4cd46Sguanjinquanexport AS=$(DragonOS_GCC)/x86_64-elf-as
405ed4cd46Sguanjinquanexport NM=$(DragonOS_GCC)/x86_64-elf-nm
41d4f3de93Sloginexport AR=$(DragonOS_GCC)/x86_64-elf-ar
425ed4cd46Sguanjinquanexport OBJCOPY=$(DragonOS_GCC)/x86_64-elf-objcopy
435ed4cd46Sguanjinquan
44e92d0228SLoGin# 检查是否需要进行fmt --check
45e92d0228SLoGin# 解析命令行参数
46e92d0228SLoGinFMT_CHECK?=0
47e92d0228SLoGin
48e92d0228SLoGinifeq ($(FMT_CHECK), 1)
49e92d0228SLoGin	FMT_CHECK=--check
50e92d0228SLoGinelse
51e92d0228SLoGin	FMT_CHECK=
52e92d0228SLoGinendif
53e92d0228SLoGin
54416a802bSfslongjin
55f479f321Sfslongjin.PHONY: all
561ab51cb3Sfslongjinall: kernel user
571ab51cb3Sfslongjin
581ab51cb3Sfslongjin
591ab51cb3Sfslongjin.PHONY: kernel
601ab51cb3Sfslongjinkernel:
610b0cce93Sfslongjin	mkdir -p bin/kernel/
6274bde36eSguanjinquan	@if [ -z $$DragonOS_GCC ]; then echo "\033[31m  [错误]尚未安装DragonOS交叉编译器, 请使用tools文件夹下的build_gcc_toolchain.sh脚本安装  \033[0m"; exit 1; fi
632813126eSlogin	$(MAKE) -C ./kernel all || (sh -c "echo 内核编译失败" && exit 1)
64f479f321Sfslongjin
65e2a59dbdSfslongjin.PHONY: user
66e2a59dbdSfslongjinuser:
672224c93eSguanjinquan
6874bde36eSguanjinquan	@if [ -z $$DragonOS_GCC ]; then echo "\033[31m  [错误]尚未安装DragonOS交叉编译器, 请使用tools文件夹下的build_gcc_toolchain.sh脚本安装  \033[0m"; exit 1; fi
692813126eSlogin	$(MAKE) -C ./user all || (sh -c "echo 用户程序编译失败" && exit 1)
70e2a59dbdSfslongjin
71f479f321Sfslongjin.PHONY: clean
72f479f321Sfslongjinclean:
73f479f321Sfslongjin	@list='$(SUBDIRS)'; for subdir in $$list; do \
74f479f321Sfslongjin		echo "Clean in dir: $$subdir";\
7505dc7ac7Sfslongjin		cd $$subdir && $(MAKE) clean;\
76f479f321Sfslongjin		cd .. ;\
77f479f321Sfslongjin	done
78e667fff2Sfslongjin
7940fe15e0SLoGin.PHONY: ECHO
8040fe15e0SLoGinECHO:
8140fe15e0SLoGin	@echo "$@"
8240fe15e0SLoGin
83d328bfceSlogincppcheck-xml:
84d328bfceSlogin	cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) --xml 2> cppcheck.xml
85d328bfceSlogin
86d328bfceSlogincppcheck:
87d328bfceSlogin	cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS)
88d328bfceSlogin
8940fe15e0SLoGindocs: ECHO
9040fe15e0SLoGin	bash -c "cd docs && make html && cd .."
9140fe15e0SLoGin
9240fe15e0SLoGinclean-docs:
9340fe15e0SLoGin	bash -c "cd docs && make clean && cd .."
9440fe15e0SLoGin
95e667fff2Sfslongjingdb:
9640fe15e0SLoGin	rust-gdb -n -x tools/.gdbinit
972813126eSlogin
982813126eSlogin# 写入磁盘镜像
992813126eSloginwrite_diskimage:
100*c7508928SLoGin	@echo "write_diskimage arch=$(ARCH)"
101*c7508928SLoGin	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 .."
1022813126eSlogin
1037f439c5dSYJwu2023# 写入磁盘镜像(uefi)
1047f439c5dSYJwu2023write_diskimage-uefi:
105*c7508928SLoGin	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 .."
1062813126eSlogin# 不编译,直接启动QEMU
1072813126eSloginqemu:
1086d345b77SHoshuChiu	sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=window && cd .."
1097f439c5dSYJwu2023# 不编译,直接启动QEMU(UEFI)
1107f439c5dSYJwu2023qemu-uefi:
1116d345b77SHoshuChiu	sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=window && cd .."
1126d345b77SHoshuChiu# 不编译,直接启动QEMU,使用VNC Display作为图像输出
1136d345b77SHoshuChiuqemu-vnc:
1146d345b77SHoshuChiu	sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=vnc && cd .."
1156d345b77SHoshuChiu# 不编译,直接启动QEMU(UEFI),使用VNC Display作为图像输出
1166d345b77SHoshuChiuqemu-uefi-vnc:
1176d345b77SHoshuChiu	sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=vnc && cd .."
1182813126eSlogin
1192813126eSlogin# 编译并写入磁盘镜像
1202813126eSloginbuild:
1212813126eSlogin	$(MAKE) all -j $(NPROCS)
1222813126eSlogin	$(MAKE) write_diskimage || exit 1
1232813126eSlogin
1242813126eSlogin# 在docker中编译,并写入磁盘镜像
1252813126eSlogindocker:
1262813126eSlogin	@echo "使用docker构建"
1272813126eSlogin	sudo bash tools/build_in_docker.sh || exit 1
1285ed4cd46Sguanjinquan	$(MAKE) write_diskimage || exit 1
1292813126eSlogin
1307f439c5dSYJwu2023# uefi方式启动
1317f439c5dSYJwu2023run-uefi:
1327f439c5dSYJwu2023	$(MAKE) all -j $(NPROCS)
1337f439c5dSYJwu2023	$(MAKE) write_diskimage-uefi || exit 1
1347f439c5dSYJwu2023	$(MAKE) qemu-uefi
1357f439c5dSYJwu2023
1362813126eSlogin# 编译并启动QEMU
1372813126eSloginrun:
1382813126eSlogin	$(MAKE) all -j $(NPROCS)
1392813126eSlogin	$(MAKE) write_diskimage || exit 1
1402813126eSlogin	$(MAKE) qemu
1412813126eSlogin
1426d345b77SHoshuChiu# uefi方式启动,使用VNC Display作为图像输出
1436d345b77SHoshuChiurun-uefi-vnc:
1446d345b77SHoshuChiu	$(MAKE) all -j $(NPROCS)
1456d345b77SHoshuChiu	$(MAKE) write_diskimage-uefi || exit 1
1466d345b77SHoshuChiu	$(MAKE) qemu-uefi-vnc
1476d345b77SHoshuChiu
1486d345b77SHoshuChiu# 编译并启动QEMU,使用VNC Display作为图像输出
1496d345b77SHoshuChiurun-vnc:
1506d345b77SHoshuChiu	$(MAKE) all -j $(NPROCS)
1516d345b77SHoshuChiu	$(MAKE) write_diskimage || exit 1
1526d345b77SHoshuChiu	$(MAKE) qemu-vnc
1536d345b77SHoshuChiu
1542813126eSlogin# 在docker中编译,并启动QEMU
1552813126eSloginrun-docker:
1562813126eSlogin	@echo "使用docker构建并运行"
1572813126eSlogin	sudo bash tools/build_in_docker.sh || exit 1
1585ed4cd46Sguanjinquan	$(MAKE) write_diskimage || exit 1
1592813126eSlogin	$(MAKE) qemu
1605f578343STptogiar
161e92d0228SLoGinfmt:
162e92d0228SLoGin	@echo "格式化代码"
163e92d0228SLoGin	FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C kernel
164e92d0228SLoGin	FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C user
165e26ca418SLoGin	FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C build-scripts
166e92d0228SLoGin
1677b32f508SLoGinlog-monitor:
1687b32f508SLoGin	@echo "启动日志监控"
1697b32f508SLoGin	@sh -c "cd tools/debugging/logmonitor && cargo run --release -- --log-dir $(ROOT_PATH)/logs/ --kernel $(ROOT_PATH)/bin/kernel/kernel.elf"
1707b32f508SLoGin
1715f578343STptogiarhelp:
1725f578343STptogiar	@echo "编译:"
1735f578343STptogiar	@echo "  make all -j <n>       - 本地编译,不运行,n为要用于编译的CPU核心数"
1745f578343STptogiar	@echo "  make build            - 本地编译,并写入磁盘镜像"
1755f578343STptogiar	@echo "  make docker           - Docker编译,并写入磁盘镜像"
1765f578343STptogiar	@echo ""
1775f578343STptogiar	@echo "编译并运行:"
1785f578343STptogiar	@echo "  make run-docker       - Docker编译,写入磁盘镜像,并在QEMU中运行"
1795f578343STptogiar	@echo "  make run              - 本地编译,写入磁盘镜像,并在QEMU中运行"
1805f578343STptogiar	@echo "  make run-uefi         - 以uefi方式启动运行"
1815f578343STptogiar	@echo ""
1825f578343STptogiar	@echo "运行:"
1835f578343STptogiar	@echo "  make qemu             - 不编译,直接从已有的磁盘镜像启动运行"
1845f578343STptogiar	@echo "  make qemu-uefi        - 不编译,直接从已有的磁盘镜像以UEFI启动运行"
1855f578343STptogiar	@echo ""
1865f578343STptogiar	@echo ""
1875f578343STptogiar	@echo "注: 对于上述的run, run-uefi, qemu, qemu-uefi命令可以在命令后加上-vnc后缀,来通过vnc连接到DragonOS, 默认会在5900端口运行vnc服务器。如:make run-vnc "
188