-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
146 lines (115 loc) · 4.31 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
const std = @import("std");
const Builder = @import("std").build.Builder;
const Target = @import("std").Target;
const CrossTarget = @import("std").zig.CrossTarget;
const Feature = @import("std").Target.Cpu.Feature;
pub fn build(b: *Builder) void {
const features = Target.aarch64.Feature;
var disabled_features = Feature.Set.empty;
disabled_features.addFeature(@enumToInt(features.ete));
disabled_features.addFeature(@enumToInt(features.fuse_aes));
disabled_features.addFeature(@enumToInt(features.neon));
disabled_features.addFeature(@enumToInt(features.perfmon));
disabled_features.addFeature(@enumToInt(features.use_postra_scheduler));
const cpu_model = cpu_model: {
const models = Target.Cpu.Arch.allCpuModels(.aarch64);
for (models) |model| {
if (std.mem.eql(u8, model.name, "cortex_a53")) {
break :cpu_model model;
}
}
unreachable;
};
const target = CrossTarget{
.cpu_arch = Target.Cpu.Arch.aarch64,
.os_tag = Target.Os.Tag.freestanding,
.abi = Target.Abi.none,
.cpu_model = .{ .explicit = cpu_model },
};
const mode = b.standardReleaseOptions();
const kernel_step = kernel_step: {
const kernel = b.addExecutable("kernel.elf", "src/main.zig");
kernel.addIncludeDir("src");
kernel.addAssemblyFile("src/boot.S");
kernel.setTarget(target);
kernel.setBuildMode(mode);
kernel.setLinkerScriptPath(.{ .path = "src/link.ld" });
kernel.install();
const kernel_step = b.step("kernel", "Build the kernel");
kernel_step.dependOn(&kernel.install_step.?.step);
break :kernel_step kernel_step;
};
// TODO: zig objcopy will eventually be a thing, and when it is, we can
// remove the dependency on llvm-objcopy
const iso_step = iso_step: {
const iso_cmd_str = &[_][]const u8{
"llvm-objcopy",
"-Obinary",
"zig-out/bin/kernel.elf",
"zig-out/bin/kernel8.img",
};
const iso_cmd = b.addSystemCommand(iso_cmd_str);
iso_cmd.step.dependOn(kernel_step);
const iso_step = b.step("img", "Build a usable img");
iso_step.dependOn(&iso_cmd.step);
break :iso_step iso_step;
};
b.default_step.dependOn(iso_step);
{
const run_cmd_str = &[_][]const u8{
"qemu-system-aarch64",
"-M",
"raspi3b",
"-cpu",
"cortex-a57",
"-serial",
"null",
"-chardev",
"stdio,id=uart1",
"-serial",
"chardev:uart1",
"-kernel",
"./zig-out/bin/kernel8.img",
};
const run_cmd = b.addSystemCommand(run_cmd_str);
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("run", "Run the Kernel in QEMU");
run_step.dependOn(&run_cmd.step);
const info_cmd_str = &[_][]const u8{
"echo",
\\The GDB Server will open on localhost:1234 .
\\
\\Open with LLDB:
\\ lldb zig-out/bin/kernel.elf
\\
\\Then, attach to the server:
\\ gdb-remote localhost:1234
\\
\\Or you can open in VS Code (may not actually work):
\\ vscode://vadimcn.vscode-lldb/launch?name=Remote%20Attach
};
const info_cmd = b.addSystemCommand(info_cmd_str);
info_cmd.step.dependOn(b.getInstallStep());
const debug_cmd_str = run_cmd_str ++ &[_][]const u8{
"-S",
"-s",
"-no-reboot",
"-no-shutdown",
};
const debug_cmd = b.addSystemCommand(debug_cmd_str);
debug_cmd.step.dependOn(&info_cmd.step);
const debug_step = b.step("debug", "Debug the Kernel in QEMU");
debug_step.dependOn(&debug_cmd.step);
}
{
const dump_cmd_str = &[_][]const u8{
"/bin/sh",
"-c",
"llvm-objdump --arch-name=aarch64 -D ./zig-out/bin/kernel.elf > ./zig-out/bin/dump.txt",
};
const dump_cmd = b.addSystemCommand(dump_cmd_str);
dump_cmd.step.dependOn(kernel_step);
const dump_step = b.step("dump", "Obj-Dump the Kernel ELF file");
dump_step.dependOn(&dump_cmd.step);
}
}