-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.zig
More file actions
330 lines (289 loc) · 12.1 KB
/
build.zig
File metadata and controls
330 lines (289 loc) · 12.1 KB
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
const std = @import("std");
// TODO(jared): Get this automatically from importing the information in
// build.zig.zon.
const version = std.SemanticVersion.parse("0.1.0") catch @compileError("invalid version");
fn tbootInitrd(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
strip: bool,
zstd: *std.Build.Step.Compile,
clap: *std.Build.Module,
) *std.Build.Step.Compile {
const tboot_initrd_module = b.createModule(.{
.root_source_file = b.path("src/tboot-initrd.zig"),
.target = target,
.optimize = optimize,
.strip = strip,
});
const tboot_initrd = b.addExecutable(.{
.name = "tboot-initrd",
.root_module = tboot_initrd_module,
});
tboot_initrd.linkLibC();
tboot_initrd.linkLibrary(zstd);
tboot_initrd.root_module.addImport("clap", clap);
return tboot_initrd;
}
pub fn build(b: *std.Build) !void {
const tboot_builtin = b.addOptions();
tboot_builtin.addOption(
[]const u8,
"version",
try std.fmt.allocPrint(b.allocator, "{f}", .{version}),
);
var env = try std.process.getEnvMap(b.allocator);
defer env.deinit();
const target = b.standardTargetOptions(.{ .default_target = .{ .cpu_model = .baseline } });
const linux_target = b: {
var linux_target = target.query;
linux_target.abi = null;
linux_target.os_tag = .linux;
break :b b.resolveTargetQuery(linux_target);
};
const uefi_target = b: {
var uefi_target = target.query;
uefi_target.os_tag = .uefi;
uefi_target.abi = .msvc;
break :b b.resolveTargetQuery(uefi_target);
};
const optimize = b.standardOptimizeOption(.{});
const do_strip = optimize != std.builtin.OptimizeMode.Debug;
// For certain outputs of this project, we always use release small (if we
// are in release mode). Smallest size is our goal in order to minimize
// footprint on flash.
const optimize_prefer_small = if (optimize == std.builtin.OptimizeMode.Debug)
std.builtin.OptimizeMode.Debug
else
std.builtin.OptimizeMode.ReleaseSmall;
const with_loader_efi_stub = b.option(
bool,
"loader-efi-stub",
"With boot loader EFI stub (not available on all architectures)",
) orelse
// We get the following error when attempting to build for armv7, so we
// default to not building the EFI stub for this architecture.
//
// "error: the following command terminated unexpectedly"
!target.result.cpu.arch.isArm();
const firmware_directory = b.option(
[]const u8,
"firmware-directory",
"Firmware directory to put in /lib/firmware of the initrd",
);
const runner_keydir = b.option([]const u8, "keydir", "Directory of keys to use when spawning VM runner (as output by tboot-keygen)");
const runner_kernel = b.option([]const u8, "kernel", "Kernel to use when spawning VM runner") orelse env.get("TINYBOOT_KERNEL");
const clap_dependency = b.dependency("clap", .{});
const clap = clap_dependency.module("clap");
const mbedtls_dependency = b.dependency("mbedtls", .{ .target = target, .optimize = optimize });
const mbedtls = mbedtls_dependency.artifact("mbedtls");
const zstd_dependency = b.dependency("zstd", .{ .target = target, .optimize = optimize });
const zstd = zstd_dependency.artifact("zstd");
const build_zstd_dependency = b.dependency("zstd", .{ .target = b.graph.host, .optimize = .Debug });
const build_zstd = build_zstd_dependency.artifact("zstd");
const linux_h = b.addWriteFile("linux.h",
\\#include <asm-generic/setup.h>
\\#include <linux/kexec.h>
\\#include <linux/keyctl.h>
\\#include <linux/major.h>
\\#include <sys/epoll.h>
\\#include <sys/ioctl.h>
\\#include <termios.h>
\\
++ @embedFile("vendor/liveupdate.h"));
const linux_headers = b.addTranslateC(.{
.root_source_file = .{ .generated = .{ .file = &linux_h.generated_directory, .sub_path = "linux.h" } },
.target = linux_target,
.optimize = .ReleaseSafe, // This doesn't seem to do anything when translating pure headers
});
const linux_headers_module = linux_headers.addModule("linux_headers");
b.installArtifact(tbootInitrd(b, target, optimize, do_strip, zstd, clap));
const tboot_sign_module = b.createModule(.{
.root_source_file = b.path("src/tboot-sign.zig"),
.target = target,
.optimize = optimize,
.strip = false,
});
const tboot_sign = b.addExecutable(.{
.name = "tboot-sign",
.root_module = tboot_sign_module,
});
tboot_sign.root_module.link_libc = true;
tboot_sign.root_module.linkLibrary(mbedtls);
tboot_sign.root_module.addImport("clap", clap);
b.installArtifact(tboot_sign);
const tboot_keygen_module = b.createModule(.{
.root_source_file = b.path("src/tboot-keygen.zig"),
.target = target,
.optimize = optimize,
.strip = false,
});
const tboot_keygen = b.addExecutable(.{
.name = "tboot-keygen",
.root_module = tboot_keygen_module,
});
tboot_keygen.root_module.link_libc = true;
tboot_keygen.root_module.linkLibrary(mbedtls);
tboot_keygen.root_module.addImport("clap", clap);
b.installArtifact(tboot_keygen);
const tboot_vpd_module = b.createModule(.{
.root_source_file = b.path("src/vpd.zig"),
.target = target,
.optimize = optimize,
.strip = false,
});
const tboot_vpd = b.addExecutable(.{
.name = "tboot-vpd",
.root_module = tboot_vpd_module,
});
tboot_vpd.root_module.addImport("clap", clap);
b.installArtifact(tboot_vpd);
// tboot-ymodem, tboot-bless-boot, tboot-bless-boot-generator, and
// tboot-nixos-install (for nixos machines) run on the machine using
// tboot-loader, so it doesn't make sense to build for non-linux targets.
if (target.result.os.tag == .linux) {
// TODO(jared): get tboot-ymodem working on non-linux targets
const tboot_ymodem_module = b.createModule(.{
.root_source_file = b.path("src/ymodem.zig"),
.target = target,
.optimize = optimize,
.strip = false,
});
const tboot_ymodem = b.addExecutable(.{
.name = "tboot-ymodem",
.root_module = tboot_ymodem_module,
});
tboot_ymodem.root_module.addImport("linux_headers", linux_headers_module);
tboot_ymodem.root_module.addImport("clap", clap);
b.installArtifact(tboot_ymodem);
const tboot_bless_boot_module = b.createModule(.{
.root_source_file = b.path("src/tboot-bless-boot.zig"),
.target = target,
.optimize = optimize,
.strip = false,
});
const tboot_bless_boot = b.addExecutable(.{
.name = "tboot-bless-boot",
.root_module = tboot_bless_boot_module,
});
tboot_bless_boot.root_module.addImport("linux_headers", linux_headers_module);
tboot_bless_boot.root_module.addImport("clap", clap);
b.installArtifact(tboot_bless_boot);
const tboot_bless_boot_generator_module = b.createModule(.{
.root_source_file = b.path("src/tboot-bless-boot-generator.zig"),
.target = target,
.optimize = optimize,
.strip = false,
});
const tboot_bless_boot_generator = b.addExecutable(.{
.name = "tboot-bless-boot-generator",
.root_module = tboot_bless_boot_generator_module,
});
tboot_bless_boot_generator.root_module.addImport("clap", clap);
b.installArtifact(tboot_bless_boot_generator);
const tboot_nixos_install_module = b.createModule(.{
.root_source_file = b.path("src/tboot-nixos-install.zig"),
.target = target,
.optimize = optimize,
.strip = false,
});
const tboot_nixos_install = b.addExecutable(.{
.name = "tboot-nixos-install",
.root_module = tboot_nixos_install_module,
});
tboot_nixos_install.root_module.link_libc = true;
tboot_nixos_install.root_module.linkLibrary(mbedtls);
tboot_nixos_install.root_module.addImport("clap", clap);
b.installArtifact(tboot_nixos_install);
}
const tboot_loader_module = b.createModule(.{
.root_source_file = b.path("src/tboot-loader.zig"),
.target = linux_target,
.optimize = optimize_prefer_small,
.strip = do_strip,
});
const tboot_loader = b.addExecutable(.{
.name = "tboot-loader",
.root_module = tboot_loader_module,
});
tboot_loader.root_module.addOptions("tboot_builtin", tboot_builtin);
tboot_loader.root_module.addImport("linux_headers", linux_headers_module);
// Use tboot-initrd built for the build host.
var run_tboot_initrd = b.addRunArtifact(tbootInitrd(b, b.graph.host, .Debug, false, build_zstd, clap));
// TODO(jared): Would be nicer to have generic
// --file=tboot_loader:/init CLI interface, but don't know how to
// obtain path and string format it into that form. Further, would
// be nicer to not shell-out to a separate tool at all and just do
// the CPIO generation in here.
run_tboot_initrd.addPrefixedFileArg("-i", tboot_loader.getEmittedBin());
if (firmware_directory) |directory| {
const directory_ = b.addWriteFiles().addCopyDirectory(
.{ .cwd_relative = directory },
"",
.{},
);
run_tboot_initrd.addPrefixedDirectoryArg("-d", directory_);
}
const initrd_output_file = run_tboot_initrd.addPrefixedOutputFileArg(
"-o",
"tboot-loader.cpio.zst",
);
run_tboot_initrd.expectExitCode(0);
const initrd_file = b.addInstallFile(
initrd_output_file,
"tboot-loader.cpio.zst",
);
// install the cpio archive during "zig build install"
b.getInstallStep().dependOn(&initrd_file.step);
if (with_loader_efi_stub) {
const tboot_efi_stub_module = b.createModule(.{
.target = uefi_target,
.root_source_file = b.path("src/tboot-efi-stub.zig"),
.optimize = optimize_prefer_small,
.strip = do_strip,
});
const tboot_efi_stub = b.addExecutable(.{
.name = "tboot-efi-stub",
.root_module = tboot_efi_stub_module,
});
const tboot_efi_stub_artifact = b.addInstallArtifact(tboot_efi_stub, .{
.dest_dir = .{ .override = .{ .custom = "efi" } },
});
b.getInstallStep().dependOn(&tboot_efi_stub_artifact.step);
}
const tboot_runner_module = b.createModule(.{
.target = b.graph.host,
.root_source_file = b.path("src/runner.zig"),
});
const tboot_runner = b.addExecutable(.{
.name = "tboot-runner",
.root_module = tboot_runner_module,
});
tboot_runner.root_module.addImport("clap", clap);
const runner_tool = b.addRunArtifact(tboot_runner);
runner_tool.step.dependOn(&initrd_file.step);
runner_tool.addArg(@tagName(target.result.cpu.arch));
runner_tool.addArg(if (runner_keydir) |keydir| keydir else "");
runner_tool.addFileArg(initrd_file.source);
runner_tool.addArg(if (runner_kernel) |kernel| try std.fs.cwd().realpathAlloc(b.allocator, kernel) else "");
// Extra arguments passed through to qemu. We add our own '--' since
// zig-clap will accept variadic extra arguments only after the
// '--', which `zig build ...` already excepts.
if (b.args) |args| {
runner_tool.addArg("--");
runner_tool.addArgs(args);
}
const run_step = b.step("run", "Run in qemu");
run_step.dependOn(&runner_tool.step);
const unit_tests_module = b.createModule(.{
.root_source_file = b.path("src/test.zig"),
.target = target,
.optimize = optimize,
});
const unit_tests = b.addTest(.{
.root_module = unit_tests_module,
});
unit_tests.root_module.addImport("linux_headers", linux_headers_module);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&b.addRunArtifact(unit_tests).step);
}