Skip to content

Commit

Permalink
build: Use separated build directory for protobuf (#1103)
Browse files Browse the repository at this point in the history
Prevent placing build artifacts in the protobuf source tree by choosing a build directory in the out dir. A `cargo clean` will clean up the build artifacts.
  • Loading branch information
caspermeijn authored Jul 19, 2024
1 parent d652301 commit a291d06
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions protobuf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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");
let src_dir = PathBuf::from("../third_party/protobuf").canonicalize()?;
if !src_dir.join("cmake").exists() {
anyhow::bail!(
"protobuf sources are not checked out; Try `git submodule update --init --recursive`"
Expand All @@ -29,14 +29,18 @@ fn main() -> Result<()> {

if !protobuf_dir.exists() {
apply_patches(&src_dir)?;

let build_dir = &out_dir.join(format!("build-protobuf-{}", version));
fs::create_dir_all(build_dir).expect("failed to create build directory");

let tempdir = tempfile::Builder::new()
.prefix("protobuf")
.tempdir_in(out_dir)
.expect("failed to create temporary directory");

let prefix_dir = &tempdir.path().join("prefix");
fs::create_dir(prefix_dir).expect("failed to create prefix directory");
install_conformance_test_runner(&src_dir, prefix_dir)?;
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")?;
}
Expand Down Expand Up @@ -112,23 +116,27 @@ fn apply_patches(src_dir: &Path) -> Result<()> {
}

#[cfg(windows)]
fn install_conformance_test_runner(_: &Path, _: &Path) -> Result<()> {
fn install_conformance_test_runner(_: &Path, _: &Path, _: &Path) -> Result<()> {
// The conformance test runner does not support Windows [1].
// [1]: https://github.com/protocolbuffers/protobuf/tree/master/conformance#portability
Ok(())
}

#[cfg(not(windows))]
fn install_conformance_test_runner(src_dir: &Path, prefix_dir: &Path) -> Result<()> {
fn install_conformance_test_runner(
src_dir: &Path,
build_dir: &Path,
prefix_dir: &Path,
) -> Result<()> {
// Build and install protoc, the protobuf libraries, and the conformance test runner.
let rc = Command::new("cmake")
.arg("-GNinja")
.arg("cmake/")
.arg(src_dir.join("cmake"))
.arg("-DCMAKE_BUILD_TYPE=DEBUG")
.arg(&format!("-DCMAKE_INSTALL_PREFIX={}", prefix_dir.display()))
.arg("-Dprotobuf_BUILD_CONFORMANCE=ON")
.arg("-Dprotobuf_BUILD_TESTS=OFF")
.current_dir(src_dir)
.current_dir(build_dir)
.status()
.context("failed to execute CMake")?;
assert!(rc.success(), "protobuf CMake failed");
Expand All @@ -139,14 +147,14 @@ fn install_conformance_test_runner(src_dir: &Path, prefix_dir: &Path) -> Result<
.arg("-j")
.arg(&num_jobs)
.arg("install")
.current_dir(src_dir)
.current_dir(build_dir)
.status()
.context("failed to execute ninja protobuf")?;
ensure!(rc.success(), "failed to make protobuf");

// Install the conformance-test-runner binary, since it isn't done automatically.
fs::copy(
src_dir.join("conformance_test_runner"),
build_dir.join("conformance_test_runner"),
prefix_dir.join("bin").join("conformance-test-runner"),
)
.context("failed to move conformance-test-runner")?;
Expand Down

0 comments on commit a291d06

Please sign in to comment.