-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxmake.lua
554 lines (423 loc) · 14.9 KB
/
xmake.lua
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
-- Copyright (C) 2024-2025 ilobilo
set_project("Ilobilix")
set_version("v0.2")
set_license("EUPL-1.2")
add_rules("plugin.compile_commands.autoupdate", { outputdir = ".vscode" })
set_policy("run.autobuild", true)
set_policy("package.install_locally", true)
set_policy("build.c++.modules", true)
set_policy("build.c++.modules.std", false)
set_languages("c++23", "c17")
set_allowedarchs("x86_64", "aarch64")
set_defaultarchs("x86_64")
set_allowedplats("ilobilix")
set_defaultplat("ilobilix")
-- debug
if is_mode("debug") then
set_symbols("debug")
set_optimize("none")
-- optimised debug
elseif is_mode("releasedbg") then
set_symbols("debug")
set_optimize("fastest")
-- release with -Os
elseif is_mode("releasesmall") then
set_strip("debug")
set_optimize("smallest")
-- release
elseif is_mode("release") then
set_strip("debug")
set_optimize("fastest")
end
set_defaultmode("releasedbg")
-- options -->
option("extra_cflags")
set_default("")
set_showmenu(true)
set_description("Extra CFLAGS")
option("extra_cxxflags")
set_default("")
set_showmenu(true)
set_description("Extra CXXFLAGS")
option("extra_qemuflags")
set_default("")
set_showmenu(true)
set_description("Extra QEMU flags")
option("qemu_proc")
set_default("6")
set_showmenu(true)
set_description("QEMU CPU count")
option("qemu_memory")
set_default("512M")
set_showmenu(true)
set_description("QEMU memory amount")
option("more_panic_msg")
set_default(true)
set_showmenu(true)
set_description("More information when panicking")
option("ubsan")
set_default(false)
set_showmenu(true)
set_description("UBSanitizer in kernel and modules")
option("syscall_log")
set_default(false)
set_showmenu(true)
set_description("Log system calls")
option("qemu_accel")
set_default(true)
set_showmenu(true)
set_description("QEMU accelerators. Disabled when debugging")
option("qemu_gdb")
set_default(false)
set_showmenu(true)
set_description("Pass '-s -S' to QEMU when debugging")
option("qemu_vnc")
set_default(false)
set_showmenu(true)
set_description("Start headless QEMU VNC server on localhost:5901")
-- <-- options
-- variables -->
local function multi_insert(list, ...)
for idx, val in ipairs({ ... }) do
list[#list + 1] = val
end
end
local function get_targetfile(tdir, prefix, ext)
local ret = path.join(tdir, prefix .. "-" .. get_config("arch") .. ext)
return ret
end
local logfile = os.projectdir() .. "/log.txt"
local qemu_args = {
"-rtc", "base=localtime", "-serial", "stdio",
"-boot", "order=d,menu=on,splash-time=0",
-- "-drive", "file=" .. os.projectdir() .. "/misc/nvme.img,format=raw,if=none,id=nvm",
-- "-device", "nvme,serial=nvme,drive=nvm",
-- "-nic", "user,model=rtl8139",
-- "-nic", "user,model=e1000"
}
local qemu_accel_args = {
"-M", "accel=kvm:hvf:whpx:haxm:tcg"
}
local qemu_dbg_args = {
"-no-reboot", "-no-shutdown",
"-d", "int", "-D", logfile,
-- "-monitor", "telnet:127.0.0.1:12345,server,nowait"
}
local bios = false
-- <-- variables
-- toolchain -->
toolchain("ilobilix-clang")
set_kind("standalone")
set_toolset("as", "clang")
set_toolset("cc", "clang")
set_toolset("cxx", "clang++", "clang")
set_toolset("ld", "ld.lld", "lld")
set_toolset("ar", "llvm-ar", "ar")
set_toolset("strip", "llvm-strip", "strip")
add_defines("ILOBILIX_SYSCALL_LOG=" .. (get_config("syscall_log") and "1" or "0"))
add_defines("ILOBILIX_EXTRA_PANIC_MSG=" .. (get_config("more_panic_msg") and "1" or "0"))
add_defines("ILOBILIX_MAX_UACPI_POINTS=0")
add_defines("LIMINE_API_REVISION=2")
add_defines("FLANTERM_FB_DISABLE_BUMP_ALLOC")
add_defines("UACPI_FORMATTED_LOGGING", "UACPI_OVERRIDE_LIBC", "UACPI_OVERRIDE_ARCH_HELPERS")
add_defines("MAGIC_ENUM_NO_STREAMS=1")
add_defines("FMT_STATIC_THOUSANDS_SEPARATOR=\"'\"", "FMT_USE_LOCALE=0", "FMT_THROW(x)=abort()")
-- TODO: performance impact
add_defines("FMT_OPTIMIZE_SIZE=2", "FMT_BUILTIN_TYPES=0")
on_load(function (toolchain)
local cx_args = {
"-ffreestanding",
"-fno-stack-protector",
"-fno-omit-frame-pointer",
"-mgeneral-regs-only",
"-nostdinc",
-- using set_warnings causes some argument ordering issues
"-Wall", "-Wextra",
"-Wno-error=#warnings",
"-Wno-c23-extensions",
-- "-Wno-builtin-macro-redefined",
-- "-Wno-macro-redefined",
-- "-Wno-deprecated-declarations",
-- "-Wno-nan-infinity-disabled",
-- "-Wno-frame-address"
}
local c_args = { }
local cxx_args = {
"-fno-rtti",
"-fno-exceptions",
"-fsized-deallocation",
"-fcheck-new"
}
local ld_args = {
"-nostdlib",
"-static",
"-znoexecstack"
}
local target = ""
if is_mode("releasesmall") or is_mode("release") then
table.insert(cx_args, "-flto")
toolchain:add("defines", "ILOBILIX_DEBUG=0");
else
toolchain:add("defines", "ILOBILIX_DEBUG=1");
end
if is_arch("x86_64") then
target = "x86_64-elf"
multi_insert(cx_args,
"-march=x86-64",
"-mno-red-zone",
"-mno-mmx",
"-mno-sse",
"-mno-sse2",
"-mno-80387",
"-mcmodel=kernel"
)
elseif is_arch("aarch64") then
target = "aarch64-elf"
table.insert(cx_args, "-mcmodel=small")
toolchain:add("defines", "UACPI_REDUCED_HARDWARE")
end
table.insert(cx_args, "--target=" .. target)
table.insert(c_args, get_config("extra_cflags"))
table.insert(cxx_args, get_config("extra_cxxflags"))
if get_config("ubsan") then
table.insert(cx_args, "-fsanitize=undefined")
toolchain:add("defines", "ILOBILIX_UBSAN")
end
toolchain:add("cxxflags", cxx_args, { force = true })
toolchain:add("cxflags", cx_args, { force = true })
toolchain:add("cflags", c_args, { force = true })
toolchain:add("asflags", cxx_args, { force = true })
toolchain:add("asflags", cx_args, { force = true })
toolchain:add("asflags", c_args, { force = true })
toolchain:add("ldflags", ld_args, { force = true })
end)
toolchain_end()
set_toolchains("ilobilix-clang")
-- <-- toolchain
-- dependencies -->
includes("dependencies/xmake.lua")
-- <-- dependencies
-- targets.build -->
includes("modules/xmake.lua")
includes("kernel/xmake.lua")
target("initramfs")
set_default(false)
set_kind("phony")
add_deps("modules")
on_clean(function (target)
os.tryrm(get_targetfile(target:targetdir(), "initramfs", ".tar"))
end)
on_build(function (target)
import("core.project.project")
import("core.project.depend")
import("lib.detect.find_program")
-- might not be created yet
os.mkdir(target:targetdir())
targetfile = get_targetfile(target:targetdir(), "initramfs", ".tar")
target:set("values", "targetfile", targetfile)
local sysroot = path.join(os.projectdir(), "userspace/sysroot")
local modules_dir = path.join(sysroot, "usr/lib/modules")
local modules = project.target("modules")
local extmods = modules:values("modules.external_modules")
local created = false
local function create_initramfs()
os.tryrm(targetfile)
os.tryrm(modules_dir)
os.mkdir(modules_dir)
print(" => copying external modules to sysroot...")
if extmods ~= nil then
for idx, val in ipairs(extmods) do
os.cp(val, modules_dir)
end
end
print(" => building the initramfs...")
os.execv(find_program("tar"), { "--format", "posix", "-cf", targetfile, "-C", sysroot, "./" })
created = true
end
if extmods ~= nil then
depend.on_changed(create_initramfs, { files = extmods })
end
if not created and not os.isfile(targetfile) then
create_initramfs()
end
end)
target("iso")
set_default(false)
set_kind("phony")
add_deps("ovmf-binaries", "limine", "initramfs", "modules", "ilobilix.elf")
on_clean(function (target)
os.rm(get_targetfile(target:targetdir(), "image", ".iso"))
end)
on_build(function (target)
import("core.project.project")
import("core.project.depend")
import("lib.detect.find_program")
targetfile = get_targetfile(target:targetdir(), "image", ".iso")
target:set("values", "targetfile", targetfile)
local kernel = project.target("ilobilix.elf")
local initramfs = project.target("initramfs")
local iso_dirname = "ilobilix.iso.dir"
local iso_dir = path.join(os.tmpdir(), iso_dirname)
local iso_dir_b = path.join(iso_dir, "boot")
local iso_dir_bl = path.join(iso_dir, "boot/limine")
local iso_dir_eb = path.join(iso_dir, "EFI/BOOT")
local limine_dep = target:deps()["limine"]
local limine_exec = limine_dep:targetfile()
local binaries = limine_dep:get("values")["binaries"]
local uefi_binaries = limine_dep:get("values")["uefi-binaries"]
local limine_files = {
"$(projectdir)/misc/limine.conf"
}
local xorriso_args = {
"-as", "mkisofs"
}
if is_arch("x86_64") then
multi_insert(xorriso_args,
"-b", "boot/limine/limine-bios-cd.bin",
"-no-emul-boot", "-boot-load-size", "4",
"-boot-info-table"
)
elseif is_arch("aarch64") then
end
multi_insert(xorriso_args,
"--efi-boot", "boot/limine/limine-uefi-cd.bin",
"-efi-boot-part", "--efi-boot-image",
"--protective-msdos-label"
)
local kernelfile = kernel:targetfile()
local initramfsfile = initramfs:get("values", "targetfile")["targetfile"]
local created = false
local function create_iso()
os.tryrm(targetfile)
os.tryrm(iso_dir)
os.mkdir(iso_dir)
os.mkdir(iso_dir_b)
os.mkdir(iso_dir_bl)
os.mkdir(iso_dir_eb)
print(" => copying target files to temporary iso directory...")
-- for idx, val in ipairs(boot_files) do
-- os.cp(val, iso_dir_b)
-- end
for idx, val in ipairs(limine_files) do
os.cp(val, iso_dir_bl)
end
for idx, val in ipairs(binaries) do
os.cp(val, iso_dir_bl)
end
for idx, val in ipairs(uefi_binaries) do
os.cp(val, iso_dir_eb)
end
os.cp(kernelfile, path.join(iso_dir_b, "kernel.elf"))
os.cp(initramfsfile, path.join(iso_dir_b, "initramfs.tar"))
multi_insert(xorriso_args,
iso_dir, "-o", targetfile
)
print(" => building the iso...")
os.execv(find_program("xorriso"), xorriso_args)
print(" => installing limine...")
os.execv(limine_exec, { "bios-install", targetfile })
created = true
os.tryrm(iso_dir)
end
depend.on_changed(create_iso, { files = { kernelfile, initramfsfile } })
if not created and not os.isfile(targetfile) then
create_iso()
end
end)
-- <-- targets.build
-- targets.run -->
task("qemu")
on_run(function ()
import("core.base.option")
import("core.project.project")
import("lib.detect.find_program")
local extra_qemu_args = get_config("extra_qemuflags")
if extra_qemu_args ~= "" then
table.insert(qemu_args, extra_qemu_args)
end
multi_insert(qemu_args, "-smp", get_config("qemu_proc"))
multi_insert(qemu_args, "-m", get_config("qemu_memory"))
if get_config("qemu_vnc") then
multi_insert(qemu_args,
"-vnc", "127.0.0.1:1"
)
end
local qemu_exec = ""
if is_arch("x86_64") then
bios = true
multi_insert(qemu_args,
"-cpu", "max,migratable=off,+invtsc,+tsc-deadline", "-M", "q35,smm=off"
-- "-audiodev", "id=audio,driver=alsa", "-machine", "pcspk-audiodev=audio"
)
qemu_exec = find_program("qemu-system-x86_64")
elseif is_arch("aarch64") then
bios = false
multi_insert(qemu_args,
"-cpu", "max", "-M", "virt", "-device", "ramfb"
)
qemu_exec = find_program("qemu-system-aarch64")
end
if not option.get("uefi") and not bios then
raise("BIOS not supported on this architecture")
end
if option.get("debug") then
multi_insert(qemu_args, unpack(qemu_dbg_args))
if get_config("qemu_gdb") then
multi_insert(qemu_args,
"-s", "-S"
)
end
elseif get_config("qemu_accel") then
multi_insert(qemu_args, unpack(qemu_accel_args))
end
local iso = project.target("iso")
local ovmf_fd = iso:deps()["ovmf-binaries"]:get("values")["ovmf-binary"]
multi_insert(qemu_args,
"-cdrom", iso:get("values", "targetfile")["targetfile"]
)
if option.get("uefi") then
multi_insert(qemu_args,
"-bios", ovmf_fd
)
end
print(" => running qemu...")
os.execv(qemu_exec, qemu_args)
end)
target("bios")
set_default(false)
set_kind("phony")
add_deps("iso")
on_run(function (target)
import("core.project.task")
import("core.project.project")
task.run("qemu", { uefi = false })
end)
target("bios-debug")
set_default(false)
set_kind("phony")
add_deps("iso")
on_run(function (target)
import("core.project.task")
import("core.project.project")
task.run("qemu", { uefi = false, debug = true })
end)
target("uefi")
set_default(true)
set_kind("phony")
add_deps("iso")
on_run(function (target)
import("core.project.task")
import("core.project.project")
task.run("qemu", { uefi = true })
end)
target("uefi-debug")
set_default(false)
set_kind("phony")
add_deps("iso")
on_run(function (target)
import("core.project.task")
import("core.project.project")
task.run("qemu", { uefi = true, debug = true })
end)
-- <-- targets.run
-- TODO: https://discord.com/channels/785173387962089492/785369106199216130/1289483363996139621