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 64.PHONY: ECHO 65ECHO: 66 @echo "$@" 67 68cppcheck-xml: 69 cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) --xml 2> cppcheck.xml 70 71cppcheck: 72 cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) 73 74docs: ECHO 75 bash -c "cd docs && make html && cd .." 76 77clean-docs: 78 bash -c "cd docs && make clean && cd .." 79 80gdb: 81 rust-gdb -n -x tools/.gdbinit 82 83# 写入磁盘镜像 84write_diskimage: 85 bash -c "cd tools && bash grub_auto_install.sh && sudo bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=legacy && cd .." 86 87# 写入磁盘镜像(uefi) 88write_diskimage-uefi: 89 bash -c "cd tools && bash grub_auto_install.sh && sudo bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=uefi && cd .." 90# 不编译,直接启动QEMU 91qemu: 92 sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=window && cd .." 93# 不编译,直接启动QEMU(UEFI) 94qemu-uefi: 95 sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=window && cd .." 96# 不编译,直接启动QEMU,使用VNC Display作为图像输出 97qemu-vnc: 98 sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=vnc && cd .." 99# 不编译,直接启动QEMU(UEFI),使用VNC Display作为图像输出 100qemu-uefi-vnc: 101 sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=vnc && cd .." 102 103# 编译并写入磁盘镜像 104build: 105 $(MAKE) all -j $(NPROCS) 106 $(MAKE) write_diskimage || exit 1 107 108# 在docker中编译,并写入磁盘镜像 109docker: 110 @echo "使用docker构建" 111 sudo bash tools/build_in_docker.sh || exit 1 112 $(MAKE) write_diskimage || exit 1 113 114# uefi方式启动 115run-uefi: 116 $(MAKE) all -j $(NPROCS) 117 $(MAKE) write_diskimage-uefi || exit 1 118 $(MAKE) qemu-uefi 119 120# 编译并启动QEMU 121run: 122 $(MAKE) all -j $(NPROCS) 123 $(MAKE) write_diskimage || exit 1 124 $(MAKE) qemu 125 126# uefi方式启动,使用VNC Display作为图像输出 127run-uefi-vnc: 128 $(MAKE) all -j $(NPROCS) 129 $(MAKE) write_diskimage-uefi || exit 1 130 $(MAKE) qemu-uefi-vnc 131 132# 编译并启动QEMU,使用VNC Display作为图像输出 133run-vnc: 134 $(MAKE) all -j $(NPROCS) 135 $(MAKE) write_diskimage || exit 1 136 $(MAKE) qemu-vnc 137 138# 在docker中编译,并启动QEMU 139run-docker: 140 @echo "使用docker构建并运行" 141 sudo bash tools/build_in_docker.sh || exit 1 142 $(MAKE) write_diskimage || exit 1 143 $(MAKE) qemu 144 145help: 146 @echo "编译:" 147 @echo " make all -j <n> - 本地编译,不运行,n为要用于编译的CPU核心数" 148 @echo " make build - 本地编译,并写入磁盘镜像" 149 @echo " make docker - Docker编译,并写入磁盘镜像" 150 @echo "" 151 @echo "编译并运行:" 152 @echo " make run-docker - Docker编译,写入磁盘镜像,并在QEMU中运行" 153 @echo " make run - 本地编译,写入磁盘镜像,并在QEMU中运行" 154 @echo " make run-uefi - 以uefi方式启动运行" 155 @echo "" 156 @echo "运行:" 157 @echo " make qemu - 不编译,直接从已有的磁盘镜像启动运行" 158 @echo " make qemu-uefi - 不编译,直接从已有的磁盘镜像以UEFI启动运行" 159 @echo "" 160 @echo "" 161 @echo "注: 对于上述的run, run-uefi, qemu, qemu-uefi命令可以在命令后加上-vnc后缀,来通过vnc连接到DragonOS, 默认会在5900端口运行vnc服务器。如:make run-vnc " 162