-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
192 lines (151 loc) · 5.27 KB
/
build.zig
File metadata and controls
192 lines (151 loc) · 5.27 KB
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
const std = @import("std");
const Builder = struct {
b: *std.Build,
conf: Conf,
mods: std.enums.EnumArray(Name, *std.Build.Module),
const Self = @This();
const Conf = struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
step: []Step,
name: ?Name,
need: std.enums.EnumSet(Name),
};
const Step = enum {
@"test", gen_abnf, toml_test,
};
const step_fns = std.enums.EnumArray(Step, *const fn(Self) void).init(.{
.@"test" = buildTest,
.gen_abnf = buildGenABNF,
.toml_test = buildAllTomlTest,
});
const Name = enum {
plib, abnf, toml,
};
const mod_fns = std.enums.EnumArray(Name, *const fn(Self) *std.Build.Module).init(.{
.plib = buildPlib,
.abnf = buildABNF,
.toml = buildToml,
});
fn init(b: *std.Build) Self {
var self: Self = undefined;
self.b = b;
self.initOpts();
return self;
}
fn initOpts(self: *Self) void {
self.conf.target = self.b.standardTargetOptions(.{});
self.conf.optimize = self.b.standardOptimizeOption(.{});
self.conf.step = self.b.option([]Step, "step", "Which step to take") orelse &.{};
self.conf.name = self.b.option(Name, "name", "Input file base name");
const need = self.b.option([]Name, "need", "list of needed mod names") orelse &.{};
for (need) |name| self.conf.need.setPresent(name, true);
self.conf.need.setPresent(.plib, true);
for (self.conf.step) |step| switch (step) {
.@"test" => if (self.conf.name) |name|
self.conf.need.setPresent(name, true),
.gen_abnf =>
self.conf.need.setPresent(.abnf, true),
.toml_test =>
self.conf.need.setPresent(.toml, true),
};
}
fn build(b: *std.Build) void {
var self = init(b);
for (std.meta.tags(Name)) |name|
if (self.conf.need.contains(name))
self.mods.set(name, mod_fns.get(name)(self));
for (self.conf.step) |step|
step_fns.get(step)(self);
}
fn buildPlib(self: Self) *std.Build.Module {
return self.b.addModule("plib", .{
.target = self.conf.target,
.optimize = self.conf.optimize,
.root_source_file = self.b.path("src/lib/root.zig"),
});
}
fn buildABNF(self: Self) *std.Build.Module {
return self.buildSubMod(.abnf);
}
fn buildToml(self: Self) *std.Build.Module {
return self.buildSubMod(.toml);
}
fn buildSubMod(self: Self, comptime file: Name) *std.Build.Module {
const mod_name = @tagName(file);
const mod_path = "src/mod/" ++ mod_name ++ "/root.zig";
const gen_path = "src/gen/" ++ mod_name ++ ".zig";
const mod_mod = self.b.addModule(mod_name, .{
.target = self.conf.target,
.optimize = self.conf.optimize,
.root_source_file = self.b.path(mod_path),
});
const gen_mod = self.b.createModule(.{
.target = self.conf.target,
.optimize = self.conf.optimize,
.root_source_file = self.b.path(gen_path),
});
mod_mod.addImport("gen", gen_mod);
self.addImport(gen_mod, .plib);
self.addImport(mod_mod, .plib);
return mod_mod;
}
fn buildTest(self: Self) void {
const step = self.b.default_step;
const name = self.conf.name orelse {
step.dependOn(&self.b.addFail("The -Dname=... option is required for this step").step);
return;
};
step.dependOn(&self.b.addInstallArtifact(self.b.addTest(.{
.optimize = self.conf.optimize,
.root_module = self.mods.get(name),
}), .{}).step);
}
fn buildGenABNF(self: Self) void {
const step = self.b.default_step;
const name = self.conf.name orelse {
step.dependOn(&self.b.addFail("The -Dname=... option is required for this step").step);
return;
};
const exe = self.b.addExecutable(.{
.name = "gen_abnf",
.target = self.conf.target,
.optimize = self.conf.optimize,
.root_source_file = self.b.path("src/exe/gen_abnf.zig"),
});
self.addImport(exe.root_module, .abnf);
const run = self.b.addRunArtifact(exe);
run.addArg(@tagName(name));
run.stdio = .inherit;
step.dependOn(&run.step);
}
fn buildAllTomlTest(self: Self) void {
self.buildOneTomlTest("decoder");
self.buildOneTomlTest("encoder");
}
fn buildOneTomlTest(self: Self, comptime name: []const u8) void {
const exe_name = "toml_" ++ name;
const test_exe = self.b.addExecutable(.{
.name = exe_name,
.target = self.conf.target,
.optimize = self.conf.optimize,
.root_source_file = self.b.path("src/exe/toml_test.zig"),
});
self.addImport(test_exe.root_module, .toml);
const test_opt = self.b.addOptions();
test_opt.addOption([]const u8, "name", name);
test_exe.root_module.addOptions("opts", test_opt);
const test_ins = self.b.addInstallArtifact(test_exe, .{});
var run_step = std.Build.Step.Run.create(self.b, exe_name);
run_step.addArg("toml-test");
run_step.addArg(self.b.getInstallPath(test_ins.dest_dir.?, test_ins.dest_sub_path));
if (std.mem.eql(u8, name, "encoder"))
run_step.addArg("-" ++ name);
run_step.step.dependOn(&test_ins.step);
self.b.default_step.dependOn(&run_step.step);
}
fn addImport(self: Self, mod: *std.Build.Module, name: Name) void {
mod.addImport(@tagName(name), self.mods.get(name));
}
};
pub fn build(b: *std.Build) !void { Builder.build(b); }