From 182a06ffbe32479ac49e8dc35c74a3607a0f61a2 Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Thu, 10 Sep 2026 15:27:50 +0200 Subject: [PATCH] deb: add -j/--jobs to control parallel build jobs By default the number of parallel jobs is detected with nproc inside the build context. Add a -j/--jobs option so an explicit count can be honored instead, threading it through build_binary_package and local::build into DEB_BUILD_OPTIONS=parallel=N. --- src/deb/local.rs | 35 ++++++++++++++++++++--------------- src/deb/mod.rs | 5 +++++ src/main.rs | 10 ++++++++++ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/deb/local.rs b/src/deb/local.rs index 93b6e15..8d24412 100644 --- a/src/deb/local.rs +++ b/src/deb/local.rs @@ -37,6 +37,7 @@ pub async fn build( inject_packages: Option<&[&str]>, ctx: Arc, ui: Option>, + jobs: Option, ) -> Result<(), Box> { let sink: Option> = ui.as_ref().map(|u| u.sink()); @@ -45,21 +46,25 @@ pub async fn build( env.insert("LANG".to_string(), "C".to_string()); env.insert("DEBIAN_FRONTEND".to_string(), "noninteractive".to_string()); - // Parallel building: find local number of cores, and use that - let num_cores = ctx - .command("nproc") - .output() - .map(|output| { - if output.status.success() { - String::from_utf8_lossy(&output.stdout) - .trim() - .parse::() - .unwrap_or(1) - } else { - 1 // Default to 1 if nproc fails - } - }) - .unwrap_or(1); // Default to 1 if we can't execute the command + // Parallel building: honor an explicit -j/--jobs count, otherwise detect + // the number of cores available inside the build context (nproc). + let num_cores = match jobs { + Some(j) => j, + None => ctx + .command("nproc") + .output() + .map(|output| { + if output.status.success() { + String::from_utf8_lossy(&output.stdout) + .trim() + .parse::() + .unwrap_or(1) + } else { + 1 // Default to 1 if nproc fails + } + }) + .unwrap_or(1), // Default to 1 if we can't execute the command + }; // Build options: parallel, disable tests by default env.insert( diff --git a/src/deb/mod.rs b/src/deb/mod.rs index 6f97cb1..658d7bf 100644 --- a/src/deb/mod.rs +++ b/src/deb/mod.rs @@ -33,6 +33,7 @@ pub async fn build_binary_package( inject_packages: Option<&[&str]>, ctx: Option>, ui: Option>, + jobs: Option, ) -> Result, Box> { let result = build_binary_package_impl( arch, @@ -45,6 +46,7 @@ pub async fn build_binary_package( inject_packages, ctx, &ui, + jobs, ) .await; @@ -68,6 +70,7 @@ async fn build_binary_package_impl( inject_packages: Option<&[&str]>, ctx: Option>, ui: &Option>, + jobs: Option, ) -> Result, Box> { let cwd = cwd.unwrap_or_else(|| Path::new(".")); @@ -160,6 +163,7 @@ async fn build_binary_package_impl( inject_packages, build_ctx.clone(), ui.clone(), + jobs, ) .await? } @@ -414,6 +418,7 @@ mod tests { None, Some(ctx), None, + None, ) .await .expect("Cannot build binary package (deb)"); diff --git a/src/main.rs b/src/main.rs index 8c8b682..54dab34 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,6 +78,7 @@ fn main() { .long_help("Cross-compile for target architecture (instead of using qemu-binfmt)\nNote that most packages cannot be cross-compiled").required(false)) .arg(arg!(--mode "Change build mode [local]").required(false) .long_help("Change build mode [local]\nDefault will chose depending on other parameters, don't provide if unsure")) + .arg(arg!(-j --jobs "Number of parallel build jobs (default: number of CPUs available in the build context)").required(false)) .arg(arg!(--verbose "Show raw tool output instead of the live build view").required(false) .long_help("Show raw tool output instead of the live build view.\nAlso implied by RUST_LOG=debug for pkh's own logs.")), ) @@ -313,6 +314,14 @@ fn main() { .copied() .unwrap_or(false); + let jobs = sub_matches.get_one::("jobs").map(|s| s.as_str()); + let jobs = jobs.map(|j| { + j.parse::().unwrap_or_else(|_| { + error!("Invalid --jobs value '{}': expected a positive integer", j); + std::process::exit(1); + }) + }); + // Live build view: disabled by --verbose or when stdout is not a // terminal (DebUi handles the non-TTY case itself) let ui = if verbose { @@ -333,6 +342,7 @@ fn main() { inject_packages, None, ui.clone(), + jobs, ) .await });