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 <[email protected]>
  • Loading branch information
charmitro committed Jun 2, 2023
1 parent bdd367f commit 2b5798f
Show file tree
Hide file tree
Showing 44 changed files with 129 additions and 55 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::JobsValue;
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<JobsValue>,
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 {
JobsValue::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,
},
JobsValue::String(j) => match j.as_str() {
"default" => default_parallelism()?,
_ => {
anyhow::bail!(
format!("Invalid value: could not parse `{}`. Value should be `default` or a number.", j))
}
},
},
};

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::JobsValue;
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(JobsValue::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::JobsValue;
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<JobsValue>,
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, JobsValue, 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<JobsValue>,
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
20 changes: 17 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::JobsValue;

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 or \"default\" of parallel jobs, defaults to # of CPUs")
.short('j')
.value_name("N")
.allow_hyphen_values(true),
Expand Down Expand Up @@ -331,6 +333,18 @@ pub trait ArgMatchesExt {
Ok(arg)
}

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

Ok(arg)
}

/// Returns value of the `name` command-line argument as an absolute path
fn value_of_path(&self, name: &str, config: &Config) -> Option<PathBuf> {
self._value_of(name).map(|path| config.cwd().join(path))
Expand Down Expand Up @@ -364,8 +378,8 @@ pub trait ArgMatchesExt {
Ok(ws)
}

fn jobs(&self) -> CargoResult<Option<i32>> {
self.value_of_i32("jobs")
fn jobs(&self) -> CargoResult<Option<JobsValue>> {
self.value_of_jobs_value("jobs")
}

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

#[derive(Debug, Deserialize, Clone)]
#[serde(untagged)]
pub enum JobsValue {
Integer(i32),
String(String),
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CargoBuildConfig {
Expand All @@ -2458,7 +2465,7 @@ pub struct CargoBuildConfig {
pub target_dir: Option<ConfigRelativePath>,
pub incremental: Option<bool>,
pub target: Option<BuildTargetConfig>,
pub jobs: Option<i32>,
pub jobs: Option<JobsValue>,
pub rustflags: Option<StringList>,
pub rustdocflags: Option<StringList>,
pub rustc_wrapper: Option<ConfigRelativePath>,
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-bench.txt
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-check.txt
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-fix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-install.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-package.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-publish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-run.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-rustc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-rustdoc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ 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.
Should not be 0.
If default, it sets the number of parallel jobs to the number of
logical CPUs. Should not be 0.

--keep-going
Build as many crates in the dependency graph as possible, rather
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
`default`, it sets the number of parallel jobs to the number of logical CPUs.
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
<code>default</code>, it sets the number of parallel jobs to the number of logical CPUs.
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
<code>default</code>, it sets the number of parallel jobs to the number of logical CPUs.
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
<code>default</code>, it sets the number of parallel jobs to the number of logical CPUs.
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
<code>default</code>, it sets the number of parallel jobs to the number of logical CPUs.
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
<code>default</code>, it sets the number of parallel jobs to the number of logical CPUs.
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
<code>default</code>, it sets the number of parallel jobs to the number of logical CPUs.
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
<code>default</code>, it sets the number of parallel jobs to the number of logical CPUs.
Should not be 0.</dd>


Expand Down
Loading

0 comments on commit 2b5798f

Please sign in to comment.