-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
154 lines (129 loc) · 5.28 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
const std = @import("std");
const BuildParams = struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
};
const Aoc = struct {
year: u16,
day: u8,
fn addTo(self: *const @This(), b: *std.Build, params: BuildParams, run_step: *std.Build.Step) void {
const source_file = b.path(b.fmt("src/{}/day{}.zig", .{ self.year, self.day }));
const exe = b.addExecutable(.{
.name = b.fmt("{}-{}", .{ self.year, self.day }),
.root_source_file = source_file,
.target = params.target,
.optimize = params.optimize,
});
b.installArtifact(exe);
if (b.modules.get("utils")) |utils| {
exe.root_module.addImport("utils", utils);
}
// input file
const input_file = b.path(b.fmt("inputs/{}/day{}.txt", .{ self.year, self.day }));
exe.root_module.addAnonymousImport("input", .{ .root_source_file = input_file });
// add to run step
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
run_step.dependOn(&run_cmd.step);
// add or create year run step
const run_year_step_name = b.fmt("run-{}", .{self.year});
if (b.top_level_steps.get(run_year_step_name)) |step_info| {
step_info.step.dependOn(&run_cmd.step);
} else {
const run_year_step = b.step(run_year_step_name, b.fmt("Run apps for year {}", .{self.year}));
run_year_step.dependOn(&run_cmd.step);
}
// create day run step
const run_day_step = b.step(b.fmt("run-{}-{}", .{ self.year, self.day }), b.fmt("Run app for year {}, day {}", .{ self.year, self.day }));
run_day_step.dependOn(&run_cmd.step);
}
fn addTestsTo(self: *const @This(), b: *std.Build, params: BuildParams, test_step: *std.Build.Step) void {
const source_file = b.path(b.fmt("src/{}/day{}.zig", .{ self.year, self.day }));
// unit tests
const unit_tests = b.addTest(.{
.root_source_file = source_file,
.target = params.target,
.optimize = params.optimize,
});
// make sure utils module is available in unit tests
if (b.modules.get("utils")) |utils| {
unit_tests.root_module.addImport("utils", utils);
}
const run_unit_tests = b.addRunArtifact(unit_tests);
test_step.dependOn(&run_unit_tests.step);
// add or create year test step
const test_year_step_name = b.fmt("test-{}", .{self.year});
if (b.top_level_steps.get(test_year_step_name)) |step_info| {
step_info.step.dependOn(&run_unit_tests.step);
} else {
const test_year_step = b.step(test_year_step_name, b.fmt("Run unit tests for year {}", .{self.year}));
test_year_step.dependOn(&run_unit_tests.step);
}
// create day test step
const test_day_step = b.step(b.fmt("test-{}-{}", .{ self.year, self.day }), b.fmt("Run unit tests for year {}, day {}", .{ self.year, self.day }));
test_day_step.dependOn(&run_unit_tests.step);
}
};
pub fn build(b: *std.Build) void {
const puzzles = [_]Aoc{
.{ .year = 2016, .day = 5 },
.{ .year = 2016, .day = 8 },
.{ .year = 2017, .day = 1 },
.{ .year = 2017, .day = 2 },
.{ .year = 2019, .day = 1 },
.{ .year = 2023, .day = 1 },
.{ .year = 2023, .day = 2 },
.{ .year = 2023, .day = 3 },
.{ .year = 2023, .day = 4 },
.{ .year = 2023, .day = 8 },
.{ .year = 2023, .day = 9 },
.{ .year = 2023, .day = 10 },
.{ .year = 2023, .day = 11 },
.{ .year = 2024, .day = 1 },
.{ .year = 2024, .day = 2 },
.{ .year = 2024, .day = 3 },
.{ .year = 2024, .day = 4 },
.{ .year = 2024, .day = 5 },
.{ .year = 2024, .day = 6 },
.{ .year = 2024, .day = 7 },
.{ .year = 2024, .day = 8 },
.{ .year = 2024, .day = 9 },
.{ .year = 2024, .day = 10 },
.{ .year = 2024, .day = 11 },
.{ .year = 2024, .day = 12 },
.{ .year = 2024, .day = 13 },
.{ .year = 2024, .day = 14 },
.{ .year = 2024, .day = 15 },
.{ .year = 2024, .day = 16 },
.{ .year = 2024, .day = 17 },
.{ .year = 2024, .day = 18 },
.{ .year = 2024, .day = 19 },
.{ .year = 2024, .day = 20 },
.{ .year = 2024, .day = 21 },
.{ .year = 2024, .day = 22 },
.{ .year = 2024, .day = 23 },
.{ .year = 2024, .day = 24 },
.{ .year = 2024, .day = 25 },
};
const params = BuildParams{
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
};
const utils = b.addModule("utils", .{
.root_source_file = b.path("src/utils.zig"),
});
const run_step = b.step("run", "Run all apps");
const test_step = b.step("test", "Run all unit tests");
const utils_tests = b.addTest(.{
.root_source_file = utils.root_source_file.?,
.optimize = params.optimize,
});
test_step.dependOn(&b.addRunArtifact(utils_tests).step);
for (puzzles) |p| {
p.addTo(b, params, run_step);
p.addTestsTo(b, params, test_step);
}
}