Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Use separated build directory for protobuf #1103

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 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 @@ -128,23 +132,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 @@ -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
Loading