1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use super::unit_dependencies::UnitGraph;
use crate::core::{PackageId, Resolve};
use crate::util::errors::CargoResult;
use std::collections::{HashMap, HashSet};
use std::fmt::Write;
pub fn validate_links(resolve: &Resolve, unit_graph: &UnitGraph<'_>) -> CargoResult<()> {
let mut validated: HashSet<PackageId> = HashSet::new();
let mut links: HashMap<String, PackageId> = HashMap::new();
let mut units: Vec<_> = unit_graph.keys().collect();
units.sort_unstable();
for unit in units {
if !validated.insert(unit.pkg.package_id()) {
continue;
}
let lib = match unit.pkg.manifest().links() {
Some(lib) => lib,
None => continue,
};
if let Some(&prev) = links.get(lib) {
let pkg = unit.pkg.package_id();
let describe_path = |pkgid: PackageId| -> String {
let dep_path = resolve.path_to_top(&pkgid);
let mut dep_path_desc = format!("package `{}`", dep_path[0]);
for dep in dep_path.iter().skip(1) {
write!(dep_path_desc, "\n ... which is depended on by `{}`", dep).unwrap();
}
dep_path_desc
};
failure::bail!(
"multiple packages link to native library `{}`, \
but a native library can be linked only once\n\
\n\
{}\nlinks to native library `{}`\n\
\n\
{}\nalso links to native library `{}`",
lib,
describe_path(prev),
lib,
describe_path(pkg),
lib
)
}
links.insert(lib.to_string(), unit.pkg.package_id());
}
Ok(())
}