This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LexStep.zig
67 lines (53 loc) · 2.24 KB
/
LexStep.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
const std = @import("std");
const LexStep = @This();
const evalChildProcess = @import("build.zig").evalChildProcessInDirectory;
step: std.Build.Step,
source: std.Build.LazyPath,
output_source: std.Build.GeneratedFile,
output_header: std.Build.GeneratedFile,
pub fn create(b: *std.Build, source: std.Build.LazyPath) *LexStep {
const self = b.allocator.create(LexStep) catch @panic("OOM");
self.* = .{
.step = std.Build.Step.init(.{
.id = .custom,
.name = b.fmt("Lex {s}", .{source.getDisplayName()}),
.owner = b,
.makeFn = make,
}),
.source = source,
.output_source = .{ .step = &self.step },
.output_header = .{ .step = &self.step },
};
source.addStepDependencies(&self.step);
return self;
}
fn make(step: *std.Build.Step, _: *std.Progress.Node) anyerror!void {
const b = step.owner;
const self = @fieldParentPtr(LexStep, "step", step);
var man = b.graph.cache.obtain();
defer man.deinit();
_ = try man.addFile(self.source.getPath2(b, step), null);
const name = std.fs.path.stem(self.source.getPath2(b, step));
if (try step.cacheHit(&man)) {
const digest = man.final();
self.output_source.path = try b.cache_root.join(b.allocator, &.{ "o", &digest, b.fmt("{s}.c", .{name}) });
self.output_header.path = try b.cache_root.join(b.allocator, &.{ "o", &digest, b.fmt("{s}.h", .{name}) });
return;
}
const digest = man.final();
const cache_path = "o" ++ std.fs.path.sep_str ++ digest;
var cache_dir = b.cache_root.handle.makeOpenPath(cache_path, .{}) catch |err| {
return step.fail("unable to make path '{}{s}': {s}", .{
b.cache_root, cache_path, @errorName(err),
});
};
defer cache_dir.close();
const cmd = try b.findProgram(&.{ "flex", "lex" }, &.{});
try evalChildProcess(step, &.{
cmd,
self.source.getPath2(b, step),
}, try b.cache_root.join(b.allocator, &.{ "o", &digest }));
self.output_source.path = try b.cache_root.join(b.allocator, &.{ "o", &digest, b.fmt("{s}.c", .{name}) });
self.output_header.path = try b.cache_root.join(b.allocator, &.{ "o", &digest, b.fmt("{s}.h", .{name}) });
try step.writeManifest(&man);
}