Makefile 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # 导入环境变量
  2. include env.mk
  3. export ROOT_PATH=$(shell pwd)
  4. SUBDIRS = kernel user tools build-scripts
  5. # todo: 增加参数,判断是否在QEMU中仿真,若是,则启用该环境变量
  6. # export EMULATOR=__QEMU_EMULATION__
  7. # 计算cpu核心数
  8. NPROCS:=1
  9. OS:=$(shell uname -s)
  10. ifeq ($(OS),Linux)
  11. NPROCS:=$(shell grep -c ^processor /proc/cpuinfo)
  12. endif
  13. ifeq ($(OS),Darwin) # Assume Mac OS X
  14. NPROCS:=$(shell system_profiler | awk '/Number Of CPUs/{print $4}{next;}')
  15. endif
  16. # 检查是否需要进行fmt --check
  17. # 解析命令行参数
  18. FMT_CHECK?=0
  19. ifeq ($(FMT_CHECK), 1)
  20. FMT_CHECK=--check
  21. else
  22. FMT_CHECK=
  23. endif
  24. # Check if ARCH matches the arch field in dadk-manifest.toml
  25. check_arch:
  26. @bash tools/check_arch.sh
  27. .PHONY: all
  28. all: kernel user
  29. .PHONY: kernel
  30. kernel: check_arch
  31. mkdir -p bin/kernel/
  32. $(MAKE) -C ./kernel all ARCH=$(ARCH) || (sh -c "echo 内核编译失败" && exit 1)
  33. .PHONY: user
  34. user: check_arch
  35. $(MAKE) -C ./user all ARCH=$(ARCH) || (sh -c "echo 用户程序编译失败" && exit 1)
  36. .PHONY: clean
  37. clean:
  38. @list='$(SUBDIRS)'; for subdir in $$list; do \
  39. echo "Clean in dir: $$subdir";\
  40. cd $$subdir && $(MAKE) clean;\
  41. cd .. ;\
  42. done
  43. .PHONY: ECHO
  44. ECHO:
  45. @echo "$@"
  46. docs: ECHO
  47. bash -c "cd docs && make html && cd .."
  48. clean-docs:
  49. bash -c "cd docs && make clean && cd .."
  50. gdb:
  51. ifeq ($(ARCH), x86_64)
  52. rust-gdb -n -x tools/.gdbinit
  53. else
  54. gdb-multiarch -n -x tools/.gdbinit
  55. endif
  56. # 写入磁盘镜像
  57. write_diskimage: check_arch
  58. @echo "write_diskimage arch=$(ARCH)"
  59. bash -c "export ARCH=$(ARCH); cd tools && bash grub_auto_install.sh && sudo DADK=$(DADK) ARCH=$(ARCH) bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=legacy && cd .."
  60. # 写入磁盘镜像(uefi)
  61. write_diskimage-uefi: check_arch
  62. bash -c "export ARCH=$(ARCH); cd tools && bash grub_auto_install.sh && sudo DADK=$(DADK) ARCH=$(ARCH) bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=uefi && cd .."
  63. # 不编译,直接启动QEMU
  64. qemu: check_arch
  65. sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=window && cd .."
  66. # 不编译,直接启动QEMU,不显示图像
  67. qemu-nographic: check_arch
  68. sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=nographic && cd .."
  69. # 不编译,直接启动QEMU(UEFI)
  70. qemu-uefi: check_arch
  71. sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=window && cd .."
  72. # 不编译,直接启动QEMU,使用VNC Display作为图像输出
  73. qemu-vnc: check_arch
  74. sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=vnc && cd .."
  75. # 不编译,直接启动QEMU(UEFI),使用VNC Display作为图像输出
  76. qemu-uefi-vnc: check_arch
  77. sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=vnc && cd .."
  78. # 编译并写入磁盘镜像
  79. build: check_arch
  80. $(MAKE) all -j $(NPROCS)
  81. $(MAKE) write_diskimage || exit 1
  82. # 在docker中编译,并写入磁盘镜像
  83. docker: check_arch
  84. @echo "使用docker构建"
  85. sudo bash tools/build_in_docker.sh || exit 1
  86. $(MAKE) write_diskimage || exit 1
  87. # uefi方式启动
  88. run-uefi: check_arch
  89. $(MAKE) all -j $(NPROCS)
  90. $(MAKE) write_diskimage-uefi || exit 1
  91. $(MAKE) qemu-uefi
  92. # 编译并启动QEMU
  93. run: check_arch
  94. $(MAKE) all -j $(NPROCS)
  95. $(MAKE) write_diskimage || exit 1
  96. $(MAKE) qemu
  97. # uefi方式启动,使用VNC Display作为图像输出
  98. run-uefi-vnc: check_arch
  99. $(MAKE) all -j $(NPROCS)
  100. $(MAKE) write_diskimage-uefi || exit 1
  101. $(MAKE) qemu-uefi-vnc
  102. # 编译并启动QEMU,使用VNC Display作为图像输出
  103. run-vnc: check_arch
  104. $(MAKE) all -j $(NPROCS)
  105. $(MAKE) write_diskimage || exit 1
  106. $(MAKE) qemu-vnc
  107. # 在docker中编译,并启动QEMU
  108. run-docker: check_arch
  109. @echo "使用docker构建并运行"
  110. sudo bash tools/build_in_docker.sh || exit 1
  111. $(MAKE) write_diskimage || exit 1
  112. $(MAKE) qemu
  113. fmt: check_arch
  114. @echo "格式化代码"
  115. FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C kernel
  116. FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C user
  117. FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C build-scripts
  118. log-monitor:
  119. @echo "启动日志监控"
  120. @sh -c "cd tools/debugging/logmonitor && cargo run --release -- --log-dir $(ROOT_PATH)/logs/ --kernel $(ROOT_PATH)/bin/kernel/kernel.elf"
  121. .PHONY: update-submodules
  122. update-submodules:
  123. @echo "更新子模块"
  124. @git submodule update --recursive --init
  125. @git submodule foreach git pull origin master
  126. .PHONY: update-submodules-by-mirror
  127. update-submodules-by-mirror:
  128. @echo "从镜像更新子模块"
  129. @git config --global url."https://git.mirrors.dragonos.org.cn/DragonOS-Community/".insteadOf https://github.com/DragonOS-Community/
  130. @$(MAKE) update-submodules
  131. @git config --global --unset url."https://git.mirrors.dragonos.org.cn/DragonOS-Community/".insteadOf
  132. help:
  133. @echo "编译:"
  134. @echo " make all -j <n> - 本地编译,不运行,n为要用于编译的CPU核心数"
  135. @echo " make build - 本地编译,并写入磁盘镜像"
  136. @echo " make docker - Docker编译,并写入磁盘镜像"
  137. @echo ""
  138. @echo "编译并运行:"
  139. @echo " make run-docker - Docker编译,写入磁盘镜像,并在QEMU中运行"
  140. @echo " make run - 本地编译,写入磁盘镜像,并在QEMU中运行"
  141. @echo " make run-uefi - 以uefi方式启动运行"
  142. @echo ""
  143. @echo "运行:"
  144. @echo " make qemu - 不编译,直接从已有的磁盘镜像启动运行"
  145. @echo " make qemu-uefi - 不编译,直接从已有的磁盘镜像以UEFI启动运行"
  146. @echo ""
  147. @echo ""
  148. @echo "注: 对于上述的run, run-uefi, qemu, qemu-uefi命令可以在命令后加上-vnc后缀,来通过vnc连接到DragonOS, 默认会在5900端口运行vnc服务器。如:make run-vnc "
  149. @echo ""
  150. @echo "其他:"
  151. @echo " make clean - 清理编译产生的文件"
  152. @echo " make fmt - 格式化代码"
  153. @echo " make log-monitor - 启动日志监控"
  154. @echo " make docs - 生成文档"
  155. @echo " make clean-docs - 清理文档"
  156. @echo ""
  157. @echo " make update-submodules - 更新子模块"
  158. @echo " make update-submodules-by-mirror - 从镜像更新子模块"