-
Notifications
You must be signed in to change notification settings - Fork 0
/
step.zig
74 lines (64 loc) · 2.1 KB
/
step.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
// .'\ /`.
// .'.-.`-'.-.`.
// ..._: .-. .-. :_...
// .' '-.(o ) (o ).-' `.
// : _ _ _`~(_)~`_ _ _ :
// : /: ' .-=_ _=-. ` ;\ :
// : :|-.._ ' ` _..-|: :
// : `:| |`:-:-.-:-:'| |:' :
// `. `.| | | | | | |.' .'
// `. `-:_| | |_:-' .'
// `-._ ```` _.-'
// ``-------''
//
// Created by ab, 14.11.2024
const std = @import("std");
const gen = @import("src/codegen/gen.zig");
const generateProtobuf = gen.generateProtobuf;
const ProtoGenStep = @This();
step: std.Build.Step,
proto_sources: std.Build.LazyPath,
gen_output: std.Build.LazyPath,
pub const ProtoGenConfig = struct {
name: []const u8 = "protobuf",
proto_sources: std.Build.LazyPath,
target: std.Build.LazyPath,
};
pub fn create(
owner: *std.Build,
config: ProtoGenConfig,
) *ProtoGenStep {
const step = owner.allocator.create(ProtoGenStep) catch @panic("OOM");
step.* = .{
.step = std.Build.Step.init(.{
.id = std.Build.Step.Id.custom,
.name = config.name,
.owner = owner,
.makeFn = make,
}),
.proto_sources = config.proto_sources,
.gen_output = config.target,
};
return step;
}
fn make(step: *std.Build.Step, _: std.Build.Step.MakeOptions) !void {
const b = step.owner;
const ps: *ProtoGenStep = @fieldParentPtr("step", step);
const proto_path = try ps.proto_sources.getPath3(b, step).toString(b.allocator);
const target_path = try ps.gen_output.getPath3(b, step).toString(b.allocator);
const build_path = b.build_root.path orelse @panic("build path unknown");
const proto_path_resolved = try std.Build.Cache.Directory.cwd().handle.realpathAlloc(b.allocator, proto_path);
defer b.allocator.free(proto_path_resolved);
generateProtobuf(
b.allocator,
proto_path_resolved,
target_path,
build_path,
) catch |err| {
std.log.err("failed to generate protobuf code: {s}", .{@errorName(err)});
return err;
};
}
test {
std.testing.refAllDecls(gen);
}