Skip to content

Commit e06b964

Browse files
committed
Auto merge of #5281 - matklad:cleanup, r=alexcrichton
Cleanup Hopefully the last round of `conext` refactoring :-) r? @alexcrichton
2 parents 551f6f3 + 7bfddee commit e06b964

File tree

7 files changed

+388
-338
lines changed

7 files changed

+388
-338
lines changed

src/cargo/ops/cargo_clean.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> {
112112
continue;
113113
}
114114

115-
for &(ref src, ref link_dst, _) in cx.target_filenames(unit)?.iter() {
116-
rm_rf(src, config)?;
117-
if let Some(ref dst) = *link_dst {
115+
for output in cx.outputs(unit)?.iter() {
116+
rm_rf(&output.path, config)?;
117+
if let Some(ref dst) = output.hardlink {
118118
rm_rf(dst, config)?;
119119
}
120120
}

src/cargo/ops/cargo_rustc/context/compilation_files.rs

Lines changed: 58 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::hash_map::{Entry, HashMap};
1+
use std::collections::HashMap;
22
use std::env;
33
use std::fmt;
44
use std::hash::{Hash, Hasher, SipHasher};
@@ -9,7 +9,7 @@ use lazycell::LazyCell;
99

1010
use core::{TargetKind, Workspace};
1111
use ops::cargo_rustc::layout::Layout;
12-
use ops::cargo_rustc::TargetFileType;
12+
use ops::cargo_rustc::FileFlavor;
1313
use ops::{Context, Kind, Unit};
1414
use util::{self, CargoResult};
1515

@@ -29,13 +29,19 @@ pub struct CompilationFiles<'a, 'cfg: 'a> {
2929
pub(super) target: Option<Layout>,
3030
ws: &'a Workspace<'cfg>,
3131
metas: HashMap<Unit<'a>, Option<Metadata>>,
32-
/// For each Unit, a list all files produced as a triple of
33-
///
34-
/// - File name that will be produced by the build process (in `deps`)
35-
/// - If it should be linked into `target`, and what it should be called (e.g. without
36-
/// metadata).
37-
/// - Type of the file (library / debug symbol / else)
38-
outputs: HashMap<Unit<'a>, LazyCell<Arc<Vec<(PathBuf, Option<PathBuf>, TargetFileType)>>>>,
32+
/// For each Unit, a list all files produced.
33+
outputs: HashMap<Unit<'a>, LazyCell<Arc<Vec<OutputFile>>>>,
34+
}
35+
36+
#[derive(Debug)]
37+
pub struct OutputFile {
38+
/// File name that will be produced by the build process (in `deps`).
39+
pub path: PathBuf,
40+
/// If it should be linked into `target`, and what it should be called
41+
/// (e.g. without metadata).
42+
pub hardlink: Option<PathBuf>,
43+
/// Type of the file (library / debug symbol / else).
44+
pub flavor: FileFlavor,
3945
}
4046

4147
impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
@@ -153,13 +159,13 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
153159
}
154160
}
155161

