-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.zig
489 lines (408 loc) · 16.3 KB
/
build.zig
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
const std = @import("std");
const abiBuild = @import("ashet-abi");
const regz = @import("regz");
const Platform = abiBuild.Platform;
pub const Machine = @import("port/machine_id.zig").MachineID;
fn create_embedded_resource(b: *std.Build, path: []const u8) *std.Build.Module {
return b.createModule(.{
.root_source_file = b.path(path),
});
}
pub fn build(b: *std.Build) void {
// Options:
const machine_id = b.option(Machine, "machine", "Selects the machine for which the kernel should be built.") orelse @panic("-Dmachine required!");
const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseSafe });
// Target configuration:
const platform_id = machine_id.get_platform();
const machine_config = machine_info_map.get(machine_id);
const platform_config = platform_info_map.get(platform_id);
const kernel_target = b.resolveTargetQuery(machine_config.target);
// Dependencies:
const abi_dep = b.dependency("ashet-abi", .{});
const virtio_dep = b.dependency("virtio", .{});
const ashet_fs_dep = b.dependency("ashet_fs", .{});
const ashet_std_dep = b.dependency("ashet_std", .{});
const args_dep = b.dependency("args", .{});
const network_dep = b.dependency("network", .{});
const vnc_dep = b.dependency("vnc", .{});
const lwip_dep = b.dependency("lwip", .{ .target = kernel_target, .optimize = .ReleaseSafe });
const libc_dep = b.dependency("foundation-libc", .{
.target = kernel_target,
.optimize = optimize,
.single_threaded = true,
});
const zfat_dep = b.dependency("zfat", .{
.@"no-libc" = true,
.target = kernel_target,
.optimize = optimize,
.max_long_name_len = @as(u8, 121),
.code_page = .us,
.@"volume-count" = @as(u8, 8),
.@"static-rtc" = @as([]const u8, "2022-07-10"), // TODO: Fix this
.mkfs = true,
});
const libashetos_dep = b.dependency("libashetos", .{
.target = platform_id,
});
const agp_dep = b.dependency("agp", .{});
const agp_swrast_dep = b.dependency("agp_swrast", .{});
const turtlefont_dep = b.dependency("turtlefont", .{});
const ashex_dep = b.dependency("ashex", .{});
const xcvt_dep = b.dependency("xcvt", .{});
const shimizu_dep = b.dependency("shimizu", .{});
const zigx_dep = b.dependency("zigx", .{});
// Modules:
const abi_mod = abi_dep.module("ashet-abi");
const abi_impl_mod = abi_dep.module("ashet-abi-provider");
const virtio_mod = virtio_dep.module("virtio");
const ashet_fs_mod = ashet_fs_dep.module("ashet-fs");
const ashet_std_mod = ashet_std_dep.module("ashet-std");
const args_mod = args_dep.module("args");
const network_mod = network_dep.module("network");
const vnc_mod = vnc_dep.module("vnc");
const zfat_mod = zfat_dep.module("zfat");
const lwip_mod = lwip_dep.module("lwip");
const ashetos_mod = libashetos_dep.module("ashet");
const agp_mod = agp_dep.module("agp");
const agp_swrast_mod = agp_swrast_dep.module("agp-swrast");
const turtlefont_mod = turtlefont_dep.module("turtlefont");
const ashex_mod = ashex_dep.module("ashex");
const xcvt_mod = xcvt_dep.module("cvt");
const shimizu_mod = shimizu_dep.module("shimizu");
const wayland_protocols_mod = shimizu_dep.module("wayland-protocols");
const zig_mod = zigx_dep.module("x");
// Build:
const machine_info_module = blk: {
const machine_info = renderMachineInfo(
b,
machine_id,
platform_id,
) catch @panic("out of memory!");
const write_file_step = b.addWriteFile("machine-info.zig", machine_info);
const module = b.createModule(.{
.root_source_file = .{
.generated = .{
.file = &write_file_step.generated_directory,
.sub_path = write_file_step.files.items[0].sub_path,
},
},
});
break :blk module;
};
const kernel_mod = b.createModule(.{
.target = kernel_target,
.optimize = optimize,
.root_source_file = b.path("main.zig"),
.imports = &.{
.{ .name = "machine-info", .module = machine_info_module },
.{ .name = "ashet-abi", .module = abi_mod },
.{ .name = "ashet-abi-impl", .module = abi_impl_mod },
.{ .name = "ashet-std", .module = ashet_std_mod },
.{ .name = "virtio", .module = virtio_mod },
.{ .name = "ashet-fs", .module = ashet_fs_mod },
.{ .name = "args", .module = args_mod },
.{ .name = "fatfs", .module = zfat_mod },
.{ .name = "vnc", .module = vnc_mod },
.{ .name = "ashet", .module = ashetos_mod },
.{ .name = "agp", .module = agp_mod },
.{ .name = "agp-swrast", .module = agp_swrast_mod },
.{ .name = "turtlefont", .module = turtlefont_mod },
.{ .name = "ashex", .module = ashex_mod },
.{ .name = "cvt", .module = xcvt_mod },
// embedded resources:
.{
.name = "sans-6.font",
.module = create_embedded_resource(b, "../../rootfs/system/fonts/sans-6.font"),
},
.{
.name = "mono-6.font",
.module = create_embedded_resource(b, "../../rootfs/system/fonts/mono-6.font"),
},
.{
.name = "mono-8.font",
.module = create_embedded_resource(b, "../../rootfs/system/fonts/mono-8.font"),
},
// only required on hosted instances:
.{ .name = "network", .module = network_mod },
.{ .name = "x11", .module = zig_mod },
// .{ .name = "sdl", .module = options.modules.sd
},
});
kernel_mod.addImport("lwip", lwip_mod);
kernel_mod.addIncludePath(b.path("components/network/include"));
lwip_mod.addIncludePath(b.path("components/network/include"));
for (lwip_mod.include_dirs.items) |dir| {
kernel_mod.include_dirs.append(b.allocator, dir) catch @panic("out of memory");
}
if (machine_id == .@"arm-ashet-hc") {
const regz_dep = b.dependency("regz", .{
.optimize = .ReleaseSafe,
});
const regz_exe = regz_dep.artifact("regz");
const regz_run = b.addRunArtifact(regz_exe);
regz_run.addArg("--microzig");
regz_run.addArg("--format");
regz_run.addArg("svd");
regz_run.addArg("--output_path"); // Write to a file
const rp2350_register_file = regz_run.addOutputFileArg("rp2350.zig");
{
const patches = @import("port/machine/arm/ashet-hc/patches/rp2350_arm.zig").patches;
if (patches.len > 0) {
// write patches to file
const patch_ndjson = serialize_patches(
b,
patches,
);
const write_file_step = b.addWriteFiles();
const patch_file = write_file_step.add(
"patch.ndjson",
patch_ndjson,
);
regz_run.addArg("--patch_path");
regz_run.addFileArg(patch_file);
}
}
regz_run.addFileArg(b.path("port/machine/arm/ashet-hc/rp2350.svd"));
const microzig_shim_mod = b.createModule(.{
.root_source_file = b.path("utils/microzig-shim.zig"),
.imports = &.{
.{ .name = "kernel", .module = kernel_mod },
},
});
const rp2350_mod = b.createModule(.{
.root_source_file = rp2350_register_file,
.imports = &.{
.{ .name = "microzig", .module = microzig_shim_mod },
},
});
kernel_mod.addImport("rp2350", rp2350_mod);
const hal_dep = b.dependency("rp2xxx-hal", .{});
const hal_mod = b.createModule(.{
.root_source_file = hal_dep.path("hal.zig"),
.imports = &.{
.{ .name = "microzig", .module = microzig_shim_mod },
},
});
microzig_shim_mod.addImport("rp2350-chip", rp2350_mod);
microzig_shim_mod.addImport("rp2350-hal", hal_mod);
kernel_mod.addImport("rp2350-hal", hal_mod);
}
const start_file = if (machine_id.is_hosted())
b.path("port/platform/startup/hosted.zig")
else
b.path("port/platform/startup/generic.zig");
const kernel_exe = b.addExecutable(.{
.name = "kernel",
.root_source_file = start_file,
.target = kernel_target,
.optimize = optimize,
});
if (machine_id == .@"arm-ashet-hc" and optimize == .Debug) {
std.debug.print("arm-ashet-hc has no C sanitization enabled in Debug mode!\nSee https://github.com/ziglang/zig/issues/23052 and https://github.com/ziglang/zig/issues/23216 for more details!\n", .{});
kernel_exe.root_module.sanitize_c = false;
}
if (kernel_target.result.cpu.arch.isThumb()) {
// Disable LTO on arm as it fails hard on the linker:
kernel_exe.want_lto = false;
}
kernel_exe.step.dependOn(machine_info_module.root_source_file.?.generated.file.step);
kernel_exe.root_module.addImport("kernel", kernel_mod);
// TODO(fqu): kernel_exe.root_module.code_model = .small;
kernel_exe.bundle_compiler_rt = true;
// kernel_exe.rdynamic = true; // Prevent the compiler from garbage collecting exported symbols
kernel_exe.root_module.single_threaded = !machine_id.is_hosted();
kernel_exe.root_module.omit_frame_pointer = false;
kernel_exe.root_module.strip = false; // never strip debug info
if (optimize == .Debug) {
// we always want frame pointers in debug build!
kernel_exe.root_module.omit_frame_pointer = false;
}
kernel_exe.setLinkerScript(b.path(machine_config.linker_script));
// for (options.platforms.include_paths.get(machine_spec.platform).items) |path| {
// kernel_exe.addSystemIncludePath(path);
// }
_ = platform_config;
if (machine_id.is_hosted()) {
// kernel_mod.linkSystemLibrary("sdl2", .{
// .use_pkg_config = .force,
// .search_strategy = .mode_first,
// });
kernel_mod.addImport("shimizu", shimizu_mod);
kernel_mod.addImport("wayland-protocols", wayland_protocols_mod);
kernel_exe.linkage = .static;
kernel_exe.linkLibC();
} else {
const libc = libc_dep.artifact("foundation");
lwip_mod.addIncludePath(libc.getEmittedIncludeTree());
zfat_mod.addIncludePath(libc.getEmittedIncludeTree());
kernel_exe.linkLibrary(libc);
}
b.installArtifact(kernel_exe);
}
const PlatformConfig = struct {
source_file: []const u8,
};
const MachineConfig = struct {
target: std.Target.Query,
linker_script: []const u8,
source_file: []const u8,
};
fn constructTargetQuery(spec: std.Target.Query) std.Target.Query {
var base: std.Target.Query = spec;
if (base.os_tag == null) {
std.debug.assert(base.dynamic_linker.len == 0);
std.debug.assert(base.ofmt == null);
base.os_tag = .freestanding;
base.ofmt = .elf;
} else {
std.debug.assert(base.os_tag != .freestanding);
// We're in a hosted environment, explicit os is set
}
return base;
}
const platform_info_map = std.EnumArray(Platform, PlatformConfig).init(.{
.x86 = .{
.source_file = "port/platform/x86.zig",
},
.arm = .{
.source_file = "port/platform/arm.zig",
},
.rv32 = .{
.source_file = "port/platform/riscv.zig",
},
});
const machine_info_map = std.EnumArray(Machine, MachineConfig).init(.{
.@"x86-pc-bios" = .{
.target = constructTargetQuery(generic_x86),
.source_file = "port/machine/bios_pc/bios_pc.zig",
.linker_script = "port/machine/bios_pc/linker.ld",
},
.@"rv32-qemu-virt" = .{
.target = constructTargetQuery(generic_rv32),
.source_file = "port/machine/rv32_virt/rv32_virt.zig",
.linker_script = "port/machine/rv32_virt/linker.ld",
},
.@"arm-ashet-vhc" = .{
.target = constructTargetQuery(arm_cortex_m33),
.source_file = "port/machine/arm/ashet-vhc/ashet-vhc.zig",
.linker_script = "port/machine/arm/ashet-vhc/linker.ld",
},
.@"arm-ashet-hc" = .{
.target = constructTargetQuery(arm_cortex_m33),
.source_file = "port/machine/arm/ashet-hc/ashet-hc.zig",
.linker_script = "port/machine/arm/ashet-hc/linker.ld",
},
.@"arm-qemu-virt" = .{
.target = constructTargetQuery(generic_arm),
.source_file = "port/machine/arm_virt/arm_virt.zig",
.linker_script = "port/machine/arm_virt/linker.ld",
},
.@"x86-hosted-linux" = .{
.target = constructTargetQuery(.{
.cpu_arch = .x86,
.os_tag = .linux,
.abi = .musl,
.cpu_model = .{ .explicit = &std.Target.x86.cpu.i686 },
// .dynamic_linker = std.Target.DynamicLinker.init("/nix/store/xlyscnvzz5l3pkvf280qp5czg387b98f-glibc-2.38-44/lib/ld-linux.so.2"),
}),
.source_file = "port/machine/linux_pc/linux_pc.zig",
.linker_script = "port/machine/linux_pc/linker.ld",
},
});
const generic_x86: std.Target.Query = .{
.cpu_arch = .x86,
.abi = .eabi,
.cpu_model = .{ .explicit = &std.Target.x86.cpu.i686 },
.cpu_features_add = std.Target.x86.featureSet(&.{
.soft_float,
}),
.cpu_features_sub = std.Target.x86.featureSet(&.{
.x87,
}),
};
const generic_arm: std.Target.Query = .{
.cpu_arch = .thumb,
.abi = .eabi,
.cpu_model = .{
// .explicit = &std.Target.arm.cpu.cortex_a7, // this seems to be a pretty reasonable base line
// .explicit = &std.Target.arm.cpu.generic,
.explicit = &std.Target.arm.cpu.cortex_a7,
},
// .cpu_features_add = std.Target.arm.featureSet(&.{
// .v7a,
// }),
// .cpu_features_sub = std.Target.arm.featureSet(&.{
// .v7a, // this is stupid, but it keeps out all the neon stuff we don't wnat
// // drop everything FPU related:
// .neon,
// .neonfp,
// .neon_fpmovs,
// .fp64,
// .fpregs,
// .fpregs64,
// .vfp2,
// .vfp2sp,
// .vfp3,
// .vfp3d16,
// .vfp3d16sp,
// .vfp3sp,
// }),
};
const arm_cortex_m33: std.Target.Query = .{
.cpu_arch = .thumb,
.abi = .eabi,
.cpu_model = .{
.explicit = &std.Target.arm.cpu.cortex_m33,
},
.cpu_features_sub = std.Target.arm.featureSet(&.{
// Disable GPU in kernel
.slowfpvfmx,
.slowfpvmlx,
}),
};
const generic_rv32: std.Target.Query = .{
.cpu_arch = .riscv32,
.abi = .eabi,
.cpu_model = .{ .explicit = &std.Target.riscv.cpu.generic_rv32 },
.cpu_features_add = std.Target.riscv.featureSet(&[_]std.Target.riscv.Feature{
.i, // integer
.m, // multiplication
.c, // compressed
.zbb, // bit instructions
.zicsr, // control registers
// .reserve_x4, // Don't allow LLVM to use the "tp" register. We want that for our own purposes
}),
};
fn renderMachineInfo(
b: *std.Build,
machine_id: Machine,
platform_id: Platform,
// machine_spec: *const build_targets.MachineSpec,
// platform_spec: *const build_targets.PlatformSpec,
) ![]const u8 {
var stream = std.ArrayList(u8).init(b.allocator);
defer stream.deinit();
const writer = stream.writer();
try writer.writeAll("//! This is a machine-generated description of the Ashet OS target machine.\n\n");
try writer.print("pub const machine_id = .{};\n", .{
std.zig.fmtId(@tagName(machine_id)),
});
try writer.print("pub const machine_name = \"{}\";\n", .{
std.zig.fmtEscapes(machine_id.get_display_name()),
});
try writer.print("pub const platform_id = .{};\n", .{
std.zig.fmtId(@tagName(platform_id)),
});
try writer.print("pub const platform_name = \"{}\";\n", .{
std.zig.fmtEscapes(platform_id.get_display_name()),
});
return try stream.toOwnedSlice();
}
fn serialize_patches(b: *std.Build, patches: []const regz.patch.Patch) []const u8 {
var buf = std.ArrayList(u8).init(b.allocator);
for (patches) |patch| {
std.json.stringify(patch, .{}, buf.writer()) catch @panic("OOM");
buf.writer().writeByte('\n') catch @panic("OOM");
}
return buf.toOwnedSlice() catch @panic("OOM");
}