-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.zig
244 lines (209 loc) · 8.79 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
const std = @import("std");
const builtin = @import("builtin");
const fs = std.fs;
fn create_bpf_prog(ctx: *const Ctx, src_path: []const u8) std.Build.LazyPath {
const name = fs.path.stem(src_path);
const prog = ctx.b.addObject(.{
.name = name,
.root_source_file = ctx.b.path(src_path),
.target = ctx.b.resolveTargetQuery(.{
.cpu_arch = switch (ctx.target.result.cpu.arch.endian()) {
.big => .bpfeb,
.little => .bpfel,
},
.os_tag = .freestanding,
}),
.optimize = .ReleaseFast,
});
prog.root_module.strip = false;
prog.root_module.addImport("bpf", ctx.bpf);
prog.root_module.addImport("build_options", ctx.bpf);
prog.root_module.addOptions("build_options", ctx.build_options);
const run_btf_sanitizer = ctx.b.addRunArtifact(ctx.btf_sanitizer);
run_btf_sanitizer.addFileArg(prog.getEmittedBin());
return run_btf_sanitizer.addOutputFileArg(ctx.b.fmt("{s}_sanitized.o", .{name}));
}
fn create_libbpf(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
return b.dependency("libbpf", .{
.target = target,
.optimize = optimize,
}).artifact("bpf");
}
fn create_vmlinux(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, libbpf: *std.Build.Step.Compile) *std.Build.Module {
const exe = b.addExecutable(.{
.name = "vmlinux_dumper",
.root_source_file = b.path("src/vmlinux_dumper/main.zig"),
.target = target,
.optimize = optimize,
});
exe.linkLibrary(libbpf);
exe.linkLibC();
b.installArtifact(exe);
const run_exe = b.addRunArtifact(exe);
const vmlinux_bin = b.option([]const u8, "vmlinux", "vmlinux binary used for BTF generation");
if (vmlinux_bin) |vmlinux| run_exe.addArg(vmlinux);
const stdout = run_exe.captureStdOut();
const vmlinux_h = b.addInstallFile(stdout, "vmlinux.h");
const zigify = b.addTranslateC(.{
.root_source_file = .{ .cwd_relative = b.getInstallPath(vmlinux_h.dir, vmlinux_h.dest_rel_path) },
.target = target,
.optimize = optimize,
});
zigify.addIncludeDir("src/vmlinux_dumper");
zigify.step.dependOn(&vmlinux_h.step);
return b.addModule("vmlinux", .{ .root_source_file = .{ .generated = .{ .file = &zigify.output_file } } });
}
fn create_btf_sanitizer(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, libbpf: *std.Build.Step.Compile) *std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = "btf_sanitizer",
.root_source_file = b.path("src/btf_sanitizer/main.zig"),
.target = target,
.optimize = optimize,
});
const libelf = b.dependency("libelf", .{
.target = target,
.optimize = optimize,
}).artifact("elf");
exe.linkLibrary(libelf);
exe.linkLibrary(libbpf);
exe.linkLibC();
b.installArtifact(exe);
return exe;
}
fn create_native_tools(b: *std.Build) struct { *std.Build.Module, *std.Build.Step.Compile } {
// build for native
const target = b.host;
const optimize: std.builtin.OptimizeMode = .ReleaseFast;
const libbpf = create_libbpf(b, target, optimize);
return .{
create_vmlinux(b, target, optimize, libbpf),
create_btf_sanitizer(b, target, optimize, libbpf),
};
}
fn create_bpf(b: *std.Build, vmlinux: *std.Build.Module) *std.Build.Module {
return b.addModule("bpf", .{
.root_source_file = b.path("src/bpf/root.zig"),
.imports = &.{.{ .name = "vmlinux", .module = vmlinux }},
});
}
const Ctx = struct {
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
vmlinux: *std.Build.Module,
bpf: *std.Build.Module,
libbpf_step: *std.Build.Step.Compile,
build_options: *std.Build.Step.Options,
btf_sanitizer: *std.Build.Step.Compile,
};
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
// WA for pointer alignment assumption of libbpf
const optimize: std.builtin.OptimizeMode = .ReleaseFast;
const libbpf = create_libbpf(b, target, optimize);
const vmlinux, const btf_sanitizer = create_native_tools(b);
const bpf = create_bpf(b, vmlinux);
const build_options = b.addOptions();
const debugging = if (b.option(bool, "debug", "enable debugging log")) |v| v else false;
const kprobes = if (b.option([]const []const u8, "kprobe", "the traced kernel function name")) |v| v else &.{};
const syscalls = if (b.option([]const []const u8, "syscall", "the traced syscall name")) |v| v else &.{};
build_options.addOption(@TypeOf(debugging), "debug", debugging);
build_options.addOption(@TypeOf(kprobes), "kprobes", kprobes);
build_options.addOption(@TypeOf(syscalls), "syscalls", syscalls);
const ctx = Ctx{
.b = b,
.target = target,
.optimize = optimize,
.bpf = bpf,
.libbpf_step = libbpf,
.build_options = build_options,
.vmlinux = vmlinux,
.btf_sanitizer = btf_sanitizer,
};
// default bpf program
const bpf_src = if (b.option([]const u8, "bpf", "bpf program source path")) |v| v else "samples/perf_event.zig";
const exe_src = if (b.option([]const u8, "main", "main executable source path")) |v| v else "src/hello.zig";
try create_target_step(&ctx, exe_src, bpf_src, null);
try create_target_step(&ctx, "src/trace.zig", "src/trace.bpf.zig", "trace");
try create_test_step(&ctx);
try create_docs_step(&ctx);
}
fn create_target_step(ctx: *const Ctx, main_path: []const u8, prog_path: []const u8, exe_name: ?[]const u8) !void {
const prog = create_bpf_prog(ctx, prog_path);
const exe = ctx.b.addExecutable(.{
.name = if (exe_name) |name| name else "zbpf",
.root_source_file = ctx.b.path(main_path),
.target = ctx.target,
.optimize = ctx.optimize,
});
exe.root_module.addAnonymousImport("@bpf_prog", .{
.root_source_file = prog,
});
exe.root_module.addImport("bpf", ctx.bpf);
exe.root_module.addImport("vmlinux", ctx.vmlinux);
exe.root_module.addOptions("build_options", ctx.build_options);
exe.linkLibrary(ctx.libbpf_step);
exe.linkLibC();
// if executable is not named, it is default target
// otherwise, create a step for it
if (exe_name) |name| {
const install_exe = ctx.b.addInstallArtifact(exe, .{});
var buf: [64]u8 = undefined;
const description = try std.fmt.bufPrint(&buf, "Build {s}", .{name});
const build_step = ctx.b.step(name, description);
build_step.dependOn(&install_exe.step);
} else {
ctx.b.installArtifact(exe);
}
}
fn create_test_step(ctx: *const Ctx) !void {
const filter = ctx.b.option([]const u8, "test", "test filter");
// Creates a step for unit testing.
const exe_tests = ctx.b.addTest(.{
.root_source_file = ctx.b.path("src/tests/root.zig"),
.target = ctx.target,
.optimize = ctx.optimize,
.filter = filter,
});
exe_tests.linkLibrary(ctx.libbpf_step);
exe_tests.root_module.addImport("bpf", ctx.bpf);
exe_tests.linkLibC();
const install_test = ctx.b.addInstallArtifact(exe_tests, .{});
// Create bpf programs for test
var sample_dir = try fs.cwd().openDir("samples", .{ .iterate = true });
defer sample_dir.close();
var it = sample_dir.iterate();
while (try it.next()) |entry| {
if (filter) |f| {
if (!std.mem.containsAtLeast(u8, entry.name, 1, f)) {
continue;
}
}
const bpf_prog = create_bpf_prog(ctx, try fs.path.join(ctx.b.allocator, &[_][]const u8{ "samples", entry.name }));
exe_tests.root_module.addAnonymousImport(try std.fmt.allocPrint(ctx.b.allocator, "@{s}", .{fs.path.stem(entry.name)}), .{
.root_source_file = bpf_prog,
});
}
// add debug option to test
exe_tests.root_module.addOptions("build_options", ctx.build_options);
const build_test_step = ctx.b.step("test", "Build unit tests");
build_test_step.dependOn(&install_test.step);
}
fn create_docs_step(ctx: *const Ctx) !void {
const exe = ctx.b.addObject(.{
.name = "docs",
.root_source_file = ctx.b.path("src/bpf/root.zig"),
.target = ctx.target,
.optimize = ctx.optimize,
});
const dumb_vmlinux = ctx.b.addModule("dumb_vmlinux", .{ .root_source_file = ctx.b.path("src/docs/dummy_vmlinux.zig") });
const bpf = create_bpf(ctx.b, dumb_vmlinux);
exe.root_module.addImport("bpf", bpf);
const install_docs = ctx.b.addInstallDirectory(.{
.source_dir = exe.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
const step = ctx.b.step("docs", "generate documents");
step.dependOn(&install_docs.step);
}