-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
80 lines (69 loc) · 2.93 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
const std = @import("std");
const Target = std.Target;
const FeatureSet = Target.Cpu.Feature.Set;
pub fn build(b: *std.Build) void {
const arch = b.standardTargetOptions(.{}).result.cpu.arch;
if (arch != .x86_64) @panic("Only x86-64 is supported at this time.");
const features = switch (arch) {
.x86_64 => x86_64: {
const Feature = Target.x86.Feature;
// Disable all hardware floating point features.
var features_sub = FeatureSet.empty;
features_sub.addFeature(@intFromEnum(Feature.x87));
features_sub.addFeature(@intFromEnum(Feature.mmx));
features_sub.addFeature(@intFromEnum(Feature.sse));
features_sub.addFeature(@intFromEnum(Feature.sse2));
features_sub.addFeature(@intFromEnum(Feature.avx));
features_sub.addFeature(@intFromEnum(Feature.avx2));
// Enable software floating point instead.
var features_add = FeatureSet.empty;
features_add.addFeature(@intFromEnum(Feature.soft_float));
// Require some modern CPU features for optimization.
features_add.addFeature(@intFromEnum(Feature.popcnt));
break :x86_64 .{ features_add, features_sub };
},
else => unreachable,
};
const target = b.resolveTargetQuery(.{
.cpu_arch = arch,
.os_tag = .freestanding,
.abi = .none,
.cpu_features_add = features[0],
.cpu_features_sub = features[1],
});
// Create the kernel executable.
const kernel = b.addExecutable(.{
.name = "lyra",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = b.standardOptimizeOption(.{}),
.strip = true, // Reduce binary size.
.code_model = .kernel, // Higher half kernel.
.linkage = .static, // Disable dynamic linking.
.pic = false, // Disable position independent code.
.omit_frame_pointer = false, // Needed for stack traces.
});
// Disable features that are problematic in kernel space.
kernel.root_module.red_zone = false;
kernel.root_module.stack_check = false;
kernel.root_module.stack_protector = false;
kernel.want_lto = false;
// Delete unused sections to reduce the kernel size.
kernel.link_function_sections = true;
kernel.link_data_sections = true;
kernel.link_gc_sections = true;
// Force the page size to 4 KiB to prevent binary bloat.
kernel.link_z_max_page_size = 0x1000;
switch (arch) {
.x86_64 => {
kernel.addAssemblyFile(b.path("src/arch/x86_64/int/isr_stubs.s"));
kernel.setLinkerScript(b.path("src/arch/x86_64/linker.ld"));
if (b.lazyDependency("cpuid", .{})) |cpuid| {
const module = cpuid.module("cpuid");
kernel.root_module.addImport("cpuid", module);
}
},
else => unreachable,
}
b.installArtifact(kernel);
}