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