Skip to content

Commit

Permalink
build(protobuf): Don't install unused test proto
Browse files Browse the repository at this point in the history
The build script of crate `protobuf` installs some proto-files for testing in the prefix dir. However the only user is the same build script. Therefore we can skip the install and use the files directly.

Use `absolute` path instead of `canonical` path, as `protoc` on Windows is not able to process `canonical` paths. This is sort of documented in https://doc.rust-lang.org/std/fs/fn.canonicalize.html
  • Loading branch information
caspermeijn committed Aug 16, 2024
1 parent 7e55cda commit 6b9f58e
Showing 1 changed file with 9 additions and 51 deletions.
60 changes: 9 additions & 51 deletions protobuf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ use std::process::Command;

use anyhow::{ensure, Context, Result};

static TEST_PROTOS: &[&str] = &[
"test_messages_proto2.proto",
"test_messages_proto3.proto",
"unittest.proto",
"unittest_import.proto",
"unittest_import_public.proto",
];

fn main() -> Result<()> {
let out_dir =
&PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR environment variable not set"));

let src_dir = PathBuf::from("../third_party/protobuf").canonicalize()?;
let src_dir = std::path::absolute(PathBuf::from("../third_party/protobuf"))?;
if !src_dir.join("cmake").exists() {
anyhow::bail!(
"protobuf sources are not checked out; Try `git submodule update --init --recursive`"
Expand All @@ -41,20 +33,17 @@ fn main() -> Result<()> {
let prefix_dir = &tempdir.path().join("prefix");
fs::create_dir(prefix_dir).expect("failed to create prefix directory");
install_conformance_test_runner(&src_dir, build_dir, prefix_dir)?;
install_protos(&src_dir, prefix_dir)?;
fs::rename(prefix_dir, protobuf_dir).context("failed to move protobuf dir")?;
}

let include_dir = &protobuf_dir.join("include");

let conformance_include_dir = include_dir.join("conformance");
let conformance_proto_dir = src_dir.join("conformance");
prost_build::compile_protos(
&[conformance_include_dir.join("conformance.proto")],
&[conformance_include_dir],
&[conformance_proto_dir.join("conformance.proto")],
&[conformance_proto_dir],
)
.unwrap();

let test_includes = &include_dir.join("google").join("protobuf");
let proto_dir = src_dir.join("src");

// Generate BTreeMap fields for all messages. This forces encoded output to be consistent, so
// that encode/decode roundtrips can use encoded output for comparison. Otherwise trying to
Expand All @@ -64,11 +53,11 @@ fn main() -> Result<()> {
.btree_map(["."])
.compile_protos(
&[
test_includes.join("test_messages_proto2.proto"),
test_includes.join("test_messages_proto3.proto"),
test_includes.join("unittest.proto"),
proto_dir.join("google/protobuf/test_messages_proto2.proto"),
proto_dir.join("google/protobuf/test_messages_proto3.proto"),
proto_dir.join("google/protobuf/unittest.proto"),
],
&[include_dir],
&[proto_dir],
)
.unwrap();

Expand Down Expand Up @@ -161,34 +150,3 @@ fn install_conformance_test_runner(

Ok(())
}

fn install_protos(src_dir: &Path, prefix_dir: &Path) -> Result<()> {
let include_dir = prefix_dir.join("include");

// Move test protos to the prefix directory.
let test_include_dir = &include_dir.join("google").join("protobuf");
fs::create_dir_all(test_include_dir).expect("failed to create test include directory");
for proto in TEST_PROTOS {
fs::copy(
src_dir
.join("src")
.join("google")
.join("protobuf")
.join(proto),
test_include_dir.join(proto),
)
.with_context(|| format!("failed to move {}", proto))?;
}

// Move conformance.proto to the install directory.
let conformance_include_dir = &include_dir.join("conformance");
fs::create_dir(conformance_include_dir)
.expect("failed to create conformance include directory");
fs::copy(
src_dir.join("conformance").join("conformance.proto"),
conformance_include_dir.join("conformance.proto"),
)
.expect("failed to move conformance.proto");

Ok(())
}

0 comments on commit 6b9f58e

Please sign in to comment.