Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
executable file
·416 lines (347 loc) · 12.9 KB
/
Copy pathbuild.zig
File metadata and controls
executable file
·416 lines (347 loc) · 12.9 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
const std = @import("std");
const Build = std.Build;
const ArrayList = std.ArrayList;
const Allocator = std.mem.Allocator;
const Writer = std.Io.Writer;
const builtin = @import("builtin");
const Encoder = std.base64.standard.Encoder;
const Data = @import("src/zmpl.zig").Data;
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const options_files = b.addWriteFiles();
var threaded: std.Io.Threaded = .init_single_threaded;
// This deinit is not necessary since it's stack allocated but it's ok to
// keep, in case we want to change the threaded initialization at some point
defer threaded.deinit();
const io = threaded.io();
const use_llvm = b.option(
bool,
"use_llvm",
"Use LLVM",
);
const test_filters = b.option(
[]const []const u8,
"test-filter",
"Skip tests that do not match any filter",
) orelse &.{};
const zmpl_auto_build_option = b.option(
bool,
"zmpl_auto_build",
"Automatically compile Zmpl templates (default: true)",
);
const zmpl_markdown_fragments_option = b.option(
[]const u8,
"zmpl_markdown_fragments",
"Custom markdown fragments",
) orelse "";
const zmpl_options_header_option = b.option(
[]const u8,
"zmpl_options_header",
"Additional options header",
) orelse "";
const zmpl_manifest_header_option = b.option(
[]const u8,
"zmpl_manifest_header",
"Additional manifest header",
) orelse "";
const zmpl_constants_option = b.option(
[]const u8,
"zmpl_constants",
"Template constants",
) orelse "";
const templates_paths = b.option(
[]const []const u8,
"zmpl_templates_paths",
"Directories to search for .zmpl templates. Format: `prefix=...,path=...",
) orelse try templatesPaths(
io,
b.allocator,
&.{.{
.prefix = "templates",
.path = &.{ "src", "templates" },
}},
);
const build_options = b.addOptions();
build_options.addOption(
bool,
"sanitize",
b.option(
bool,
"sanitize",
"Disable default sanitization of data references.",
) orelse true,
);
const zmpl_options = b.addModule("zmpl_options", .{
.root_source_file = options_files.add(
"zmpl_options.zig",
try generateZmplOptions(
b.allocator,
zmpl_options_header_option,
zmpl_markdown_fragments_option,
zmpl_constants_option,
zmpl_manifest_header_option,
),
),
});
const zmd = b.dependency("zmd", .{
.target = target,
.optimize = optimize,
}).module("zmd");
const jetcommon = b.dependency("jetcommon", .{
.target = target,
.optimize = optimize,
}).module("jetcommon");
const entry = b.createModule(.{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/main.zig"),
});
const manifest_m = b.createModule(.{
.root_source_file = b.path("src/manifest/main.zig"),
.target = b.graph.host,
.optimize = optimize,
});
const manifest_exe = b.addExecutable(.{
.name = "manifest",
.use_llvm = use_llvm,
.root_module = manifest_m,
});
const zmpl = b.addModule("zmpl", .{
.root_source_file = b.path("src/zmpl.zig"),
});
const dummy_manifest = b.createModule(.{
.root_source_file = b.path("src/dummy_manifest.zig"),
});
const dummy_zmpl_options = b.createModule(.{
.root_source_file = b.path("src/manifest/dummy_zmpl_options.zig"),
});
const tests = b.createModule(.{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
});
const zmpl_m = b.createModule(.{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/zmpl.zig"),
});
const lib = b.addLibrary(.{
.name = "zmpl",
.linkage = .static,
.use_llvm = use_llvm,
.root_module = zmpl_m,
});
const exe = b.addExecutable(.{
.name = "zmpl",
.use_llvm = use_llvm,
.root_module = entry,
});
const run_artifact = b.addRunArtifact(exe);
const run_step = b.step("run", "Run benchmarking");
run_step.dependOn(&run_artifact.step);
lib.root_module.addImport("zmpl", zmpl);
exe.root_module.addImport("zmpl", zmpl);
zmpl.addOptions("build_options", build_options);
lib.root_module.addImport("zmd", zmd);
zmpl.addImport("zmd", zmd);
lib.root_module.addImport("jetcommon", jetcommon);
zmpl.addImport("jetcommon", jetcommon);
const auto_build = if (zmpl_auto_build_option) |opt| opt else true;
zmpl_options.addImport("zmd", zmd);
manifest_exe.root_module.addImport("zmpl_options", zmpl_options);
manifest_exe.root_module.addImport("zmd", zmd);
manifest_exe.root_module.addImport("jetcommon", jetcommon);
const manifest_exe_run = b.addRunArtifact(manifest_exe);
b.getInstallStep().dependOn(&manifest_exe_run.step);
const manifest_lazy_path = manifest_exe_run.addOutputFileArg("zmpl.manifest.zig");
manifest_exe_run.setCwd(.{
.cwd_relative = try std.Io.Dir.cwd().realPathFileAlloc(io, ".", b.allocator),
});
manifest_exe_run.expectExitCode(0);
manifest_exe_run.addArg(try std.mem.join(b.allocator, ";", templates_paths));
lib.step.dependOn(&manifest_exe_run.step);
for (try findTemplates(b, io, templates_paths)) |path|
manifest_exe_run.addFileArg(.{ .cwd_relative = path });
const compile_step = b.step("compile", "Compile Zmpl templates");
compile_step.dependOn(&manifest_exe_run.step);
const manifest = b.addModule("zmpl.manifest", .{
.root_source_file = manifest_lazy_path,
});
manifest.addImport("zmpl", zmpl);
manifest.addImport("zmd", zmd);
zmpl.addImport("zmpl.manifest", manifest);
if (auto_build) {
const template_tests = b.addTest(.{
.filters = test_filters,
.root_module = tests,
});
const zmpl_tests = b.addTest(.{
.filters = test_filters,
.root_module = b.createModule(.{
.root_source_file = b.path("src/zmpl.zig"),
.target = target,
.optimize = optimize,
}),
});
const manifest_tests = b.addTest(.{
.filters = test_filters,
.root_module = b.createModule(.{
.root_source_file = b.path("src/manifest/main.zig"),
.target = target,
.optimize = optimize,
}),
});
template_tests.root_module.addImport("zmpl", zmpl);
template_tests.root_module.addImport("zmpl.manifest", manifest);
template_tests.root_module.addImport("jetcommon", jetcommon);
zmpl_tests.root_module.addImport("jetcommon", jetcommon);
zmpl_tests.root_module.addImport("zmpl.manifest", dummy_manifest);
zmpl_tests.root_module.addImport("zmd", zmd);
manifest_tests.root_module.addImport("zmpl_options", dummy_zmpl_options);
manifest_tests.root_module.addImport("zmd", zmd);
const run_template_tests = b.addRunArtifact(template_tests);
const run_zmpl_tests = b.addRunArtifact(zmpl_tests);
const run_manifest_tests = b.addRunArtifact(manifest_tests);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_template_tests.step);
test_step.dependOn(&run_zmpl_tests.step);
test_step.dependOn(&run_manifest_tests.step);
}
b.installArtifact(lib);
const docs_step = b.step("docs", "Generate documentation");
const docs_install = b.addInstallDirectory(.{
.source_dir = lib.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
docs_step.dependOn(&docs_install.step);
}
const TemplatesPath = struct {
prefix: []const u8,
path: []const []const u8,
};
pub fn templatesPaths(io: std.Io, allocator: Allocator, paths: []const TemplatesPath) ![]const []const u8 {
var buf: ArrayList([]const u8) = .empty;
defer buf.deinit(allocator);
for (paths) |path| {
const joined = try std.fs.path.join(allocator, path.path);
defer allocator.free(joined);
const absolute_path = if (std.fs.path.isAbsolute(joined))
try allocator.dupe(u8, joined)
else
std.Io.Dir.cwd().realPathFileAlloc(io, joined, allocator) catch |err|
switch (err) {
error.FileNotFound => "_",
else => return err,
};
try buf.append(
allocator,
try std.mem.concat(
allocator,
u8,
&.{ "prefix=", path.prefix, ",path=", absolute_path },
),
);
}
return buf.toOwnedSlice(allocator);
}
pub fn addTemplateConstants(b: *Build, comptime constants: type) ![]const u8 {
const fields = switch (@typeInfo(constants)) {
.@"struct" => |info| info.fields,
else => @panic("Expected struct, found: " ++ @typeName(constants)),
};
var array: [fields.len][]const u8 = undefined;
inline for (fields, 0..) |field, index| {
array[index] = std.fmt.comptimePrint(
"{s}#{s}",
.{ field.name, @typeName(field.type) },
);
}
return std.mem.join(b.allocator, "|", &array);
}
fn findTemplates(b: *Build, io: std.Io, templates_paths: []const []const u8) ![][]const u8 {
var templates: ArrayList([]const u8) = .empty;
defer templates.deinit(b.allocator);
var templates_paths_buf: ArrayList([]const u8) = .empty;
defer templates_paths_buf.deinit(b.allocator);
for (templates_paths) |syntax| {
const prefix_end = std.mem.indexOf(u8, syntax, ",path=").?;
const path_start = prefix_end + ",path=".len;
const path = syntax[path_start..];
try templates_paths_buf.append(b.allocator, path);
}
for (templates_paths_buf.items) |templates_path| {
if (std.mem.eql(u8, templates_path, "_")) continue;
var dir = std.Io.Dir.cwd().openDir(
io,
templates_path,
.{ .iterate = true },
) catch |err| {
switch (err) {
error.FileNotFound => {
std.log.warn(
"[zmpl] Template directory `{s}` not found, skipping.",
.{templates_path},
);
continue;
},
else => return err,
}
};
var walker = try dir.walk(b.allocator);
defer walker.deinit();
while (try walker.next(io)) |entry| {
if (entry.kind != .file) continue;
const extension = std.fs.path.extension(entry.path);
if (!std.mem.eql(u8, extension, ".zmpl")) continue;
try templates.append(b.allocator, try dir.realPathFileAlloc(io, entry.path, b.allocator));
}
}
return templates.toOwnedSlice(b.allocator);
}
fn generateZmplOptions(
allocator: Allocator,
options_header_option: []const u8,
markdown_fragments: []const u8,
constants: []const u8,
manifest_header_option: []const u8,
) ![]const u8 {
var aw: Writer.Allocating = .init(allocator);
try aw.writer.print(
"//Generated in build.zig\n{[options]s}\n\n",
.{ .options = options_header_option },
);
try parseZmplConstants(
&aw.writer,
constants,
);
const encodedHeader: []u8 = try allocator.alloc(u8, Encoder.calcSize(manifest_header_option.len));
defer allocator.free(encodedHeader);
const base64Header = Encoder.encode(encodedHeader, manifest_header_option);
try aw.writer.print(
"{s}\n\npub const manifest_header: []const u8 = \"{s}\";\n",
.{ markdown_fragments, base64Header },
);
return aw.toOwnedSlice();
}
fn parseZmplConstants(writer: *Writer, constants_string: []const u8) !void {
if (constants_string.len == 0) return;
try writer.writeAll("pub const template_constants = struct {\n");
var pairs_it = std.mem.splitScalar(u8, constants_string, '|');
while (pairs_it.next()) |pair| {
var arg_it = std.mem.splitScalar(u8, pair, '#');
const const_name = arg_it.first();
const const_type = arg_it.next() orelse {
std.log.err("Incomplete Zmpl constants argument: {s}", .{constants_string});
return error.ZmplConstantsOptionError;
};
// this should be null
if (arg_it.next()) |_| {
std.log.err("Incoherent Zmpl constants argument: {s}", .{constants_string});
return error.ZmplConstantsOptionError;
}
try writer.print(" {s}: {s},\n", .{ const_name, const_type });
}
try writer.writeAll("};\n");
}