Skip to content

Commit

Permalink
Add an example crate with a few small tests using the codegen crate.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 678734677
  • Loading branch information
dbenson24 authored and copybara-github committed Sep 25, 2024
1 parent a51f98c commit 3b62052
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 6 deletions.
34 changes: 34 additions & 0 deletions rust/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,45 @@ pkg_zip(
],
)

pkg_files(
name = "protobuf_codegen_files",
srcs = glob(["protobuf_codegen/src/*"]) + ["protobuf_codegen/Cargo.toml"],
strip_prefix = strip_prefix.from_root("rust/protobuf_codegen"),
)

pkg_zip(
name = "codegen_crate",
srcs = [
":protobuf_codegen_files",
"//:LICENSE",
],
visibility = ["//rust:__pkg__"],
)

pkg_files(
name = "codegen_example_files",
srcs = glob(["protobuf_codegen/example/**/*"]),
strip_prefix = strip_prefix.from_root("rust/protobuf_codegen/example"),
)

pkg_zip(
name = "codegen_example",
srcs = [
":codegen_example_files",
"//:LICENSE",
],
visibility = ["//rust:__pkg__"],
)

sh_binary(
name = "cargo_test",
srcs = ["cargo_test.sh"],
data = [
":codegen_crate",
":codegen_example",
":rust_crate",
"//:protoc",
"//upb_generator/minitable:protoc-gen-upb_minitable",
],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
29 changes: 26 additions & 3 deletions rust/cargo_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,33 @@ mkdir $CARGO_HOME
CRATE_ROOT=$TMP_DIR/protobuf
mkdir $CRATE_ROOT

CRATE_ZIP=$(rlocation com_google_protobuf/rust/rust_crate.zip)
PROTOBUF_ZIP=$(rlocation com_google_protobuf/rust/rust_crate.zip)

unzip -d $CRATE_ROOT $CRATE_ZIP
cd $CRATE_ROOT
unzip -d $CRATE_ROOT $PROTOBUF_ZIP

CODEGEN_ROOT=$TMP_DIR/protobuf_codegen
mkdir $CODEGEN_ROOT

CODEGEN_ZIP=$(rlocation com_google_protobuf/rust/codegen_crate.zip)

unzip -d $CODEGEN_ROOT $CODEGEN_ZIP

EXAMPLE_ROOT=$TMP_DIR/codegen_example
mkdir $EXAMPLE_ROOT

EXAMPLE_ZIP=$(rlocation com_google_protobuf/rust/codegen_example.zip)

unzip -d $EXAMPLE_ROOT $EXAMPLE_ZIP

cd $CRATE_ROOT
# Run all tests except doctests
CARGO_HOME=$CARGO_HOME cargo test --lib --bins --tests

cd $CODEGEN_ROOT
CARGO_HOME=$CARGO_HOME cargo test --lib --bins --tests

PROTOC=$(rlocation com_google_protobuf/protoc)
PROTOC_GEN_UPB_MINITABLE=$(rlocation com_google_protobuf/upb_generator/minitable/protoc-gen-upb_minitable)

cd $EXAMPLE_ROOT
CARGO_HOME=$CARGO_HOME PROTOC=$PROTOC PROTOC_GEN_UPB_MINITABLE=$PROTOC_GEN_UPB_MINITABLE cargo test
10 changes: 10 additions & 0 deletions rust/protobuf_codegen/example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "protobuf-codegen-example"
version = "0.1.0"
edition = "2021"

[dependencies]
protobuf = { path = "../protobuf" }

[build-dependencies]
protobuf-codegen = { path = "../protobuf_codegen" }
14 changes: 14 additions & 0 deletions rust/protobuf_codegen/example/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use protobuf_codegen::CodeGen;
use std::env;

fn main() {
let mut codegen = CodeGen::new();
codegen
.protoc_path(env::var("PROTOC").expect("PROTOC should be set to the path to protoc"))
.protoc_gen_upb_minitable_path(env::var("PROTOC_GEN_UPB_MINITABLE").expect(
"PROTOC_GEN_UPB_MINITABLE should be set to the path to protoc-gen-upb_minitable",
))
.inputs(["foo.proto", "bar/bar.proto"])
.include("proto");
codegen.compile().unwrap();
}
9 changes: 9 additions & 0 deletions rust/protobuf_codegen/example/proto/bar/bar.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
edition = "2023";

package proto_example;

message Bar {
int32 int = 1;
repeated int32 numbers = 2;
string name = 3;
}
12 changes: 12 additions & 0 deletions rust/protobuf_codegen/example/proto/foo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
edition = "2023";

package proto_example;

import "bar/bar.proto";

message Foo {
int32 int = 1;
repeated int32 numbers = 2;
string name = 3;
Bar bar = 4;
}
33 changes: 33 additions & 0 deletions rust/protobuf_codegen/example/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#[path = "protos/foo.u.pb.rs"]
mod protos;

use protobuf::proto;

use protos::Foo;

fn main() {
let foo = proto!(Foo { name: "foo", bar: __ { name: "bar" } });
dbg!(foo);
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn set_strings() {
let foo = proto!(Foo { name: "foo", bar: __ { name: "bar" } });

assert_eq!(foo.name(), "foo");
assert_eq!(foo.bar().name(), "bar");
}

#[test]
fn set_ints() {
let foo = proto!(Foo { int: 42, bar: __ { numbers: [1, 2, 3] } });

assert_eq!(foo.int(), 42);
let nums: Vec<_> = foo.bar().numbers().iter().collect();
assert_eq!(nums, vec![1, 2, 3]);
}
}
9 changes: 6 additions & 3 deletions rust/protobuf_codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs::{self, OpenOptions};
use std::io::{self, prelude::*};
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

Expand Down Expand Up @@ -84,15 +84,18 @@ impl CodeGen {
for input in &self.inputs {
cmd.arg(input);
}
if !self.output_dir.exists() {
// Attempt to make the directory if it doesn't exist
let _ = std::fs::create_dir(&self.output_dir);
}
cmd.arg(format!("--rust_out={}", self.output_dir.display()))
.arg("--rust_opt=experimental-codegen=enabled,kernel=upb")
.arg(format!("--plugin=protoc-gen-upb={}", self.protoc_gen_upb_path.display()))
.arg(format!(
"--plugin=protoc-gen-upb_minitable={}",
self.protoc_gen_upb_minitable_path.display()
))
.arg(format!("--upb_minitable_out={}", self.output_dir.display()))
.arg(format!("--upb_out={}", self.output_dir.display()));
.arg(format!("--upb_minitable_out={}", self.output_dir.display()));
for include in &self.includes {
cmd.arg(format!("--proto_path={}", include.display()));
}
Expand Down
1 change: 1 addition & 0 deletions upb_generator/minitable/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ bootstrap_cc_binary(
visibility = [
"//editions/codegen_tests:__pkg__",
"//net/proto2/contrib/protoc_explorer:__pkg__",
"//rust:__pkg__",
"//third_party/prototiller/transformer:__pkg__",
],
)
Expand Down

0 comments on commit 3b62052

Please sign in to comment.