-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.zig
167 lines (143 loc) · 4.84 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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const zubench = b.addModule("zubench", .{
.root_source_file = b.path("src/bench.zig"),
});
const fib2 = b.addExecutable(.{
.name = "fib2",
.root_source_file = b.path("examples/fib2.zig"),
.target = target,
.optimize = optimize,
});
fib2.root_module.addImport("zubench", zubench);
const fib_build = addBenchInner(
b,
"examples/fib_build.zig",
target,
optimize,
&.{},
zubench,
b.path("src/bench_runner.zig"),
);
const fib_test = addTestBenchInner(
b,
"examples/fib.zig",
.ReleaseSafe,
zubench,
b.path("src/bench_runner.zig"),
);
fib_test.filters = &.{"bench"};
const examples = [_]*std.Build.Step.Compile{
fib2,
fib_build,
fib_test,
};
const examples_step = b.step("examples", "Build the examples");
const bench_step = b.step("run", "Run the examples");
for (examples) |example| {
const install = b.addInstallArtifact(example, .{});
const run_cmd = b.addRunArtifact(example);
// NOTE: this works around Zig issue #15119 and should be removed when
// Zig PR #15120 or an equivalent is merged
run_cmd.stdio = .infer_from_args;
while (run_cmd.argv.items.len > 1) _ = run_cmd.argv.pop();
run_cmd.step.dependOn(&install.step);
examples_step.dependOn(&install.step);
bench_step.dependOn(&run_cmd.step);
}
const main_tests = b.addTest(.{
.root_source_file = b.path("src/bench.zig"),
.optimize = optimize,
});
const run_main_tests = b.addRunArtifact(main_tests);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_main_tests.step);
}
pub fn addBench(
b: *std.Build,
path: []const u8,
target: std.Build.ResolvedTarget,
mode: std.builtin.OptimizeMode,
dependencies: []const std.Build.Module.Import,
) *std.Build.Step.Compile {
const zubench_dep = b.dependencyFromBuildZig(@This(), .{});
const zubench_mod = zubench_dep.module("zubench");
const bench_runner_path: std.Build.LazyPath = .{
.dependency = .{
.dependency = zubench_dep,
.sub_path = "src/bench_runner.zig",
},
};
return addBenchInner(b, path, target, mode, dependencies, zubench_mod, bench_runner_path);
}
fn addBenchInner(
b: *std.Build,
path: []const u8,
target: std.Build.ResolvedTarget,
mode: std.builtin.OptimizeMode,
dependencies: []const std.Build.Module.Import,
zubench_mod: *std.Build.Module,
bench_runner_path: std.Build.LazyPath,
) *std.Build.Step.Compile {
const name = benchExeName(b.allocator, path, mode);
var deps = b.allocator.alloc(std.Build.Module.Import, dependencies.len + 1) catch unreachable;
@memcpy(deps[0..dependencies.len], dependencies);
deps[deps.len - 1] = .{ .name = "zubench", .module = zubench_mod };
const root = b.createModule(.{
.root_source_file = b.path(path),
.imports = deps,
});
const exe = b.addExecutable(.{
.name = name,
.root_source_file = bench_runner_path,
.target = target,
.optimize = mode,
});
exe.root_module.addImport("@bench", root);
exe.root_module.addImport("zubench", zubench_mod);
return exe;
}
pub fn addTestBench(
b: *std.Build,
path: []const u8,
mode: std.builtin.OptimizeMode,
) *std.Build.Step.Compile {
const zubench_dep = b.dependencyFromBuildZig(@This(), .{});
const zubench_mod = zubench_dep.module("zubench");
const bench_runner_path: std.Build.LazyPath = .{
.dependency = zubench_dep,
.sub_path = "src/bench_runner.zig",
};
return addTestBenchInner(b, path, mode, zubench_mod, bench_runner_path);
}
fn addTestBenchInner(
b: *std.Build,
path: []const u8,
mode: std.builtin.OptimizeMode,
zubench_mod: *std.Build.Module,
bench_runner_path: std.Build.LazyPath,
) *std.Build.Step.Compile {
const name = benchExeName(b.allocator, path, mode);
const exe = b.addTest(.{
.name = name,
.root_source_file = b.path(path),
.optimize = mode,
.test_runner = bench_runner_path,
});
exe.root_module.addImport("zubench", zubench_mod);
return exe;
}
fn benchExeName(allocator: std.mem.Allocator, path: []const u8, mode: std.builtin.Mode) []const u8 {
const basename = std.fs.path.basename(path);
const no_ext = if (std.mem.lastIndexOfScalar(u8, basename, '.')) |index|
basename[0..index]
else
basename;
const name = std.fmt.allocPrint(allocator, "zubench-{s}-{s}", .{
no_ext,
@tagName(mode),
}) catch unreachable;
return name;
}