dlprotoc: Download protoc for Cargo build scripts
This crate downloads the official binary releases of protoc from Google's protobuf Github repo, verifies a SHA256 hash, then extracts it. It easy to use with Prost or Tonic.
This fixes Cargo errors like the following:
error: failed to run custom build command for `example v0.1.0 (/home/user/example)`
Caused by:
process didn't exit successfully: `target/debug/build/example-3f5090329e3c4e4b/build-script-build` (exit status: 1)
--- stderr
Error: Custom { kind: NotFound, error: "Could not find `protoc`. If `protoc` is installed, try setting the `PROTOC` environment variable to the path of the `protoc` binary. To install it on Debian, run `apt-get install protobuf-compiler`. It is also available at https://github.com/protocolbuffers/protobuf/releases For more information: https://docs.rs/prost-build/#sourcing-protoc" }
An alternative is the protobuf-src crate, which compiles protoc from source. Unfortunately, compiling protoc is quite slow time (approximately 2 minutes on my 2020 4 core Intel Linux desktop), and has more dependencies. Notably: protobuf-src requires cmake and a C++ compiler.
Quick Start
In your build.rs, call dlprotoc::download_protoc() before calling compile_protos(...):
Trust/Security
This downloads pre-compiled executables on Github, which is somewhat dangerous. You need to trust:
- The author of this crate (me! Evan Jones)
- I manually add SHA256 hashes for protoc releases, to allow them to be downloaded.
- I wrote this crate. Let's hope it is mostly free from bugs
- Crates.io: To give you a non-malicious version of this crate.
- Github: To provide the protoc binaries that Google uploaded.
- Google Protobuf maintainers: To upload non-malicious protoc binaries to Github.
Updating to new protoc releases (for maintainers)
- Run:
cargo run -- (version e.g 27.0) - Append the printed struct definitions into the
KNOWN_VERSIONSarray inlib.rs. - Run
maketo execute all checks. - Update the version in
Cargo.tomlto include the version of protoc. E.g."0.1.0+27.0". - Send a pull request.