Skip to content

Commit f6cf5ab

Browse files
committed
Auto merge of #11633 - weihanglo:reduce-rustc-query-calls, r=ehuss
Reduce target info rustc query calls
2 parents 97b7073 + 1cef6d7 commit f6cf5ab

File tree

2 files changed

+82
-26
lines changed

2 files changed

+82
-26
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,11 @@ impl TargetInfo {
168168
loop {
169169
let extra_fingerprint = kind.fingerprint_hash();
170170

171-
// Query rustc for supported -Csplit-debuginfo values
172-
let support_split_debuginfo = rustc
173-
.cached_output(
174-
rustc.workspace_process().arg("--print=split-debuginfo"),
175-
extra_fingerprint,
176-
)
177-
.unwrap_or_default()
178-
.0
179-
.lines()
180-
.map(String::from)
181-
.collect();
182-
183171
// Query rustc for several kinds of info from each line of output:
184172
// 0) file-names (to determine output file prefix/suffix for given crate type)
185173
// 1) sysroot
186-
// 2) cfg
174+
// 2) split-debuginfo
175+
// 3) cfg
187176
//
188177
// Search `--print` to see what we query so far.
189178
let mut process = rustc.workspace_process();
@@ -213,6 +202,8 @@ impl TargetInfo {
213202
}
214203

215204
process.arg("--print=sysroot");
205+
process.arg("--print=split-debuginfo");
206+
process.arg("--print=crate-name"); // `___` as a delimiter.
216207
process.arg("--print=cfg");
217208

218209
let (output, error) = rustc
@@ -228,13 +219,8 @@ impl TargetInfo {
228219
map.insert(crate_type.clone(), out);
229220
}
230221

231-
let line = match lines.next() {
232-
Some(line) => line,
233-
None => anyhow::bail!(
234-
"output of --print=sysroot missing when learning about \
235-
target-specific information from rustc\n{}",
236-
output_err_info(&process, &output, &error)
237-
),
222+
let Some(line) = lines.next() else {
223+
return error_missing_print_output("sysroot", &process, &output, &error);
238224
};
239225
let sysroot = PathBuf::from(line);
240226
let sysroot_host_libdir = if cfg!(windows) {
@@ -251,6 +237,26 @@ impl TargetInfo {
251237
});
252238
sysroot_target_libdir.push("lib");
253239

240+
let support_split_debuginfo = {
241+
// HACK: abuse `--print=crate-name` to use `___` as a delimiter.
242+
let mut res = Vec::new();
243+
loop {
244+
match lines.next() {
245+
Some(line) if line == "___" => break,
246+
Some(line) => res.push(line.into()),
247+
None => {
248+
return error_missing_print_output(
249+
"split-debuginfo",
250+
&process,
251+
&output,
252+
&error,
253+
)
254+
}
255+
}
256+
}
257+
res
258+
};
259+
254260
let cfg = lines
255261
.map(|line| Ok(Cfg::from_str(line)?))
256262
.filter(TargetInfo::not_user_specific_cfg)
@@ -590,17 +596,27 @@ fn parse_crate_type(
590596
};
591597
let mut parts = line.trim().split("___");
592598
let prefix = parts.next().unwrap();
593-
let suffix = match parts.next() {
594-
Some(part) => part,
595-
None => anyhow::bail!(
596-
"output of --print=file-names has changed in the compiler, cannot parse\n{}",
597-
output_err_info(cmd, output, error)
598-
),
599+
let Some(suffix) = parts.next() else {
600+
return error_missing_print_output("file-names", cmd, output, error);
599601
};
600602

601603
Ok(Some((prefix.to_string(), suffix.to_string())))
602604
}
603605

606+
/// Helper for creating an error message for missing output from a certain `--print` request.
607+
fn error_missing_print_output<T>(
608+
request: &str,
609+
cmd: &ProcessBuilder,
610+
stdout: &str,
611+
stderr: &str,
612+
) -> CargoResult<T> {
613+
let err_info = output_err_info(cmd, stdout, stderr);
614+
anyhow::bail!(
615+
"output of --print={request} missing when learning about \
616+
target-specific information from rustc\n{err_info}",
617+
)
618+
}
619+
604620
/// Helper for creating an error message when parsing rustc output fails.
605621
fn output_err_info(cmd: &ProcessBuilder, stdout: &str, stderr: &str) -> String {
606622
let mut result = format!("command was: {}\n", cmd);

tests/testsuite/cfg.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,22 @@ fn bad_cfg_discovery() {
356356
return;
357357
}
358358
println!("{}", sysroot);
359+
360+
if mode == "no-split-debuginfo" {
361+
return;
362+
}
363+
loop {
364+
let line = lines.next().unwrap();
365+
if line == "___" {
366+
println!("\n{line}");
367+
break;
368+
} else {
369+
// As the number split-debuginfo options varies,
370+
// concat them into one line.
371+
print!("{line},");
372+
}
373+
};
374+
359375
if mode != "bad-cfg" {
360376
panic!("unexpected");
361377
}
@@ -412,6 +428,28 @@ command was: `[..]compiler[..]--crate-type [..]`
412428
[..]___[..]
413429
[..]___[..]
414430
431+
",
432+
)
433+
.run();
434+
435+
p.cargo("build")
436+
.env("RUSTC", &funky_rustc)
437+
.env("FUNKY_MODE", "no-split-debuginfo")
438+
.with_status(101)
439+
.with_stderr(
440+
"\
441+
[ERROR] output of --print=split-debuginfo missing when learning about target-specific information from rustc
442+
command was: `[..]compiler[..]--crate-type [..]`
443+
444+
--- stdout
445+
[..]___[..]
446+
[..]___[..]
447+
[..]___[..]
448+
[..]___[..]
449+
[..]___[..]
450+
[..]___[..]
451+
[..]
452+
415453
",
416454
)
417455
.run();
@@ -430,6 +468,8 @@ command was: `[..]compiler[..]--crate-type [..]`
430468
[..]___[..]
431469
[..]___[..]
432470
[..]
471+
[..],[..]
472+
___
433473
123
434474
435475

0 commit comments

Comments
 (0)