forked from TayhuangOS-Development-Team/TayHuangOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
244 lines (195 loc) · 5.72 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#
# SPDX-License-Identifier: LGPL-2.1-only
# -------------------------------*-TayhuangOS-*-----------------------------------
#
# Copyright (C) 2022, 2022 TayhuangOS Development Team - All Rights Reserved
#
# --------------------------------------------------------------------------------
#
# 作者: theflysong 383494
#
# Makefile
#
# Makefile
#
#
# 配置区
#include ./config.mk
#定义区
ARCHITECTURE ?= x86_64
BOOT_TYPE := BIOS #可选:BIOS UEFI
ARCHDEF_C := -DARCH_$(ARCHITECTURE) #架构宏
FILESYSTEM ?= fat32
MODE ?= debug
VBE_MODE ?= DISABLE
KERNEL_PARTITION_OFFSET ?= 1048576
IMAGE_SECTORS ?= 262144
PROGRAM_FORMAT ?= elf
COMPILER_PREFIX := $(ARCHITECTURE)-$(PROGRAM_FORMAT)
BUILD_VERSION ?= 1
# 这里本来有LOOPA和LOOPB,但依赖于get_loop.sh,故放在工具区
include ./version.mk
VERSION := $(CODE_VERSION)-$(MAJOR_VERSION).$(MINOR_VERSION).$(PATCH_VERSION):build $(BUILD_VERSION)
export ARCHITECTURE ARCHDEF_C MODE VBE_MODE CODE_VERSION MAJOR_VERSION MINOR_VERSION PATCH_VERSION BUILD_VERSION VERSION
#目录区
ROOTDIR := $(shell pwd)
BUILDDIR := $(ROOTDIR)/build/$(MODE)/
BINDIR := $(BUILDDIR)/bin/
OBJECTSDIR := $(BUILDDIR)/objects/
TAYHUANGOS_MOUNT_DIR := /mnt/tayhuangOS
TAYHUANGOS_IMG := tayhuangOS.img
#工具区
MKDIR := mkdir -p -v
GCC := $(COMPILER_PREFIX)-gcc
GPP := $(COMPILER_PREFIX)-g++
ASM := nasm
GAS := $(COMPILER_PREFIX)-as
RM := rm
LD := $(COMPILER_PREFIX)-ld
DD := dd
_MKFS := mkfs
GRUB_INSTALL := grub-install
GRUB_MODULES := normal part_msdos ext2 fat multiboot all_video
SUDO := sudo
MOUNT := mount
UMOUNT := umount
ECHO := echo
CD := cd
COPY := cp
OBJCOPY := $(COMPILER_PREFIX)-objcopy
FDISK := fdisk
LOOP_SETUP := losetup
CHMOD := chmod
AR := $(COMPILER_PREFIX)-ar
ifeq ($(ARCHITECTURE), x86_64)
QEMU := qemu-system-x86_64
endif
QEMU_ARGS ?=
QEMU_ARGS += -m size=256m,maxmem=256m \
--enable-kvm \
-hda tayhuangOS.img \
-serial stdio \
-rtc base=localtime \
-name "TayhuangOS"
QEMU_DEBUG_ARGS := $(QEMU_ARGS) -s -S
ifeq ($(FILESYSTEM), fat32)
MKFS := $(_MKFS).msdos -F 32 -n "TayhuangOS" -v -f 2
endif
export ROOTDIR BUILDDIR BINDIR OBJECTSDIR TAYHUANGOS_MOUNT_DIR TAYHUANGBOOT_MOUNT_DIR
export GCC GPP ASM GAS RM MKDIR LD DD _MKFS FILESYSTEM MKFS GRUB_INSTALL SUDO MOUNT UMOUNT ECHO CD COPY OBJCOPY FDISK AR
RAW_ICON := $(BINDIR)/tayicon.raw
TAYHUANG_ICON := $(ROOTDIR)/TayhuangOS.png
TOOLS_DIR := $(ROOTDIR)/tools/
PNG_CONV := $(TOOLS_DIR)/png_converter/converter.py
COUNTER := $(TOOLS_DIR)/build_counter/counter.py
COMMENTS_STAT := $(TOOLS_DIR)/comments_stat/stat.py
GET_LOOP := $(TOOLS_DIR)/get_loop_devices/get_loop.sh
# 本来应该放在定义区,但依赖于get_loop.sh
LOOPA ?= $(shell $(GET_LOOP) 0)
LOOPB ?= $(shell $(GET_LOOP) $(LOOPA))
# 任务区
# 下划线结尾的任务不应直接调用
#编译并写入映像
.PHONY: all
all: build image
$(ECHO) "done"
#重编译并写入映像
.PHONY: rebuild
rebuild: clean build image
$(ECHO) "done"
#设置环境并构建
.PHONY: setup_and_build
setup_and_build: setup_workspace all
$(ECHO) "done"
#设置环境
.PHONY: setup_workspace
setup_workspace:
$(ECHO) "TayhuangOS Version: $(VERSION)"
if [ ! -d "$(TAYHUANGOS_MOUNT_DIR)" ];then \
$(SUDO) $(MKDIR) $(TAYHUANGOS_MOUNT_DIR); \
else \
$(ECHO) "mount directory already created"; \
fi;
$(DD) if=/dev/zero of=$(TAYHUANGOS_IMG) bs=512 count=$(IMAGE_SECTORS)
$(MKDIR) $(BUILDDIR)
$(MKDIR) $(OBJECTSDIR)
$(MKDIR) $(BINDIR)
$(FDISK) $(TAYHUANGOS_IMG)
-$(MAKE) setup_workspace_losetup_
-$(SUDO) $(LOOP_SETUP) -d $(LOOPB) #losetup LOOPA失败不会影响LOOPB
$(SUDO) $(LOOP_SETUP) -d $(LOOPA)
$(SUDO) $(CHMOD) +x $(PNG_CONV)
$(SUDO) $(CHMOD) +x $(COUNTER)
$(SUDO) $(CHMOD) +x $(COMMENTS_STAT)
.PHONY: setup_workspace_losetup_
setup_workspace_losetup_:
$(SUDO) $(LOOP_SETUP) $(LOOPA) $(TAYHUANGOS_IMG)
$(SUDO) $(LOOP_SETUP) $(LOOPB) $(TAYHUANGOS_IMG) -o $(KERNEL_PARTITION_OFFSET)
$(SUDO) $(MKFS) $(LOOPB)
$(SUDO) $(MOUNT) $(LOOPB) $(TAYHUANGOS_MOUNT_DIR)
#TODO:添加UEFI支持
-$(SUDO) $(GRUB_INSTALL) --target=i386-pc --root-directory=$(TAYHUANGOS_MOUNT_DIR) --no-floppy --modules="$(GRUB_MODULES)" $(LOOPA)
$(SUDO) $(UMOUNT) $(TAYHUANGOS_MOUNT_DIR)
.PHONY: do_count
do_count:
$(COUNTER)
$(ECHO) "TayhuangOS Version: $(VERSION)"
.PHONY: show_version
show_version:
$(ECHO) "TayhuangOS Version: $(VERSION)"
.PHONY: stat_code_density
stat_code_density:
$(COMMENTS_STAT)
#编译
.PHONY: build
build: do_count
$(ECHO) "TayhuangOS Version: $(VERSION)"
$(CD) libs && $(MAKE) build
$(CD) loader && $(MAKE) build
$(CD) kernel && $(MAKE) build
$(CD) module && $(MAKE) build
ifeq ($(VBE_MODE), ENABLE)
$(MAKE) $(RAW_ICON)
endif
$(RAW_ICON): $(TAYHUANG_ICON)
$(PNG_CONV) $(TAYHUANG_ICON) $(RAW_ICON)
#清理
.PHONY: clean
clean:
$(ECHO) "TayhuangOS Version: $(VERSION)"
-$(CD) libs && $(MAKE) clean
-$(CD) loader && $(MAKE) clean
-$(CD) kernel && $(MAKE) clean
-$(CD) module && $(MAKE) clean
#写入映像
.PHONY: image
image:
$(ECHO) "TayhuangOS Version: $(VERSION)"
-$(MAKE) image_losetup_
-$(SUDO) $(UMOUNT) $(TAYHUANGOS_MOUNT_DIR)
-$(SUDO) $(LOOP_SETUP) -d $(LOOPB)
.PHONY: image_losetup_
image_losetup_:
$(SUDO) $(LOOP_SETUP) $(LOOPB) $(TAYHUANGOS_IMG) -o 1048576
$(SUDO) $(MOUNT) $(LOOPB) $(TAYHUANGOS_MOUNT_DIR)
$(SUDO) $(COPY) ./configs/grub.cfg $(TAYHUANGOS_MOUNT_DIR)/boot/grub
ifeq ($(VBE_MODE), ENABLE)
$(SUDO) $(COPY) $(RAW_ICON) $(TAYHUANGOS_MOUNT_DIR)/
endif
$(CD) loader && $(MAKE) image
$(CD) kernel && $(MAKE) image
$(CD) module && $(MAKE) image
.PHONY: run
run:
$(ECHO) "TayhuangOS Version: $(VERSION)"
$(QEMU) $(QEMU_ARGS)
.PHONY: debug
debug:
$(ECHO) "TayhuangOS Version: $(VERSION)"
$(QEMU) $(QEMU_DEBUG_ARGS)
.PHONY: build_and_run
build_and_run: all run
$(ECHO) "done"
.PHONY: build_and_debug
build_and_debug: all debug
$(ECHO) "done"