Skip to content

Commit d4d816a

Browse files
committed
protobuf: allow compiling without protoc
Our goal is to have our builds compile without external dependencies. `protox` helps achieve this. Signed-off-by: John Howard <john.howard@solo.io>
1 parent 1ae7e90 commit d4d816a

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The `build.rs` script in this library depends upon the
66
[Protocol Buffers compiler][protoc]. Be sure that `protoc` is installed and
77
available within your `PATH`.
88

9+
If you enable the off-by-default `protobuf-protox` feature, the build uses
10+
`protox` instead and does not require `protoc`.
11+
912
[protoc]: https://docs.rs/prost-build/latest/prost_build/#sourcing-protoc
1013

1114
## Python Dependencies

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ documentation = "https://docs.rs/prometheus-client"
1313
[features]
1414
default = []
1515
protobuf = ["dep:prost", "dep:prost-types", "dep:prost-build"]
16+
protobuf-protox = ["protobuf", "dep:protox"]
1617

1718
# This feature provides additional APIs for testing downstream code using
1819
# `prometheus-client`.
@@ -50,6 +51,7 @@ http-body-util = "0.1.1"
5051

5152
[build-dependencies]
5253
prost-build = { version = "0.14", optional = true }
54+
protox = { version = "0.9.1", optional = true }
5355

5456
[[bench]]
5557
name = "baseline"

build.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
use std::io::Result;
1+
use std::error::Error;
22

3-
fn main() -> Result<()> {
3+
fn main() -> Result<(), Box<dyn Error>> {
44
#[cfg(feature = "protobuf")]
5-
prost_build::compile_protos(
6-
&["src/encoding/proto/openmetrics_data_model.proto"],
7-
&["src/encoding/proto/"],
8-
)?;
5+
compile_protos()?;
6+
7+
Ok(())
8+
}
9+
10+
#[cfg(feature = "protobuf")]
11+
fn compile_protos() -> Result<(), Box<dyn Error>> {
12+
let protos = ["src/encoding/proto/openmetrics_data_model.proto"];
13+
let includes = ["src/encoding/proto/"];
14+
15+
#[cfg(feature = "protobuf-protox")]
16+
prost_build::compile_fds(protox::compile(protos, includes)?)?;
17+
18+
#[cfg(not(feature = "protobuf-protox"))]
19+
prost_build::compile_protos(&protos, &includes)?;
920

1021
Ok(())
1122
}

0 commit comments

Comments
 (0)