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