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: Add the parallel feature, set -j arg based on NUM_JOBS #25

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions protobuf-src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ links = "protobuf-src"

[build-dependencies]
cmake = "0.1.50"

[features]
default = ["parallel"]
parallel = []
25 changes: 22 additions & 3 deletions protobuf-src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
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", "")
Expand All @@ -25,8 +26,26 @@ fn main() -> Result<(), Box<dyn Error>> {
// 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: <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
let maybe_num_jobs: Result<_, Box<dyn Error>> = std::env::var("NUM_JOBS")
.map_err(|err| Box::new(err).into())
.and_then(|val| val.parse::<usize>().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());
Expand Down