-
Notifications
You must be signed in to change notification settings - Fork 4
/
sdk.zig
116 lines (107 loc) · 4.08 KB
/
sdk.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
const std = @import("std");
pub const plugin_arg = "--plugin=zig-out/bin/protoc-gen-zig" ++
(if (@import("builtin").target.os.tag == .windows) ".exe" else "");
pub const GenStep = struct {
step: std.Build.Step,
b: *std.Build,
sources: std.ArrayListUnmanaged(std.Build.GeneratedFile) = .{},
cache_path: []const u8,
lib_file: std.Build.GeneratedFile,
module: *std.Build.Module,
/// init a GenStep, create zig-cache/protobuf-zig if not exists, setup
/// dependencies, and setup args to exe.run()
pub fn create(
b: *std.Build,
protoc_filename: []const u8,
exe: *std.Build.Step.Compile,
files: []const []const u8,
) !*GenStep {
const self = b.allocator.create(GenStep) catch unreachable;
const cache_root = std.fs.path.basename(b.cache_root.path orelse ".");
const protobuf_zig_path = "protobuf-zig";
const cache_path = try std.fs.path.join(
b.allocator,
&.{ cache_root, protobuf_zig_path },
);
const lib_path = try std.fs.path.join(
b.allocator,
&.{ cache_path, "lib.zig" },
);
self.* = GenStep{
.step = std.Build.Step.init(.{
.id = .custom,
.name = "build-template",
.owner = b,
.makeFn = &make,
}),
.b = b,
.cache_path = cache_path,
.lib_file = .{
.step = &self.step,
.path = lib_path,
},
.module = b.createModule(.{ .root_source_file = b.path(lib_path) }),
};
for (files) |file| {
const source = try self.sources.addOne(b.allocator);
source.* = .{ .path = file, .step = &self.step };
}
const run_cmd = b.addSystemCommand(&.{
b.getInstallPath(.bin, protoc_filename),
plugin_arg,
"--zig_out",
cache_path,
"-I",
"examples",
});
run_cmd.step.dependOn(&exe.step);
for (files) |file| run_cmd.addArg(file);
self.step.dependOn(&run_cmd.step);
try b.cache_root.handle.makePath(protobuf_zig_path);
return self;
}
/// creates a 'lib.zig' file at self.lib_file.path which exports all
/// generated .pb.zig files
fn make(step: *std.Build.Step, _: std.Build.Step.MakeOptions) anyerror!void {
const self: *GenStep = @fieldParentPtr("step", step);
var file = try std.fs.cwd().createFile(self.lib_file.path.?, .{});
defer file.close();
const writer = file.writer();
for (self.sources.items) |source| {
const endidx = std.mem.lastIndexOf(u8, source.path.?, ".proto") orelse {
std.log.err(
"invalid path '{s}'. expected to end with '.proto'",
.{source.path.?},
);
return error.InvalidPath;
};
const startidx = if (std.mem.lastIndexOfScalar(
u8,
source.path.?[0..endidx],
'/',
)) |i| i + 1 else 0;
const name = source.path.?[startidx..endidx];
// remove illegal characters to make a zig identifier
var buf: [256]u8 = undefined;
@memcpy(buf[0..name.len], name);
if (!std.ascii.isAlphabetic(name[0]) and name[0] != '_') {
std.log.err(
"invalid identifier '{s}'. filename must start with alphabetic or underscore",
.{name},
);
return error.InvalidIdentifier;
}
for (name[1..], 0..) |c, i| {
if (!std.ascii.isAlphanumeric(c)) buf[i + 1] = '_';
}
const path = if (std.mem.startsWith(u8, source.path.?, "examples/"))
source.path.?[0..endidx]["examples/".len..]
else
source.path.?[0..endidx];
try writer.print(
\\pub const {s} = @import("{s}.pb.zig");
\\
, .{ buf[0..name.len], path });
}
}
};