1SUBDIRS = kernel user tools build-scripts 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 21# if arch not defined, set it to x86_64 22export ARCH?=x86_64 23 24CFLAGS_DEFINE_ARCH="__$(ARCH)__" 25 26 27export ROOT_PATH=$(shell pwd) 28 29export DEBUG=DEBUG 30export GLOBAL_CFLAGS := -mcmodel=large -fno-builtin -m64 -fno-stack-protector -D $(CFLAGS_DEFINE_ARCH) -D $(EMULATOR) -O1 31 32ifeq ($(DEBUG), DEBUG) 33GLOBAL_CFLAGS += -g 34endif 35 36 37export CC=$(DragonOS_GCC)/x86_64-elf-gcc 38export LD=ld 39export AS=$(DragonOS_GCC)/x86_64-elf-as 40export NM=$(DragonOS_GCC)/x86_64-elf-nm 41export AR=$(DragonOS_GCC)/x86_64-elf-ar 42export OBJCOPY=$(DragonOS_GCC)/x86_64-elf-objcopy 43 44# 检查是否需要进行fmt --check 45# 解析命令行参数 46FMT_CHECK?=0 47 48ifeq ($(FMT_CHECK), 1) 49 FMT_CHECK=--check 50else 51 FMT_CHECK= 52endif 53 54 55.PHONY: all 56all: kernel user 57 58 59.PHONY: kernel 60kernel: 61 mkdir -p bin/kernel/ 62 @if [ -z $$DragonOS_GCC ]; then echo "\033[31m [错误]尚未安装DragonOS交叉编译器, 请使用tools文件夹下的build_gcc_toolchain.sh脚本安装 \033[0m"; exit 1; fi 63 $(MAKE) -C ./kernel all || (sh -c "echo 内核编译失败" && exit 1) 64 65.PHONY: user 66user: 67 68 @if [ -z $$DragonOS_GCC ]; then echo "\033[31m [错误]尚未安装DragonOS交叉编译器, 请使用tools文件夹下的build_gcc_toolchain.sh脚本安装 \033[0m"; exit 1; fi 69 $(MAKE) -C ./user all || (sh -c "echo 用户程序编译失败" && exit 1) 70 71.PHONY: clean 72clean: 73 @list='$(SUBDIRS)'; for subdir in $$list; do \ 74 echo "Clean in dir: $$subdir";\ 75 cd $$subdir && $(MAKE) clean;\ 76 cd .. ;\ 77 done 78 79.PHONY: ECHO 80ECHO: 81 @echo "$@" 82 83cppcheck-xml: 84 cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) --xml 2> cppcheck.xml 85 86cppcheck: 87 cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) 88 89docs: ECHO 90 bash -c "cd docs && make html && cd .." 91 92clean-docs: 93 bash -c "cd docs && make clean && cd .." 94 95gdb: 96 rust-gdb -n -x tools/.gdbinit 97 98# 写入磁盘镜像 99write_diskimage: 100 @echo "write_diskimage arch=$(ARCH)" 101 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 .." 102 103# 写入磁盘镜像(uefi) 104write_diskimage-uefi: 105 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 .." 106# 不编译,直接启动QEMU 107qemu: 108 sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=window && cd .." 109# 不编译,直接启动QEMU(UEFI) 110qemu-uefi: 111 sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=window && cd .." 112# 不编译,直接启动QEMU,使用VNC Display作为图像输出 113qemu-vnc: 114 sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=vnc && cd .." 115# 不编译,直接启动QEMU(UEFI),使用VNC Display作为图像输出 116qemu-uefi-vnc: 117 sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=vnc && cd .." 118 119# 编译并写入磁盘镜像 120build: 121 $(MAKE) all -j $(NPROCS) 122 $(MAKE) write_diskimage || exit 1 123 124# 在docker中编译,并写入磁盘镜像 125docker: 126 @echo "使用docker构建" 127 sudo bash tools/build_in_docker.sh || exit 1 128 $(MAKE) write_diskimage || exit 1 129 130# uefi方式启动 131run-uefi: 132 $(MAKE) all -j $(NPROCS) 133 $(MAKE) write_diskimage-uefi || exit 1 134 $(MAKE) qemu-uefi 135 136# 编译并启动QEMU 137run: 138 $(MAKE) all -j $(NPROCS) 139 $(MAKE) write_diskimage || exit 1 140 $(MAKE) qemu 141 142# uefi方式启动,使用VNC Display作为图像输出 143run-uefi-vnc: 144 $(MAKE) all -j $(NPROCS) 145 $(MAKE) write_diskimage-uefi || exit 1 146 $(MAKE) qemu-uefi-vnc 147 148# 编译并启动QEMU,使用VNC Display作为图像输出 149run-vnc: 150 $(MAKE) all -j $(NPROCS) 151 $(MAKE) write_diskimage || exit 1 152 $(MAKE) qemu-vnc 153 154# 在docker中编译,并启动QEMU 155run-docker: 156 @echo "使用docker构建并运行" 157 sudo bash tools/build_in_docker.sh || exit 1 158 $(MAKE) write_diskimage || exit 1 159 $(MAKE) qemu 160 161fmt: 162 @echo "格式化代码" 163 FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C kernel 164 FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C user 165 FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C build-scripts 166 167log-monitor: 168 @echo "启动日志监控" 169 @sh -c "cd tools/debugging/logmonitor && cargo run --release -- --log-dir $(ROOT_PATH)/logs/ --kernel $(ROOT_PATH)/bin/kernel/kernel.elf" 170 171help: 172 @echo "编译:" 173 @echo " make all -j <n> - 本地编译,不运行,n为要用于编译的CPU核心数" 174 @echo " make build - 本地编译,并写入磁盘镜像" 175 @echo " make docker - Docker编译,并写入磁盘镜像" 176 @echo "" 177 @echo "编译并运行:" 178 @echo " make run-docker - Docker编译,写入磁盘镜像,并在QEMU中运行" 179 @echo " make run - 本地编译,写入磁盘镜像,并在QEMU中运行" 180 @echo " make run-uefi - 以uefi方式启动运行" 181 @echo "" 182 @echo "运行:" 183 @echo " make qemu - 不编译,直接从已有的磁盘镜像启动运行" 184 @echo " make qemu-uefi - 不编译,直接从已有的磁盘镜像以UEFI启动运行" 185 @echo "" 186 @echo "" 187 @echo "注: 对于上述的run, run-uefi, qemu, qemu-uefi命令可以在命令后加上-vnc后缀,来通过vnc连接到DragonOS, 默认会在5900端口运行vnc服务器。如:make run-vnc " 188