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: 79 rust-gdb -n -x tools/.gdbinit 80 81# 写入磁盘镜像 82write_diskimage: 83 @echo "write_diskimage arch=$(ARCH)" 84 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 .." 85 86# 写入磁盘镜像(uefi) 87write_diskimage-uefi: 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=uefi && cd .." 89# 不编译,直接启动QEMU 90qemu: 91 sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=window && cd .." 92# 不编译,直接启动QEMU(UEFI) 93qemu-uefi: 94 sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=window && cd .." 95# 不编译,直接启动QEMU,使用VNC Display作为图像输出 96qemu-vnc: 97 sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=vnc && cd .." 98# 不编译,直接启动QEMU(UEFI),使用VNC Display作为图像输出 99qemu-uefi-vnc: 100 sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=vnc && cd .." 101 102# 编译并写入磁盘镜像 103build: 104 $(MAKE) all -j $(NPROCS) 105 $(MAKE) write_diskimage || exit 1 106 107# 在docker中编译,并写入磁盘镜像 108docker: 109 @echo "使用docker构建" 110 sudo bash tools/build_in_docker.sh || exit 1 111 $(MAKE) write_diskimage || exit 1 112 113# uefi方式启动 114run-uefi: 115 $(MAKE) all -j $(NPROCS) 116 $(MAKE) write_diskimage-uefi || exit 1 117 $(MAKE) qemu-uefi 118 119# 编译并启动QEMU 120run: 121 $(MAKE) all -j $(NPROCS) 122 $(MAKE) write_diskimage || exit 1 123 $(MAKE) qemu 124 125# uefi方式启动,使用VNC Display作为图像输出 126run-uefi-vnc: 127 $(MAKE) all -j $(NPROCS) 128 $(MAKE) write_diskimage-uefi || exit 1 129 $(MAKE) qemu-uefi-vnc 130 131# 编译并启动QEMU,使用VNC Display作为图像输出 132run-vnc: 133 $(MAKE) all -j $(NPROCS) 134 $(MAKE) write_diskimage || exit 1 135 $(MAKE) qemu-vnc 136 137# 在docker中编译,并启动QEMU 138run-docker: 139 @echo "使用docker构建并运行" 140 sudo bash tools/build_in_docker.sh || exit 1 141 $(MAKE) write_diskimage || exit 1 142 $(MAKE) qemu 143 144fmt: 145 @echo "格式化代码" 146 FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C kernel 147 FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C user 148 FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C build-scripts 149 150log-monitor: 151 @echo "启动日志监控" 152 @sh -c "cd tools/debugging/logmonitor && cargo run --release -- --log-dir $(ROOT_PATH)/logs/ --kernel $(ROOT_PATH)/bin/kernel/kernel.elf" 153 154help: 155 @echo "编译:" 156 @echo " make all -j <n> - 本地编译,不运行,n为要用于编译的CPU核心数" 157 @echo " make build - 本地编译,并写入磁盘镜像" 158 @echo " make docker - Docker编译,并写入磁盘镜像" 159 @echo "" 160 @echo "编译并运行:" 161 @echo " make run-docker - Docker编译,写入磁盘镜像,并在QEMU中运行" 162 @echo " make run - 本地编译,写入磁盘镜像,并在QEMU中运行" 163 @echo " make run-uefi - 以uefi方式启动运行" 164 @echo "" 165 @echo "运行:" 166 @echo " make qemu - 不编译,直接从已有的磁盘镜像启动运行" 167 @echo " make qemu-uefi - 不编译,直接从已有的磁盘镜像以UEFI启动运行" 168 @echo "" 169 @echo "" 170 @echo "注: 对于上述的run, run-uefi, qemu, qemu-uefi命令可以在命令后加上-vnc后缀,来通过vnc连接到DragonOS, 默认会在5900端口运行vnc服务器。如:make run-vnc " 171