Skip to content

Commit c1dd25a

Browse files
committed
Auto merge of #4616 - kennytm:fix-4490, r=alexcrichton
Uplift *.dSYM Fixed #4490. The solution is based on #4570. Simply adding `.dSYM` into `add_target_specific_suffixes` will cause cargo trying to actually run that `.dSYM` folder, so I've upgraded the `linkable` boolean into a 3-value enum `TargetFileType`, to tell `cargo run` and `cargo test` to avoid these debug symbol files. (I haven't checked if it can solve #4056, don't wanna mess with Spotlight 😝)
2 parents c0173be + dfd964a commit c1dd25a

File tree

4 files changed

+96
-35
lines changed

4 files changed

+96
-35
lines changed

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ pub struct Unit<'a> {
6060
pub kind: Kind,
6161
}
6262

63+
/// Type of each file generated by a Unit.
64+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
65+
pub enum TargetFileType {
66+
/// Not a special file type.
67+
Normal,
68+
/// It is something you can link against (e.g. a library)
69+
Linkable,
70+
/// It is a piece of external debug information (e.g. *.dSYM and *.pdb)
71+
DebugInfo,
72+
}
73+
6374
/// The build context, containing all information about a build task
6475
pub struct Context<'a, 'cfg: 'a> {
6576
/// The workspace the build is for
@@ -96,8 +107,8 @@ pub struct Context<'a, 'cfg: 'a> {
96107
/// - File name that will be produced by the build process (in `deps`)
97108
/// - If it should be linked into `target`, and what it should be called (e.g. without
98109
/// metadata).
99-
/// - Whether it is something you can link against (e.g. a library)
100-
target_filenames: HashMap<Unit<'a>, Arc<Vec<(PathBuf, Option<PathBuf>, bool)>>>,
110+
/// - Type of the file (library / debug symbol / else)
111+
target_filenames: HashMap<Unit<'a>, Arc<Vec<(PathBuf, Option<PathBuf>, TargetFileType)>>>,
101112
target_metadatas: HashMap<Unit<'a>, Option<Metadata>>,
102113
}
103114

