From 07cc897a4ffd0743f26c43a210089dedfc27fac2 Mon Sep 17 00:00:00 2001
From: Orson Peters
Date: Mon, 1 Mar 2021 17:05:22 +0100
Subject: [PATCH 1/7] Added support for negative --jobs parameter, counting
backwards from max CPUs.
---
src/cargo/core/compiler/build_config.rs | 10 +++++++---
src/cargo/core/compiler/context/mod.rs | 6 +++---
src/cargo/core/compiler/timings.rs | 2 +-
src/cargo/ops/cargo_package.rs | 2 +-
src/cargo/ops/registry.rs | 2 +-
src/cargo/util/command_prelude.rs | 17 +++++++++++++---
src/cargo/util/config/mod.rs | 2 +-
tests/testsuite/bad_config.rs | 26 -------------------------
tests/testsuite/build.rs | 20 ++++++++++++++-----
9 files changed, 43 insertions(+), 44 deletions(-)
diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs
index d0f5431a47e..d2c7398a08c 100644
--- a/src/cargo/core/compiler/build_config.rs
+++ b/src/cargo/core/compiler/build_config.rs
@@ -50,14 +50,14 @@ impl BuildConfig {
/// * `target.$target.libfoo.metadata`
pub fn new(
config: &Config,
- jobs: Option,
+ jobs: Option,
requested_targets: &[String],
mode: CompileMode,
) -> CargoResult {
let cfg = config.build_config()?;
let requested_kinds = CompileKind::from_requested_targets(config, requested_targets)?;
if jobs == Some(0) {
- anyhow::bail!("jobs must be at least 1")
+ anyhow::bail!("jobs must not be zero")
}
if jobs.is_some() && config.jobserver_from_env().is_some() {
config.shell().warn(
@@ -66,7 +66,11 @@ impl BuildConfig {
its environment, ignoring the `-j` parameter",
)?;
}
- let jobs = jobs.or(cfg.jobs).unwrap_or(::num_cpus::get() as u32);
+ let jobs = match jobs.or(cfg.jobs) {
+ None => ::num_cpus::get() as u32,
+ Some(j) if j < 0 => (::num_cpus::get() as i32 + j).max(1) as u32,
+ Some(j) => j as u32,
+ };
Ok(BuildConfig {
requested_kinds,
diff --git a/src/cargo/core/compiler/context/mod.rs b/src/cargo/core/compiler/context/mod.rs
index cb1dbb6240b..dfb4ccf8e91 100644
--- a/src/cargo/core/compiler/context/mod.rs
+++ b/src/cargo/core/compiler/context/mod.rs
@@ -95,8 +95,8 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
let jobserver = match bcx.config.jobserver_from_env() {
Some(c) => c.clone(),
None => {
- let client = Client::new(bcx.build_config.jobs as usize)
- .chain_err(|| "failed to create jobserver")?;
+ let client =
+ Client::new(bcx.jobs() as usize).chain_err(|| "failed to create jobserver")?;
client.acquire_raw()?;
client
}
@@ -558,7 +558,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
}
pub fn new_jobserver(&mut self) -> CargoResult {
- let tokens = self.bcx.build_config.jobs as usize;
+ let tokens = self.bcx.jobs() as usize;
let client = Client::new(tokens).chain_err(|| "failed to create jobserver")?;
// Drain the client fully
diff --git a/src/cargo/core/compiler/timings.rs b/src/cargo/core/compiler/timings.rs
index a206023a99e..4891526f20d 100644
--- a/src/cargo/core/compiler/timings.rs
+++ b/src/cargo/core/compiler/timings.rs
@@ -461,7 +461,7 @@ impl<'cfg> Timings<'cfg> {
self.total_dirty,
self.total_fresh + self.total_dirty,
max_concurrency,
- bcx.build_config.jobs,
+ bcx.jobs(),
num_cpus::get(),
self.start_str,
total_time,
diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs
index a07f9fe072d..8aadc6ec214 100644
--- a/src/cargo/ops/cargo_package.rs
+++ b/src/cargo/ops/cargo_package.rs
@@ -27,7 +27,7 @@ pub struct PackageOpts<'cfg> {
pub check_metadata: bool,
pub allow_dirty: bool,
pub verify: bool,
- pub jobs: Option,
+ pub jobs: Option,
pub targets: Vec,
pub features: Vec,
pub all_features: bool,
diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs
index 7032ae13090..68e5479dc60 100644
--- a/src/cargo/ops/registry.rs
+++ b/src/cargo/ops/registry.rs
@@ -47,7 +47,7 @@ pub struct PublishOpts<'cfg> {
pub index: Option,
pub verify: bool,
pub allow_dirty: bool,
- pub jobs: Option,
+ pub jobs: Option,
pub targets: Vec,
pub dry_run: bool,
pub registry: Option,
diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs
index 3d7268caac5..c72743aa4a0 100644
--- a/src/cargo/util/command_prelude.rs
+++ b/src/cargo/util/command_prelude.rs
@@ -67,7 +67,8 @@ pub trait AppExt: Sized {
self._arg(
opt("jobs", "Number of parallel jobs, defaults to # of CPUs")
.short("j")
- .value_name("N"),
+ .value_name("N")
+ .allow_hyphen_values(true),
)
}
@@ -287,6 +288,16 @@ pub trait ArgMatchesExt {
Ok(arg)
}
+ fn value_of_i32(&self, name: &str) -> CargoResult
--jobsN
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
-the number of CPUs.
+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.
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
-the number of CPUs.
+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.
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
-the number of CPUs.
+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.
--keep-going
diff --git a/src/doc/src/commands/cargo-run.md b/src/doc/src/commands/cargo-run.md
index e0fa5ccac3e..5b5f79f2fbe 100644
--- a/src/doc/src/commands/cargo-run.md
+++ b/src/doc/src/commands/cargo-run.md
@@ -281,7 +281,9 @@ for more information about how toolchain overrides work.
--jobsN
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
-the number of CPUs.
+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.
--keep-going
diff --git a/src/doc/src/commands/cargo-rustc.md b/src/doc/src/commands/cargo-rustc.md
index 7396d679e2e..ba42ca43e2e 100644
--- a/src/doc/src/commands/cargo-rustc.md
+++ b/src/doc/src/commands/cargo-rustc.md
@@ -370,7 +370,9 @@ for more information about how toolchain overrides work.
--jobsN
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
-the number of CPUs.
+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.
--keep-going
diff --git a/src/doc/src/commands/cargo-rustdoc.md b/src/doc/src/commands/cargo-rustdoc.md
index 1fe506d108e..5ae475d7972 100644
--- a/src/doc/src/commands/cargo-rustdoc.md
+++ b/src/doc/src/commands/cargo-rustdoc.md
@@ -361,7 +361,9 @@ for more information about how toolchain overrides work.
--jobsN
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
-the number of CPUs.
+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.
--keep-going
diff --git a/src/doc/src/commands/cargo-test.md b/src/doc/src/commands/cargo-test.md
index 9ff65a10723..ca40d80f6ab 100644
--- a/src/doc/src/commands/cargo-test.md
+++ b/src/doc/src/commands/cargo-test.md
@@ -473,7 +473,9 @@ includes an option to control the number of threads used:
--jobsN
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
-the number of CPUs.
+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.
--keep-going
diff --git a/src/etc/man/cargo-bench.1 b/src/etc/man/cargo-bench.1
index 002f7562a21..18b6fc8a794 100644
--- a/src/etc/man/cargo-bench.1
+++ b/src/etc/man/cargo-bench.1
@@ -469,7 +469,9 @@ Rust test harness runs benchmarks serially in a single thread.
.RS 4
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
diff --git a/src/etc/man/cargo-build.1 b/src/etc/man/cargo-build.1
index edbc8f90b61..34b0d52335d 100644
--- a/src/etc/man/cargo-build.1
+++ b/src/etc/man/cargo-build.1
@@ -396,7 +396,9 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
.RS 4
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
diff --git a/src/etc/man/cargo-check.1 b/src/etc/man/cargo-check.1
index a8ecef5f7b1..b1018202932 100644
--- a/src/etc/man/cargo-check.1
+++ b/src/etc/man/cargo-check.1
@@ -377,7 +377,9 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
.RS 4
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
diff --git a/src/etc/man/cargo-doc.1 b/src/etc/man/cargo-doc.1
index 5d3ffc2dd2a..b49c4f2e8bf 100644
--- a/src/etc/man/cargo-doc.1
+++ b/src/etc/man/cargo-doc.1
@@ -344,7 +344,9 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
.RS 4
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
diff --git a/src/etc/man/cargo-fix.1 b/src/etc/man/cargo-fix.1
index 3aaa1f89bce..e103de2d068 100644
--- a/src/etc/man/cargo-fix.1
+++ b/src/etc/man/cargo-fix.1
@@ -472,7 +472,9 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
.RS 4
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
diff --git a/src/etc/man/cargo-install.1 b/src/etc/man/cargo-install.1
index 72fc87ff136..7f2db3f5fe9 100644
--- a/src/etc/man/cargo-install.1
+++ b/src/etc/man/cargo-install.1
@@ -325,7 +325,9 @@ May also be specified with the \fBnet.offline\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
diff --git a/src/etc/man/cargo-package.1 b/src/etc/man/cargo-package.1
index 6e451ada63b..ce7f7ae64e0 100644
--- a/src/etc/man/cargo-package.1
+++ b/src/etc/man/cargo-package.1
@@ -233,7 +233,9 @@ May also be specified with the \fBnet.offline\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
diff --git a/src/etc/man/cargo-publish.1 b/src/etc/man/cargo-publish.1
index aa2490ed0eb..0b065510634 100644
--- a/src/etc/man/cargo-publish.1
+++ b/src/etc/man/cargo-publish.1
@@ -183,7 +183,9 @@ May also be specified with the \fBnet.offline\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
diff --git a/src/etc/man/cargo-run.1 b/src/etc/man/cargo-run.1
index 241e0b2e161..c1902aa822f 100644
--- a/src/etc/man/cargo-run.1
+++ b/src/etc/man/cargo-run.1
@@ -277,7 +277,9 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
.RS 4
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
diff --git a/src/etc/man/cargo-rustc.1 b/src/etc/man/cargo-rustc.1
index 07076e24463..6e24e8fd65f 100644
--- a/src/etc/man/cargo-rustc.1
+++ b/src/etc/man/cargo-rustc.1
@@ -381,7 +381,9 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
.RS 4
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
diff --git a/src/etc/man/cargo-rustdoc.1 b/src/etc/man/cargo-rustdoc.1
index 34a37d52f61..d5bc15d8f36 100644
--- a/src/etc/man/cargo-rustdoc.1
+++ b/src/etc/man/cargo-rustdoc.1
@@ -363,7 +363,9 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
.RS 4
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
diff --git a/src/etc/man/cargo-test.1 b/src/etc/man/cargo-test.1
index 00fe416d5a2..3c12b2d8b6b 100644
--- a/src/etc/man/cargo-test.1
+++ b/src/etc/man/cargo-test.1
@@ -494,7 +494,9 @@ cargo test \-j 2 \-\- \-\-test\-threads=2
.RS 4
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
-the number of CPUs.
+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.
.RE
.sp
\fB\-\-keep\-going\fR
From 81f78af15264b2d2bdb02e6477395df7337f7d26 Mon Sep 17 00:00:00 2001
From: Yerkebulan Tulibergenov
Date: Sun, 10 Jul 2022 16:13:39 -0700
Subject: [PATCH 5/7] make consistent error message
---
src/cargo/core/compiler/build_config.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs
index b2c8f28fdc8..1aca44b802d 100644
--- a/src/cargo/core/compiler/build_config.rs
+++ b/src/cargo/core/compiler/build_config.rs
@@ -64,7 +64,7 @@ impl BuildConfig {
let cfg = config.build_config()?;
let requested_kinds = CompileKind::from_requested_targets(config, requested_targets)?;
if jobs == Some(0) {
- anyhow::bail!("jobs must not be zero")
+ anyhow::bail!("jobs may not be 0")
}
if jobs.is_some() && config.jobserver_from_env().is_some() {
config.shell().warn(
From 2a75b44bc5c866753d50d63ca4df5d8465029b0b Mon Sep 17 00:00:00 2001
From: Yerkebulan Tulibergenov
Date: Sun, 10 Jul 2022 16:31:24 -0700
Subject: [PATCH 6/7] fix the test
---
tests/testsuite/build.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs
index e951e038584..68e20666107 100644
--- a/tests/testsuite/build.rs
+++ b/tests/testsuite/build.rs
@@ -5455,7 +5455,7 @@ fn invalid_jobs() {
p.cargo("build --jobs 0")
.with_status(101)
- .with_stderr_contains("error: jobs must not be zero")
+ .with_stderr_contains("error: jobs may not be 0")
.run();
p.cargo("build --jobs over9000")
From 7c932a471327ec30d78293da044bbd34122b40f4 Mon Sep 17 00:00:00 2001
From: Yerkebulan Tulibergenov
Date: Sun, 31 Jul 2022 14:34:34 -0700
Subject: [PATCH 7/7] remove duplicate check
---
src/cargo/core/compiler/build_config.rs | 3 ---
1 file changed, 3 deletions(-)
diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs
index 1aca44b802d..51c9bf77871 100644
--- a/src/cargo/core/compiler/build_config.rs
+++ b/src/cargo/core/compiler/build_config.rs
@@ -63,9 +63,6 @@ impl BuildConfig {
) -> CargoResult {
let cfg = config.build_config()?;
let requested_kinds = CompileKind::from_requested_targets(config, requested_targets)?;
- if jobs == Some(0) {
- anyhow::bail!("jobs may not be 0")
- }
if jobs.is_some() && config.jobserver_from_env().is_some() {
config.shell().warn(
"a `-j` argument was passed to Cargo but Cargo is \