-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
160 lines (138 loc) · 5.36 KB
/
build.zig
File metadata and controls
160 lines (138 loc) · 5.36 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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// runz container runtime library
const runz_dep = b.dependency("runz", .{
.target = target,
.optimize = optimize,
});
const runz_module = runz_dep.module("runz");
// Compile xenomorph-init binary (embedded into xenomorph at build time)
const init_exe = b.addExecutable(.{
.name = "xenomorph-init",
.root_module = b.createModule(.{
.root_source_file = b.path("src/xenomorph-init.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
// Create a module that provides the init binary bytes via @embedFile.
// We write a small Zig file that imports the binary, and add the binary
// as a named resource so @embedFile can find it.
const wf = b.addWriteFiles();
_ = wf.addCopyFile(init_exe.getEmittedBin(), "xenomorph-init");
const embed_mod = b.createModule(.{
.root_source_file = wf.add("init_bin.zig",
\\pub const data = @embedFile("xenomorph-init");
),
});
// Helper to create a xenomorph module with all dependencies
const makeXenomorphModule = struct {
fn f(
b_: *std.Build,
target_: std.Build.ResolvedTarget,
optimize_: std.builtin.OptimizeMode,
runz_mod: *std.Build.Module,
embed: *std.Build.Module,
) *std.Build.Module {
const m = b_.createModule(.{
.root_source_file = b_.path("src/main.zig"),
.target = target_,
.optimize = optimize_,
.link_libc = true,
});
m.addImport("runz", runz_mod);
m.addImport("init_bin", embed);
return m;
}
}.f;
// Main executable
const exe = b.addExecutable(.{
.name = "xenomorph",
.root_module = makeXenomorphModule(b, target, optimize, runz_module, embed_mod),
});
b.installArtifact(exe);
b.installArtifact(init_exe);
// Run command
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run xenomorph");
run_step.dependOn(&run_cmd.step);
// Inline tests
const inline_tests = b.addTest(.{
.root_module = makeXenomorphModule(b, target, optimize, runz_module, embed_mod),
});
// Unit test suite
const unit_module = b.createModule(.{
.root_source_file = b.path("tests/unit/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
unit_module.addImport("xenomorph", makeXenomorphModule(b, target, optimize, runz_module, embed_mod));
unit_module.addImport("runz", runz_module);
unit_module.addImport("init_bin", embed_mod);
const unit_tests = b.addTest(.{
.root_module = unit_module,
});
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&b.addRunArtifact(inline_tests).step);
test_step.dependOn(&b.addRunArtifact(unit_tests).step);
// Integration tests
const integration_module = b.createModule(.{
.root_source_file = b.path("tests/integration/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
integration_module.addImport("xenomorph", makeXenomorphModule(b, target, optimize, runz_module, embed_mod));
const integration_tests = b.addTest(.{
.root_module = integration_module,
});
const integration_test_step = b.step("test-integration", "Run integration tests (requires root)");
integration_test_step.dependOn(&b.addRunArtifact(integration_tests).step);
// QEMU test
const qemu_test_exe = b.addExecutable(.{
.name = "qemu-test",
.root_module = b.createModule(.{
.root_source_file = b.path("tests/integration/qemu_test.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
b.installArtifact(qemu_test_exe);
const run_qemu_test = b.addRunArtifact(qemu_test_exe);
run_qemu_test.step.dependOn(b.getInstallStep());
const qemu_test_step = b.step("test-qemu", "Run QEMU integration test (requires kernel, busybox, qemu)");
qemu_test_step.dependOn(&run_qemu_test.step);
// Fuzz targets
const fuzz_module = b.createModule(.{
.root_source_file = b.path("tests/fuzz.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
fuzz_module.addImport("xenomorph", makeXenomorphModule(b, target, optimize, runz_module, embed_mod));
fuzz_module.addImport("runz", runz_module);
const fuzz_tests = b.addTest(.{
.root_module = fuzz_module,
});
const fuzz_step = b.step("fuzz", "Run fuzz tests (use -ffuzz for continuous fuzzing)");
fuzz_step.dependOn(&b.addRunArtifact(fuzz_tests).step);
// Valgrind step
const valgrind_step = b.step("valgrind", "Run tests under valgrind");
const valgrind_run = b.addSystemCommand(&.{
"valgrind",
"--leak-check=full",
"--error-exitcode=1",
"--track-origins=yes",
});
valgrind_run.addArtifactArg(inline_tests);
valgrind_step.dependOn(&valgrind_run.step);
}