Skip to content

Commit 00dfee6

Browse files
authored
Simplify public facing compiler API, enable custom protoc executable (#180)
* simplify public facing compiler api * fix: getPath() was called on a GeneratedFile that wasnt built yet
1 parent 0e4347e commit 00dfee6

4 files changed

Lines changed: 22 additions & 19 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ pub fn build(b: *std.Build) !void {
6060
const protoc_step = protobuf.RunProtocStep.create(protobuf_dep.builder, target, .{
6161
// out directory for the generated zig files
6262
.destination_directory = b.path("src/proto"),
63-
// Optional custom generator, otherwise it will use the built-in generator + google's protoc
64-
// .generator = protobuf_dep.artifact("protoc-gen-zig"),
63+
// Optional LazyPath to `protoc`. If null, zig-protobuf will download Google's release of
64+
// the compiler.
65+
// .protoc = b.path("protoc"),
6566
.source_files = &.{
6667
b.path("protocol/all.proto"),
6768
},

build.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ pub fn build(b: *std.Build) !void {
152152
.destination_directory = b.path("tests/generated"),
153153
.source_files = &.{
154154
b.path("tests/protos_for_test/all.proto"),
155-
b.path("tests/protos_for_test/whitespace-in-name.proto"),
156155
b.path("tests/protos_for_test/complex_type.proto"),
156+
b.path("tests/protos_for_test/issue-143-self-ref-union.proto"),
157+
b.path("tests/protos_for_test/onnx.proto"),
157158
b.path("tests/protos_for_test/test_service.proto"),
159+
b.path("tests/protos_for_test/whitespace-in-name.proto"),
158160
},
159161
.include_directories = &.{b.path("tests/protos_for_test")},
160162
});

build_util.zig

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,8 @@ pub const RunProtocStep = struct {
7979
include_directories: []std.Build.LazyPath,
8080
destination_directory: std.Build.LazyPath,
8181
generator: *std.Build.Step.Compile,
82-
protoc_owner: *std.Build,
83-
/// Optional external protoc binary. When set, skips the built-in download
84-
/// mechanism and uses this artifact's emitted binary instead. Useful for
85-
/// consumers (e.g. conformance tests) that already have protoc from another
86-
/// dependency.
87-
protoc_override: ?*std.Build.Step.Compile = null,
82+
generator_bin: std.Build.LazyPath,
83+
protoc_override_bin: ?std.Build.LazyPath = null,
8884
preserve_unknown_fields: bool = false,
8985
verbose: bool = false,
9086

@@ -98,9 +94,9 @@ pub const RunProtocStep = struct {
9894
/// protoc step can be owned by a consumer builder while the generator
9995
/// stays owned by the zig-protobuf dependency builder.
10096
generator: ?*std.Build.Step.Compile = null,
101-
/// Optional pre-built protoc artifact. When provided, overrides the
97+
/// Optional pre-built protoc binary. When provided, overrides the
10298
/// built-in protoc download mechanism.
103-
protoc: ?*std.Build.Step.Compile = null,
99+
protoc: ?std.Build.LazyPath = null,
104100
/// When true, every generated message preserves unknown fields during
105101
/// binary decode/encode round trips. Defaults to false.
106102
preserve_unknown_fields: bool = false,
@@ -128,13 +124,13 @@ pub const RunProtocStep = struct {
128124
.include_directories = dupeLazyPaths(owner, options.include_directories),
129125
.destination_directory = options.destination_directory.dupe(owner),
130126
.generator = generator,
131-
.protoc_owner = generator.step.owner,
132-
.protoc_override = options.protoc,
127+
.generator_bin = generator.getEmittedBin(),
128+
.protoc_override_bin = options.protoc,
133129
.preserve_unknown_fields = options.preserve_unknown_fields,
134130
};
135131

136132
self.step.dependOn(&self.generator.step);
137-
if (options.protoc) |p| self.step.dependOn(&p.step);
133+
138134
return self;
139135
}
140136

@@ -166,17 +162,17 @@ pub const RunProtocStep = struct {
166162
{ // run protoc
167163
var argv: std.ArrayList([]const u8) = .empty;
168164

169-
const maybe_protoc_path: ?[]const u8 = if (self.protoc_override) |p|
170-
p.getEmittedBin().getPath2(b, step)
165+
const maybe_protoc_path: ?[]const u8 = if (self.protoc_override_bin) |bin|
166+
bin.getPath2(b, step)
171167
else
172-
try ensureProtocBinaryDownloaded(self.protoc_owner, step);
168+
try ensureProtocBinaryDownloaded(self.generator.step.owner, step);
173169

174170
if (maybe_protoc_path) |protoc_path| {
175171
try argv.append(b.allocator, protoc_path);
176172

177173
try argv.append(b.allocator, try std.mem.concat(b.allocator, u8, &.{
178174
"--plugin=protoc-gen-zig=",
179-
self.generator.getEmittedBin().getPath2(b, step),
175+
self.generator_bin.getPath2(b, step),
180176
}));
181177

182178
const zig_out = if (self.preserve_unknown_fields)

conformance/build.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ pub fn build(b: *std.Build) void {
2424
.include_directories = &.{ b.path("protos"), upstream.path("src") },
2525
.destination_directory = b.path("generated"),
2626
.generator = protoc_gen_zig,
27-
.protoc = protoc,
27+
.protoc = protoc.getEmittedBin(),
2828
.preserve_unknown_fields = true,
2929
});
30+
31+
// Instruct zig build system to first build protoc before trying to generate the .pb.zig!
32+
gen_zig.step.dependOn(&protoc.step);
33+
3034
b.step("generate", "Regenerate Zig bindings for conformance protos").dependOn(&gen_zig.step);
3135

3236
// Testee binary — uses pre-generated bindings from conformance/generated/.

0 commit comments

Comments
 (0)