Skip to content

Commit c03cc59

Browse files
committed
Auto merge of #11347 - kamirr:fix-split-debuginfo-windows, r=weihanglo
Fix split-debuginfo support detection ### What does this PR try to resolve? cargo assumed that if `-Csplit-debuginfo=packed` worked, all values would be correct. This however is not the case -- as of Rust 1.65.0, rustc supports `packed`, but not `unpacked` or `off` on Windows. Because of this, having `split-debuginfo="unpacked`" on Windows has caused builds to fail, as cargo assumed that the option is fine (`split-debuginfo=packed` worked), but rustc then failed when being passed `-Csplit-debuginfo=unpacked`. ### How should we test and review this PR? Consider an empty project with the following change to `Cargo.toml`: ```toml [profile.dev] split-debuginfo="unpacked" ``` `cargo +1.64.0 build` will work, but `cargo +1.65.0 build` will fail with the following error message: ``` PS C:\REDACTED> cargo build Compiling tmp v0.1.0 (C:\REDACTED) error: `-Csplit-debuginfo=unpacked` is unstable on this platform error: could not compile `tmp` due to previous error ``` With this patch and 1.65.0 rustc, cargo will ignore all `split-debuginfo` settings, but with rust-lang/rust#104104 rustc (approved, awaiting merge), it will properly detect supported values for `split-debuginfo` and only ignore those that are unsupported.
2 parents 2ccd950 + fe95630 commit c03cc59

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

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

+23-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::core::compiler::{
1212
};
1313
use crate::core::{Dependency, Package, Target, TargetKind, Workspace};
1414
use crate::util::config::{Config, StringList, TargetConfig};
15+
use crate::util::interning::InternedString;
1516
use crate::util::{CargoResult, Rustc};
1617
use anyhow::Context as _;
1718
use cargo_platform::{Cfg, CfgExpr};
@@ -43,6 +44,8 @@ pub struct TargetInfo {
4344
crate_types: RefCell<HashMap<CrateType, Option<(String, String)>>>,
4445
/// `cfg` information extracted from `rustc --print=cfg`.
4546
cfg: Vec<Cfg>,
47+
/// Supported values for `-Csplit-debuginfo=` flag, queried from rustc
48+
support_split_debuginfo: Vec<String>,
4649
/// Path to the sysroot.
4750
pub sysroot: PathBuf,
4851
/// Path to the "lib" or "bin" directory that rustc uses for its dynamic
@@ -55,8 +58,6 @@ pub struct TargetInfo {
5558
pub rustflags: Vec<String>,
5659
/// Extra flags to pass to `rustdoc`, see [`extra_args`].
5760
pub rustdocflags: Vec<String>,
58-
/// Whether or not rustc supports the `-Csplit-debuginfo` flag.
59-
pub supports_split_debuginfo: bool,
6061
}
6162

6263
/// Kind of each file generated by a Unit, part of `FileType`.
@@ -167,6 +168,18 @@ impl TargetInfo {
167168
loop {
168169
let extra_fingerprint = kind.fingerprint_hash();
169170

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+
170183
// Query rustc for several kinds of info from each line of output:
171184
// 0) file-names (to determine output file prefix/suffix for given crate type)
172185
// 1) sysroot
@@ -199,14 +212,6 @@ impl TargetInfo {
199212
process.arg("--crate-type").arg(crate_type.as_str());
200213
}
201214

202-
// An extra `rustc` call to determine `-Csplit-debuginfo=packed` support.
203-
let supports_split_debuginfo = rustc
204-
.cached_output(
205-
process.clone().arg("-Csplit-debuginfo=packed"),
206-
extra_fingerprint,
207-
)
208-
.is_ok();
209-
210215
process.arg("--print=sysroot");
211216
process.arg("--print=cfg");
212217

@@ -303,7 +308,7 @@ impl TargetInfo {
303308
Flags::Rustdoc,
304309
)?,
305310
cfg,
306-
supports_split_debuginfo,
311+
support_split_debuginfo,
307312
});
308313
}
309314
}
@@ -543,6 +548,13 @@ impl TargetInfo {
543548
}
544549
Ok((result, unsupported))
545550
}
551+
552+
/// Checks if the debuginfo-split value is supported by this target
553+
pub fn supports_debuginfo_split(&self, split: InternedString) -> bool {
554+
self.support_split_debuginfo
555+
.iter()
556+
.any(|sup| sup.as_str() == split.as_str())
557+
}
546558
}
547559

548560
/// Takes rustc output (using specialized command line args), and calculates the file prefix and

src/cargo/core/compiler/mod.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -980,10 +980,17 @@ fn build_base_args(
980980
cmd.args(&lto_args(cx, unit));
981981

982982
// This is generally just an optimization on build time so if we don't pass
983-
// it then it's ok. As of the time of this writing it's a very new flag, so
984-
// we need to dynamically check if it's available.
985-
if cx.bcx.target_data.info(unit.kind).supports_split_debuginfo {
986-
if let Some(split) = split_debuginfo {
983+
// it then it's ok. The values for the flag (off, packed, unpacked) may be supported
984+
// or not depending on the platform, so availability is checked per-value.
985+
// For example, at the time of writing this code, on Windows the only stable valid
986+
// value for split-debuginfo is "packed", while on Linux "unpacked" is also stable.
987+
if let Some(split) = split_debuginfo {
988+
if cx
989+
.bcx
990+
.target_data
991+
.info(unit.kind)
992+
.supports_debuginfo_split(split)
993+
{
987994
cmd.arg("-C").arg(format!("split-debuginfo={}", split));
988995
}
989996
}

0 commit comments

Comments
 (0)