From cbb6676dded16fbfe95f268310b14392a67ceecd Mon Sep 17 00:00:00 2001 From: Parker Timmerman Date: Thu, 14 Nov 2024 15:49:40 -0500 Subject: [PATCH] start, set the -j flag for 'make' based on the NUM_JOBS environment var --- protobuf-src/Cargo.toml | 4 ++++ protobuf-src/build.rs | 25 ++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/protobuf-src/Cargo.toml b/protobuf-src/Cargo.toml index 7336a036..34fbc7fd 100644 --- a/protobuf-src/Cargo.toml +++ b/protobuf-src/Cargo.toml @@ -31,3 +31,7 @@ links = "protobuf-src" [build-dependencies] cmake = "0.1.50" + +[features] +default = ["parallel"] +parallel = [] diff --git a/protobuf-src/build.rs b/protobuf-src/build.rs index 1f6705e6..2ff6ca50 100644 --- a/protobuf-src/build.rs +++ b/protobuf-src/build.rs @@ -16,7 +16,8 @@ use std::error::Error; fn main() -> Result<(), Box> { - let install_dir = cmake::Config::new("protobuf") + let mut build_config = cmake::Config::new("protobuf"); + build_config .define("ABSL_PROPAGATE_CXX_STD", "ON") .define("protobuf_BUILD_TESTS", "OFF") .define("protobuf_DEBUG_POSTFIX", "") @@ -25,8 +26,26 @@ fn main() -> Result<(), Box> { // want a stable location that we can add to the linker search path. // Since we're not actually installing to /usr or /usr/local, there's no // harm to always using "lib" here. - .define("CMAKE_INSTALL_LIBDIR", "lib") - .build(); + .define("CMAKE_INSTALL_LIBDIR", "lib"); + + #[cfg(feature = "parallel")] + { + // The `cmake` crate does not enable parallelism for Makefile backed builds, instead it + // tries to parallelize the build jobs with Cargo's jobserver. This doesn't seem to work + // very well and manually specifying the `-j` flag based on the value from `NUM_JOBS` more + // effectively parallelizes the build. Also, using `NUM_JOBS` is recommended by the Cargo + // documentation for this purpose. + // + // Cargo Docs: + let maybe_num_jobs: Result<_, Box> = std::env::var("NUM_JOBS") + .map_err(|err| Box::new(err).into()) + .and_then(|val| val.parse::().map_err(|err| Box::new(err).into())); + if let Ok(num_jobs) = maybe_num_jobs { + build_config.build_arg(format!("-j{num_jobs}")); + } + } + + let install_dir = build_config.build(); println!("cargo:rustc-env=INSTALL_DIR={}", install_dir.display()); println!("cargo:CXXBRIDGE_DIR0={}/include", install_dir.display());