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.
This commit is contained in:
+20
-15
@@ -37,6 +37,7 @@ pub async fn build(
|
|||||||
inject_packages: Option<&[&str]>,
|
inject_packages: Option<&[&str]>,
|
||||||
ctx: Arc<Context>,
|
ctx: Arc<Context>,
|
||||||
ui: Option<Arc<DebUi>>,
|
ui: Option<Arc<DebUi>>,
|
||||||
|
jobs: Option<usize>,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> Result<(), Box<dyn Error>> {
|
||||||
let sink: Option<Arc<dyn LineSink>> = ui.as_ref().map(|u| u.sink());
|
let sink: Option<Arc<dyn LineSink>> = 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("LANG".to_string(), "C".to_string());
|
||||||
env.insert("DEBIAN_FRONTEND".to_string(), "noninteractive".to_string());
|
env.insert("DEBIAN_FRONTEND".to_string(), "noninteractive".to_string());
|
||||||
|
|
||||||
// Parallel building: find local number of cores, and use that
|
// Parallel building: honor an explicit -j/--jobs count, otherwise detect
|
||||||
let num_cores = ctx
|
// the number of cores available inside the build context (nproc).
|
||||||
.command("nproc")
|
let num_cores = match jobs {
|
||||||
.output()
|
Some(j) => j,
|
||||||
.map(|output| {
|
None => ctx
|
||||||
if output.status.success() {
|
.command("nproc")
|
||||||
String::from_utf8_lossy(&output.stdout)
|
.output()
|
||||||
.trim()
|
.map(|output| {
|
||||||
.parse::<usize>()
|
if output.status.success() {
|
||||||
.unwrap_or(1)
|
String::from_utf8_lossy(&output.stdout)
|
||||||
} else {
|
.trim()
|
||||||
1 // Default to 1 if nproc fails
|
.parse::<usize>()
|
||||||
}
|
.unwrap_or(1)
|
||||||
})
|
} else {
|
||||||
.unwrap_or(1); // Default to 1 if we can't execute the command
|
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
|
// Build options: parallel, disable tests by default
|
||||||
env.insert(
|
env.insert(
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ pub async fn build_binary_package(
|
|||||||
inject_packages: Option<&[&str]>,
|
inject_packages: Option<&[&str]>,
|
||||||
ctx: Option<Arc<Context>>,
|
ctx: Option<Arc<Context>>,
|
||||||
ui: Option<Arc<DebUi>>,
|
ui: Option<Arc<DebUi>>,
|
||||||
|
jobs: Option<usize>,
|
||||||
) -> Result<Vec<PathBuf>, Box<dyn Error>> {
|
) -> Result<Vec<PathBuf>, Box<dyn Error>> {
|
||||||
let result = build_binary_package_impl(
|
let result = build_binary_package_impl(
|
||||||
arch,
|
arch,
|
||||||
@@ -45,6 +46,7 @@ pub async fn build_binary_package(
|
|||||||
inject_packages,
|
inject_packages,
|
||||||
ctx,
|
ctx,
|
||||||
&ui,
|
&ui,
|
||||||
|
jobs,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
@@ -68,6 +70,7 @@ async fn build_binary_package_impl(
|
|||||||
inject_packages: Option<&[&str]>,
|
inject_packages: Option<&[&str]>,
|
||||||
ctx: Option<Arc<Context>>,
|
ctx: Option<Arc<Context>>,
|
||||||
ui: &Option<Arc<DebUi>>,
|
ui: &Option<Arc<DebUi>>,
|
||||||
|
jobs: Option<usize>,
|
||||||
) -> Result<Vec<PathBuf>, Box<dyn Error>> {
|
) -> Result<Vec<PathBuf>, Box<dyn Error>> {
|
||||||
let cwd = cwd.unwrap_or_else(|| Path::new("."));
|
let cwd = cwd.unwrap_or_else(|| Path::new("."));
|
||||||
|
|
||||||
@@ -160,6 +163,7 @@ async fn build_binary_package_impl(
|
|||||||
inject_packages,
|
inject_packages,
|
||||||
build_ctx.clone(),
|
build_ctx.clone(),
|
||||||
ui.clone(),
|
ui.clone(),
|
||||||
|
jobs,
|
||||||
)
|
)
|
||||||
.await?
|
.await?
|
||||||
}
|
}
|
||||||
@@ -414,6 +418,7 @@ mod tests {
|
|||||||
None,
|
None,
|
||||||
Some(ctx),
|
Some(ctx),
|
||||||
None,
|
None,
|
||||||
|
None,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.expect("Cannot build binary package (deb)");
|
.expect("Cannot build binary package (deb)");
|
||||||
|
|||||||
+10
@@ -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))
|
.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 <mode> "Change build mode [local]").required(false)
|
.arg(arg!(--mode <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"))
|
.long_help("Change build mode [local]\nDefault will chose depending on other parameters, don't provide if unsure"))
|
||||||
|
.arg(arg!(-j --jobs <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)
|
.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.")),
|
.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()
|
.copied()
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
|
|
||||||
|
let jobs = sub_matches.get_one::<String>("jobs").map(|s| s.as_str());
|
||||||
|
let jobs = jobs.map(|j| {
|
||||||
|
j.parse::<usize>().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
|
// Live build view: disabled by --verbose or when stdout is not a
|
||||||
// terminal (DebUi handles the non-TTY case itself)
|
// terminal (DebUi handles the non-TTY case itself)
|
||||||
let ui = if verbose {
|
let ui = if verbose {
|
||||||
@@ -333,6 +342,7 @@ fn main() {
|
|||||||
inject_packages,
|
inject_packages,
|
||||||
None,
|
None,
|
||||||
ui.clone(),
|
ui.clone(),
|
||||||
|
jobs,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user