Skip to content

Commit 30fe923

Browse files
committed
perf: reduce target info rustc query call to one
This is kinda a hack since we abuse `--print=crate-name` where the crate name is `___` and used as a delimiter.
1 parent c03cc59 commit 30fe923

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

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

Lines changed: 38 additions & 13 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
@@ -251,6 +242,26 @@ impl TargetInfo {
251242
});
252243
sysroot_target_libdir.push("lib");
253244

245+
let support_split_debuginfo = {
246+
// HACK: abuse `--print=crate-name` to use `___` as a delimiter.
247+
let mut res = Vec::new();
248+
loop {
249+
match lines.next() {
250+
Some(line) if line == "___" => break,
251+
Some(line) => res.push(line.into()),
252+
None => {
253+
return error_missing_print_output(
254+
"split-debuginfo",
255+
&process,
256+
&output,
257+
&error,
258+
)
259+
}
260+
}
261+
}
262+
res
263+
};
264+
254265
let cfg = lines
255266
.map(|line| Ok(Cfg::from_str(line)?))
256267
.filter(TargetInfo::not_user_specific_cfg)
@@ -601,6 +612,20 @@ fn parse_crate_type(
601612
Ok(Some((prefix.to_string(), suffix.to_string())))
602613
}
603614

615+
/// Helper for creating an error message for missing output from a certain `--print` request.
616+
fn error_missing_print_output<T>(
617+
request: &str,
618+
cmd: &ProcessBuilder,
619+
stdout: &str,
620+
stderr: &str,
621+
) -> CargoResult<T> {
622+
let err_info = output_err_info(cmd, stdout, stderr);
623+
anyhow::bail!(
624+
"output of --print={request} missing when learning about \
625+
target-specific information from rustc\n{err_info}",
626+
)
627+
}
628+
604629
/// Helper for creating an error message when parsing rustc output fails.
605630
fn output_err_info(cmd: &ProcessBuilder, stdout: &str, stderr: &str) -> String {
606631
let mut result = format!("command was: {}\n", cmd);

0 commit comments

Comments
 (0)