1*7b32f508SLoGinSUBDIRS = kernel user tools 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 21edb21695Sfslongjinexport ARCH=__x86_64__ 220aec6827Sfslongjinexport ROOT_PATH=$(shell pwd) 230aec6827Sfslongjin 240aec6827Sfslongjinexport DEBUG=DEBUG 25416a802bSfslongjinexport GLOBAL_CFLAGS := -mcmodel=large -fno-builtin -m64 -fno-stack-protector -D $(ARCH) -D $(EMULATOR) -O1 260aec6827Sfslongjin 270aec6827Sfslongjinifeq ($(DEBUG), DEBUG) 280aec6827SfslongjinGLOBAL_CFLAGS += -g 290aec6827Sfslongjinendif 30f479f321Sfslongjin 315ed4cd46Sguanjinquan 325ed4cd46Sguanjinquanexport CC=$(DragonOS_GCC)/x86_64-elf-gcc 335ed4cd46Sguanjinquanexport LD=ld 345ed4cd46Sguanjinquanexport AS=$(DragonOS_GCC)/x86_64-elf-as 355ed4cd46Sguanjinquanexport NM=$(DragonOS_GCC)/x86_64-elf-nm 36d4f3de93Sloginexport AR=$(DragonOS_GCC)/x86_64-elf-ar 375ed4cd46Sguanjinquanexport OBJCOPY=$(DragonOS_GCC)/x86_64-elf-objcopy 385ed4cd46Sguanjinquan 39e92d0228SLoGin# 检查是否需要进行fmt --check 40e92d0228SLoGin# 解析命令行参数 41e92d0228SLoGinFMT_CHECK?=0 42e92d0228SLoGin 43e92d0228SLoGinifeq ($(FMT_CHECK), 1) 44e92d0228SLoGin FMT_CHECK=--check 45e92d0228SLoGinelse 46e92d0228SLoGin FMT_CHECK= 47e92d0228SLoGinendif 48e92d0228SLoGin 49416a802bSfslongjin 50f479f321Sfslongjin.PHONY: all 511ab51cb3Sfslongjinall: kernel user 521ab51cb3Sfslongjin 531ab51cb3Sfslongjin 541ab51cb3Sfslongjin.PHONY: kernel 551ab51cb3Sfslongjinkernel: 560b0cce93Sfslongjin mkdir -p bin/kernel/ 5774bde36eSguanjinquan @if [ -z $$DragonOS_GCC ]; then echo "\033[31m [错误]尚未安装DragonOS交叉编译器, 请使用tools文件夹下的build_gcc_toolchain.sh脚本安装 \033[0m"; exit 1; fi 582813126eSlogin $(MAKE) -C ./kernel all || (sh -c "echo 内核编译失败" && exit 1) 59f479f321Sfslongjin 60e2a59dbdSfslongjin.PHONY: user 61e2a59dbdSfslongjinuser: 622224c93eSguanjinquan 6374bde36eSguanjinquan @if [ -z $$DragonOS_GCC ]; then echo "\033[31m [错误]尚未安装DragonOS交叉编译器, 请使用tools文件夹下的build_gcc_toolchain.sh脚本安装 \033[0m"; exit 1; fi 642813126eSlogin $(MAKE) -C ./user all || (sh -c "echo 用户程序编译失败" && exit 1) 65e2a59dbdSfslongjin 66f479f321Sfslongjin.PHONY: clean 67f479f321Sfslongjinclean: 68f479f321Sfslongjin @list='$(SUBDIRS)'; for subdir in $$list; do \ 69f479f321Sfslongjin echo "Clean in dir: $$subdir";\ 7005dc7ac7Sfslongjin cd $$subdir && $(MAKE) clean;\ 71f479f321Sfslongjin cd .. ;\ 72f479f321Sfslongjin done 73e667fff2Sfslongjin 7440fe15e0SLoGin.PHONY: ECHO 7540fe15e0SLoGinECHO: 7640fe15e0SLoGin @echo "$@" 7740fe15e0SLoGin 78d328bfceSlogincppcheck-xml: 79d328bfceSlogin cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) --xml 2> cppcheck.xml 80d328bfceSlogin 81d328bfceSlogincppcheck: 82d328bfceSlogin cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) 83d328bfceSlogin 8440fe15e0SLoGindocs: ECHO 8540fe15e0SLoGin bash -c "cd docs && make html && cd .." 8640fe15e0SLoGin 8740fe15e0SLoGinclean-docs: 8840fe15e0SLoGin bash -c "cd docs && make clean && cd .." 8940fe15e0SLoGin 90e667fff2Sfslongjingdb: 9140fe15e0SLoGin rust-gdb -n -x tools/.gdbinit 922813126eSlogin 932813126eSlogin# 写入磁盘镜像 942813126eSloginwrite_diskimage: 955ed4cd46Sguanjinquan bash -c "cd tools && bash grub_auto_install.sh && sudo bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=legacy && cd .." 962813126eSlogin 977f439c5dSYJwu2023# 写入磁盘镜像(uefi) 987f439c5dSYJwu2023write_diskimage-uefi: 995ed4cd46Sguanjinquan bash -c "cd tools && bash grub_auto_install.sh && sudo bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=uefi && cd .." 1002813126eSlogin# 不编译,直接启动QEMU 1012813126eSloginqemu: 1026d345b77SHoshuChiu sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=window && cd .." 1037f439c5dSYJwu2023# 不编译,直接启动QEMU(UEFI) 1047f439c5dSYJwu2023qemu-uefi: 1056d345b77SHoshuChiu sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=window && cd .." 1066d345b77SHoshuChiu# 不编译,直接启动QEMU,使用VNC Display作为图像输出 1076d345b77SHoshuChiuqemu-vnc: 1086d345b77SHoshuChiu sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=vnc && cd .." 1096d345b77SHoshuChiu# 不编译,直接启动QEMU(UEFI),使用VNC Display作为图像输出 1106d345b77SHoshuChiuqemu-uefi-vnc: 1116d345b77SHoshuChiu sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=vnc && cd .." 1122813126eSlogin 1132813126eSlogin# 编译并写入磁盘镜像 1142813126eSloginbuild: 1152813126eSlogin $(MAKE) all -j $(NPROCS) 1162813126eSlogin $(MAKE) write_diskimage || exit 1 1172813126eSlogin 1182813126eSlogin# 在docker中编译,并写入磁盘镜像 1192813126eSlogindocker: 1202813126eSlogin @echo "使用docker构建" 1212813126eSlogin sudo bash tools/build_in_docker.sh || exit 1 1225ed4cd46Sguanjinquan $(MAKE) write_diskimage || exit 1 1232813126eSlogin 1247f439c5dSYJwu2023# uefi方式启动 1257f439c5dSYJwu2023run-uefi: 1267f439c5dSYJwu2023 $(MAKE) all -j $(NPROCS) 1277f439c5dSYJwu2023 $(MAKE) write_diskimage-uefi || exit 1 1287f439c5dSYJwu2023 $(MAKE) qemu-uefi 1297f439c5dSYJwu2023 1302813126eSlogin# 编译并启动QEMU 1312813126eSloginrun: 1322813126eSlogin $(MAKE) all -j $(NPROCS) 1332813126eSlogin $(MAKE) write_diskimage || exit 1 1342813126eSlogin $(MAKE) qemu 1352813126eSlogin 1366d345b77SHoshuChiu# uefi方式启动,使用VNC Display作为图像输出 1376d345b77SHoshuChiurun-uefi-vnc: 1386d345b77SHoshuChiu $(MAKE) all -j $(NPROCS) 1396d345b77SHoshuChiu $(MAKE) write_diskimage-uefi || exit 1 1406d345b77SHoshuChiu $(MAKE) qemu-uefi-vnc 1416d345b77SHoshuChiu 1426d345b77SHoshuChiu# 编译并启动QEMU,使用VNC Display作为图像输出 1436d345b77SHoshuChiurun-vnc: 1446d345b77SHoshuChiu $(MAKE) all -j $(NPROCS) 1456d345b77SHoshuChiu $(MAKE) write_diskimage || exit 1 1466d345b77SHoshuChiu $(MAKE) qemu-vnc 1476d345b77SHoshuChiu 1482813126eSlogin# 在docker中编译,并启动QEMU 1492813126eSloginrun-docker: 1502813126eSlogin @echo "使用docker构建并运行" 1512813126eSlogin sudo bash tools/build_in_docker.sh || exit 1 1525ed4cd46Sguanjinquan $(MAKE) write_diskimage || exit 1 1532813126eSlogin $(MAKE) qemu 1545f578343STptogiar 155e92d0228SLoGinfmt: 156e92d0228SLoGin @echo "格式化代码" 157e92d0228SLoGin FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C kernel 158e92d0228SLoGin FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C user 159e92d0228SLoGin 160*7b32f508SLoGinlog-monitor: 161*7b32f508SLoGin @echo "启动日志监控" 162*7b32f508SLoGin @sh -c "cd tools/debugging/logmonitor && cargo run --release -- --log-dir $(ROOT_PATH)/logs/ --kernel $(ROOT_PATH)/bin/kernel/kernel.elf" 163*7b32f508SLoGin 1645f578343STptogiarhelp: 1655f578343STptogiar @echo "编译:" 1665f578343STptogiar @echo " make all -j <n> - 本地编译,不运行,n为要用于编译的CPU核心数" 1675f578343STptogiar @echo " make build - 本地编译,并写入磁盘镜像" 1685f578343STptogiar @echo " make docker - Docker编译,并写入磁盘镜像" 1695f578343STptogiar @echo "" 1705f578343STptogiar @echo "编译并运行:" 1715f578343STptogiar @echo " make run-docker - Docker编译,写入磁盘镜像,并在QEMU中运行" 1725f578343STptogiar @echo " make run - 本地编译,写入磁盘镜像,并在QEMU中运行" 1735f578343STptogiar @echo " make run-uefi - 以uefi方式启动运行" 1745f578343STptogiar @echo "" 1755f578343STptogiar @echo "运行:" 1765f578343STptogiar @echo " make qemu - 不编译,直接从已有的磁盘镜像启动运行" 1775f578343STptogiar @echo " make qemu-uefi - 不编译,直接从已有的磁盘镜像以UEFI启动运行" 1785f578343STptogiar @echo "" 1795f578343STptogiar @echo "" 1805f578343STptogiar @echo "注: 对于上述的run, run-uefi, qemu, qemu-uefi命令可以在命令后加上-vnc后缀,来通过vnc连接到DragonOS, 默认会在5900端口运行vnc服务器。如:make run-vnc " 181