-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.zig
325 lines (264 loc) · 9.91 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
const std = @import("std");
const Builder = std.build.Builder;
const builtin = std.builtin;
const assert = std.debug.assert;
fn here() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}
fn cpu_features(arch: std.Target.Cpu.Arch, ctarget: std.zig.CrossTarget) std.zig.CrossTarget {
var disabled_features = std.Target.Cpu.Feature.Set.empty;
var enabled_feautres = std.Target.Cpu.Feature.Set.empty;
if (arch == .aarch64) {
const features = std.Target.aarch64.Feature;
// This is equal to -mgeneral-regs-only
disabled_features.addFeature(@enumToInt(features.fp_armv8));
disabled_features.addFeature(@enumToInt(features.crypto));
disabled_features.addFeature(@enumToInt(features.neon));
}
return std.zig.CrossTarget{
.cpu_arch = arch,
.os_tag = ctarget.os_tag,
.abi = ctarget.abi,
.cpu_features_sub = disabled_features,
.cpu_features_add = enabled_feautres,
};
}
fn freestanding_target(elf: *std.build.LibExeObjStep, arch: std.Target.Cpu.Arch, do_code_model: bool) void {
if (arch == .aarch64) {
// We don't need the code model in asm blobs
if (do_code_model)
elf.code_model = .tiny;
}
if(arch == .riscv64) {
// Where are the riscv code models??
elf.code_model = .medium;
}
elf.setTarget(cpu_features(arch, .{
.os_tag = std.Target.Os.Tag.freestanding,
.abi = std.Target.Abi.none,
}));
}
fn executable_common(b: *Builder, exec: *std.build.LibExeObjStep, board_name: []const u8) void {
var options = b.addOptions();
options.addOption([]const u8, "board_name", board_name);
exec.addOptions("build_options", options);
exec.setBuildMode(.ReleaseSmall);
if (@hasField(@TypeOf(exec.*), "want_lto"))
exec.want_lto = false;
exec.setMainPkgPath(comptime here() ++ "/src/");
exec.setOutputDir(b.cache_root);
exec.install();
exec.disable_stack_probing = true;
}
pub fn build_uefi(b: *Builder, arch: std.Target.Cpu.Arch) !*std.build.LibExeObjStep {
const filename = "BOOTAA64";
const platform_path = b.fmt(comptime here() ++ "/src/platform/uefi_{s}", .{@tagName(arch)});
const exec = b.addExecutable(filename, b.fmt("{s}/main.zig", .{platform_path}));
executable_common(b, exec, "UEFI");
exec.code_model = .small;
exec.setTarget(cpu_features(arch, .{
.os_tag = std.Target.Os.Tag.uefi,
.abi = std.Target.Abi.msvc,
}));
exec.setOutputDir(comptime here() ++ "/uefidir/image/EFI/BOOT/");
return exec;
}
fn build_elf(b: *Builder, arch: std.Target.Cpu.Arch, target_name: []const u8) !*std.build.LibExeObjStep {
const elf_filename = b.fmt("Sabaton_{s}_{s}.elf", .{ target_name, @tagName(arch) });
const platform_path = b.fmt(comptime here() ++ "/src/platform/{s}_{s}", .{ target_name, @tagName(arch) });
const elf = b.addExecutable(elf_filename, b.fmt("{s}/main.zig", .{platform_path}));
elf.setLinkerScriptPath(.{ .path = b.fmt("{s}/linker.ld", .{platform_path}) });
elf.addAssemblyFile(b.fmt("{s}/entry.S", .{platform_path}));
executable_common(b, elf, target_name);
freestanding_target(elf, arch, true);
return elf;
}
fn assembly_blob(b: *Builder, arch: std.Target.Cpu.Arch, name: []const u8, asm_file: []const u8) !*std.build.InstallRawStep {
const elf_filename = b.fmt("{s}_{s}.elf", .{ name, @tagName(arch) });
const elf = b.addExecutable(elf_filename, null);
elf.setLinkerScriptPath(.{ .path = "src/blob.ld" });
elf.addAssemblyFile(asm_file);
freestanding_target(elf, arch, false);
elf.setBuildMode(.ReleaseSafe);
elf.setMainPkgPath("src/");
elf.setOutputDir(b.cache_root);
elf.install();
return elf.installRaw(b.fmt("{s}.bin", .{elf_filename}), .{
.format = .bin,
.only_section = ".blob",
.pad_to = null,
});
}
pub fn aarch64VirtBlob(b: *Builder) *std.build.InstallRawStep {
const elf = try build_elf(b, .aarch64, "virt");
return elf.installRaw(b.fmt("{s}.bin", .{elf.out_filename}), .{
.format = .bin,
.only_section = ".blob",
.pad_to = 64 * 1024 * 1024, // 64M
});
}
pub fn riscvVirtBlob(b: *Builder) *std.build.InstallRawStep {
const elf = try build_elf(b, .riscv64, "virt");
return elf.installRaw(b.fmt("{s}.bin", .{elf.out_filename}), .{
.format = .bin,
.only_section = ".blob",
.pad_to = 32 * 1024 * 1024, // 64M
});
}
fn qemu_aarch64(b: *Builder, board_name: []const u8, desc: []const u8) !void {
const command_step = b.step(board_name, desc);
const blob = aarch64VirtBlob(b);
const build_step = b.step("qemu-virt-build", "Build the qemu virt blob");
build_step.dependOn(&blob.step);
const blob_path = b.getInstallPath(blob.dest_dir, blob.dest_filename);
const run_step = b.addSystemCommand(&[_][]const u8{
// zig fmt: off
"qemu-system-aarch64",
"-M", board_name,
"-cpu", "cortex-a57",
"-m", "4G",
"-serial", "stdio",
//"-S", "-s",
"-d", "int",
"-smp", "8",
"-device", "ramfb",
"-kernel", "test/Flork_stivale2_aarch64",
"-drive", b.fmt("if=pflash,format=raw,file={s},readonly=on", .{blob_path}),
// zig fmt: on
});
run_step.step.dependOn(&blob.step);
command_step.dependOn(&run_step.step);
}
fn qemu_riscv(b: *Builder, desc: []const u8) !void {
const command_step = b.step("riscv-virt", desc);
const blob = riscvVirtBlob(b);
const build_step = b.step("qemu-riscv-virt-build", "Build the qemu riscv virt blob");
build_step.dependOn(&blob.step);
const blob_path = b.getInstallPath(blob.dest_dir, blob.dest_filename);
const run_step = b.addSystemCommand(&[_][]const u8{
// zig fmt: off
"qemu-system-riscv64",
"-M", "virt",
//"-cpu", "cortex-a57",
"-m", "4G",
"-serial", "stdio",
//"-S", "-s",
//"-d", "int",
//"-smp", "8",
"-device", "ramfb",
"-fw_cfg", "opt/Sabaton/kernel,file=test/uart_riscv.elf",
"-bios", blob_path,
// zig fmt: on
});
run_step.step.dependOn(&blob.step);
command_step.dependOn(&run_step.step);
}
fn qemu_pi3_aarch64(b: *Builder, desc: []const u8, elf: *std.build.LibExeObjStep) !void {
const command_step = b.step("pi3", desc);
const blob = elf.installRaw(b.fmt("{s}.bin", .{elf.out_filename}), .{
.format = .bin,
.only_section = ".blob",
.pad_to = null,
});
const blob_path = b.getInstallPath(blob.dest_dir, blob.dest_filename);
const run_step = b.addSystemCommand(&[_][]const u8{
// zig fmt: off
"qemu-system-aarch64",
"-M", "raspi3",
"-device", "loader,file=test/Flork_stivale2_aarch64,addr=0x200000,force-raw=on",
"-serial", "null",
"-serial", "stdio",
"-d", "int",
"-kernel", blob_path,
// zig fmt: off
});
run_step.step.dependOn(&blob.step);
command_step.dependOn(&run_step.step);
}
fn qemu_uefi_aarch64(b: *Builder, desc: []const u8, dep: *std.build.LibExeObjStep) !void {
const command_step = b.step("uefi", desc);
const run_step = b.addSystemCommand(&[_][]const u8{
// zig fmt: off
"qemu-system-aarch64",
"-M", "virt",
"-m", "4G",
"-cpu", "cortex-a57",
"-serial", "stdio",
"-device", "ramfb",
"-drive", b.fmt("if=pflash,format=raw,file={s}/QEMU_EFI.fd,readonly=on", .{std.os.getenv("AARCH64_EDK_PATH").?}),
"-drive", b.fmt("if=pflash,format=raw,file={s}/QEMU_VARS.fd", .{std.os.getenv("AARCH64_EDK_PATH").?}),
"-hdd", "fat:rw:uefidir/image",
"-usb",
"-device", "usb-ehci",
"-device", "usb-kbd",
// zig fmt: off
});
run_step.step.dependOn(&dep.install_step.?.step);
command_step.dependOn(&run_step.step);
}
const Device = struct {
name: []const u8,
arch: std.Target.Cpu.Arch,
};
const AssemblyBlobSpec = struct {
name: []const u8,
arch: std.Target.Cpu.Arch,
path: []const u8,
};
pub fn build(b: *Builder) !void {
//make_source_blob(b);
try qemu_aarch64(
b,
"virt",
"Run aarch64 sabaton for the qemu virt board",
);
try qemu_riscv(b,
"Run riscv64 sabaton on for the qemu virt board",
);
try qemu_pi3_aarch64(
b,
"Run aarch64 sabaton for the qemu raspi3 board",
try build_elf(b, .aarch64, "pi3"),
);
try qemu_uefi_aarch64(
b,
"Run aarch64 sabaton for UEFI",
try build_uefi(b, .aarch64),
);
{
const assembly_blobs = &[_]AssemblyBlobSpec{
.{ .path = "src/platform/pine_aarch64/identity.S", .name = "identity_pine", .arch = .aarch64 },
};
for (assembly_blobs) |spec| {
const blob_file = try assembly_blob(b, spec.arch, spec.name, spec.path);
b.default_step.dependOn(&blob_file.step);
}
}
{
const elf_devices = &[_]Device{};
for (elf_devices) |dev| {
const elf_file = try build_elf(b, .aarch64, dev.name);
const s = b.step(dev.name, b.fmt("Build the blob for {s}", .{dev.name}));
s.dependOn(&elf_file.step);
b.default_step.dependOn(s);
}
}
{
const blob_devices = &[_]Device{
.{ .name = "pine", .arch = .aarch64 },
.{ .name = "sdm665", .arch = .aarch64 },
.{ .name = "vision_five_v1", .arch = .riscv64 },
};
for (blob_devices) |dev| {
const elf = try build_elf(b, dev.arch, dev.name);
const blob_file = elf.installRaw(b.fmt("{s}_{s}.bin", .{dev.name, @tagName(dev.arch)}), .{
.format = .bin,
.only_section = ".blob",
.pad_to = null,
});
const s = b.step(dev.name, b.fmt("Build the blob for {s}", .{dev.name}));
s.dependOn(&blob_file.step);
b.default_step.dependOn(s);
}
}
}