Crate zksync_protobuf_build

source ·
Expand description

Generates rust code from the proto files.

Protobuf files are collected recursively from $CARGO_MANIFEST_DIR/<input_root>/ directory. Corresponding “cargo:rerun-if-changed=…” line is printed to stdout, so that the build script running this function is rerun whenever proto files change. A single rust file is generated and stored at $OUT_DIR/<input_root>/gen.rs file.

Protobuf files are compiled to a protobuf descriptor stored at $OUT_DIR/<input_root>/gen.binpb. Additionally a “PROTOBUF_DESCRIPTOR=” line is printed to stdout. This can be used to collect all the descriptors across the build as follows:

  1. Checkout the repo to a fresh directory and then run “cargo build –all-targets” We need it fresh so that every crate containing protobufs has only one build in the cargo cache.
  2. grep through all target/debug/build/*/output files to find all “PROTOBUF_DESCRIPTOR=…” lines and merge the descriptor files by simply concatenating them. Note that you can run this procedure for 2 revisions of the repo and look for breaking changes by running “buf breaking <after.binpb> –against <before.binpb>” where before.binpb and after.binpb are the concatenated descriptors from those 2 revisions.

The proto files are not expected to be self-contained - to import proto files from different crates you need to specify them as dependencies in the Config.dependencies. It is not possible to depend on a different proto bundle within the same crate (because these are being built simultaneously from the same build script).

Structs§

  • Code generation config. Use it in build scripts.
  • Path relative to $CARGO_MANIFEST_DIR.
  • In addition to input paths and proto paths, there are also proto names which are used to reference message types from different proto files. Theoretically proto names can be totally independent from the proto paths (i.e. you can have “my.favorite.package” proto package defined in “totally/unrelated/file/name.proto” file, but we recommend the following naming convention: proto package “a.b.c” should be defined either: a) in a single file “a/b/c.proto”, or b) in a collection of files under “a/b/c/” directory Option b) is useful for defining large packages, because there is no equivalent of “pub use” in proto syntax.
  • Absolute path of the proto file used for importing other proto files.
  • A rust module/type name that the generated code is available at. Although generated code is location agnostic (it can be embedded in an arbitrary module within the crate), you need to manually (in the Config) specify the rust modules containing the generated code of the dependencies, so that it can be referenced from the newly generated code.