156-
pub(super) fn target_filenames(
162+
pub(super) fn outputs(
157163
&self,
158164
unit: &Unit<'a>,
159165
cx: &Context<'a, 'cfg>,
160-
) -> CargoResult<Arc<Vec<(PathBuf, Option<PathBuf>, TargetFileType)>>> {
166+
) -> CargoResult<Arc<Vec<OutputFile>>> {
161167
self.outputs[unit]
162-
.try_borrow_with(|| self.calc_target_filenames(unit, cx))
168+
.try_borrow_with(|| self.calc_outputs(unit, cx))
163169
.map(Arc::clone)
164170
}
165171

@@ -181,20 +187,20 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
181187
/// Returns an Option because in some cases we don't want to link
182188
/// (eg a dependent lib)
183189
fn link_stem(&self, unit: &Unit<'a>) -> Option<(PathBuf, String)> {
184-
let src_dir = self.out_dir(unit);
190+
let out_dir = self.out_dir(unit);
185191
let bin_stem = self.bin_stem(unit);
186192
let file_stem = self.file_stem(unit);
187193

188194
// We currently only lift files up from the `deps` directory. If
189195
// it was compiled into something like `example/` or `doc/` then
190196
// we don't want to link it up.
191-
if src_dir.ends_with("deps") {
197+
if out_dir.ends_with("deps") {
192198
// Don't lift up library dependencies
193199
if self.ws.members().find(|&p| p == unit.pkg).is_none() && !unit.target.is_bin() {
194200
None
195201
} else {
196202
Some((
197-
src_dir.parent().unwrap().to_owned(),
203+
out_dir.parent().unwrap().to_owned(),
198204
if unit.profile.test {
199205
file_stem
200206
} else {
@@ -204,20 +210,20 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
204210
}
205211
} else if bin_stem == file_stem {
206212
None
207-
} else if src_dir.ends_with("examples") || src_dir.parent().unwrap().ends_with("build") {
208-
Some((src_dir, bin_stem))
213+
} else if out_dir.ends_with("examples") || out_dir.parent().unwrap().ends_with("build") {
214+
Some((out_dir, bin_stem))
209215
} else {
210216
None
211217
}
212218
}
213219

214-
fn calc_target_filenames(
220+
fn calc_outputs(
215221
&self,
216222
unit: &Unit<'a>,
217223
cx: &Context<'a, 'cfg>,
218-
) -> CargoResult<Arc<Vec<(PathBuf, Option<PathBuf>, TargetFileType)>>> {
224+
) -> CargoResult<Arc<Vec<OutputFile>>> {
219225
let out_dir = self.out_dir(unit);
220-
let stem = self.file_stem(unit);
226+
let file_stem = self.file_stem(unit);
221227
let link_stem = self.link_stem(unit);
222228
let info = if unit.target.for_host() {
223229
&cx.host_info
@@ -229,68 +235,47 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
229235
let mut unsupported = Vec::new();
230236
{
231237
if unit.profile.check {
232-
let filename = out_dir.join(format!("lib{}.rmeta", stem));
233-
let link_dst = link_stem
238+
let path = out_dir.join(format!("lib{}.rmeta", file_stem));
239+
let hardlink = link_stem
234240
.clone()
235241
.map(|(ld, ls)| ld.join(format!("lib{}.rmeta", ls)));
236-
ret.push((filename, link_dst, TargetFileType::Linkable));
242+
ret.push(OutputFile {
243+
path,
244+
hardlink,
245+
flavor: FileFlavor::Linkable,
246+
});
237247
} else {
238-
let mut add = |crate_type: &str, file_type: TargetFileType| -> CargoResult<()> {
248+
let mut add = |crate_type: &str, flavor: FileFlavor| -> CargoResult<()> {
239249
let crate_type = if crate_type == "lib" {
240250
"rlib"
241251
} else {
242252
crate_type
243253
};
244-
let mut crate_types = info.crate_types.borrow_mut();
245-
let entry = crate_types.entry(crate_type.to_string());
246-
let crate_type_info = match entry {
247-
Entry::Occupied(o) => &*o.into_mut(),
248-
Entry::Vacant(v) => {
249-
let value = info.discover_crate_type(v.key())?;
250-
&*v.insert(value)
251-
}
252-
};
253-
match *crate_type_info {
254-
Some((ref prefix, ref suffix)) => {
255-
let suffixes = add_target_specific_suffixes(
256-
cx.target_triple(),
257-
crate_type,
258-
unit.target.kind(),
259-
suffix,
260-
file_type,
261-
);
262-
for (suffix, file_type, should_replace_hyphens) in suffixes {
263-
// wasm bin target will generate two files in deps such as
264-
// "web-stuff.js" and "web_stuff.wasm". Note the different usages of
265-
// "-" and "_". should_replace_hyphens is a flag to indicate that
266-
// we need to convert the stem "web-stuff" to "web_stuff", so we
267-
// won't miss "web_stuff.wasm".
268-
let conv = |s: String| {
269-
if should_replace_hyphens {
270-
s.replace("-", "_")
271-
} else {
272-
s
273-
}
274-
};
275-
let filename = out_dir.join(format!(
276-
"{}{}{}",
277-
prefix,
278-
conv(stem.clone()),
279-
suffix
280-
));
281-
let link_dst = link_stem.clone().map(|(ld, ls)| {
282-
ld.join(format!("{}{}{}", prefix, conv(ls), suffix))
283-
});
284-
ret.push((filename, link_dst, file_type));
285-
}
286-
Ok(())
287-
}
254+
let file_types = info.file_types(
255+
crate_type,
256+
flavor,
257+
unit.target.kind(),
258+
cx.target_triple(),
259+
)?;
260+
261+
match file_types {
262+
Some(types) => for file_type in types {
263+
let path = out_dir.join(file_type.filename(&file_stem));
264+
let hardlink = link_stem
265+
.as_ref()
266+
.map(|&(ref ld, ref ls)| ld.join(file_type.filename(ls)));
267+
ret.push(OutputFile {
268+
path,
269+
hardlink,
270+
flavor: file_type.flavor,
271+
});
272+
},
288273
// not supported, don't worry about it
289274
None => {
290275
unsupported.push(crate_type.to_string());
291-
Ok(())
292276
}
293277
}
278+
Ok(())
294279
};
295280
//info!("{:?}", unit);
296281
match *unit.target.kind() {
@@ -299,19 +284,19 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
299284
| TargetKind::ExampleBin
300285
| TargetKind::Bench
301286
| TargetKind::Test => {
302-
add("bin", TargetFileType::Normal)?;
287+
add("bin", FileFlavor::Normal)?;
303288
}
304289
TargetKind::Lib(..) | TargetKind::ExampleLib(..) if unit.profile.test => {
305-
add("bin", TargetFileType::Normal)?;
290+
add("bin", FileFlavor::Normal)?;
306291
}
307292
TargetKind::ExampleLib(ref kinds) | TargetKind::Lib(ref kinds) => {
308293
for kind in kinds {
309294
add(
310295
kind.crate_type(),
311296
if kind.linkable() {
312-
TargetFileType::Linkable
297+
FileFlavor::Linkable
313298
} else {
314-
TargetFileType::Normal
299+
FileFlavor::Normal
315300
},
316301
)?;
317302
}
@@ -450,45 +435,3 @@ fn compute_metadata<'a, 'cfg>(
450435
}
451436
Some(Metadata(hasher.finish()))
452437
}
453-
454-
// (not a rustdoc)
455-
// Return a list of 3-tuples (suffix, file_type, should_replace_hyphens).
456-
//
457-
// should_replace_hyphens will be used by the caller to replace "-" with "_"
458-
// in a bin_stem. See the caller side (calc_target_filenames()) for details.
459-
fn add_target_specific_suffixes(
460-
target_triple: &str,
461-
crate_type: &str,
462-
target_kind: &TargetKind,
463-
suffix: &str,
464-
file_type: TargetFileType,
465-
) -> Vec<(String, TargetFileType, bool)> {
466-
let mut ret = vec![(suffix.to_string(), file_type, false)];
467-
468-
// rust-lang/cargo#4500
469-
if target_triple.ends_with("pc-windows-msvc") && crate_type.ends_with("dylib")
470-
&& suffix == ".dll"
471-
{
472-
ret.push((".dll.lib".to_string(), TargetFileType::Normal, false));
473-
}
474-
475-
// rust-lang/cargo#4535
476-
if target_triple.starts_with("wasm32-") && crate_type == "bin" && suffix == ".js" {
477-
ret.push((".wasm".to_string(), TargetFileType::Normal, true));
478-
}
479-
480-
// rust-lang/cargo#4490, rust-lang/cargo#4960
481-
// - only uplift debuginfo for binaries.
482-
// tests are run directly from target/debug/deps/
483-
// and examples are inside target/debug/examples/ which already have symbols next to them
484-
// so no need to do anything.
485-
if *target_kind == TargetKind::Bin {
486-
if target_triple.contains("-apple-") {
487-
ret.push((".dSYM".to_string(), TargetFileType::DebugInfo, false));
488-
} else if target_triple.ends_with("-msvc") {
489-
ret.push((".pdb".to_string(), TargetFileType::DebugInfo, false));
490-
}
491-
}
492-
493-
ret
494-
}

0 commit comments

Comments
 (0)