Skip to content

Commit 671ae8f

Browse files
Make protobuf_example publishable by having it only perform code generation behind a feature.
PiperOrigin-RevId: 703150378
1 parent c18384c commit 671ae8f

File tree

5 files changed

+17
-6
lines changed

5 files changed

+17
-6
lines changed

rust/release_crates/cargo_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ CARGO_HOME=$CARGO_HOME cargo test --lib --bins --tests
6868
CARGO_HOME=$CARGO_HOME cargo publish --dry-run
6969

7070
cd $EXAMPLE_ROOT
71-
CARGO_HOME=$CARGO_HOME cargo test
71+
CARGO_HOME=$CARGO_HOME cargo test --features run-protobuf-codegen
7272
# TODO: Cannot enable this dry-run yet because it checks that the versions of
7373
# its dependencies are published on crates.io, which they are definitely not
7474
# in this case.

rust/release_crates/protobuf_codegen/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ impl CodeGen {
6464
self
6565
}
6666

67-
/// Generate Rust, upb, and upb minitables. Build and link the C code.
68-
pub fn compile(&self) -> Result<(), String> {
67+
pub fn generate_and_compile(&self) -> Result<(), String> {
6968
let libupb_version = std::env::var("DEP_LIBUPB_VERSION").expect("DEP_LIBUPB_VERSION should have been set, make sure that the Protobuf crate is a dependency");
7069
if VERSION != libupb_version {
7170
panic!(
@@ -112,7 +111,11 @@ impl CodeGen {
112111
println!("{}", std::str::from_utf8(&output.stdout).unwrap());
113112
eprintln!("{}", std::str::from_utf8(&output.stderr).unwrap());
114113
assert!(output.status.success());
114+
self.compile_only()
115+
}
115116

117+
/// Builds and links the C code.
118+
pub fn compile_only(&self) -> Result<(), String> {
116119
let mut cc_build = cc::Build::new();
117120
cc_build
118121
.include(

rust/release_crates/protobuf_example/Cargo-template.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ edition = "2021"
55
description = "Example use of Rust Protocol Buffers bindings"
66
license = "BSD-3-Clause"
77

8+
[features]
9+
run-protobuf-codegen = []
10+
811
[dependencies]
912
protobuf = { version = "{VERSION}", path = "../protobuf", package = "staging-protobuf" }
1013

rust/release_crates/protobuf_example/build.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ use protobuf_codegen::CodeGen;
33
fn main() {
44
let mut codegen = CodeGen::new();
55
codegen.inputs(["foo.proto", "bar/bar.proto"]).include("proto");
6-
codegen.compile().unwrap();
6+
if std::env::var("CARGO_FEATURE_RUN_PROTOBUF_CODEGEN").is_ok() {
7+
codegen.generate_and_compile().unwrap();
8+
} else {
9+
codegen.compile_only().unwrap();
10+
}
711
}

rust/release_crates/release.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ CARGO_HOME=$CARGO_HOME CARGO_REGISTRY_TOKEN=$AUTH_TOKEN cargo publish
6464

6565
# The example crate cannot be published due to it having a build.rs that
6666
# modifies files outside of the OUT_DIR.
67-
#cd $EXAMPLE_ROOT
68-
# CARGO_HOME=$CARGO_HOME CARGO_REGISTRY_TOKEN=$AUTH_TOKEN cargo publish
67+
cd $EXAMPLE_ROOT
68+
CARGO_HOME=$CARGO_HOME cargo build --features run-protobuf-codegen
69+
CARGO_HOME=$CARGO_HOME CARGO_REGISTRY_TOKEN=$AUTH_TOKEN cargo publish
6970

0 commit comments

Comments
 (0)