-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakefile
More file actions
371 lines (256 loc) · 10.3 KB
/
makefile
File metadata and controls
371 lines (256 loc) · 10.3 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# REQUIRES ROOT ACCESS TO BUILD THE FINAL DISK IMAGE
# make init_edk2 : fetch EDK2 and it's submodules.
# make all : build full system with disk images.
# make testbuildall : build full system in $(FSDIR) without generating disk images.
# make bootloader : build the FuzeBoot bootloader only.
# make kernel : build the kernel only.
# make testbuild : build the kernel and copies the bootloader over into in $(FSDIR), if present.
# make testboot : build the bootloader and copies the kernel over into in $(FSDIR), if present.
# make run : run full system from the disk image in $(IMGDIR).
# make testrun : run full system from the $(FSDIR) with the disk in R/W mode.
# STEPS FOR A FULL MANUAL BUILD:
# make init_edk2 # (optional; binary included)
# make dirs
# make bootloader
# make kernel
# make fs
# (at this stage, make any manual changes to the filesystem if any)
# make img
# DEV WORKFLOW:
# code...
# make testbuild
# make testrun
# toolchain:
CC = clang
LD = ld
AS = nasm
QEMU = qemu-system-x86_64
# outputs:
KNL = kernel
BOOTX64.EFI = BootX64.efi
IMG = OS.img
# disk image size in MiB:
IMGMB = 1
# QEMU Memory:
RAM = 8G
# output dirs:
# extra sources:
EDKDIR = edk2
EDKBRANCH = master
BOOTSRCDIR = boot
# EDK2 init & FuzeBootPkg:
EDKCFGDIR = edk2config
# output:
# base build folder:
BUILDDIR = build
# base binary folder:
BINDIR = $(BUILDDIR)/bin
# kernel:
KNLDIR = $(BINDIR)/kernel
# bootloader:
LDRBINDIR = $(BOOTSRCDIR)/$(BUILDDIR)
# root filesystem directory:
FSDIR = $(BUILDDIR)/fs
# bootloader in $(FSDIR):
EFIDIR = $(FSDIR)/EFI/BOOT
# kernel in $(FSDIR):
SYSDIR = $(FSDIR)/sys
# disk image output folder:
IMGDIR = $(BUILDDIR)/img
# loop-disk mount point:
MNTPNT = $(BUILDDIR)/mnt
# kernel sources:
C_KERNELSRC := $(shell find ./kernel/ -type f -name '*.c')
ASM_KERNELSRC := $(shell find ./kernel/ -type f -name '*.asm')
##$(shell find ./ -type d \( -path ./boot -o -path ./progs \) -prune -false -o -name '*.c')
# objs:
OBJ := ${C_KERNELSRC:.c=.o} ${ASM_KERNELSRC:.asm=.o}
# toolchain flags:
CFLAGS = -target x86_64-unknown-none-elf64 \
-O2 \
-std=c11 \
-Wall \
-Wextra \
-Werror \
-Wno-unused-but-set-variable \
-Wno-unused-parameter \
-Wno-unused-variable \
-Wno-pointer-arith \
-Wno-int-conversion \
-Wno-gnu-label-as-value \
-ffreestanding \
-nostdlib \
-nostdinc \
-nodefaultlibs \
-fno-pic \
-fno-stack-protector \
-fno-omit-frame-pointer \
-mcmodel=kernel \
-mno-red-zone \
-mno-mmx \
-mno-sse \
-mno-sse2 \
-mno-sse3 \
-mno-ssse3 \
-mno-sse4 \
-mno-sse4.1 \
-mno-sse4.2 \
-mno-80387 \
-I. \
-Ikernel -g3
LDFLAGS = -T linker.ld \
-no-pie \
-nostdlib \
-z max-page-size=0x1000
ASFLAGS = -O0 \
-f elf64
QEMUFLAGS = -bios ovmf/OVMF.fd \
-machine q35 \
-cpu max,kvm=off \
-d cpu_reset \
-serial stdio \
-device VGA,vgamem_mb=64 \
-hda $(IMGDIR)/$(IMG) \
-m $(RAM) \
-display sdl \
-smp cpus=4
# testing:
QEMURUNFLAGS = -bios ovmf/OVMF.fd \
-machine q35 \
-cpu max,kvm=off \
-d cpu_reset \
-serial stdio \
-device VGA,vgamem_mb=64 \
fat:rw:$(FSDIR)/ \
-m $(RAM) \
-display gtk \
-D log.txt \
-smp cpus=4 -no-reboot -no-shutdown -d int
# /dev/loop???:
LOOPDSK := 0
# START:
all : build_system
@echo "Output: $(IMGDIR)/$(IMG)"
@echo "make run"
# build the whole system with disk images:
build_system : dirs bootloader kernel fs img
# generate the build folder hierarchy:
dirs : build_dir bin_dir knl_dir fs_dir efi_dir_fs knl_dir_fs img_dir mnt_pnt
build_dir :
@mkdir -p $(BUILDDIR)
bin_dir :
@mkdir -p $(BUILDDIR)
@mkdir -p $(BINDIR)
knl_dir :
@mkdir -p $(BUILDDIR)
@mkdir -p $(BINDIR)
@mkdir -p $(KNLDIR)
fs_dir :
@mkdir -p $(BUILDDIR)
@mkdir -p $(FSDIR)
efi_dir_fs :
@mkdir -p $(BUILDDIR)
@mkdir -p $(FSDIR)
@mkdir -p $(EFIDIR)
knl_dir_fs :
@mkdir -p $(BUILDDIR)
@mkdir -p $(FSDIR)
@mkdir -p $(SYSDIR)
img_dir :
@mkdir -p $(BUILDDIR)
@mkdir -p $(IMGDIR)
mnt_pnt :
@mkdir -p $(BUILDDIR)
@mkdir -p $(MNTPNT)
# build FuzeBoot bootloader:
bootloader : compile_loader fetch_loader
compile_loader :
\cp -rf $(EDKCFGDIR)/* $(EDKDIR)/
cd $(EDKDIR); bash build.sh
fetch_loader :
mkdir -p $(LDRBINDIR)
\cp -f $(EDKDIR)/Build/FuzeBootPkg/RELEASE_GCC5/X64/$(BOOTX64.EFI) $(LDRBINDIR)/
# build Fuze kernel:
kernel : build_dir bin_dir knl_dir compile_kernel
# compile & link kernel:
compile_kernel : $(OBJ)
$(LD) $(LDFLAGS) $^ -o $(KNLDIR)/$(KNL)
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
%.o : %.asm
$(AS) $(ASFLAGS) $< -o $@
# load the system into the filesystem directory in $(FSDIR):
fs : fs_dir load_bootloader load_kernel
# load the bootloader into the filesystem:
load_bootloader : efi_dir_fs
\cp -f $(BOOTSRCDIR)/$(BUILDDIR)/$(BOOTX64.EFI) $(EFIDIR)/ 2> /dev/null || :
# load the kernel into the filesystem:
load_kernel : knl_dir knl_dir_fs
\cp -f $(KNLDIR)/$(KNL) $(SYSDIR) 2> /dev/null || :
# generate disk image from $(FSDIR):
img : gen_img fmt_img mount_img load_img umount_img
gen_img : img_dir
dd if=/dev/zero of=$(IMGDIR)/$(IMG) bs=1M count=$(IMGMB) oflag=sync
fmt_img : gen_img
@echo "sudo losetup -fP $(IMGDIR)/$(IMG)"
$(eval LOOPDSK=$(shell sudo losetup --show -fP $(IMGDIR)/$(IMG)))
sudo parted -s $(IMGDIR)/$(IMG) mktable gpt
sudo parted -s $(LOOPDSK) mkpart OS 1MiB 100%
sudo parted -s $(LOOPDSK) set 1 esp on
sudo mkfs.fat -F 32 $(LOOPDSK)p1
mount_img : mnt_pnt
sudo mount $(LOOPDSK)p1 -t msdos -o umask=0000 $(MNTPNT)
load_img : mount_img
\cp -rf $(FSDIR)/* $(MNTPNT)
umount_img :
sudo umount $(MNTPNT)
sudo losetup -d $(LOOPDSK)
# TESTING:
# build both bootloader and kernel into $(FSDIR) without generating disk images:
testbuildall : dirs bootloader kernel fs
# build bootloader only:
testbuildboot : testbuildloader
testbuildloader : testbuildbootloader
testbuildbootloader : bootloader fs
# build kernel only:
testbuild : testbuildkernel
testbuildkernel : kernel fs
# CI/CD:
ci : ci_ldr ci_knl
ci_knl : kernel
ci_ldr : compile_loader
# TEST-RUNNING:
# run the system from $(FSDIR) with the disk in R/W mode:
testrun :
@clear
$(QEMU) $(QEMURUNFLAGS)
# RUNNING:
run :
@clear
$(QEMU) $(QEMUFLAGS)
# EDK2: https://github.com/tianocore/edk2
edk : init_edk
init_edk : edk2
edk2 : init_edk2
init_edk2 :
rm -rf $(EDKDIR)
@mkdir -p $(EDKDIR)
git clone --depth=1 --branch $(EDKBRANCH) https://github.com/tianocore/edk2.git edk2
git -C ./$(EDKDIR)/ submodule update --init --recursive --progress
delete_edk2 :
rm -rf $(EDKDIR)
# clean:
clean :
rm -rf $(BUILDDIR)
rm -rf $(shell find ./ -type d \( -path ./edk2 \) -prune -false -o -name '*.o')
rm -rf edk2/Build/
rm -rf log.txt
clean_ci :
rm -rf $(BUILDDIR)
rm -rf $(shell find ./ -type d \( -path ./edk2 \) -prune -false -o -name '*.o')
rm -rf $(EDKDIR)/Build/FuzeBootPkg/RELEASE_GCC5/X64/$(BOOTX64.EFI)
clean_ci_knl :
rm -rf $(BUILDDIR)
rm -rf $(shell find ./ -type d \( -path ./edk2 \) -prune -false -o -name '*.o')
clean_ci_ldr :
rm -rf $(EDKDIR)/Build/FuzeBootPkg/RELEASE_GCC5/X64/$(BOOTX64.EFI)