-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.zig
321 lines (285 loc) · 12 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
const std = @import("std");
const Build = std.Build;
const Module = std.Build.Module;
pub fn build(b: *std.Build) void {
// default to -freference-trace, but respect -fnoreference-trace
if (b.reference_trace == null) {
b.reference_trace = 256;
}
// cli options
const single_threaded = b.option(bool, "single-threaded", "Build a single-threaded executable");
const debug_release = b.option(bool, "debug-release", "Build with debug info in release mode") orelse false;
// const version = b.option([]const u8, "version", "ZLint version") orelse "0.0.0";
var l = Linker.init(b);
defer l.deinit();
if (debug_release) {
l.optimize = .ReleaseSafe;
}
// dependencies
l.dependency("chameleon", .{});
l.dependency("smart-pointers", .{});
l.devDependency("zig-recover", "recover", .{});
// modules
l.createModule("util", .{
.root_source_file = b.path("src/util.zig"),
});
// artifacts
var lib = b.addStaticLibrary(.{
.name = "zlint",
.root_source_file = b.path("src/root.zig"),
.single_threaded = single_threaded,
.target = l.target,
.optimize = l.optimize,
.error_tracing = if (debug_release) true else null,
.unwind_tables = if (debug_release) true else null,
.omit_frame_pointer = if (debug_release) false else null,
.strip = if (debug_release) false else null,
});
const zlint = &lib.root_module;
l.link(zlint, false, .{});
b.modules.put(b.dupe("zlint"), zlint) catch @panic("OOM");
b.installArtifact(lib);
const exe = b.addExecutable(.{
.name = "zlint",
.root_source_file = b.path("src/main.zig"),
.single_threaded = single_threaded,
.target = l.target,
.optimize = l.optimize,
.error_tracing = if (debug_release) true else null,
.unwind_tables = if (debug_release) true else null,
.omit_frame_pointer = if (debug_release) false else null,
.strip = if (debug_release) false else null,
});
// exe.want_lto
l.link(&exe.root_module, false, .{});
b.installArtifact(exe);
const e2e = b.addExecutable(.{
.name = "test-e2e",
.root_source_file = b.path("test/test_e2e.zig"),
.single_threaded = single_threaded,
.target = l.target,
.optimize = l.optimize,
.error_tracing = if (debug_release) true else null,
.unwind_tables = if (debug_release) true else null,
.strip = if (debug_release) false else null,
});
// util and chameleon omitted
e2e.root_module.addImport("zlint", zlint);
l.link(&e2e.root_module, true, .{ "smart-pointers", "recover" });
b.installArtifact(e2e);
const test_exe = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.single_threaded = single_threaded,
.target = l.target,
.optimize = l.optimize,
.error_tracing = if (debug_release) true else null,
.strip = if (debug_release) false else null,
});
l.link(&test_exe.root_module, true, .{});
b.installArtifact(test_exe);
const test_utils = b.addTest(.{
.name = "test-utils",
.root_source_file = b.path("src/util.zig"),
.single_threaded = single_threaded,
.target = l.target,
.optimize = l.optimize,
.error_tracing = if (debug_release) true else null,
.strip = if (debug_release) false else null,
});
b.installArtifact(test_utils);
// steps
const run_exe = b.addRunArtifact(exe);
if (b.args) |args| {
run_exe.addArgs(args);
}
const run = b.step("run", "Run zlint from the current directory");
run.dependOn(&run_exe.step);
// zig build test
{
const run_exe_tests = b.addRunArtifact(test_exe);
const run_utils_tests = b.addRunArtifact(test_utils);
const unit_step = b.step("test", "Run unit tests");
unit_step.dependOn(&run_exe_tests.step);
unit_step.dependOn(&run_utils_tests.step);
const run_e2e = b.addRunArtifact(e2e);
const e2e_step = b.step("test-e2e", "Run e2e tests");
e2e_step.dependOn(&run_e2e.step);
const test_all_step = b.step("test-all", "Run all tests");
test_all_step.dependOn(&run_exe_tests.step);
test_all_step.dependOn(&run_utils_tests.step);
test_all_step.dependOn(&run_e2e.step);
}
// zig build docs
{
const docs_step = b.step("docs", "Generate documentation");
const docs_rules_step = Tasks.generateRuleDocs(&l);
const lib_docs = b.addInstallDirectory(.{
.source_dir = lib.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
docs_step.dependOn(docs_rules_step);
docs_step.dependOn(&lib_docs.step);
}
// zig build codegen
{
const codegen = b.step("codegen", "Codegen");
const confgen_task = Tasks.generateRulesConfig(&l);
codegen.dependOn(confgen_task);
}
// check is down here because it's weird. We create mocks of each artifacts
// that never get installed. This (allegedly) skips llvm emit.
{
const check_exe = b.addExecutable(.{ .name = "zlint", .root_source_file = b.path("src/main.zig"), .target = l.target });
// mock library so zlint module is checked
const check_lib = b.addStaticLibrary(.{ .name = "zlint", .root_source_file = b.path("src/root.zig"), .target = l.target, .optimize = l.optimize });
const check_test_lib = b.addTest(.{ .root_source_file = b.path("src/root.zig") });
const check_test_exe = b.addTest(.{ .root_source_file = b.path("src/main.zig") });
const check_e2e = b.addExecutable(.{ .name = "test-e2e", .root_source_file = b.path("test/test_e2e.zig"), .target = l.target });
l.link(&check_e2e.root_module, true, .{"recover"});
// tasks
const check_docgen = b.addExecutable(.{ .name = "docgen", .root_source_file = b.path("tasks/docgen.zig"), .target = l.target });
const check_confgen = b.addExecutable(.{ .name = "confgen", .root_source_file = b.path("tasks/confgen.zig"), .target = l.target });
// these compilation targets depend on zlint as a module
const needs_zlint = .{ check_e2e, check_docgen, check_confgen };
inline for (needs_zlint) |exe_to_check| {
exe_to_check.root_module.addImport("zlint", zlint);
}
const check = b.step("check", "Check for semantic errors");
const substeps = .{
check_exe,
check_lib,
check_test_lib,
check_test_exe,
check_e2e,
check_docgen,
check_confgen,
};
inline for (substeps) |c| {
l.link(&c.root_module, false, .{});
check.dependOn(&c.step);
}
}
}
const Tasks = struct {
fn generateRuleDocs(l: *Linker) *Build.Step {
const docgen_exe = l.b.addExecutable(.{
.name = "docgen",
.root_source_file = l.b.path("tasks/docgen.zig"),
.target = l.target,
.optimize = l.optimize,
});
const zlint = l.b.modules.get("zlint") orelse @panic("Missing module: zlint");
docgen_exe.root_module.addImport("zlint", zlint);
const docgen_run = l.b.addRunArtifact(docgen_exe);
const bunx_prettier = Tasks.bunx(l, "prettier", &[_][]const u8{ "--write", "docs/rules/*.md" });
bunx_prettier.step.dependOn(&docgen_run.step);
const docgen = l.b.step("docs:rules", "Generate lint rule documentation");
docgen.dependOn(&bunx_prettier.step);
return docgen;
}
fn generateRulesConfig(l: *Linker) *Build.Step {
const confgen_exe = l.b.addExecutable(.{
.name = "confgen",
.root_source_file = l.b.path("tasks/confgen.zig"),
.target = l.target,
.optimize = l.optimize,
});
const zlint = l.b.modules.get("zlint") orelse @panic("Missing module: zlint");
confgen_exe.root_module.addImport("zlint", zlint);
const confgen_run = l.b.addRunArtifact(confgen_exe);
const confgen = l.b.step("codegen:rules-config", "Generate RulesConfig");
confgen.dependOn(&confgen_run.step);
return confgen;
}
fn bunx(l: *Linker, comptime cmd: []const u8, comptime args: []const []const u8) *Build.Step.Run {
const b = l.b;
return b.addSystemCommand(.{ "bunx", cmd } ++ args);
}
// fn generateLibDocs(l: *Linker) *Build.Step {
// const b = l.b;
// }
// fn formatDocs(l: *Linker) *Build.Step {
// const b = l.b;
// }
};
/// Stores modules and dependencies. Use `link` to register them as imports.
const Linker = struct {
b: *Build,
options: *Build.Step.Options,
target: Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
dependencies: std.StringHashMapUnmanaged(*Build.Dependency) = .{},
modules: std.StringHashMapUnmanaged(*Module) = .{},
dev_modules: std.StringHashMapUnmanaged(*Module) = .{},
fn init(b: *Build) Linker {
var opts = b.addOptions();
opts.addOption([]const u8, "version", b.option([]const u8, "version", "ZLint version") orelse "v0.0.0");
var linker = Linker{
.b = b,
.options = opts,
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
};
const opts_module = opts.createModule();
linker.modules.put(b.allocator, "config", opts_module) catch @panic("OOM");
return linker;
}
fn dependency(self: *Linker, comptime name: []const u8, options: anytype) void {
const dep = self.b.dependency(name, options);
self.dependencies.put(self.b.allocator, name, dep) catch @panic("OOM");
self.modules.put(self.b.allocator, name, dep.module(name)) catch @panic("OOM");
}
fn devDependency(self: *Linker, comptime dep_name: []const u8, mod_name: []const u8, options: anytype) void {
const dep = self.b.dependency(dep_name, options);
self.dependencies.put(self.b.allocator, dep_name, dep) catch @panic("OOM");
self.dev_modules.put(self.b.allocator, mod_name, dep.module(mod_name)) catch @panic("OOM");
}
fn addModule(self: *Linker, comptime name: []const u8, options: Module.CreateOptions) void {
var opts = options;
opts.target = opts.target orelse self.target;
opts.optimize = opts.optimize orelse self.optimize;
const mod = self.b.addModule(name, opts);
self.modules.put(self.b.allocator, name, mod) catch @panic("OOM");
}
fn createModule(self: *Linker, comptime name: []const u8, options: Module.CreateOptions) void {
var opts = options;
opts.target = opts.target orelse self.target;
opts.optimize = opts.optimize orelse self.optimize;
const mod = self.b.createModule(opts);
self.modules.put(self.b.allocator, name, mod) catch @panic("OOM");
}
/// Link a set of modules as imports. When `imports` is empty, all modules
/// are linked.
fn link(self: *Linker, mod: *Module, dev: bool, comptime imports: anytype) void {
if (imports.len > 0) {
inline for (imports) |import| {
const dep = self.modules.get(import) orelse self.dev_modules.get(import) orelse @panic("Missing module: " ++ import);
mod.addImport(import, dep);
}
return;
}
{
var it = self.modules.iterator();
while (it.next()) |ent| {
const name = ent.key_ptr.*;
const dep = ent.value_ptr.*;
if (mod == dep) continue;
mod.addImport(name, dep);
}
}
if (dev) {
var it = self.dev_modules.iterator();
while (it.next()) |ent| {
const name = ent.key_ptr.*;
const dep = ent.value_ptr.*;
if (mod == dep) continue;
mod.addImport(name, dep);
}
}
}
fn deinit(self: *Linker) void {
self.dependencies.deinit(self.b.allocator);
self.modules.deinit(self.b.allocator);
}
};