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# ifeq ($(DragonOS_GCC), ) 32# $(error 尚未安装DragonOS交叉编译器, 请使用tools文件夹下的build_gcc_toolchain.sh脚本安装) 33# endif 34 35export CC=$(DragonOS_GCC)/x86_64-elf-gcc 36export LD=ld 37export AS=$(DragonOS_GCC)/x86_64-elf-as 38export NM=$(DragonOS_GCC)/x86_64-elf-nm 39export OBJCOPY=$(DragonOS_GCC)/x86_64-elf-objcopy 40 41 42.PHONY: all 43all: kernel user 44 45 46.PHONY: kernel 47kernel: 48 mkdir -p bin/kernel/ 49 $(MAKE) -C ./kernel all || (sh -c "echo 内核编译失败" && exit 1) 50 51.PHONY: user 52user: 53 mkdir -p bin/user/ 54 mkdir -p bin/tmp/user 55 $(MAKE) -C ./user all || (sh -c "echo 用户程序编译失败" && exit 1) 56 57.PHONY: clean 58clean: 59 @list='$(SUBDIRS)'; for subdir in $$list; do \ 60 echo "Clean in dir: $$subdir";\ 61 cd $$subdir && $(MAKE) clean;\ 62 cd .. ;\ 63 done 64 65cppcheck-xml: 66 cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) --xml 2> cppcheck.xml 67 68cppcheck: 69 cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) 70 71gdb: 72 gdb -n -x tools/.gdbinit 73 74# 写入磁盘镜像 75write_diskimage: 76 bash -c "cd tools && bash grub_auto_install.sh && sudo bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=legacy && cd .." 77 78# 写入磁盘镜像(uefi) 79write_diskimage-uefi: 80 bash -c "cd tools && bash grub_auto_install.sh && sudo bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=uefi && cd .." 81# 不编译,直接启动QEMU 82qemu: 83 sh -c "cd tools && bash run-qemu.sh --bios=legacy && cd .." 84# 不编译,直接启动QEMU(UEFI) 85qemu-uefi: 86 sh -c "cd tools && bash run-qemu.sh --bios=uefi && cd .." 87 88# 编译并写入磁盘镜像 89build: 90 $(MAKE) all -j $(NPROCS) 91 $(MAKE) write_diskimage || exit 1 92 93# 在docker中编译,并写入磁盘镜像 94docker: 95 @echo "使用docker构建" 96 sudo bash tools/build_in_docker.sh || exit 1 97 $(MAKE) write_diskimage || exit 1 98 99# uefi方式启动 100run-uefi: 101 $(MAKE) all -j $(NPROCS) 102 $(MAKE) write_diskimage-uefi || exit 1 103 $(MAKE) qemu-uefi 104 105# 编译并启动QEMU 106run: 107 $(MAKE) all -j $(NPROCS) 108 $(MAKE) write_diskimage || exit 1 109 $(MAKE) qemu 110 111# 在docker中编译,并启动QEMU 112run-docker: 113 @echo "使用docker构建并运行" 114 sudo bash tools/build_in_docker.sh || exit 1 115 $(MAKE) write_diskimage || exit 1 116 $(MAKE) qemu 117