Makefile 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. .PHONY: all
  25. all: kernel user
  26. .PHONY: kernel
  27. kernel:
  28. mkdir -p bin/kernel/
  29. $(MAKE) -C ./kernel all ARCH=$(ARCH) || (sh -c "echo 内核编译失败" && exit 1)
  30. .PHONY: user
  31. user:
  32. $(MAKE) -C ./user all ARCH=$(ARCH) || (sh -c "echo 用户程序编译失败" && exit 1)
  33. .PHONY: clean
  34. clean:
  35. @list='$(SUBDIRS)'; for subdir in $$list; do \
  36. echo "Clean in dir: $$subdir";\
  37. cd $$subdir && $(MAKE) clean;\
  38. cd .. ;\
  39. done
  40. .PHONY: ECHO
  41. ECHO:
  42. @echo "$@"
  43. cppcheck-xml:
  44. cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) --xml 2> cppcheck.xml
  45. cppcheck:
  46. cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS)
  47. docs: ECHO
  48. bash -c "cd docs && make html && cd .."
  49. clean-docs:
  50. bash -c "cd docs && make clean && cd .."
  51. gdb:
  52. ifeq ($(ARCH), x86_64)
  53. rust-gdb -n -x tools/.gdbinit
  54. else
  55. gdb-multiarch -n -x tools/.gdbinit
  56. endif
  57. # 写入磁盘镜像
  58. write_diskimage:
  59. @echo "write_diskimage arch=$(ARCH)"
  60. 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 .."
  61. # 写入磁盘镜像(uefi)
  62. write_diskimage-uefi:
  63. 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 .."
  64. # 不编译,直接启动QEMU
  65. qemu:
  66. sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=window && cd .."
  67. # 不编译,直接启动QEMU,不显示图像
  68. qemu-nographic:
  69. sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=nographic && cd .."
  70. # 不编译,直接启动QEMU(UEFI)
  71. qemu-uefi:
  72. sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=window && cd .."
  73. # 不编译,直接启动QEMU,使用VNC Display作为图像输出
  74. qemu-vnc:
  75. sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=vnc && cd .."
  76. # 不编译,直接启动QEMU(UEFI),使用VNC Display作为图像输出
  77. qemu-uefi-vnc:
  78. sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=vnc && cd .."
  79. # 编译并写入磁盘镜像
  80. build:
  81. $(MAKE) all -j $(NPROCS)
  82. $(MAKE) write_diskimage || exit 1
  83. # 在docker中编译,并写入磁盘镜像
  84. docker:
  85. @echo "使用docker构建"
  86. sudo bash tools/build_in_docker.sh || exit 1
  87. $(MAKE) write_diskimage || exit 1
  88. # uefi方式启动
  89. run-uefi:
  90. $(MAKE) all -j $(NPROCS)
  91. $(MAKE) write_diskimage-uefi || exit 1
  92. $(MAKE) qemu-uefi
  93. # 编译并启动QEMU
  94. run:
  95. $(MAKE) all -j $(NPROCS)
  96. $(MAKE) write_diskimage || exit 1
  97. $(MAKE) qemu
  98. # uefi方式启动,使用VNC Display作为图像输出
  99. run-uefi-vnc:
  100. $(MAKE) all -j $(NPROCS)
  101. $(MAKE) write_diskimage-uefi || exit 1
  102. $(MAKE) qemu-uefi-vnc
  103. # 编译并启动QEMU,使用VNC Display作为图像输出
  104. run-vnc:
  105. $(MAKE) all -j $(NPROCS)
  106. $(MAKE) write_diskimage || exit 1
  107. $(MAKE) qemu-vnc
  108. # 在docker中编译,并启动QEMU
  109. run-docker:
  110. @echo "使用docker构建并运行"
  111. sudo bash tools/build_in_docker.sh || exit 1
  112. $(MAKE) write_diskimage || exit 1
  113. $(MAKE) qemu
  114. fmt:
  115. @echo "格式化代码"
  116. FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C kernel
  117. FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C user
  118. FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C build-scripts
  119. log-monitor:
  120. @echo "启动日志监控"
  121. @sh -c "cd tools/debugging/logmonitor && cargo run --release -- --log-dir $(ROOT_PATH)/logs/ --kernel $(ROOT_PATH)/bin/kernel/kernel.elf"
  122. .PHONY: update-submodules
  123. update-submodules:
  124. @echo "更新子模块"
  125. @git submodule update --recursive --init
  126. @git submodule foreach git pull origin master
  127. .PHONY: update-submodules-by-mirror
  128. update-submodules-by-mirror:
  129. @echo "从镜像更新子模块"
  130. @git config --global url."https://git.mirrors.dragonos.org.cn/DragonOS-Community/".insteadOf https://github.com/DragonOS-Community/
  131. @$(MAKE) update-submodules
  132. @git config --global --unset url."https://git.mirrors.dragonos.org.cn/DragonOS-Community/".insteadOf
  133. help:
  134. @echo "编译:"
  135. @echo " make all -j <n> - 本地编译,不运行,n为要用于编译的CPU核心数"
  136. @echo " make build - 本地编译,并写入磁盘镜像"
  137. @echo " make docker - Docker编译,并写入磁盘镜像"
  138. @echo ""
  139. @echo "编译并运行:"
  140. @echo " make run-docker - Docker编译,写入磁盘镜像,并在QEMU中运行"
  141. @echo " make run - 本地编译,写入磁盘镜像,并在QEMU中运行"
  142. @echo " make run-uefi - 以uefi方式启动运行"
  143. @echo ""
  144. @echo "运行:"
  145. @echo " make qemu - 不编译,直接从已有的磁盘镜像启动运行"
  146. @echo " make qemu-uefi - 不编译,直接从已有的磁盘镜像以UEFI启动运行"
  147. @echo ""
  148. @echo ""
  149. @echo "注: 对于上述的run, run-uefi, qemu, qemu-uefi命令可以在命令后加上-vnc后缀,来通过vnc连接到DragonOS, 默认会在5900端口运行vnc服务器。如:make run-vnc "
  150. @echo ""
  151. @echo "其他:"
  152. @echo " make clean - 清理编译产生的文件"
  153. @echo " make fmt - 格式化代码"
  154. @echo " make log-monitor - 启动日志监控"
  155. @echo " make docs - 生成文档"
  156. @echo " make clean-docs - 清理文档"
  157. @echo ""
  158. @echo " make update-submodules - 更新子模块"
  159. @echo " make update-submodules-by-mirror - 从镜像更新子模块"