Skip to content

Commit

Permalink
build: Use separated build directory for protobuf
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 committed Jul 12, 2024
1 parent f19104a commit 787ab64
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions protobuf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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 @@ -35,14 +35,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)?;
install_datasets(&src_dir, prefix_dir)?;
fs::rename(prefix_dir, protobuf_dir).context("failed to move protobuf dir")?;
Expand Down Expand Up @@ -135,16 +139,20 @@ fn install_conformance_test_runner(_: &Path, _: &Path) -> Result<()> {
}

#[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 @@ -155,14 +163,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 787ab64

Please sign in to comment.