Skip to content

Commit 7f1d08e

Browse files
committed
bootstrap: Don't eagerly format verbose messages
1 parent 4ccbb7d commit 7f1d08e

File tree

10 files changed

+62
-52
lines changed

10 files changed

+62
-52
lines changed

src/bootstrap/src/core/build_steps/compile.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,8 @@ impl Step for Sysroot {
15361536
};
15371537
let sysroot = sysroot_dir(compiler.stage);
15381538

1539-
builder.verbose(&format!("Removing sysroot {} to avoid caching bugs", sysroot.display()));
1539+
builder
1540+
.verbose(|| println!("Removing sysroot {} to avoid caching bugs", sysroot.display()));
15401541
let _ = fs::remove_dir_all(&sysroot);
15411542
t!(fs::create_dir_all(&sysroot));
15421543

@@ -1606,7 +1607,7 @@ impl Step for Sysroot {
16061607
return true;
16071608
}
16081609
if !filtered_files.iter().all(|f| f != path.file_name().unwrap()) {
1609-
builder.verbose_than(1, &format!("ignoring {}", path.display()));
1610+
builder.verbose_than(1, || println!("ignoring {}", path.display()));
16101611
false
16111612
} else {
16121613
true
@@ -2085,7 +2086,7 @@ pub fn stream_cargo(
20852086
cargo.arg(arg);
20862087
}
20872088

2088-
builder.verbose(&format!("running: {cargo:?}"));
2089+
builder.verbose(|| println!("running: {cargo:?}"));
20892090

20902091
if builder.config.dry_run() {
20912092
return true;

src/bootstrap/src/core/build_steps/dist.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2107,7 +2107,7 @@ fn maybe_install_llvm(
21072107
{
21082108
let mut cmd = Command::new(llvm_config);
21092109
cmd.arg("--libfiles");
2110-
builder.verbose(&format!("running {cmd:?}"));
2110+
builder.verbose(|| println!("running {cmd:?}"));
21112111
let files = if builder.config.dry_run() { "".into() } else { output(&mut cmd) };
21122112
let build_llvm_out = &builder.llvm_out(builder.config.build);
21132113
let target_llvm_out = &builder.llvm_out(target);

src/bootstrap/src/core/build_steps/test.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -551,15 +551,15 @@ impl Miri {
551551
if builder.config.dry_run() {
552552
String::new()
553553
} else {
554-
builder.verbose(&format!("running: {cargo:?}"));
554+
builder.verbose(|| println!("running: {cargo:?}"));
555555
let out =
556556
cargo.output().expect("We already ran `cargo miri setup` before and that worked");
557557
assert!(out.status.success(), "`cargo miri setup` returned with non-0 exit code");
558558
// Output is "<sysroot>\n".
559559
let stdout = String::from_utf8(out.stdout)
560560
.expect("`cargo miri setup` stdout is not valid UTF-8");
561561
let sysroot = stdout.trim_end();
562-
builder.verbose(&format!("`cargo miri setup --print-sysroot` said: {sysroot:?}"));
562+
builder.verbose(|| println!("`cargo miri setup --print-sysroot` said: {sysroot:?}"));
563563
sysroot.to_owned()
564564
}
565565
}
@@ -2326,7 +2326,7 @@ fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) ->
23262326
}
23272327
}
23282328

2329-
builder.verbose(&format!("doc tests for: {}", markdown.display()));
2329+
builder.verbose(|| println!("doc tests for: {}", markdown.display()));
23302330
let mut cmd = builder.rustdoc_cmd(compiler);
23312331
builder.add_rust_test_threads(&mut cmd);
23322332
// allow for unstable options such as new editions

src/bootstrap/src/core/builder.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,12 @@ impl StepDescription {
382382
}
383383

384384
if !builder.config.skip.is_empty() && !matches!(builder.config.dry_run, DryRun::SelfCheck) {
385-
builder.verbose(&format!(
386-
"{:?} not skipped for {:?} -- not in {:?}",
387-
pathset, self.name, builder.config.skip
388-
));
385+
builder.verbose(|| {
386+
println!(
387+
"{:?} not skipped for {:?} -- not in {:?}",
388+
pathset, self.name, builder.config.skip
389+
)
390+
});
389391
}
390392
false
391393
}
@@ -1093,10 +1095,9 @@ impl<'a> Builder<'a> {
10931095
// Avoid deleting the rustlib/ directory we just copied
10941096
// (in `impl Step for Sysroot`).
10951097
if !builder.download_rustc() {
1096-
builder.verbose(&format!(
1097-
"Removing sysroot {} to avoid caching bugs",
1098-
sysroot.display()
1099-
));
1098+
builder.verbose(|| {
1099+
println!("Removing sysroot {} to avoid caching bugs", sysroot.display())
1100+
});
11001101
let _ = fs::remove_dir_all(&sysroot);
11011102
t!(fs::create_dir_all(&sysroot));
11021103
}
@@ -1436,7 +1437,7 @@ impl<'a> Builder<'a> {
14361437

14371438
let sysroot_str = sysroot.as_os_str().to_str().expect("sysroot should be UTF-8");
14381439
if !matches!(self.config.dry_run, DryRun::SelfCheck) {
1439-
self.verbose_than(0, &format!("using sysroot {sysroot_str}"));
1440+
self.verbose_than(0, || println!("using sysroot {sysroot_str}"));
14401441
}
14411442

14421443
let mut rustflags = Rustflags::new(target);
@@ -2102,11 +2103,11 @@ impl<'a> Builder<'a> {
21022103
panic!("{}", out);
21032104
}
21042105
if let Some(out) = self.cache.get(&step) {
2105-
self.verbose_than(1, &format!("{}c {:?}", " ".repeat(stack.len()), step));
2106+
self.verbose_than(1, || println!("{}c {:?}", " ".repeat(stack.len()), step));
21062107

21072108
return out;
21082109
}
2109-
self.verbose_than(1, &format!("{}> {:?}", " ".repeat(stack.len()), step));
2110+
self.verbose_than(1, || println!("{}> {:?}", " ".repeat(stack.len()), step));
21102111
stack.push(Box::new(step.clone()));
21112112
}
21122113

@@ -2144,7 +2145,7 @@ impl<'a> Builder<'a> {
21442145
let cur_step = stack.pop().expect("step stack empty");
21452146
assert_eq!(cur_step.downcast_ref(), Some(&step));
21462147
}
2147-
self.verbose_than(1, &format!("{}< {:?}", " ".repeat(self.stack.borrow().len()), step));
2148+
self.verbose_than(1, || println!("{}< {:?}", " ".repeat(self.stack.borrow().len()), step));
21482149
self.cache.put(step, out.clone());
21492150
out
21502151
}

