-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
349 lines (294 loc) · 11.2 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const std = @import("std");
const builtin = @import("builtin");
const Build = std.Build;
const CompileStep = Build.CompileStep;
const Step = Build.Step;
const Child = std.process.Child;
const assert = std.debug.assert;
const join = std.fs.path.join;
const print = std.debug.print;
comptime {
const req_ver = "0.13.0";
const cur_ver = builtin.zig_version;
const min_ver = std.SemanticVersion.parse(req_ver) catch unreachable;
if (cur_ver.order(min_ver) == .lt) {
const error_msg =
\\Sorry, it looks like your version of zig is too old. :-(
\\
\\AoC requires at least
\\
\\{}
\\
\\or higher.
\\
\\Please download an appropriate version from
\\
\\https://ziglang.org/download/
\\
\\
;
@compileError(std.fmt.comptimePrint(error_msg, .{min_ver}));
}
}
pub const RunMode = enum {
RUN,
TEST,
};
pub const Solution = struct {
main_file: []const u8,
year: usize,
day: usize,
mode: RunMode = .RUN,
};
pub const logo = @embedFile("./logos/0000.txt");
pub fn build(b: *Build) !void {
use_color_escapes = false;
if (std.io.getStdErr().supportsAnsiEscapeCodes()) {
use_color_escapes = true;
} else if (builtin.os.tag == .windows) {
const w32 = struct {
const WINAPI = std.os.windows.WINAPI;
const DWORD = std.os.windows.DWORD;
const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
const STD_ERROR_HANDLE: DWORD = @bitCast(@as(i32, -12));
extern "kernel32" fn GetStdHandle(id: DWORD) callconv(WINAPI) ?*anyopaque;
extern "kernel32" fn GetConsoleMode(console: ?*anyopaque, out_mode: *DWORD) callconv(WINAPI) u32;
extern "kernel32" fn SetConsoleMode(console: ?*anyopaque, mode: DWORD) callconv(WINAPI) u32;
};
const handle = w32.GetStdHandle(w32.STD_ERROR_HANDLE);
var mode: w32.DWORD = 0;
if (w32.GetConsoleMode(handle, &mode) != 0) {
mode |= w32.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
use_color_escapes = w32.SetConsoleMode(handle, mode) != 0;
}
}
if (use_color_escapes) {
red_text = "\x1b[31m";
red_bold_text = "\x1b[31;1m";
red_dim_text = "\x1b[31;2m";
green_text = "\x1b[32m";
bold_text = "\x1b[1m";
reset_text = "\x1b[0m";
}
b.top_level_steps = .{};
const year: usize = b.option(usize, "year", "Select Yeay") orelse 0;
const day: usize = b.option(usize, "day", "Select Day") orelse 0;
const req_logo = logo;
const header_step = PrintStep.create(b, req_logo);
const work_path = "solutions";
const req_solution = get_solution(b, work_path, year, day) catch {
print("File `{s}/{d:0>4}/{d:0>2}.zig` does not exist.\n", .{ work_path, year, day });
std.process.exit(2);
};
const aoc_step = b.step("aoc", b.fmt("Running the solution for {d:0>4}-{d:0>2}", .{ year, day }));
b.default_step = aoc_step;
aoc_step.dependOn(&header_step.step);
const verify_step = AoCStep.create(b, req_solution, work_path);
verify_step.step.dependOn(&header_step.step);
aoc_step.dependOn(&verify_step.step);
const aoc_test_step = b.step("aoc-test", b.fmt("Testing the solution for {d:0>4}-{d:0>2}", .{ year, day }));
aoc_test_step.dependOn(&header_step.step);
const req_test: Solution = .{
.mode = .TEST,
.main_file = req_solution.main_file,
.year = req_solution.year,
.day = req_solution.day,
};
const verify_test_step = AoCStep.create(b, req_test, work_path);
verify_test_step.step.dependOn(&header_step.step);
aoc_test_step.dependOn(&verify_test_step.step);
}
var use_color_escapes = false;
var red_text: []const u8 = "";
var red_bold_text: []const u8 = "";
var red_dim_text: []const u8 = "";
var green_text: []const u8 = "";
var bold_text: []const u8 = "";
var reset_text: []const u8 = "";
fn get_solution(b: *Build, work_path: []const u8, y: usize, d: usize) !Solution {
if (y == 0 or d == 0) return .{ .year = 0, .day = 0, .main_file = "null" };
try std.fs.cwd().access(b.fmt("{s}/{d:0>4}/{d:0>2}.zig", .{ work_path, y, d }), .{});
return .{
.year = y,
.day = d,
.main_file = b.fmt("{d:0>4}/{d:0>2}.zig", .{ y, d }),
};
}
fn resetLine() void {
if (use_color_escapes) print("{s}", .{"\x1b[2K\r"});
}
const PrintStep = struct {
step: Step,
message: []const u8,
pub fn create(owner: *Build, message: []const u8) *PrintStep {
const self = owner.allocator.create(PrintStep) catch @panic("OOM");
self.* = .{
.step = Step.init(.{
.id = .custom,
.name = "print",
.owner = owner,
.makeFn = make,
}),
.message = message,
};
return self;
}
fn make(step: *Step, _: std.Progress.Node) !void {
const self: *PrintStep = @alignCast(@fieldParentPtr("step", step));
print("{s}", .{self.message});
}
};
const AoCStep = struct {
step: Step,
solution: Solution,
work_path: []const u8,
pub fn create(b: *Build, sol: Solution, work_path: []const u8) *AoCStep {
const self = b.allocator.create(AoCStep) catch @panic("OOM");
self.* = .{
.step = Step.init(.{
.id = .custom,
.name = sol.main_file,
.owner = b,
.makeFn = make,
}),
.solution = sol,
.work_path = work_path,
};
return self;
}
fn make(step: *Step, prog_node: std.Progress.Node) !void {
const self: *AoCStep = @alignCast(@fieldParentPtr("step", step));
if (self.solution.year == 0 or self.solution.day == 0) {
self.help();
return;
}
const exe_path = self.compile(prog_node) catch {
self.printErrors();
self.help();
std.process.exit(2);
};
self.run(exe_path.?, prog_node) catch {
self.printErrors();
self.help();
std.process.exit(2);
};
self.printErrors();
}
fn run(self: *AoCStep, exe_path: []const u8, _: std.Progress.Node) !void {
resetLine();
print("Executing: {s}/{s}\n", .{ self.work_path, self.solution.main_file });
const b = self.step.owner;
const max_output_bytes: usize = 0x100000;
const result = Child.run(.{
.allocator = b.allocator,
.argv = &.{
exe_path,
if (self.solution.mode == .RUN) b.fmt("{d:0>4}", .{self.solution.year}) else "test",
if (self.solution.mode == .RUN) b.fmt("{d:0>2}", .{self.solution.day}) else "test",
},
.cwd = b.build_root.path.?,
.cwd_dir = b.build_root.handle,
.max_output_bytes = max_output_bytes,
}) catch |err| {
return self.step.fail("unable to spawn {s}: {s}", .{ exe_path, @errorName(err) });
};
return self.save_output(result);
}
fn save_output(self: *AoCStep, result: Child.RunResult) !void {
switch (result.term) {
.Exited => |code| {
if (code != 0) {
return self.step.fail("{s} exited with error code {d} (expected {})", .{
self.solution.main_file, code, 0,
});
}
},
else => {
return self.step.fail("{s} terminated unexpectedly\n{s}{s}{s}\n", .{
self.solution.main_file, red_text, result.stderr, reset_text,
});
},
}
const raw_output = result.stderr;
print("--------------------------------------------------\n", .{});
print("{s}\n", .{raw_output});
print("--------------------------------------------------\n", .{});
if (self.solution.mode == .RUN) {
const file = try self.get_output_file();
defer file.close();
try file.writeAll(raw_output);
}
}
fn get_output_file(self: *AoCStep) !std.fs.File {
const b = self.step.owner;
const cwd = std.fs.cwd();
var dd = cwd.openDir("outputs", .{}) catch |err| switch (err) {
std.fs.Dir.OpenError.FileNotFound => blk: {
try cwd.makeDir("outputs");
break :blk try cwd.openDir("outputs", .{});
},
else => {
return err;
},
};
defer dd.close();
var de = dd.openDir(b.fmt("{d:0>4}", .{self.solution.year}), .{}) catch |err| switch (err) {
std.fs.Dir.OpenError.FileNotFound => blk: {
try dd.makeDir(b.fmt("{d:0>4}", .{self.solution.year}));
break :blk try dd.openDir(b.fmt("{d:0>4}", .{self.solution.year}), .{});
},
else => {
return err;
},
};
defer de.close();
return de.createFile(b.fmt("{d:0>2}.txt", .{self.solution.day}), .{});
}
fn compile(self: *AoCStep, prog_node: std.Progress.Node) !?[]const u8 {
print("Compiling: {s}/{s}\n", .{ self.work_path, self.solution.main_file });
const b = self.step.owner;
const solution_path = self.solution.main_file;
const path = join(b.allocator, &.{ self.work_path, solution_path }) catch @panic("OOM");
var zig_args = std.ArrayList([]const u8).init(b.allocator);
defer zig_args.deinit();
zig_args.append(b.graph.zig_exe) catch @panic("OOM");
zig_args.append("build-exe") catch @panic("OOM");
zig_args.append(b.pathFromRoot(path)) catch @panic("OOM");
zig_args.append("--listen=-") catch @panic("OOM");
return try self.step.evalZigProcess(zig_args.items, prog_node);
}
fn help(self: *AoCStep) void {
const b = self.step.owner;
const path = self.solution.main_file;
const year = self.solution.year;
const day = self.solution.day;
if (year == 0 or day == 0) {
print(
\\To run a solution of year `y`, day `d` run the following command:
\\zig build aoc{s} -Dyear=y -Dday=d
\\With the code and input present.
\\
, .{if (self.solution.mode == .TEST) "-test" else ""});
return;
}
const cmd = b.fmt("zig build aoc{s} -Dyear={d} -Dday={d}", .{ if (self.solution.mode == .TEST) "-test" else "", year, day });
print("\n{s}Update solutions/{s} and run '{s}' again.{s}\n", .{
red_bold_text, path, cmd, reset_text,
});
}
fn printErrors(self: *AoCStep) void {
resetLine();
if (self.step.result_error_msgs.items.len > 0) {
for (self.step.result_error_msgs.items) |msg| {
print("{s}error: {s}{s}{s}{s}\n", .{ red_bold_text, reset_text, red_dim_text, msg, reset_text });
}
}
const ttyconf: std.io.tty.Config = if (use_color_escapes)
.escape_codes
else
.no_color;
if (self.step.result_error_bundle.errorMessageCount() > 0) {
self.step.result_error_bundle.renderToStdErr(.{ .ttyconf = ttyconf });
}
}
};