#protobuf-compiler #protobuf #protoc #api-bindings

protoc-prebuilt

Protobuf compiler protoc pre-built binaries installer

3 releases (breaking)

0.3.0 Mar 5, 2024
0.2.0 May 30, 2023
0.1.0 Feb 22, 2023

#3 in #protoc

Download history 103/week @ 2023-12-22 314/week @ 2023-12-29 480/week @ 2024-01-05 2130/week @ 2024-01-12 10527/week @ 2024-01-19 13023/week @ 2024-01-26 13259/week @ 2024-02-02 13593/week @ 2024-02-09 13523/week @ 2024-02-16 13793/week @ 2024-02-23 13498/week @ 2024-03-01 13521/week @ 2024-03-08 14432/week @ 2024-03-15 15976/week @ 2024-03-22 13929/week @ 2024-03-29 11704/week @ 2024-04-05

58,014 downloads per month
Used in 5 crates (3 directly)

MIT license

42KB
827 lines

protoc-prebuilt

crates.io docs.rs license

Protobuf compiler protoc pre-built binaries installer.

Installed binaries stored in OUT_DIR of the crate using the library.

Usage

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();
}

GitHub API limits

To avoid GitHub API limits library add Authorization header to requests to API with GITHUB_TOKEN environment variable content.

To prevent this behavior, set PROTOC_PREBUILT_NOT_ADD_GITHUB_TOKEN environment variable to any value reduced to true (see var_bool function in sources).

To force this library to use autorization token from another environment variable, set its name to PROTOC_PREBUILT_GITHUB_TOKEN_ENV_NAME environment variable.

Using custom protobuf installation

If you have custom protobuf installation and need to use this installed version, use next environment variables to change default behavior:

  • PROTOC_PREBUILT_FORCE_PROTOC_PATH to set force use path to protoc binary from value of this variable, if it variable exists, protoc-prebuilt not download protobuf from GitHub;

  • PROTOC_PREBUILT_FORCE_INCLUDE_PATH to set force use path to includes directory from value of this variable, if it variable not exists, protoc-prebuilt calculate path to includes directory himself from protoc binary path depending on version (see get_include_path function in sources).

Using HTTP proxy for request to GitHub API

For setup HTTP proxy protoc-prebuilt use environment variables same as curl does it. Library use HTTP_PROXY, HTTPS_PROXY and them lowercase analogues.

To disable proxy usage with curl agreement you can add github.com (to bypass proxy in asset downloading), api.github.com (to bypass proxy in version exists cheking), .github.com (to bypass proxy in both variants) to NO_PROXY or no_proxy environment variable.

To disable any use of proxy in protoc-prebuilt set PROTOC_PREBUILT_NOT_USE_PROXY environment variable to any value reduced to true (see var_bool function in sources).

Version checking

After installation protoc-prebuilt run protoc binary with "--version" argument and compare result with required version. It need to make sure the installation is correct and check version of custom protobuf installation.

If you need disable this behavior, set PROTOC_PREBUILT_NOT_CHECK_VERSION environment variable to any value reduced to true (see var_bool function in sources).

Comparison with analogues

  • 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.

Dependencies

~10–20MB
~358K SLoC