Skip to content

Commit 42e9412

Browse files
committed
Move Links into context module
1 parent 509c5b4 commit 42e9412

File tree

3 files changed

+68
-75
lines changed

3 files changed

+68
-75
lines changed

src/cargo/core/compiler/context/mod.rs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#![allow(deprecated)]
22
use std::collections::{HashMap, HashSet};
3+
use std::fmt::Write;
34
use std::path::PathBuf;
45
use std::sync::Arc;
56

67
use jobserver::Client;
78

8-
use core::{Package, PackageId, Target};
9+
use core::{Package, PackageId, Resolve, Target};
910
use core::profiles::Profile;
1011
use ops::CompileMode;
1112
use util::errors::{CargoResult, CargoResultExt};
@@ -15,7 +16,6 @@ use super::custom_build::{self, BuildDeps, BuildScripts, BuildState};
1516
use super::fingerprint::Fingerprint;
1617
use super::job_queue::JobQueue;
1718
use super::layout::Layout;
18-
use super::links::Links;
1919
use super::{BuildContext, Compilation, Executor, FileFlavor, Kind};
2020

2121
mod unit_dependencies;
@@ -430,3 +430,69 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
430430
Ok(vec!["-C".to_string(), format!("incremental={}", dir)])
431431
}
432432
}
433+
434+
#[derive(Default)]
435+
pub struct Links<'a> {
436+
validated: HashSet<&'a PackageId>,
437+
links: HashMap<String, &'a PackageId>,
438+
}
439+
440+
impl<'a> Links<'a> {
441+
pub fn new() -> Links<'a> {
442+
Links {
443+
validated: HashSet::new(),
444+
links: HashMap::new(),
445+
}
446+
}
447+
448+
pub fn validate(&mut self, resolve: &Resolve, unit: &Unit<'a>) -> CargoResult<()> {
449+
if !self.validated.insert(unit.pkg.package_id()) {
450+
return Ok(());
451+
}
452+
let lib = match unit.pkg.manifest().links() {
453+
Some(lib) => lib,
454+
None => return Ok(()),
455+
};
456+
if let Some(prev) = self.links.get(lib) {
457+
let pkg = unit.pkg.package_id();
458+
459+
let describe_path = |pkgid: &PackageId| -> String {
460+
let dep_path = resolve.path_to_top(pkgid);
461+
let mut dep_path_desc = format!("package `{}`", dep_path[0]);
462+
for dep in dep_path.iter().skip(1) {
463+
write!(dep_path_desc, "\n ... which is depended on by `{}`", dep).unwrap();
464+
}
465+
dep_path_desc
466+
};
467+
468+
bail!(
469+
"multiple packages link to native library `{}`, \
470+
but a native library can be linked only once\n\
471+
\n\
472+
{}\nlinks to native library `{}`\n\
473+
\n\
474+
{}\nalso links to native library `{}`",
475+
lib,
476+
describe_path(prev),
477+
lib,
478+
describe_path(pkg),
479+
lib
480+
)
481+
}
482+
if !unit.pkg
483+
.manifest()
484+
.targets()
485+
.iter()
486+
.any(|t| t.is_custom_build())
487+
{
488+
bail!(
489+
"package `{}` specifies that it links to `{}` but does not \
490+
have a custom build script",
491+
unit.pkg.package_id(),
492+
lib
493+
)
494+
}
495+
self.links.insert(lib.to_string(), unit.pkg.package_id());
496+
Ok(())
497+
}
498+
}

src/cargo/core/compiler/links.rs

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/cargo/core/compiler/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ mod fingerprint;
3737
mod job;
3838
mod job_queue;
3939
mod layout;
40-
mod links;
4140
mod output_depinfo;
4241

4342
/// Whether an object is for the host arch, or the target arch.

0 commit comments

Comments
 (0)