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 && cd .." 70 71# 不编译,直接启动QEMU 72qemu: 73 sh -c "cd tools && bash run-qemu.sh && cd .." 74 75# 编译并写入磁盘镜像 76build: 77 $(MAKE) all -j $(NPROCS) 78 $(MAKE) write_diskimage || exit 1 79 80# 在docker中编译,并写入磁盘镜像 81docker: 82 @echo "使用docker构建" 83 sudo bash tools/build_in_docker.sh || exit 1 84 85# 编译并启动QEMU 86run: 87 $(MAKE) all -j $(NPROCS) 88 $(MAKE) write_diskimage || exit 1 89 $(MAKE) qemu 90 91# 在docker中编译,并启动QEMU 92run-docker: 93 @echo "使用docker构建并运行" 94 sudo bash tools/build_in_docker.sh || exit 1 95 $(MAKE) qemu 96