Skip to content

Commit

Permalink
Support "default" option for build.jobs
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
charmitro committed Jun 3, 2023
1 parent 2579f25 commit 33e600c
Show file tree
Hide file tree
Showing 45 changed files with 186 additions and 44 deletions.
22 changes: 17 additions & 5 deletions src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
@@ -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 _};
Expand Down Expand Up @@ -64,7 +65,7 @@ impl BuildConfig {
/// * `target.$target.libfoo.metadata`
pub fn new(
config: &Config,
jobs: Option<i32>,
jobs: Option<JobsConfig>,
keep_going: bool,
requested_targets: &[String],
mode: CompileMode,
Expand All @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/ops/cargo_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -31,7 +32,7 @@ pub struct PackageOpts<'cfg> {
pub check_metadata: bool,
pub allow_dirty: bool,
pub verify: bool,
pub jobs: Option<i32>,
pub jobs: Option<JobsConfig>,
pub keep_going: bool,
pub to_package: ops::Packages,
pub targets: Vec<String>,
Expand Down Expand Up @@ -198,7 +199,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option
check_metadata: opts.check_metadata,
allow_dirty: opts.allow_dirty,
verify: opts.verify,
jobs: opts.jobs,
jobs: opts.jobs.clone(),
keep_going: opts.keep_going,
to_package: ops::Packages::Default,
targets: opts.targets.clone(),
Expand Down Expand Up @@ -861,7 +862,7 @@ fn run_verify(
&ops::CompileOptions {
build_config: BuildConfig::new(
config,
opts.jobs,
opts.jobs.clone(),
opts.keep_going,
&opts.targets,
CompileMode::Build,
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::sources::{RegistrySource, SourceConfigMap, CRATES_IO_DOMAIN, CRATES_I
use crate::util::auth::{
paserk_public_from_paserk_secret, Secret, {self, AuthorizationError},
};
use crate::util::config::{Config, SslVersionConfig, SslVersionConfigRange};
use crate::util::config::{Config, JobsConfig, SslVersionConfig, SslVersionConfigRange};
use crate::util::errors::CargoResult;
use crate::util::important_paths::find_root_manifest_for_wd;
use crate::util::{truncate_with_ellipsis, IntoUrl};
Expand Down Expand Up @@ -101,7 +101,7 @@ pub struct PublishOpts<'cfg> {
pub index: Option<String>,
pub verify: bool,
pub allow_dirty: bool,
pub jobs: Option<i32>,
pub jobs: Option<JobsConfig>,
pub keep_going: bool,
pub to_publish: ops::Packages,
pub targets: Vec<String>,
Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
allow_dirty: opts.allow_dirty,
to_package: ops::Packages::Default,
targets: opts.targets.clone(),
jobs: opts.jobs,
jobs: opts.jobs.clone(),
keep_going: opts.keep_going,
cli_features: cli_features,
},
Expand Down
16 changes: 13 additions & 3 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub use clap::{value_parser, Arg, ArgAction, ArgMatches};

pub use clap::Command;

use super::config::JobsConfig;

pub trait CommandExt: Sized {
fn _arg(self, arg: Arg) -> Self;

Expand Down Expand Up @@ -66,7 +68,7 @@ pub trait CommandExt: Sized {

fn arg_jobs(self) -> Self {
self._arg(
opt("jobs", "Number of parallel jobs, defaults to # of CPUs")
opt("jobs", "Number of parallel jobs, defaults to # of CPUs.")
.short('j')
.value_name("N")
.allow_hyphen_values(true),
Expand Down Expand Up @@ -364,8 +366,16 @@ pub trait ArgMatchesExt {
Ok(ws)
}

fn jobs(&self) -> CargoResult<Option<i32>> {
self.value_of_i32("jobs")
fn jobs(&self) -> CargoResult<Option<JobsConfig>> {
let arg = match self._value_of("jobs") {
None => None,
Some(arg) => match arg.parse::<i32>() {
Ok(j) => Some(JobsConfig::Integer(j)),
Err(_) => Some(JobsConfig::String(arg.to_string())),
},
};

Ok(arg)
}

fn verbose(&self) -> u32 {
Expand Down
21 changes: 20 additions & 1 deletion src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,25 @@ pub struct CargoSshConfig {
pub known_hosts: Option<Vec<Value<String>>>,
}

/// Configuration for `jobs` in `build` section. There are two
/// ways to configure: An integer or a simple string expression.
///
/// ```toml
/// [build]
/// jobs = 1
/// ```
///
/// ```toml
/// [build]
/// jobs = "default" # Currently only support "default".
/// ```
#[derive(Debug, Deserialize, Clone)]
#[serde(untagged)]
pub enum JobsConfig {
Integer(i32),
String(String),
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CargoBuildConfig {
Expand All @@ -2458,7 +2477,7 @@ pub struct CargoBuildConfig {
pub target_dir: Option<ConfigRelativePath>,
pub incremental: Option<bool>,
pub target: Option<BuildTargetConfig>,
pub jobs: Option<i32>,
pub jobs: Option<JobsConfig>,
pub rustflags: Option<StringList>,
pub rustdocflags: Option<StringList>,
pub rustc_wrapper: Option<ConfigRelativePath>,
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-bench.txt
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-check.txt
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-fix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-install.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-package.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-publish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-run.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-rustc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-rustdoc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/generated_txt/cargo-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number
of parallel jobs to the number of logical CPUs plus provided value.
If a string default is provided, it sets the value back to defaults.
Should not be 0.

--keep-going
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/includes/options-jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Number of parallel jobs to run. May also be specified with the
`build.jobs` [config value](../reference/config.html). Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
parallel jobs to the number of logical CPUs plus provided value.
parallel jobs to the number of logical CPUs plus provided value. If
a string `default` is provided, it sets the value back to defaults.
Should not be 0.
{{/option}}
3 changes: 2 additions & 1 deletion src/doc/src/commands/cargo-bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,8 @@ Rust test harness runs benchmarks serially in a single thread.
<dd class="option-desc">Number of parallel jobs to run. May also be specified with the
<code>build.jobs</code> <a href="../reference/config.html">config value</a>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
parallel jobs to the number of logical CPUs plus provided value.
parallel jobs to the number of logical CPUs plus provided value. If
a string <code>default</code> is provided, it sets the value back to defaults.
Should not be 0.</dd>


Expand Down
3 changes: 2 additions & 1 deletion src/doc/src/commands/cargo-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ requires the <code>-Z unstable-options</code> flag to enable (see
<dd class="option-desc">Number of parallel jobs to run. May also be specified with the
<code>build.jobs</code> <a href="../reference/config.html">config value</a>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
parallel jobs to the number of logical CPUs plus provided value.
parallel jobs to the number of logical CPUs plus provided value. If
a string <code>default</code> is provided, it sets the value back to defaults.
Should not be 0.</dd>


Expand Down
3 changes: 2 additions & 1 deletion src/doc/src/commands/cargo-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ requires the <code>-Z unstable-options</code> flag to enable (see
<dd class="option-desc">Number of parallel jobs to run. May also be specified with the
<code>build.jobs</code> <a href="../reference/config.html">config value</a>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
parallel jobs to the number of logical CPUs plus provided value.
parallel jobs to the number of logical CPUs plus provided value. If
a string <code>default</code> is provided, it sets the value back to defaults.
Should not be 0.</dd>


Expand Down
3 changes: 2 additions & 1 deletion src/doc/src/commands/cargo-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ requires the <code>-Z unstable-options</code> flag to enable (see
<dd class="option-desc">Number of parallel jobs to run. May also be specified with the
<code>build.jobs</code> <a href="../reference/config.html">config value</a>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
parallel jobs to the number of logical CPUs plus provided value.
parallel jobs to the number of logical CPUs plus provided value. If
a string <code>default</code> is provided, it sets the value back to defaults.
Should not be 0.</dd>


Expand Down
3 changes: 2 additions & 1 deletion src/doc/src/commands/cargo-fix.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ requires the <code>-Z unstable-options</code> flag to enable (see
<dd class="option-desc">Number of parallel jobs to run. May also be specified with the
<code>build.jobs</code> <a href="../reference/config.html">config value</a>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
parallel jobs to the number of logical CPUs plus provided value.
parallel jobs to the number of logical CPUs plus provided value. If
a string <code>default</code> is provided, it sets the value back to defaults.
Should not be 0.</dd>


Expand Down
3 changes: 2 additions & 1 deletion src/doc/src/commands/cargo-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ offline.</p>
<dd class="option-desc">Number of parallel jobs to run. May also be specified with the
<code>build.jobs</code> <a href="../reference/config.html">config value</a>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
parallel jobs to the number of logical CPUs plus provided value.
parallel jobs to the number of logical CPUs plus provided value. If
a string <code>default</code> is provided, it sets the value back to defaults.
Should not be 0.</dd>


Expand Down
3 changes: 2 additions & 1 deletion src/doc/src/commands/cargo-package.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ offline.</p>
<dd class="option-desc">Number of parallel jobs to run. May also be specified with the
<code>build.jobs</code> <a href="../reference/config.html">config value</a>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
parallel jobs to the number of logical CPUs plus provided value.
parallel jobs to the number of logical CPUs plus provided value. If
a string <code>default</code> is provided, it sets the value back to defaults.
Should not be 0.</dd>


Expand Down
3 changes: 2 additions & 1 deletion src/doc/src/commands/cargo-publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ offline.</p>
<dd class="option-desc">Number of parallel jobs to run. May also be specified with the
<code>build.jobs</code> <a href="../reference/config.html">config value</a>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
parallel jobs to the number of logical CPUs plus provided value.
parallel jobs to the number of logical CPUs plus provided value. If
a string <code>default</code> is provided, it sets the value back to defaults.
Should not be 0.</dd>


Expand Down
3 changes: 2 additions & 1 deletion src/doc/src/commands/cargo-run.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ requires the <code>-Z unstable-options</code> flag to enable (see
<dd class="option-desc">Number of parallel jobs to run. May also be specified with the
<code>build.jobs</code> <a href="../reference/config.html">config value</a>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
parallel jobs to the number of logical CPUs plus provided value.
parallel jobs to the number of logical CPUs plus provided value. If
a string <code>default</code> is provided, it sets the value back to defaults.
Should not be 0.</dd>


Expand Down
3 changes: 2 additions & 1 deletion src/doc/src/commands/cargo-rustc.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ requires the <code>-Z unstable-options</code> flag to enable (see
<dd class="option-desc">Number of parallel jobs to run. May also be specified with the
<code>build.jobs</code> <a href="../reference/config.html">config value</a>. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
parallel jobs to the number of logical CPUs plus provided value.
parallel jobs to the number of logical CPUs plus provided value. If
a string <code>default</code> is provided, it sets the value back to defaults.
Should not be 0.</dd>


Expand Down
Loading

0 comments on commit 33e600c

Please sign in to comment.