-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
227 lines (209 loc) · 14 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
const std = @import("std");
const main = @import("src/main.zig");
const Mode = main.Mode;
const Testdata = struct {
mode: Mode,
dirpath: []const u8,
exp_exit_code: u8,
};
// 0123, 3 only with file output, 01 only with check
// TODO behavior on multiple errors: priority for exit code?
const Testcases = [_]Testdata{
// TODO utf8 control sequences, deprecated characters
// => question: how to write the test cases?
// TODO pipe stdout
Testdata{ .mode = .CheckOnly, .dirpath = "zig-out/", .exp_exit_code = 0 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/bad_patterns/", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/bad_patterns/-fname", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/bad_patterns/--fname", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/bad_patterns/~fname", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/bad_patterns/ fname", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/bad_patterns/fname ", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/bad_patterns/fname1 -fname2", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/bad_patterns/fname1 ~fname2", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/bad_patterns/", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x01", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x02", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x03", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x04", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x05", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x06", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x07", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x08", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x09", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x1c", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x1d", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x1e", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x1f", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/f_\x20", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnly, .dirpath = "test_dirs/control_sequences/", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "zig-out/", .exp_exit_code = 0 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "test_dirs/bad_patterns/", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "test_dirs/bad_patterns/-fname", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "test_dirs/bad_patterns/--fname", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "test_dirs/bad_patterns/~fname", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "test_dirs/bad_patterns/ fname", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "test_dirs/bad_patterns/fname ", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "test_dirs/bad_patterns/fname1 -fname2", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "test_dirs/bad_patterns/fname1 ~fname2", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "test_dirs/bad_patterns/", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "test_dirs/control_sequences/f_\x01", .exp_exit_code = 1 },
Testdata{ .mode = .CheckOnlyAscii, .dirpath = "test_dirs/control_sequences/", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutput, .dirpath = "zig-out/", .exp_exit_code = 0 },
Testdata{ .mode = .ShellOutput, .dirpath = "test_dirs/bad_patterns/", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutput, .dirpath = "test_dirs/bad_patterns/-fname", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutput, .dirpath = "test_dirs/bad_patterns/--fname", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutput, .dirpath = "test_dirs/bad_patterns/~fname", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutput, .dirpath = "test_dirs/bad_patterns/ fname", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutput, .dirpath = "test_dirs/bad_patterns/fname ", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutput, .dirpath = "test_dirs/bad_patterns/fname1 -fname2", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutput, .dirpath = "test_dirs/bad_patterns/fname1 ~fname2", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutput, .dirpath = "test_dirs/control_sequences/f_\x01", .exp_exit_code = 2 },
Testdata{ .mode = .ShellOutput, .dirpath = "test_dirs/control_sequences/", .exp_exit_code = 2 },
Testdata{ .mode = .ShellOutputAscii, .dirpath = "zig-out/", .exp_exit_code = 0 },
Testdata{ .mode = .ShellOutputAscii, .dirpath = "test_dirs/bad_patterns/", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutputAscii, .dirpath = "test_dirs/bad_patterns/-fname", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutputAscii, .dirpath = "test_dirs/bad_patterns/--fname", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutputAscii, .dirpath = "test_dirs/bad_patterns/~fname", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutputAscii, .dirpath = "test_dirs/bad_patterns/ fname", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutputAscii, .dirpath = "test_dirs/bad_patterns/fname ", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutputAscii, .dirpath = "test_dirs/bad_patterns/fname1 -fname2", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutputAscii, .dirpath = "test_dirs/bad_patterns/fname1 ~fname2", .exp_exit_code = 1 },
Testdata{ .mode = .ShellOutputAscii, .dirpath = "test_dirs/control_sequences/f_\x01", .exp_exit_code = 2 },
Testdata{ .mode = .ShellOutputAscii, .dirpath = "test_dirs/control_sequences/", .exp_exit_code = 2 },
Testdata{ .mode = .FileOutput, .dirpath = "zig-out/", .exp_exit_code = 0 },
Testdata{ .mode = .FileOutput, .dirpath = "test_dirs/bad_patterns/", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutput, .dirpath = "test_dirs/bad_patterns/-fname", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutput, .dirpath = "test_dirs/bad_patterns/--fname", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutput, .dirpath = "test_dirs/bad_patterns/~fname", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutput, .dirpath = "test_dirs/bad_patterns/ fname", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutput, .dirpath = "test_dirs/bad_patterns/fname ", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutput, .dirpath = "test_dirs/bad_patterns/fname1 -fname2", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutput, .dirpath = "test_dirs/bad_patterns/fname1 ~fname2", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutput, .dirpath = "test_dirs/ctrl_seq_nonewline/", .exp_exit_code = 2 },
Testdata{ .mode = .FileOutput, .dirpath = "test_dirs/control_sequences/f_\x01", .exp_exit_code = 2 },
Testdata{ .mode = .FileOutput, .dirpath = "test_dirs/control_sequences/", .exp_exit_code = 3 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "zig-out/", .exp_exit_code = 0 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "test_dirs/bad_patterns/", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "test_dirs/bad_patterns/-fname", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "test_dirs/bad_patterns/--fname", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "test_dirs/bad_patterns/~fname", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "test_dirs/bad_patterns/ fname", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "test_dirs/bad_patterns/fname ", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "test_dirs/bad_patterns/fname1 -fname2", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "test_dirs/bad_patterns/fname1 ~fname2", .exp_exit_code = 1 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "test_dirs/ctrl_seq_nonewline/", .exp_exit_code = 2 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "test_dirs/control_sequences/f_\x01", .exp_exit_code = 2 },
Testdata{ .mode = .FileOutputAscii, .dirpath = "test_dirs/control_sequences/", .exp_exit_code = 3 },
};
fn createTests(b: *std.Build, exe: *std.Build.Step.Compile, dep_step: *std.Build.Step) [Testcases.len]*std.Build.Step.Run {
const tcases = Testcases;
// idea: parallel test execution?
//var prev_test_case: ?*std.build.RunStep = null;
var test_cases: [Testcases.len]*std.Build.Step.Run = undefined;
for (tcases, 0..) |_, i| {
test_cases[i] = b.addRunArtifact(exe); // *RunStep
test_cases[i].expectExitCode(tcases[i].exp_exit_code);
test_cases[i].step.dependOn(dep_step);
// if (prev_test_case != null)
// test_cases[i].step.dependOn(&(prev_test_case.?.step));
//
const inttest_arg = b.pathJoin(&.{ b.build_root.path.?, tcases[i].dirpath });
test_cases[i].addArgs(&.{inttest_arg});
switch (tcases[i].mode) {
Mode.CheckOnly => test_cases[i].addArgs(&.{"-c"}),
Mode.CheckOnlyAscii => test_cases[i].addArgs(&.{ "-a", "-c" }),
Mode.FileOutput => {
// multiple executables write same file
const tmpfile_path = b.pathJoin(&.{ b.build_root.path.?, "zig-cache/tmp/inttest.txt" });
test_cases[i].addArgs(&.{ "-outfile", tmpfile_path });
},
Mode.FileOutputAscii => {
// multiple executables write same file
const tmpfile_path = b.pathJoin(&.{ b.build_root.path.?, "zig-cache/tmp/inttest.txt" });
test_cases[i].addArgs(&.{ "-a", "-outfile", tmpfile_path });
},
Mode.ShellOutput => {},
Mode.ShellOutputAscii => test_cases[i].addArgs(&.{"-a"}),
}
//prev_test_case = test_cases[i];
}
return test_cases;
}
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// install
const exe = b.addExecutable(.{
.name = "chepa",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(exe);
// run
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args|
run_cmd.addArgs(args);
const run_cmd_step = b.step("run", "Run the app");
run_cmd_step.dependOn(&run_cmd.step);
// unit tests
const exe_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
// build and run binary to create test dirs
const tfgen = b.addExecutable(.{
.name = "tfgen",
.root_source_file = b.path("src/testdir_gen.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(tfgen);
const run_tfgen = b.addRunArtifact(tfgen); // integration test generation
run_tfgen.step.dependOn(b.getInstallStep());
const tfgen_arg = b.pathJoin(&.{ b.build_root.path.?, "test_dirs" });
run_tfgen.addArgs(&.{tfgen_arg});
const run_tfgen_step = b.step("tfgen", "Test dirs generation");
run_tfgen_step.dependOn(&run_tfgen.step); // integration test generation
const run_inttest_step = b.step("inttest", "Run integration tests");
const testdata = createTests(b, exe, run_tfgen_step);
// idea: how to enumerate test sequences?
// workaround https://github.com/ziglang/zig/issues/14734
var i: u64 = 0;
while (i < testdata.len) : (i += 1) {
run_inttest_step.dependOn(&testdata[i].step);
}
// TODO expand build.zig: StdIoAction limits *make*, which executes *RunStep
// => requires comptime-selection of string compare function,
const perfgen = b.addExecutable(.{
.name = "perfgen",
.root_source_file = b.path("src/perfdir_gen.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(perfgen);
const run_perfgen = b.addRunArtifact(perfgen); // perf bench data generationn
run_perfgen.step.dependOn(b.getInstallStep());
const perfgen_arg = b.pathJoin(&.{ b.build_root.path.?, "perf_dirs" });
run_perfgen.addArgs(&.{perfgen_arg});
const run_perfgen_step = b.step("perfgen", "Perf benchmark dirs generation (requires ~440MB memory)");
run_perfgen_step.dependOn(&run_perfgen.step); // perf bench data generation
//idea: check, if hyperfine is installed or build+use a proper c/c++ equivalent
//hyperfine './zig-out/bin/chepa perf_dirs/ -c' 'fd -j1 "blabla" perf_dirs/'
//hyperfine './zig-out/bin/chepa perf_dirs/' 'fd -j1 "blabla" perf_dirs/'
//'./zig-out/bin/chepa perf_dirs/ -c' ran
// 2.23 ± 0.07 times faster than 'fd -j1 "blabla" perf_dirs/'
//'./zig-out/bin/chepa perf_dirs/' ran
// 2.30 ± 0.04 times faster than 'fd -j1 "blabla" perf_dirs/'
//const run_perfbench = exe.run(); // run perf benchmarks
//run_perfbench.step.dependOn(run_perfgen_step);
//const perfbench_arg = b.pathJoin(&.{ b.build_root.path.?, "perf_dirs" });
//run_perfbench.addArgs(&.{perfbench_arg});
//const run_perfbench_step = b.step("inttest", "Run integration tests");
//run_perfbench_step.dependOn(&run_perfbench.step); // run perf benchmarks
}