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 RUSTC=$(shell which rustc) 25 26export DEBUG=DEBUG 27export GLOBAL_CFLAGS := -mcmodel=large -fno-builtin -m64 -fno-stack-protector -D $(ARCH) -D $(EMULATOR) -O1 28 29ifeq ($(DEBUG), DEBUG) 30GLOBAL_CFLAGS += -g 31endif 32 33export CC=gcc 34 35.PHONY: all 36all: kernel user 37 38 39.PHONY: kernel 40kernel: 41 mkdir -p bin/kernel/ 42 $(MAKE) -C ./kernel all || (sh -c "echo 内核编译失败" && exit 1) 43 44.PHONY: user 45user: 46 mkdir -p bin/user/ 47 mkdir -p bin/tmp/user 48 $(MAKE) -C ./user all || (sh -c "echo 用户程序编译失败" && exit 1) 49 50.PHONY: clean 51clean: 52 @list='$(SUBDIRS)'; for subdir in $$list; do \ 53 echo "Clean in dir: $$subdir";\ 54 cd $$subdir && $(MAKE) clean;\ 55 cd .. ;\ 56 done 57 58cppcheck-xml: 59 cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) --xml 2> cppcheck.xml 60 61cppcheck: 62 cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) 63 64gdb: 65 gdb -n -x tools/.gdbinit 66 67# 写入磁盘镜像 68write_diskimage: 69 sudo sh -c "cd tools && bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=legacy && cd .." 70 71# 写入磁盘镜像(uefi) 72write_diskimage-uefi: 73 sudo sh -c "cd tools && bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=uefi && cd .." 74# 不编译,直接启动QEMU 75qemu: 76 sh -c "cd tools && bash run-qemu.sh --bios=legacy && cd .." 77# 不编译,直接启动QEMU(UEFI) 78qemu-uefi: 79 sh -c "cd tools && bash run-qemu.sh --bios=uefi && cd .." 80 81# 编译并写入磁盘镜像 82build: 83 $(MAKE) all -j $(NPROCS) 84 $(MAKE) write_diskimage || exit 1 85 86# 在docker中编译,并写入磁盘镜像 87docker: 88 @echo "使用docker构建" 89 sudo bash tools/build_in_docker.sh || exit 1 90 91# uefi方式启动 92run-uefi: 93 $(MAKE) all -j $(NPROCS) 94 $(MAKE) write_diskimage-uefi || exit 1 95 $(MAKE) qemu-uefi 96 97# 编译并启动QEMU 98run: 99 $(MAKE) all -j $(NPROCS) 100 $(MAKE) write_diskimage || exit 1 101 $(MAKE) qemu 102 103# 在docker中编译,并启动QEMU 104run-docker: 105 @echo "使用docker构建并运行" 106 sudo bash tools/build_in_docker.sh || exit 1 107 $(MAKE) qemu 108