-
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
// Copyright (C) 2025 Shota FUJI
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-only
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const man_opt = b.option(bool, "man", "Builds and installs man pages") orelse false;
const legacy = b.option(bool, "legacy", "Build legacy C program") orelse false;
const version = b.option([]const u8, "version", "Application version, without \"v\" prefix") orelse "dev";
const update_snapshot = b.option(bool, "update-snapshot", "Update snapshot on snapshot tests") orelse false;
const exe = addExe(b, .{
.target = target,
.optimize = optimize,
.legacy = false,
.version = version,
});
const legacy_exe = addExe(b, .{
.target = target,
.optimize = optimize,
.legacy = true,
.version = "0.91",
});
// "zig build run"
{
const step = b.step("run", "Build and run sunwait");
const run = b.addRunArtifact(if (legacy) legacy_exe else exe);
if (b.args) |args| {
run.addArgs(args);
}
step.dependOn(&run.step);
}
// "zig build man"
const man = man: {
const step = b.step("man", "Build man pages");
const ManPage = struct {
source: std.Build.LazyPath,
outname: []const u8,
};
for ([_]ManPage{
.{ .source = b.path("docs/sunwait.adoc"), .outname = "sunwait.1" },
.{ .source = b.path("docs/sunwait-poll.adoc"), .outname = "sunwait-poll.1" },
.{ .source = b.path("docs/sunwait-wait.adoc"), .outname = "sunwait-wait.1" },
.{ .source = b.path("docs/sunwait-list.adoc"), .outname = "sunwait-list.1" },
.{ .source = b.path("docs/sunwait-report.adoc"), .outname = "sunwait-report.1" },
}) |page| {
const cmd = b.addSystemCommand(&.{"asciidoctor"});
cmd.addArgs(&.{ "-b", "manpage" });
cmd.addFileArg(page.source);
cmd.addArg("--out-file");
const out = cmd.addOutputFileArg(page.outname);
const install = b.addInstallFile(out, b.fmt("share/man/man1/{s}", .{page.outname}));
step.dependOn(&install.step);
}
break :man step;
};
// "zig build"
{
b.installArtifact(if (legacy) legacy_exe else exe);
if (man_opt) {
b.getInstallStep().dependOn(man);
}
}
// "zig build unit-test"
const unit_tests = unit_tests: {
const step = b.step("unit-test", "Run unit tests");
const t = b.addTest(.{
.name = "unit_test",
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/main.zig"),
});
t.linkLibC();
t.addCSourceFiles(.{
.files = &.{
"src/print.c",
"src/sunriset.c",
"src/sunwait.c",
},
});
t.addIncludePath(b.path("src"));
t.root_module.addCMacro("SUNWAIT_NOMAIN", "");
const run = b.addRunArtifact(t);
step.dependOn(&run.step);
break :unit_tests step;
};
// "zig build behavior-test"
const behavior_tests = behavior_tests: {
const step = b.step("behavior-test", "Run behavior matching tests");
const t = b.addTest(.{
.name = "behavior_matching_test",
.target = target,
.optimize = optimize,
.root_source_file = b.path("tests/behavior_matching/main.zig"),
});
const config = b.addOptions();
config.addOptionPath("legacy_bin", legacy_exe.getEmittedBin());
config.addOptionPath("new_bin", exe.getEmittedBin());
t.root_module.addOptions("config", config);
const run = b.addRunArtifact(t);
step.dependOn(&run.step);
break :behavior_tests step;
};
// "zig build e2e-test"
const e2e_tests = e2e_tests: {
const step = b.step("e2e-test", "Run end-to-end tests");
const t = b.addTest(.{
.name = "e2e_test",
.target = target,
.optimize = optimize,
.root_source_file = b.path("tests/e2e/main.zig"),
});
const config = b.addOptions();
config.addOptionPath("bin", exe.getEmittedBin());
t.root_module.addOptions("config", config);
const run = b.addRunArtifact(t);
step.dependOn(&run.step);
break :e2e_tests step;
};
// "zig build snapshot-test"
const snapshot_tests = snapshot_test: {
const step = b.step("snapshot-test", "Run snapshot tests");
const t = b.addTest(.{
.name = "snapshot_test",
.target = target,
.optimize = optimize,
.root_source_file = b.path("tests/snapshot/main.zig"),
});
const config = b.addOptions();
config.addOptionPath("bin", exe.getEmittedBin());
config.addOptionPath("test_src_root", b.path("tests/snapshot/"));
config.addOption(bool, "update_snapshot", update_snapshot);
t.root_module.addOptions("config", config);
const run = b.addRunArtifact(t);
step.dependOn(&run.step);
break :snapshot_test step;
};
// "zig build test"
{
const step = b.step("test", "Run all tests");
step.dependOn(unit_tests);
step.dependOn(behavior_tests);
step.dependOn(e2e_tests);
step.dependOn(snapshot_tests);
}
}
const AddExeOptions = struct {
target: ?std.Build.ResolvedTarget = null,
optimize: std.builtin.OptimizeMode,
legacy: bool = false,
version: []const u8,
};
fn addExe(b: *std.Build, opts: AddExeOptions) *std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = "sunwait",
.target = opts.target,
.optimize = opts.optimize,
});
if (!opts.legacy) {
exe.root_module.root_source_file = b.path("src/main.zig");
exe.root_module.addCMacro("SUNWAIT_NOMAIN", "");
const config = b.addOptions();
config.addOption([]const u8, "version", opts.version);
exe.root_module.addOptions("config", config);
}
exe.linkLibC();
exe.addCSourceFiles(.{
.files = &.{
"src/print.c",
"src/sunriset.c",
"src/sunwait.c",
},
});
exe.addIncludePath(b.path("src"));
return exe;
}