@@ -649,7 +660,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
649660
/// - link_dst: Optional file to link/copy the result to (without metadata suffix)
650661
/// - linkable: Whether possible to link against file (eg it's a library)
651662
pub fn target_filenames(&mut self, unit: &Unit<'a>)
652-
-> CargoResult<Arc<Vec<(PathBuf, Option<PathBuf>, bool)>>> {
663+
-> CargoResult<Arc<Vec<(PathBuf, Option<PathBuf>, TargetFileType)>>> {
653664
if let Some(cache) = self.target_filenames.get(unit) {
654665
return Ok(Arc::clone(cache))
655666
}
@@ -662,7 +673,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
662673
}
663674

664675
fn calc_target_filenames(&mut self, unit: &Unit<'a>)
665-
-> CargoResult<Arc<Vec<(PathBuf, Option<PathBuf>, bool)>>> {
676+
-> CargoResult<Arc<Vec<(PathBuf, Option<PathBuf>, TargetFileType)>>> {
666677
let out_dir = self.out_dir(unit);
667678
let stem = self.file_stem(unit);
668679
let link_stem = self.link_stem(unit);
@@ -680,9 +691,9 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
680691
let link_dst = link_stem.clone().map(|(ld, ls)| {
681692
ld.join(format!("lib{}.rmeta", ls))
682693
});
683-
ret.push((filename, link_dst, true));
694+
ret.push((filename, link_dst, TargetFileType::Linkable));
684695
} else {
685-
let mut add = |crate_type: &str, linkable: bool| -> CargoResult<()> {
696+
let mut add = |crate_type: &str, file_type: TargetFileType| -> CargoResult<()> {
686697
let crate_type = if crate_type == "lib" {"rlib"} else {crate_type};
687698
let mut crate_types = info.crate_types.borrow_mut();
688699
let entry = crate_types.entry(crate_type.to_string());
@@ -698,10 +709,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
698709
let suffixes = add_target_specific_suffixes(
699710
&self.target_triple(),
700711
&crate_type,
712+
unit.target.kind(),
701713
suffix,
702-
linkable,
714+
file_type,
703715
);
704-
for (suffix, linkable, should_replace_hyphens) in suffixes {
716+
for (suffix, file_type, should_replace_hyphens) in suffixes {
705717
// wasm bin target will generate two files in deps such as
706718
// "web-stuff.js" and "web_stuff.wasm". Note the different usages of
707719
// "-" and "_". should_replace_hyphens is a flag to indicate that
@@ -717,7 +729,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
717729
let link_dst = link_stem.clone().map(|(ld, ls)| {
718730
ld.join(format!("{}{}{}", prefix, conv(ls), suffix))
719731
});
720-
ret.push((filename, link_dst, linkable));
732+
ret.push((filename, link_dst, file_type));
721733
}
722734
Ok(())
723735
}
@@ -735,17 +747,21 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
735747
TargetKind::ExampleBin |
736748
TargetKind::Bench |
737749
TargetKind::Test => {
738-
add("bin", false)?;
750+
add("bin", TargetFileType::Normal)?;
739751
}
740752
TargetKind::Lib(..) |
741753
TargetKind::ExampleLib(..)
742754
if unit.profile.test => {
743-
add("bin", false)?;
755+
add("bin", TargetFileType::Normal)?;
744756
}
745757
TargetKind::ExampleLib(ref kinds) |
746758
TargetKind::Lib(ref kinds) => {
747759
for kind in kinds {
748-
add(kind.crate_type(), kind.linkable())?;
760+
add(kind.crate_type(), if kind.linkable() {
761+
TargetFileType::Linkable
762+
} else {
763+
TargetFileType::Normal
764+
})?;
749765
}
750766
}
751767
}
@@ -1257,30 +1273,40 @@ fn parse_crate_type(
12571273
}
12581274

12591275
// (not a rustdoc)
1260-
// Return a list of 3-tuples (suffix, linkable, should_replace_hyphens).
1276+
// Return a list of 3-tuples (suffix, file_type, should_replace_hyphens).
12611277
//
12621278
// should_replace_hyphens will be used by the caller to replace "-" with "_"
12631279
// in a bin_stem. See the caller side (calc_target_filenames()) for details.
12641280
fn add_target_specific_suffixes(
12651281
target_triple: &str,
12661282
crate_type: &str,
1283+
target_kind: &TargetKind,
12671284
suffix: &str,
1268-
linkable: bool,
1269-
) -> Vec<(String, bool, bool)> {
1270-
let mut ret = vec![(suffix.to_string(), linkable, false)];
1285+
file_type: TargetFileType,
1286+
) -> Vec<(String, TargetFileType, bool)> {
1287+
let mut ret = vec![(suffix.to_string(), file_type, false)];
12711288

12721289
// rust-lang/cargo#4500
12731290
if target_triple.ends_with("pc-windows-msvc") && crate_type.ends_with("dylib") &&
12741291
suffix == ".dll"
12751292
{
1276-
ret.push((".dll.lib".to_string(), false, false));
1293+
ret.push((".dll.lib".to_string(), TargetFileType::Normal, false));
12771294
}
12781295

12791296
// rust-lang/cargo#4535
12801297
if target_triple.starts_with("wasm32-") && crate_type == "bin" &&
12811298
suffix == ".js"
12821299
{
1283-
ret.push((".wasm".to_string(), false, true));
1300+
ret.push((".wasm".to_string(), TargetFileType::Normal, true));
1301+
}
1302+
1303+
// rust-lang/cargo#4490
1304+
// - only uplift *.dSYM for binaries.
1305+
// tests are run directly from target/debug/deps/
1306+
// and examples are inside target/debug/examples/ which already have *.dSYM next to them
1307+
// so no need to do anything.
1308+
if target_triple.contains("-apple-") && *target_kind == TargetKind::Bin {
1309+
ret.push((".dSYM".to_string(), TargetFileType::DebugInfo, false));
12841310
}
12851311

12861312
ret

src/cargo/ops/cargo_rustc/fingerprint.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use util::errors::{CargoResult, CargoResultExt};
1818
use util::paths;
1919

2020
use super::job::Work;
21-
use super::context::{Context, Unit};
21+
use super::context::{Context, Unit, TargetFileType};
2222
use super::custom_build::BuildDeps;
2323

2424
/// A tuple result of the `prepare_foo` functions in this module.
@@ -87,7 +87,10 @@ pub fn prepare_target<'a, 'cfg>(cx: &mut Context<'a, 'cfg>,
8787
missing_outputs = !root.join(unit.target.crate_name())
8888
.join("index.html").exists();
8989
} else {
90-
for &(ref src, ref link_dst, _) in cx.target_filenames(unit)?.iter() {
90+
for &(ref src, ref link_dst, file_type) in cx.target_filenames(unit)?.iter() {
91+
if file_type == TargetFileType::DebugInfo {
92+
continue;
93+
}
9194
missing_outputs |= !src.exists();
9295
if let Some(ref link_dst) = *link_dst {
9396
missing_outputs |= !link_dst.exists();

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use self::job_queue::JobQueue;
2323
use self::output_depinfo::output_depinfo;
2424

2525
pub use self::compilation::Compilation;
26-
pub use self::context::{Context, Unit};
26+
pub use self::context::{Context, Unit, TargetFileType};
2727
pub use self::custom_build::{BuildOutput, BuildMap, BuildScripts};
2828
pub use self::layout::is_bad_artifact_name;
2929

@@ -179,7 +179,11 @@ pub fn compile_targets<'a, 'cfg: 'a>(ws: &Workspace<'cfg>,
179179
queue.execute(&mut cx)?;
180180

181181
for unit in units.iter() {
182-
for &(ref dst, ref link_dst, _) in cx.target_filenames(unit)?.iter() {
182+
for &(ref dst, ref link_dst, file_type) in cx.target_filenames(unit)?.iter() {
183+
if file_type == TargetFileType::DebugInfo {
184+
continue;
185+
}
186+
183187
let bindst = match *link_dst {
184188
Some(ref link_dst) => link_dst,
185189
None => dst,
@@ -505,7 +509,7 @@ fn link_targets<'a, 'cfg>(cx: &mut Context<'a, 'cfg>,
505509
// above. This means that `cargo build` will produce binaries in
506510
// `target/debug` which one probably expects.
507511
let mut destinations = vec![];
508-
for &(ref src, ref link_dst, _linkable) in filenames.iter() {
512+
for &(ref src, ref link_dst, _file_type) in filenames.iter() {
509513
// This may have been a `cargo rustc` command which changes the
510514
// output, so the source may not actually exist.
511515
if !src.exists() {
@@ -887,8 +891,8 @@ fn build_deps_args<'a, 'cfg>(cmd: &mut ProcessBuilder,
887891
fn link_to<'a, 'cfg>(cmd: &mut ProcessBuilder,
888892
cx: &mut Context<'a, 'cfg>,
889893
unit: &Unit<'a>) -> CargoResult<()> {
890-
for &(ref dst, _, ref linkable) in cx.target_filenames(unit)?.iter() {
891-
if !*linkable {
894+
for &(ref dst, _, file_type) in cx.target_filenames(unit)?.iter() {
895+
if file_type != TargetFileType::Linkable {
892896
continue
893897
}
894898
let mut v = OsString::new();

tests/build.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use cargotest::support::paths::{CargoPathExt,root};
1515
use cargotest::support::{ProjectBuilder};
1616
use cargotest::support::{project, execs, main_file, basic_bin_manifest};
1717
use cargotest::support::registry::Package;
18-
use hamcrest::{assert_that, existing_file, is_not};
18+
use hamcrest::{assert_that, existing_file, existing_dir, is_not};
1919
use tempdir::TempDir;
2020

2121
#[test]
@@ -2718,6 +2718,9 @@ fn compiler_json_error_format() {
27182718
version = "0.5.0"
27192719
authors = ["[email protected]"]
27202720
2721+
[profile.dev]
2722+
debug = false # prevent the *.dSYM from affecting the test result
2723+
27212724
[dependencies.bar]
27222725
path = "bar"
27232726
"#)
@@ -2751,7 +2754,7 @@ fn compiler_json_error_format() {
27512754
"reason":"compiler-artifact",
27522755
"profile": {
27532756
"debug_assertions": true,
2754-
"debuginfo": 2,
2757+
"debuginfo": null,
27552758
"opt_level": "0",
27562759
"overflow_checks": true,
27572760
"test": false
@@ -2791,7 +2794,7 @@ fn compiler_json_error_format() {
27912794
},
27922795
"profile": {
27932796
"debug_assertions": true,
2794-
"debuginfo": 2,
2797+
"debuginfo": null,
27952798
"opt_level": "0",
27962799
"overflow_checks": true,
27972800
"test": false
@@ -2811,7 +2814,7 @@ fn compiler_json_error_format() {
28112814
"reason":"compiler-artifact",
28122815
"profile": {
28132816
"debug_assertions": true,
2814-
"debuginfo": 2,
2817+
"debuginfo": null,
28152818
"opt_level": "0",
28162819
"overflow_checks": true,
28172820
"test": false
@@ -2839,7 +2842,7 @@ fn compiler_json_error_format() {
28392842
},
28402843
"profile": {
28412844
"debug_assertions": true,
2842-
"debuginfo": 2,
2845+
"debuginfo": null,
28432846
"opt_level": "0",
28442847
"overflow_checks": true,
28452848
"test": false
@@ -2871,7 +2874,7 @@ fn message_format_json_forward_stderr() {
28712874
.file("src/main.rs", "fn main() { let unused = 0; }")
28722875
.build();
28732876

2874-
assert_that(p.cargo("rustc").arg("--bin").arg("foo")
2877+
assert_that(p.cargo("rustc").arg("--release").arg("--bin").arg("foo")
28752878
.arg("--message-format").arg("JSON"),
28762879
execs().with_status(0)
28772880
.with_json(r#"
@@ -2897,10 +2900,10 @@ fn message_format_json_forward_stderr() {
28972900
"src_path":"[..]"
28982901
},
28992902
"profile":{
2900-
"debug_assertions":true,
2901-
"debuginfo":2,
2902-
"opt_level":"0",
2903-
"overflow_checks": true,
2903+
"debug_assertions":false,
2904+
"debuginfo":null,
2905+
"opt_level":"3",
2906+
"overflow_checks": false,
29042907
"test":false
29052908
},
29062909
"features":[],
@@ -3874,3 +3877,28 @@ fn building_a_dependent_crate_witout_bin_should_fail() {
38743877
"[..]can't find `a_bin` bin, specify bin.path"
38753878
));
38763879
}
3880+
3881+
#[cfg(any(target_os = "macos", target_os = "ios"))]
3882+
#[test]
3883+
fn uplift_dsym_of_bin_on_mac() {
3884+
let p = project("foo")
3885+
.file("Cargo.toml", r#"
3886+
[project]
3887+
name = "foo"
3888+
version = "0.1.0"
3889+
"#)
3890+
.file("src/main.rs", "fn main() { panic!(); }")
3891+
.file("src/bin/b.rs", "fn main() { panic!(); }")
3892+
.file("examples/c.rs", "fn main() { panic!(); }")
3893+
.file("tests/d.rs", "fn main() { panic!(); }")
3894+
.build();
3895+
3896+
assert_that(
3897+
p.cargo("build").arg("--bins").arg("--examples").arg("--tests"),
3898+
execs().with_status(0)
3899+
);
3900+
assert_that(&p.bin("foo.dSYM"), existing_dir());
3901+
assert_that(&p.bin("b.dSYM"), existing_dir());
3902+
assert_that(&p.bin("c.dSYM"), is_not(existing_dir()));
3903+
assert_that(&p.bin("d.dSYM"), is_not(existing_dir()));
3904+
}

0 commit comments

Comments
 (0)