diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index 1ca5f525cf3..c8936c3e144 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -832,12 +832,17 @@ impl Execs { self } + pub fn enable_split_debuginfo_packed(&mut self) -> &mut Self { + self.env("CARGO_PROFILE_DEV_SPLIT_DEBUGINFO", "packed") + .env("CARGO_PROFILE_TEST_SPLIT_DEBUGINFO", "packed") + .env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed") + .env("CARGO_PROFILE_BENCH_SPLIT_DEBUGINFO", "packed"); + self + } + pub fn enable_mac_dsym(&mut self) -> &mut Self { if cfg!(target_os = "macos") { - self.env("CARGO_PROFILE_DEV_SPLIT_DEBUGINFO", "packed") - .env("CARGO_PROFILE_TEST_SPLIT_DEBUGINFO", "packed") - .env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed") - .env("CARGO_PROFILE_BENCH_SPLIT_DEBUGINFO", "packed"); + return self.enable_split_debuginfo_packed(); } self } diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 1ec0d9b5b4e..31702bc94d0 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -473,6 +473,25 @@ impl TargetInfo { // preserved. should_replace_hyphens: true, }) + } else { + // Because DWARF Package (dwp) files are produced after the + // fact by another tool, there is nothing in the binary that + // provides a means to locate them. By convention, debuggers + // take the binary filename and append ".dwp" (including to + // binaries that already have an extension such as shared libs) + // to find the dwp. + ret.push(FileType { + // It is important to preserve the existing suffix for + // e.g. shared libraries, where the dwp for libfoo.so is + // expected to be at libfoo.so.dwp. + suffix: format!("{suffix}.dwp"), + prefix: prefix.clone(), + flavor: FileFlavor::DebugInfo, + crate_type: Some(crate_type.clone()), + // Likewise, the dwp needs to match the primary artifact's + // hyphenation exactly. + should_replace_hyphens: crate_type != CrateType::Bin, + }) } } diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index 507c2d89b1c..b3e1bf48302 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -203,6 +203,8 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> { rm_rf_glob(&split_debuginfo_obj, config, &mut progress)?; let split_debuginfo_dwo = dir_glob.join(format!("{}.*.dwo", crate_name)); rm_rf_glob(&split_debuginfo_dwo, config, &mut progress)?; + let split_debuginfo_dwp = dir_glob.join(format!("{}.*.dwp", crate_name)); + rm_rf_glob(&split_debuginfo_dwp, config, &mut progress)?; // Remove the uplifted copy. if let Some(uplift_dir) = uplift_dir { diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 58c3f6baf01..8ad9f70ac52 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -5216,6 +5216,29 @@ fn uplift_pdb_of_bin_on_windows() { assert!(!p.target_debug_dir().join("d.pdb").exists()); } +#[cargo_test] +#[cfg(target_os = "linux")] +fn uplift_dwp_of_bin_on_linux() { + let p = project() + .file("src/main.rs", "fn main() { panic!(); }") + .file("src/bin/b.rs", "fn main() { panic!(); }") + .file("src/bin/foo-bar.rs", "fn main() { panic!(); }") + .file("examples/c.rs", "fn main() { panic!(); }") + .file("tests/d.rs", "fn main() { panic!(); }") + .build(); + + p.cargo("build --bins --examples --tests") + .enable_split_debuginfo_packed() + .run(); + assert!(p.target_debug_dir().join("foo.dwp").is_file()); + assert!(p.target_debug_dir().join("b.dwp").is_file()); + assert!(p.target_debug_dir().join("examples/c.dwp").exists()); + assert!(p.target_debug_dir().join("foo-bar").is_file()); + assert!(p.target_debug_dir().join("foo-bar.dwp").is_file()); + assert!(!p.target_debug_dir().join("c.dwp").exists()); + assert!(!p.target_debug_dir().join("d.dwp").exists()); +} + // Ensure that `cargo build` chooses the correct profile for building // targets based on filters (assuming `--profile` is not specified). #[cargo_test]