Skip to content

Commit

Permalink
fix: remove dependency on protoc in pbjson-types
Browse files Browse the repository at this point in the history
This change moves pbjson-type's code generation to a manual executable
instead of build.rs. Now the generated files are checked into the repo,
and users do not need `protoc` to build the pbjson-types crate.

This has a slight drawback in that pbjson developers will need to invoke
the generator manually after any relevant code-gen changes. This should
generally be a worthwhile tradeoff, as several users have encountered
trouble with requiring protoc (see influxdata#62).

That drawback could potentially be averted with some CI steps to run the
generator and check-in the code automatically; this change does not
include such automation however.
  • Loading branch information
rnarubin committed Oct 14, 2022
1 parent 464915c commit d844f33
Show file tree
Hide file tree
Showing 8 changed files with 7,599 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ members = [
"pbjson-build",
"pbjson-test",
"pbjson-types",
"pbjson-types/code-gen",
]
4 changes: 0 additions & 4 deletions pbjson-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,3 @@ serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
serde_json = "1.0"

[build-dependencies] # In alphabetical order
prost-build = "0.11"
pbjson-build = { path = "../pbjson-build", version = "0.5" }
12 changes: 12 additions & 0 deletions pbjson-types/code-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "code_gen"
version = "0.1.0"
edition = "2021"
license = "MIT"
publish = false

[dependencies]
clap = { version = "3", features = ["derive"] }
prost-build = "0.11"
pbjson-build = { path = "../../pbjson-build", version = "0.5" }
tempfile = "3.3.0"
31 changes: 25 additions & 6 deletions pbjson-types/build.rs → pbjson-types/code-gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
//! Compiles Protocol Buffers and FlatBuffers schema definitions into
//! native Rust types.
//!
//! This is kept as a separate binary to generate code for manual check-in, instead of running at
//! compile time as `build.rs` -- that way downstream consumers do not require the build
//! dependencies (espeically protoc) and can import rust sources directly.

use std::env;
use std::path::PathBuf;

use clap::Parser;

type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Parser)]
struct Args {
/// The path of the directory containing the protobuf sources.
#[clap(short, long, default_value = concat!(env!("CARGO_MANIFEST_DIR"), "/../protos"))]
input_proto_dir: PathBuf,

/// The destination directory for generated code.
#[clap(short, long, default_value = concat!(env!("CARGO_MANIFEST_DIR"), "/../src/pb/"))]
output_dir: PathBuf,
}

fn main() -> Result<()> {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("protos");
let args = Args::parse();
let root = args.input_proto_dir;
let out_dir = &args.output_dir;

let proto_files = vec![root.join("google/protobuf/types.proto")];

// Tell cargo to recompile if any of these proto files are changed
for proto_file in &proto_files {
println!("cargo:rerun-if-changed={}", proto_file.display());
}
std::fs::create_dir_all(out_dir)?;

let descriptor_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("proto_descriptor.bin");
let temp_dir = tempfile::tempdir()?;
let descriptor_path = temp_dir.path().join("proto_descriptor.bin");
prost_build::Config::new()
.file_descriptor_set_path(&descriptor_path)
.compile_well_known_types()
.disable_comments(&["."])
.bytes(&[".google"])
.out_dir(out_dir)
.compile_protos(&proto_files, &[root])?;

let descriptor_set = std::fs::read(descriptor_path)?;
Expand All @@ -45,6 +63,7 @@ fn main() -> Result<()> {
".google.protobuf.UInt32Value",
".google.protobuf.UInt64Value",
])
.out_dir(out_dir)
.build(&[".google"])?;

Ok(())
Expand Down
9 changes: 1 addition & 8 deletions pbjson-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@
clippy::enum_variant_names,
clippy::use_self
)]
mod pb {
pub mod google {
pub mod protobuf {
include!(concat!(env!("OUT_DIR"), "/google.protobuf.rs"));
include!(concat!(env!("OUT_DIR"), "/google.protobuf.serde.rs"));
}
}
}
mod pb;

mod duration;
mod list_value;
Expand Down
Loading

0 comments on commit d844f33

Please sign in to comment.