Skip to content

Commit 7caeb5b

Browse files
committed
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 <charmitro@gmail.com>
1 parent 2579f25 commit 7caeb5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+192
-47
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::core::compiler::CompileKind;
2+
use crate::util::config::JobsConfig;
23
use crate::util::interning::InternedString;
34
use crate::util::{CargoResult, Config, RustfixDiagnosticServer};
45
use anyhow::{bail, Context as _};
@@ -64,7 +65,7 @@ impl BuildConfig {
6465
/// * `target.$target.libfoo.metadata`
6566
pub fn new(
6667
config: &Config,
67-
jobs: Option<i32>,
68+
jobs: Option<JobsConfig>,
6869
keep_going: bool,
6970
requested_targets: &[String],
7071
mode: CompileMode,
@@ -78,11 +79,22 @@ impl BuildConfig {
7879
its environment, ignoring the `-j` parameter",
7980
)?;
8081
}
81-
let jobs = match jobs.or(cfg.jobs) {
82+
let jobs = match jobs.or(cfg.jobs.clone()) {
8283
None => default_parallelism()?,
83-
Some(0) => anyhow::bail!("jobs may not be 0"),
84-
Some(j) if j < 0 => (default_parallelism()? as i32 + j).max(1) as u32,
85-
Some(j) => j as u32,
84+
Some(value) => match value {
85+
JobsConfig::Integer(j) => match j {
86+
0 => anyhow::bail!("jobs may not be 0"),
87+
j if j < 0 => (default_parallelism()? as i32 + j).max(1) as u32,
88+
j => j as u32,
89+
},
90+
JobsConfig::String(j) => match j.as_str() {
91+
"default" => default_parallelism()?,
92+
_ => {
93+
anyhow::bail!(
94+
format!("could not parse `{j}`. Number of parallel jobs should be `default` or a number."))
95+
}
96+
},
97+
},
8698
};
8799

88100
if config.cli_unstable().build_std.is_some() && requested_kinds[0].is_host() {

src/cargo/ops/cargo_fetch.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::core::compiler::standard_lib;
22
use crate::core::compiler::{BuildConfig, CompileMode, RustcTargetData};
33
use crate::core::{PackageSet, Resolve, Workspace};
44
use crate::ops;
5+
use crate::util::config::JobsConfig;
56
use crate::util::CargoResult;
67
use crate::util::Config;
78
use std::collections::HashSet;
@@ -20,7 +21,7 @@ pub fn fetch<'a>(
2021
ws.emit_warnings()?;
2122
let (mut packages, resolve) = ops::resolve_ws(ws)?;
2223

23-
let jobs = Some(1);
24+
let jobs = Some(JobsConfig::Integer(1));
2425
let keep_going = false;
2526
let config = ws.config();
2627
let build_config = BuildConfig::new(

src/cargo/ops/cargo_package.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::core::{registry::PackageRegistry, resolver::HasDevUnits};
1313
use crate::core::{Feature, Shell, Verbosity, Workspace};
1414
use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
1515
use crate::sources::PathSource;
16+
use crate::util::config::JobsConfig;
1617
use crate::util::errors::CargoResult;
1718
use crate::util::toml::TomlManifest;
1819
use crate::util::{self, human_readable_bytes, restricted_names, Config, FileLock};
@@ -31,7 +32,7 @@ pub struct PackageOpts<'cfg> {
3132
pub check_metadata: bool,
3233
pub allow_dirty: bool,
3334
pub verify: bool,
34-
pub jobs: Option<i32>,
35+
pub jobs: Option<JobsConfig>,
3536
pub keep_going: bool,
3637
pub to_package: ops::Packages,
3738
pub targets: Vec<String>,
@@ -198,7 +199,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option
198199
check_metadata: opts.check_metadata,
199200
allow_dirty: opts.allow_dirty,
200201
verify: opts.verify,
201-
jobs: opts.jobs,
202+
jobs: opts.jobs.clone(),
202203
keep_going: opts.keep_going,
203204
to_package: ops::Packages::Default,
204205
targets: opts.targets.clone(),
@@ -861,7 +862,7 @@ fn run_verify(
861862
&ops::CompileOptions {
862863
build_config: BuildConfig::new(
863864
config,
864-
opts.jobs,
865+
opts.jobs.clone(),
865866
opts.keep_going,
866867
&opts.targets,
867868
CompileMode::Build,

src/cargo/ops/registry.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::sources::{RegistrySource, SourceConfigMap, CRATES_IO_DOMAIN, CRATES_I
3232
use crate::util::auth::{
3333
paserk_public_from_paserk_secret, Secret, {self, AuthorizationError},
3434
};
35-
use crate::util::config::{Config, SslVersionConfig, SslVersionConfigRange};
35+
use crate::util::config::{Config, JobsConfig, SslVersionConfig, SslVersionConfigRange};
3636
use crate::util::errors::CargoResult;
3737
use crate::util::important_paths::find_root_manifest_for_wd;
3838
use crate::util::{truncate_with_ellipsis, IntoUrl};
@@ -101,7 +101,7 @@ pub struct PublishOpts<'cfg> {
101101
pub index: Option<String>,
102102
pub verify: bool,
103103
pub allow_dirty: bool,
104-
pub jobs: Option<i32>,
104+
pub jobs: Option<JobsConfig>,
105105
pub keep_going: bool,
106106
pub to_publish: ops::Packages,
107107
pub targets: Vec<String>,
@@ -196,7 +196,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
196196
allow_dirty: opts.allow_dirty,
197197
to_package: ops::Packages::Default,
198198
targets: opts.targets.clone(),
199-
jobs: opts.jobs,
199+
jobs: opts.jobs.clone(),
200200
keep_going: opts.keep_going,
201201
cli_features: cli_features,
202202
},

src/cargo/util/command_prelude.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub use clap::{value_parser, Arg, ArgAction, ArgMatches};
2323

2424
pub use clap::Command;
2525

26+
use super::config::JobsConfig;
27+
2628
pub trait CommandExt: Sized {
2729
fn _arg(self, arg: Arg) -> Self;
2830

@@ -66,10 +68,13 @@ pub trait CommandExt: Sized {
6668

6769
fn arg_jobs(self) -> Self {
6870
self._arg(
69-
opt("jobs", "Number of parallel jobs, defaults to # of CPUs")
70-
.short('j')
71-
.value_name("N")
72-
.allow_hyphen_values(true),
71+
opt(
72+
"jobs",
73+
"Number of parallel jobs, defaults to # of CPUs. Set \"default\"\nto unset to defaults",
74+
)
75+
.short('j')
76+
.value_name("N")
77+
.allow_hyphen_values(true),
7378
)
7479
._arg(flag(
7580
"keep-going",
@@ -364,8 +369,16 @@ pub trait ArgMatchesExt {
364369
Ok(ws)
365370
}
366371

367-
fn jobs(&self) -> CargoResult<Option<i32>> {
368-
self.value_of_i32("jobs")
372+
fn jobs(&self) -> CargoResult<Option<JobsConfig>> {
373+
let arg = match self._value_of("jobs") {
374+
None => None,
375+
Some(arg) => match arg.parse::<i32>() {
376+
Ok(j) => Some(JobsConfig::Integer(j)),
377+
Err(_) => Some(JobsConfig::String(arg.to_string())),
378+
},
379+
};
380+
381+
Ok(arg)
369382
}
370383

371384
fn verbose(&self) -> u32 {

src/cargo/util/config/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,25 @@ pub struct CargoSshConfig {
24492449
pub known_hosts: Option<Vec<Value<String>>>,
24502450
}
24512451

2452+
/// Configuration for `jobs` in `build` section. There are two
2453+
/// ways to configure: An integer or a simple string expression.
2454+
///
2455+
/// ```toml
2456+
/// [build]
2457+
/// jobs = 1
2458+
/// ```
2459+
///
2460+
/// ```toml
2461+
/// [build]
2462+
/// jobs = "default" # Currently only support "default".
2463+
/// ```
2464+
#[derive(Debug, Deserialize, Clone)]
2465+
#[serde(untagged)]
2466+
pub enum JobsConfig {
2467+
Integer(i32),
2468+
String(String),
2469+
}
2470+
24522471
#[derive(Debug, Deserialize)]
24532472
#[serde(rename_all = "kebab-case")]
24542473
pub struct CargoBuildConfig {
@@ -2458,7 +2477,7 @@ pub struct CargoBuildConfig {
24582477
pub target_dir: Option<ConfigRelativePath>,
24592478
pub incremental: Option<bool>,
24602479
pub target: Option<BuildTargetConfig>,
2461-
pub jobs: Option<i32>,
2480+
pub jobs: Option<JobsConfig>,
24622481
pub rustflags: Option<StringList>,
24632482
pub rustdocflags: Option<StringList>,
24642483
pub rustc_wrapper: Option<ConfigRelativePath>,

src/doc/man/generated_txt/cargo-bench.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ OPTIONS
410410
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
411411
the number of logical CPUs. If negative, it sets the maximum number
412412
of parallel jobs to the number of logical CPUs plus provided value.
413+
If a string default is provided, it sets the value back to defaults.
413414
Should not be 0.
414415

415416
--keep-going

src/doc/man/generated_txt/cargo-build.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ OPTIONS
341341
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
342342
the number of logical CPUs. If negative, it sets the maximum number
343343
of parallel jobs to the number of logical CPUs plus provided value.
344+
If a string default is provided, it sets the value back to defaults.
344345
Should not be 0.
345346

346347
--keep-going

src/doc/man/generated_txt/cargo-check.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ OPTIONS
326326
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
327327
the number of logical CPUs. If negative, it sets the maximum number
328328
of parallel jobs to the number of logical CPUs plus provided value.
329+
If a string default is provided, it sets the value back to defaults.
329330
Should not be 0.
330331

331332
--keep-going

src/doc/man/generated_txt/cargo-doc.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ OPTIONS
297297
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
298298
the number of logical CPUs. If negative, it sets the maximum number
299299
of parallel jobs to the number of logical CPUs plus provided value.
300+
If a string default is provided, it sets the value back to defaults.
300301
Should not be 0.
301302

302303
--keep-going

0 commit comments

Comments
 (0)