-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.zig
100 lines (85 loc) · 2.68 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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
_ = b.addModule("user", .{
.root_source_file = b.path("src/user.zig"),
});
_ = b.addModule("kern", .{
.root_source_file = b.path("src/kern.zig"),
});
_ = b.addModule("VM", .{
.root_source_file = b.path("src/VM.zig"),
.target = target,
.optimize = optimize,
});
const vm_unit_tests = b.addTest(.{
.root_source_file = b.path("src/VM.zig"),
});
add_bpf_files(b, vm_unit_tests, .{
.name = "examples",
.path = b.path("src/insn/examples.bpf.zig"),
.sections = &.{
"load",
"return_one",
},
});
const run_vm_unit_tests = b.addRunArtifact(vm_unit_tests);
const test_vm_step = b.step("test-vm", "Run unit tests");
test_vm_step.dependOn(&run_vm_unit_tests.step);
const test_step = b.step("test", "Run all tests");
test_step.dependOn(test_vm_step);
}
const Add_BPF_FileOptions = struct {
name: []const u8,
path: std.Build.LazyPath,
sections: []const []const u8,
};
fn add_bpf_files(
b: *std.Build,
compile: *std.Build.Step.Compile,
opts: Add_BPF_FileOptions,
) void {
add_bpf_file(b, compile, .little, opts);
add_bpf_file(b, compile, .big, opts);
}
fn add_bpf_file(
b: *std.Build,
compile: *std.Build.Step.Compile,
endian: std.builtin.Endian,
opts: Add_BPF_FileOptions,
) void {
const module_name = b.fmt("{s}-{s}", .{ opts.name, switch (endian) {
.little => "el",
.big => "eb",
} });
const obj = b.addObject(.{
.name = module_name,
.target = b.resolveTargetQuery(.{
.cpu_arch = switch (endian) {
.little => .bpfel,
.big => .bpfeb,
},
}),
.optimize = .ReleaseSmall,
.root_source_file = opts.path,
});
obj.link_function_sections = true;
const install = b.addInstallFile(obj.getEmittedBin(), b.fmt("{s}.elf", .{module_name}));
b.getInstallStep().dependOn(&install.step);
const options = b.addOptions();
for (opts.sections) |section| {
const objcopy = b.addObjCopy(
obj.getEmittedBin(),
.{
.format = .bin,
.only_section = switch (@import("builtin").os.tag) {
.macos => b.fmt(".text._{s}", .{section}),
else => b.fmt(".text.{s}", .{section}),
},
},
);
options.addOptionPath(section, objcopy.getOutput());
}
compile.root_module.addOptions(module_name, options);
}