Protobuf compiler protoc
pre-built binaries installer.
Installed binaries stored in OUT_DIR
of the crate using the library.
Library export init
function which takes version
parameter. Version parameter should be a tag name from protobuf repository without v
prefix, for example, "21.12" or "22.0-rc3" (see protobuf repository tags). Function return a tuple contains paths to protoc
binary and include
directory.
In next examples provided build.rs
script content for different generators. For example, we have next simplified project structure with protobuf files:
src/
proto/
apple.proto
orange.proto
build.rs
With prost-build
use prost_build::compile_protos;
use protoc_prebuilt::init;
use std::env::set_var;
fn main() {
let (protoc_bin, _) = init("22.0").unwrap();
set_var("PROTOC", protoc_bin);
compile_protos(
&["src/proto/apple.proto", "src/proto/orange.proto"],
&["src/proto"]
).unwrap();
}
With protobuf-codegen
use protobuf_codegen::Codegen;
use protoc_prebuilt::init;
use std::env::set_var;
fn main() {
let (protoc_bin, _) = init("22.0").unwrap();
Codegen::new()
.protoc()
.protoc_path(&protoc_bin)
.includes(&["src/proto"])
.inputs(&["src/proto/apple.proto", "src/proto/orange.proto"])
.cargo_out_dir("proto")
.run_from_script();
}
- protoc-bin-vendored store pre-built protobuf compiler in dependencies crates, so you can't use latest or specify version of compiler, if it's not provide by crate author
- protobuf-src build protobuf compiler from sources, it not support
windows
target and compilers versions hardcoded, so you can't use specify version