src/bootstrap/src/core/config/config.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,7 @@ impl Config {
20432043
if self.dry_run() {
20442044
return Ok(());
20452045
}
2046-
self.verbose(&format!("running: {cmd:?}"));
2046+
self.verbose(|| println!("running: {cmd:?}"));
20472047
build_helper::util::try_run(cmd, self.is_verbose())
20482048
}
20492049

@@ -2230,9 +2230,10 @@ impl Config {
22302230
}
22312231
}
22322232

2233-
pub fn verbose(&self, msg: &str) {
2233+
/// Runs a function if verbosity is greater than 0
2234+
pub fn verbose(&self, f: impl Fn()) {
22342235
if self.verbose > 0 {
2235-
println!("{msg}");
2236+
f()
22362237
}
22372238
}
22382239

src/bootstrap/src/core/download.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Config {
6161
if self.dry_run() {
6262
return true;
6363
}
64-
self.verbose(&format!("running: {cmd:?}"));
64+
self.verbose(|| println!("running: {cmd:?}"));
6565
check_run(cmd, self.is_verbose())
6666
}
6767

@@ -195,7 +195,7 @@ impl Config {
195195
}
196196

197197
fn download_file(&self, url: &str, dest_path: &Path, help_on_error: &str) {
198-
self.verbose(&format!("download {url}"));
198+
self.verbose(|| println!("download {url}"));
199199
// Use a temporary file in case we crash while downloading, to avoid a corrupt download in cache/.
200200
let tempfile = self.tempdir().join(dest_path.file_name().unwrap());
201201
// While bootstrap itself only supports http and https downloads, downstream forks might
@@ -300,7 +300,9 @@ impl Config {
300300
}
301301
short_path = t!(short_path.strip_prefix(pattern));
302302
let dst_path = dst.join(short_path);
303-
self.verbose(&format!("extracting {} to {}", original_path.display(), dst.display()));
303+
self.verbose(|| {
304+
println!("extracting {} to {}", original_path.display(), dst.display())
305+
});
304306
if !t!(member.unpack_in(dst)) {
305307
panic!("path traversal attack ??");
306308
}
@@ -323,7 +325,7 @@ impl Config {
323325
pub(crate) fn verify(&self, path: &Path, expected: &str) -> bool {
324326
use sha2::Digest;
325327

326-
self.verbose(&format!("verifying {}", path.display()));
328+
self.verbose(|| println!("verifying {}", path.display()));
327329

328330
if self.dry_run() {
329331
return false;
@@ -379,7 +381,7 @@ enum DownloadSource {
379381
/// Functions that are only ever called once, but named for clarify and to avoid thousand-line functions.
380382
impl Config {
381383
pub(crate) fn download_clippy(&self) -> PathBuf {
382-
self.verbose("downloading stage0 clippy artifacts");
384+
self.verbose(|| println!("downloading stage0 clippy artifacts"));
383385

384386
let date = &self.stage0_metadata.compiler.date;
385387
let version = &self.stage0_metadata.compiler.version;
@@ -469,7 +471,7 @@ impl Config {
469471
}
470472

471473
pub(crate) fn download_ci_rustc(&self, commit: &str) {
472-
self.verbose(&format!("using downloaded stage2 artifacts from CI (commit {commit})"));
474+
self.verbose(|| println!("using downloaded stage2 artifacts from CI (commit {commit})"));
473475

474476
let version = self.artifact_version_part(commit);
475477
// download-rustc doesn't need its own cargo, it can just use beta's. But it does need the
@@ -486,7 +488,7 @@ impl Config {
486488
}
487489

488490
pub(crate) fn download_beta_toolchain(&self) {
489-
self.verbose("downloading stage0 beta artifacts");
491+
self.verbose(|| println!("downloading stage0 beta artifacts"));
490492

491493
let date = &self.stage0_metadata.compiler.date;
492494
let version = &self.stage0_metadata.compiler.version;
@@ -625,10 +627,12 @@ impl Config {
625627
self.unpack(&tarball, &bin_root, prefix);
626628
return;
627629
} else {
628-
self.verbose(&format!(
629-
"ignoring cached file {} due to failed verification",
630-
tarball.display()
631-
));
630+
self.verbose(|| {
631+
println!(
632+
"ignoring cached file {} due to failed verification",
633+
tarball.display()
634+
)
635+
});
632636
self.remove(&tarball);
633637
}
634638
}

src/bootstrap/src/lib.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ macro_rules! forward {
288288
}
289289

290290
forward! {
291-
verbose(msg: &str),
291+
verbose(f: impl Fn()),
292292
is_verbose() -> bool,
293293
create(path: &Path, s: &str),
294294
remove(f: &Path),
@@ -440,19 +440,19 @@ impl Build {
440440
.unwrap()
441441
.trim();
442442
if local_release.split('.').take(2).eq(version.split('.').take(2)) {
443-
build.verbose(&format!("auto-detected local-rebuild {local_release}"));
443+
build.verbose(|| println!("auto-detected local-rebuild {local_release}"));
444444
build.local_rebuild = true;
445445
}
446446

447-
build.verbose("finding compilers");
447+
build.verbose(|| println!("finding compilers"));
448448
utils::cc_detect::find(&build);
449449
// When running `setup`, the profile is about to change, so any requirements we have now may
450450
// be different on the next invocation. Don't check for them until the next time x.py is
451451
// run. This is ok because `setup` never runs any build commands, so it won't fail if commands are missing.
452452
//
453453
// Similarly, for `setup` we don't actually need submodules or cargo metadata.
454454
if !matches!(build.config.cmd, Subcommand::Setup { .. }) {
455-
build.verbose("running sanity check");
455+
build.verbose(|| println!("running sanity check"));
456456
crate::core::sanity::check(&mut build);
457457

458458
// Make sure we update these before gathering metadata so we don't get an error about missing
@@ -464,7 +464,7 @@ impl Build {
464464
// Now, update all existing submodules.
465465
build.update_existing_submodules();
466466

467-
build.verbose("learning about cargo");
467+
build.verbose(|| println!("learning about cargo"));
468468
crate::core::metadata::build(&mut build);
469469
}
470470

@@ -693,7 +693,7 @@ impl Build {
693693
let stamp = dir.join(".stamp");
694694
let mut cleared = false;
695695
if mtime(&stamp) < mtime(input) {
696-
self.verbose(&format!("Dirty - {}", dir.display()));
696+
self.verbose(|| println!("Dirty - {}", dir.display()));
697697
let _ = fs::remove_dir_all(dir);
698698
cleared = true;
699699
} else if stamp.exists() {
@@ -986,7 +986,7 @@ impl Build {
986986
}
987987

988988
let command = cmd.into();
989-
self.verbose(&format!("running: {command:?}"));
989+
self.verbose(|| println!("running: {command:?}"));
990990

991991
let (output, print_error) = match command.output_mode {
992992
mode @ (OutputMode::PrintAll | OutputMode::PrintOutput) => (
@@ -1044,14 +1044,15 @@ impl Build {
10441044
}
10451045
}
10461046

1047+
/// Check if verbosity is greater than the `level`
10471048
pub fn is_verbose_than(&self, level: usize) -> bool {
10481049
self.verbosity > level
10491050
}
10501051

1051-
/// Prints a message if this build is configured in more verbose mode than `level`.
1052-
fn verbose_than(&self, level: usize, msg: &str) {
1052+
/// Runs a function if verbosity is greater than `level`.
1053+
fn verbose_than(&self, level: usize, f: impl Fn()) {
10531054
if self.is_verbose_than(level) {
1054-
println!("{msg}");
1055+
f()
10551056
}
10561057
}
10571058

@@ -1627,7 +1628,7 @@ impl Build {
16271628
if self.config.dry_run() {
16281629
return;
16291630
}
1630-
self.verbose_than(1, &format!("Copy {src:?} to {dst:?}"));
1631+
self.verbose_than(1, || println!("Copy {src:?} to {dst:?}"));
16311632
if src == dst {
16321633
return;
16331634
}
@@ -1718,7 +1719,7 @@ impl Build {
17181719
return;
17191720
}
17201721
let dst = dstdir.join(src.file_name().unwrap());
1721-
self.verbose_than(1, &format!("Install {src:?} to {dst:?}"));
1722+
self.verbose_than(1, || println!("Install {src:?} to {dst:?}"));
17221723
t!(fs::create_dir_all(dstdir));
17231724
if !src.exists() {
17241725
panic!("ERROR: File \"{}\" not found!", src.display());

src/bootstrap/src/utils/cc_detect.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ pub fn find_target(build: &Build, target: TargetSelection) {
145145
build.cxx.borrow_mut().insert(target, compiler);
146146
}
147147

148-
build.verbose(&format!("CC_{} = {:?}", &target.triple, build.cc(target)));
149-
build.verbose(&format!("CFLAGS_{} = {:?}", &target.triple, cflags));
148+
build.verbose(|| println!("CC_{} = {:?}", &target.triple, build.cc(target)));
149+
build.verbose(|| println!("CFLAGS_{} = {:?}", &target.triple, cflags));
150150
if let Ok(cxx) = build.cxx(target) {
151151
let cxxflags = build.cflags(target, GitRepo::Rustc, CLang::Cxx);
152-
build.verbose(&format!("CXX_{} = {:?}", &target.triple, cxx));
153-
build.verbose(&format!("CXXFLAGS_{} = {:?}", &target.triple, cxxflags));
152+
build.verbose(|| println!("CXX_{} = {:?}", &target.triple, cxx));
153+
build.verbose(|| println!("CXXFLAGS_{} = {:?}", &target.triple, cxxflags));
154154
}
155155
if let Some(ar) = ar {
156-
build.verbose(&format!("AR_{} = {:?}", &target.triple, ar));
156+
build.verbose(|| println!("AR_{} = {:?}", &target.triple, ar));
157157
build.ar.borrow_mut().insert(target, ar);
158158
}
159159

src/bootstrap/src/utils/render_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub(crate) fn try_run_tests(builder: &Builder<'_>, cmd: &mut Command, stream: bo
4444
fn run_tests(builder: &Builder<'_>, cmd: &mut Command, stream: bool) -> bool {
4545
cmd.stdout(Stdio::piped());
4646

47-
builder.verbose(&format!("running: {cmd:?}"));
47+
builder.verbose(|| println!("running: {cmd:?}"));
4848

4949
let mut process = cmd.spawn().unwrap();
5050

src/bootstrap/src/utils/tarball.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ impl<'a> Tarball<'a> {
328328

329329
// For `x install` tarball files aren't needed, so we can speed up the process by not producing them.
330330
let compression_profile = if self.builder.kind == Kind::Install {
331-
self.builder.verbose("Forcing dist.compression-profile = 'no-op' for `x install`.");
331+
self.builder.verbose(|| {
332+
println!("Forcing dist.compression-profile = 'no-op' for `x install`.")
333+
});
332334
// "no-op" indicates that the rust-installer won't produce compressed tarball sources.
333335
"no-op"
334336
} else {

0 commit comments

Comments
 (0)