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:
text
src/
proto/
apple.proto
orange.proto
build.rs
With prost-build
```rust,norun use prostbuild::compileprotos; use protocprebuilt::init; use std::env::set_var;
fn main() { let (protocbin, _) = init("22.0").unwrap(); setvar("PROTOC", protoc_bin);
compile_protos( &["src/proto/apple.proto", "src/proto/orange.proto"], &["src/proto"] ).unwrap(); } ```
With protobuf-codegen
```rust,norun use protobufcodegen::Codegen; use protocprebuilt::init; use std::env::setvar;
fn main() { let (protoc_bin, _) = init("22.0").unwrap();
Codegen::new() .protoc() .protocpath(&protocbin) .includes(&["src/proto"]) .inputs(&["src/proto/apple.proto", "src/proto/orange.proto"]) .cargooutdir("proto") .runfromscript(); } ```
windows
target and compilers versions hardcoded, so you can't use specify version