Skip to content

Commit

Permalink
Programatically add rust config to vendored protos (pantsbuild#7000)
Browse files Browse the repository at this point in the history
I'm about to add a second build script which will use prost for its
codegen, and which needs the pristine protos, so instead of checking
them in altered, alter them on the fly specifically for protoc_grpcio.

This also makes it easier to pull in updates.
  • Loading branch information
illicitonion authored Dec 28, 2018
1 parent 9cf23b0 commit a1480aa
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 39 deletions.
9 changes: 0 additions & 9 deletions 3rdparty/protobuf/bazelbuild_remote-apis/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
This is a dump of the .proto files from https://github.com/bazelbuild/remote-apis directory build.

This dump was taken at git sha cbf6ada7f5b2a0ce14646bf983d03b49118f0ec8.

The following script was run to enable Bytes fields for Rust:
```
sed -i '' '/^package /a\
\
import "rustproto.proto";\
option (rustproto.carllerche_bytes_for_bytes_all) = true;\
' build/bazel/remote/execution/v2/remote_execution.proto
```
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ syntax = "proto3";

package build.bazel.remote.execution.v2;

import "rustproto.proto";
option (rustproto.carllerche_bytes_for_bytes_all) = true;

import "google/api/annotations.proto";
import "google/longrunning/operations.proto";
import "google/protobuf/duration.proto";
Expand Down
9 changes: 0 additions & 9 deletions 3rdparty/protobuf/googleapis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,3 @@ This is a dump of the .proto files from https://github.com/googleapis/googleapis
This dump was taken at git sha e17dbfb19652240490cae8adeb89991d13cf9df7.

It is a selective view of only the protos we actually need.

The following script was run to enable Bytes fields for Rust:
```
sed -i '' '/^package /a\
\
import "rustproto.proto";\
option (rustproto.carllerche_bytes_for_bytes_all) = true;\
' {google/devtools/remoteexecution/v1test/remote_execution.proto,google/bytestream/bytestream.proto,google/rpc/{code,error_details,status}.proto,google/longrunning/operations.proto}
```
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ syntax = "proto3";

package google.bytestream;

import "rustproto.proto";
option (rustproto.carllerche_bytes_for_bytes_all) = true;

import "google/api/annotations.proto";
import "google/protobuf/wrappers.proto";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ syntax = "proto3";

package google.longrunning;

import "rustproto.proto";
option (rustproto.carllerche_bytes_for_bytes_all) = true;

import "google/api/annotations.proto";
import "google/protobuf/any.proto";
import "google/protobuf/empty.proto";
Expand Down
3 changes: 0 additions & 3 deletions 3rdparty/protobuf/googleapis/google/rpc/code.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ syntax = "proto3";

package google.rpc;

import "rustproto.proto";
option (rustproto.carllerche_bytes_for_bytes_all) = true;

option go_package = "google.golang.org/genproto/googleapis/rpc/code;code";
option java_multiple_files = true;
option java_outer_classname = "CodeProto";
Expand Down
3 changes: 0 additions & 3 deletions 3rdparty/protobuf/googleapis/google/rpc/error_details.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ syntax = "proto3";

package google.rpc;

import "rustproto.proto";
option (rustproto.carllerche_bytes_for_bytes_all) = true;

import "google/protobuf/duration.proto";

option go_package = "google.golang.org/genproto/googleapis/rpc/errdetails;errdetails";
Expand Down
3 changes: 0 additions & 3 deletions 3rdparty/protobuf/googleapis/google/rpc/status.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ syntax = "proto3";

package google.rpc;

import "rustproto.proto";
option (rustproto.carllerche_bytes_for_bytes_all) = true;

import "google/protobuf/any.proto";

option go_package = "google.golang.org/genproto/googleapis/rpc/status;status";
Expand Down
2 changes: 2 additions & 0 deletions src/rust/engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/rust/engine/process_execution/bazel_protos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ protobuf = { version = "2.0.4", features = ["with-bytes"] }
build_utils = { path = "../../build_utils" }
grpcio-compiler = "0.3"
protoc-grpcio = "0.2"
tempfile = "3"
walkdir = "2"
48 changes: 45 additions & 3 deletions src/rust/engine/process_execution/bazel_protos/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use protoc_grpcio;

use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use build_utils::BuildRoot;

Expand All @@ -14,6 +14,9 @@ fn main() {
thirdpartyprotobuf.to_str().unwrap()
);

let amended_proto_root =
add_rustproto_header(&thirdpartyprotobuf).expect("Error adding proto bytes header");

let gen_dir = PathBuf::from("src/gen");

// Re-gen if, say, someone does a git clean on the gen dir but not the target dir. This ensures
Expand All @@ -31,8 +34,7 @@ fn main() {
"google/protobuf/empty.proto",
],
&[
thirdpartyprotobuf.join("bazelbuild_remote-apis"),
thirdpartyprotobuf.join("googleapis"),
amended_proto_root.path().to_owned(),
thirdpartyprotobuf.join("standard"),
thirdpartyprotobuf.join("rust-protobuf"),
],
Expand Down Expand Up @@ -64,3 +66,43 @@ fn main() {
.and_then(|mut f| f.write_all(contents.as_bytes()))
.expect("Failed to write mod.rs")
}

const EXTRA_HEADER: &'static str = r#"import "rustproto.proto";
option (rustproto.carllerche_bytes_for_bytes_all) = true;
"#;

///
/// Copies protos from thirdpartyprotobuf, adds a header to make protoc_grpcio uses Bytes instead
/// of Vec<u8>s, and rewrites them into a temporary directory
///
fn add_rustproto_header(thirdpartyprotobuf: &Path) -> Result<tempfile::TempDir, String> {
let amended_proto_root = tempfile::TempDir::new().unwrap();
for f in &["bazelbuild_remote-apis", "googleapis"] {
let src_root = thirdpartyprotobuf.join(f);
for entry in walkdir::WalkDir::new(&src_root)
.into_iter()
.filter_map(|entry| entry.ok())
.filter(|entry| entry.file_type().is_file())
.filter(|entry| entry.file_name().to_string_lossy().ends_with(".proto"))
{
let dst = amended_proto_root
.path()
.join(entry.path().strip_prefix(&src_root).unwrap());
std::fs::create_dir_all(dst.parent().unwrap())
.map_err(|err| format!("Error making dir in temp proto root: {}", err))?;
let original = std::fs::read_to_string(entry.path())
.map_err(|err| format!("Error reading proto {}: {}", entry.path().display(), err))?;
let mut copy = String::with_capacity(original.len() + EXTRA_HEADER.len());
for line in original.lines() {
copy += line;
copy += "\n";
if line.starts_with("package ") {
copy += EXTRA_HEADER
}
}
std::fs::write(&dst, copy.as_bytes())
.map_err(|err| format!("Error writing {}: {}", dst.display(), err))?;
}
}
Ok(amended_proto_root)
}

0 comments on commit a1480aa

Please sign in to comment.