Copy `.pdb` files to `target` directory
authorAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 14 Mar 2018 19:21:02 +0000 (22:21 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 14 Mar 2018 19:31:19 +0000 (22:31 +0300)
`.pdb` files are for windows debug info (unlike on linux, debug info is
in a separate file). Windows executable actually hard-code paths to
`.pdb` files, so debugging mvsc rust programs works even without this
patch. However, if you want to distribute the executable to other
machines, you'd better distribute both `foo.exe` and `foo.pdb`, because
absolute paths won't work on another machine. Having same-named .pdb
file alongside the binary would work though.

closes #4960

src/cargo/ops/cargo_rustc/context.rs
tests/testsuite/build.rs

index 9f7766e0ef809de314df729a1cc94d4304611e33..f243aa57a3b08fa32041d33d563d82072e02f981 100644 (file)
@@ -1326,13 +1326,17 @@ fn add_target_specific_suffixes(
         ret.push((".wasm".to_string(), TargetFileType::Normal, true));
     }
 
-    // rust-lang/cargo#4490
-    //  - only uplift *.dSYM for binaries.
+    // rust-lang/cargo#4490, rust-lang/cargo#4960
+    //  - only uplift debuginfo for binaries.
     //    tests are run directly from target/debug/deps/
-    //    and examples are inside target/debug/examples/ which already have *.dSYM next to them
+    //    and examples are inside target/debug/examples/ which already have symbols next to them
     //    so no need to do anything.
-    if target_triple.contains("-apple-") && *target_kind == TargetKind::Bin {
-        ret.push((".dSYM".to_string(), TargetFileType::DebugInfo, false));
+    if *target_kind == TargetKind::Bin {
+        if target_triple.contains("-apple-") {
+            ret.push((".dSYM".to_string(), TargetFileType::DebugInfo, false));
+        } else if target_triple.ends_with("-msvc") {
+            ret.push((".pdb".to_string(), TargetFileType::DebugInfo, false));
+        }
     }
 
     ret
index 7a80acb2c4fe39efce122913ccd11585ac53c41f..e21597f84f084be6024eb96c117c9bb1fe5f1403 100644 (file)
@@ -4179,6 +4179,33 @@ fn uplift_dsym_of_bin_on_mac() {
     assert_that(&p.bin("d.dSYM"), is_not(existing_dir()));
 }
 
+#[test]
+fn uplift_pdb_of_bin_on_windows() {
+    if !cfg!(all(target_os = "windows", target_env = "msvc")) {
+        return
+    }
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.1.0"
+        "#)
+        .file("src/main.rs", "fn main() { panic!(); }")
+        .file("src/bin/b.rs", "fn main() { panic!(); }")
+        .file("examples/c.rs", "fn main() { panic!(); }")
+        .file("tests/d.rs", "fn main() { panic!(); }")
+        .build();
+
+    assert_that(
+        p.cargo("build").arg("--bins").arg("--examples").arg("--tests"),
+        execs().with_status(0)
+    );
+    assert_that(&p.target_debug_dir().join("foo.pdb"), existing_file());
+    assert_that(&p.target_debug_dir().join("b.pdb"), existing_file());
+    assert_that(&p.target_debug_dir().join("c.pdb"), is_not(existing_file()));
+    assert_that(&p.target_debug_dir().join("d.pdb"), is_not(existing_file()));
+}
+
 // Make sure that `cargo build` chooses the correct profile for building
 // targets based on filters (assuming --profile is not specified).
 #[test]