Skip to content

Commit 2fdc3b1

Browse files
committed
bootstrap: allow setting --jobs in config.toml
1 parent 03983fb commit 2fdc3b1

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed

Diff for: config.example.toml

+5
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,11 @@
414414
# Specify the location of the Android NDK. Used when targeting Android.
415415
#android-ndk = "/path/to/android-ndk-r26d"
416416

417+
# Number of parallel jobs to be used for building and testing. If set to `0` or
418+
# omitted, it will be automatically determined. This is the `-j`/`--jobs` flag
419+
# passed to cargo invocations.
420+
#jobs = 0
421+
417422
# =============================================================================
418423
# General install configuration options
419424
# =============================================================================

Diff for: src/bootstrap/src/core/config/config.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ define_config! {
891891
metrics: Option<bool> = "metrics",
892892
android_ndk: Option<PathBuf> = "android-ndk",
893893
optimized_compiler_builtins: Option<bool> = "optimized-compiler-builtins",
894+
jobs: Option<u32> = "jobs",
894895
}
895896
}
896897

@@ -1289,7 +1290,8 @@ impl Config {
12891290
config.rustc_error_format = flags.rustc_error_format;
12901291
config.json_output = flags.json_output;
12911292
config.on_fail = flags.on_fail;
1292-
config.jobs = Some(threads_from_config(flags.jobs as u32));
1293+
config.jobs = flags.jobs.filter(|j| *j > 0);
1294+
12931295
config.cmd = flags.cmd;
12941296
config.incremental = flags.incremental;
12951297
config.dry_run = if flags.dry_run { DryRun::UserSelected } else { DryRun::Disabled };
@@ -1511,8 +1513,19 @@ impl Config {
15111513
metrics: _,
15121514
android_ndk,
15131515
optimized_compiler_builtins,
1516+
jobs,
15141517
} = toml.build.unwrap_or_default();
15151518

1519+
if config.jobs.is_none() {
1520+
config.jobs = match jobs {
1521+
Some(jobs) if jobs > 0 => Some(jobs),
1522+
_ => Some(
1523+
std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get)
1524+
as u32,
1525+
),
1526+
};
1527+
}
1528+
15161529
if let Some(file_build) = build {
15171530
config.build = TargetSelection::from_user(&file_build);
15181531
};

Diff for: src/bootstrap/src/core/config/flags.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,10 @@ pub struct Flags {
110110
short,
111111
long,
112112
value_hint = clap::ValueHint::Other,
113-
default_value_t = std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get),
114113
value_name = "JOBS"
115114
)]
116115
/// number of jobs to run in parallel
117-
pub jobs: usize,
116+
pub jobs: Option<u32>,
118117
// This overrides the deny-warnings configuration option,
119118
// which passes -Dwarnings to the compiler invocations.
120119
#[arg(global = true, long)]

Diff for: src/bootstrap/src/core/config/tests.rs

+59
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ runner = "x86_64-runner"
171171
Some(["cargo".to_string()].into_iter().collect()),
172172
"setting list value"
173173
);
174+
assert_eq!(config.jobs, Some(1));
174175
assert_eq!(
175176
config.llvm_build_config,
176177
[("foo".to_string(), "bar".to_string())].into_iter().collect(),
@@ -352,3 +353,61 @@ fn parse_rust_std_features_empty() {
352353
fn parse_rust_std_features_invalid() {
353354
parse("rust.std-features = \"backtrace\"");
354355
}
356+
357+
#[test]
358+
fn parse_jobs() {
359+
assert_eq!(parse("build.jobs = 1").jobs, Some(1));
360+
}
361+
362+
#[test]
363+
fn jobs_precedence() {
364+
// `--jobs` should take precedence over using `--set build.jobs`.
365+
366+
let config = Config::parse_inner(
367+
Flags::parse(&[
368+
"check".to_owned(),
369+
"--config=/does/not/exist".to_owned(),
370+
"--jobs=67890".to_owned(),
371+
"--set=build.jobs=12345".to_owned(),
372+
]),
373+
|&_| toml::from_str(""),
374+
);
375+
assert_eq!(config.jobs, Some(67890));
376+
377+
// `--set build.jobs` should take precedence over `config.toml`.
378+
let config = Config::parse_inner(
379+
Flags::parse(&[
380+
"check".to_owned(),
381+
"--config=/does/not/exist".to_owned(),
382+
"--set=build.jobs=12345".to_owned(),
383+
]),
384+
|&_| {
385+
toml::from_str(
386+
r#"
387+
[build]
388+
jobs = 67890
389+
"#,
390+
)
391+
},
392+
);
393+
assert_eq!(config.jobs, Some(67890));
394+
395+
// `--jobs` > `--set build.jobs` > `config.toml`
396+
let config = Config::parse_inner(
397+
Flags::parse(&[
398+
"check".to_owned(),
399+
"--jobs=123".to_owned(),
400+
"--config=/does/not/exist".to_owned(),
401+
"--set=build.jobs=456".to_owned(),
402+
]),
403+
|&_| {
404+
toml::from_str(
405+
r#"
406+
[build]
407+
jobs = 789
408+
"#,
409+
)
410+
},
411+
);
412+
assert_eq!(config.jobs, Some(123));
413+
}

Diff for: src/bootstrap/src/utils/change_tracker.rs

+5
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
275275
severity: ChangeSeverity::Info,
276276
summary: "New option `./x setup editor` added, replacing `./x setup vscode` and adding support for vim, emacs and helix.",
277277
},
278+
ChangeInfo {
279+
change_id: 131838,
280+
severity: ChangeSeverity::Info,
281+
summary: "Allow setting `--jobs` in config.toml with `build.jobs`.",
282+
},
278283
];

0 commit comments

Comments
 (0)