From a777b82d5fec35a72ac9e3d61b3458a36b80349d Mon Sep 17 00:00:00 2001 From: Charalampos Mitrodimas Date: Fri, 2 Jun 2023 17:10:10 +0300 Subject: [PATCH] Support "default" option for build.jobs This commit adds support for passing the keyword "default" to either the CLI "--jobs" argument on the "[build.jobs]" section of ".cargo/config". This is dony by: 1. Changing the "jobs" config type to an enum that holds a String or an Integer(i.e. i32). 2. Matching the enum & casting it to an integer Signed-off-by: Charalampos Mitrodimas --- src/cargo/core/compiler/build_config.rs | 22 ++++++-- src/cargo/ops/cargo_fetch.rs | 3 +- src/cargo/ops/cargo_package.rs | 7 ++- src/cargo/ops/registry.rs | 6 +- src/cargo/util/command_prelude.rs | 16 +++++- src/cargo/util/config/mod.rs | 21 ++++++- src/doc/man/generated_txt/cargo-bench.txt | 1 + src/doc/man/generated_txt/cargo-build.txt | 1 + src/doc/man/generated_txt/cargo-check.txt | 1 + src/doc/man/generated_txt/cargo-doc.txt | 1 + src/doc/man/generated_txt/cargo-fix.txt | 1 + src/doc/man/generated_txt/cargo-install.txt | 1 + src/doc/man/generated_txt/cargo-package.txt | 1 + src/doc/man/generated_txt/cargo-publish.txt | 1 + src/doc/man/generated_txt/cargo-run.txt | 1 + src/doc/man/generated_txt/cargo-rustc.txt | 1 + src/doc/man/generated_txt/cargo-rustdoc.txt | 1 + src/doc/man/generated_txt/cargo-test.txt | 1 + src/doc/man/includes/options-jobs.md | 3 +- src/doc/src/commands/cargo-bench.md | 3 +- src/doc/src/commands/cargo-build.md | 3 +- src/doc/src/commands/cargo-check.md | 3 +- src/doc/src/commands/cargo-doc.md | 3 +- src/doc/src/commands/cargo-fix.md | 3 +- src/doc/src/commands/cargo-install.md | 3 +- src/doc/src/commands/cargo-package.md | 3 +- src/doc/src/commands/cargo-publish.md | 3 +- src/doc/src/commands/cargo-run.md | 3 +- src/doc/src/commands/cargo-rustc.md | 3 +- src/doc/src/commands/cargo-rustdoc.md | 3 +- src/doc/src/commands/cargo-test.md | 3 +- src/doc/src/reference/config.md | 5 +- src/etc/man/cargo-bench.1 | 3 +- src/etc/man/cargo-build.1 | 3 +- src/etc/man/cargo-check.1 | 3 +- src/etc/man/cargo-doc.1 | 3 +- src/etc/man/cargo-fix.1 | 3 +- src/etc/man/cargo-install.1 | 3 +- src/etc/man/cargo-package.1 | 3 +- src/etc/man/cargo-publish.1 | 3 +- src/etc/man/cargo-run.1 | 3 +- src/etc/man/cargo-rustc.1 | 3 +- src/etc/man/cargo-rustdoc.1 | 3 +- src/etc/man/cargo-test.1 | 3 +- tests/testsuite/build.rs | 6 +- tests/testsuite/config.rs | 62 ++++++++++++++++++++- 46 files changed, 189 insertions(+), 46 deletions(-) diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index 885b124b976..5d4d754bff1 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -1,4 +1,5 @@ use crate::core::compiler::CompileKind; +use crate::util::config::JobsConfig; use crate::util::interning::InternedString; use crate::util::{CargoResult, Config, RustfixDiagnosticServer}; use anyhow::{bail, Context as _}; @@ -64,7 +65,7 @@ impl BuildConfig { /// * `target.$target.libfoo.metadata` pub fn new( config: &Config, - jobs: Option, + jobs: Option, keep_going: bool, requested_targets: &[String], mode: CompileMode, @@ -78,11 +79,22 @@ impl BuildConfig { its environment, ignoring the `-j` parameter", )?; } - let jobs = match jobs.or(cfg.jobs) { + let jobs = match jobs.or(cfg.jobs.clone()) { None => default_parallelism()?, - Some(0) => anyhow::bail!("jobs may not be 0"), - Some(j) if j < 0 => (default_parallelism()? as i32 + j).max(1) as u32, - Some(j) => j as u32, + Some(value) => match value { + JobsConfig::Integer(j) => match j { + 0 => anyhow::bail!("jobs may not be 0"), + j if j < 0 => (default_parallelism()? as i32 + j).max(1) as u32, + j => j as u32, + }, + JobsConfig::String(j) => match j.as_str() { + "default" => default_parallelism()?, + _ => { + anyhow::bail!( + format!("could not parse `{j}`. Number of parallel jobs should be `default` or a number.")) + } + }, + }, }; if config.cli_unstable().build_std.is_some() && requested_kinds[0].is_host() { diff --git a/src/cargo/ops/cargo_fetch.rs b/src/cargo/ops/cargo_fetch.rs index bac3f0278d8..273bce28492 100644 --- a/src/cargo/ops/cargo_fetch.rs +++ b/src/cargo/ops/cargo_fetch.rs @@ -2,6 +2,7 @@ use crate::core::compiler::standard_lib; use crate::core::compiler::{BuildConfig, CompileMode, RustcTargetData}; use crate::core::{PackageSet, Resolve, Workspace}; use crate::ops; +use crate::util::config::JobsConfig; use crate::util::CargoResult; use crate::util::Config; use std::collections::HashSet; @@ -20,7 +21,7 @@ pub fn fetch<'a>( ws.emit_warnings()?; let (mut packages, resolve) = ops::resolve_ws(ws)?; - let jobs = Some(1); + let jobs = Some(JobsConfig::Integer(1)); let keep_going = false; let config = ws.config(); let build_config = BuildConfig::new( diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index f80848c7596..37bc6bd0767 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -13,6 +13,7 @@ use crate::core::{registry::PackageRegistry, resolver::HasDevUnits}; use crate::core::{Feature, Shell, Verbosity, Workspace}; use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId}; use crate::sources::PathSource; +use crate::util::config::JobsConfig; use crate::util::errors::CargoResult; use crate::util::toml::TomlManifest; use crate::util::{self, human_readable_bytes, restricted_names, Config, FileLock}; @@ -31,7 +32,7 @@ pub struct PackageOpts<'cfg> { pub check_metadata: bool, pub allow_dirty: bool, pub verify: bool, - pub jobs: Option, + pub jobs: Option, pub keep_going: bool, pub to_package: ops::Packages, pub targets: Vec, @@ -198,7 +199,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult