mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Move crate graph generation to ra_project_model
This commit is contained in:
parent
fcd615e4b7
commit
ddbf43b630
5 changed files with 116 additions and 83 deletions
|
@ -9,13 +9,12 @@ use ra_ide_api::{
|
|||
SourceRootId
|
||||
};
|
||||
use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot};
|
||||
use rustc_hash::FxHashMap;
|
||||
use relative_path::RelativePathBuf;
|
||||
use parking_lot::RwLock;
|
||||
use failure::format_err;
|
||||
|
||||
use crate::{
|
||||
project_model::{ProjectWorkspace, TargetKind},
|
||||
project_model::ProjectWorkspace,
|
||||
Result,
|
||||
};
|
||||
|
||||
|
@ -57,88 +56,10 @@ impl ServerWorldState {
|
|||
change.add_root(SourceRootId(r.0.into()), is_local);
|
||||
}
|
||||
|
||||
// Create crate graph from all the workspaces
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
for ws in workspaces.iter() {
|
||||
// First, load std
|
||||
let mut sysroot_crates = FxHashMap::default();
|
||||
for krate in ws.sysroot.crates() {
|
||||
if let Some(file_id) = vfs.load(krate.root(&ws.sysroot)) {
|
||||
let file_id = FileId(file_id.0.into());
|
||||
sysroot_crates.insert(krate, crate_graph.add_crate_root(file_id));
|
||||
}
|
||||
}
|
||||
for from in ws.sysroot.crates() {
|
||||
for to in from.deps(&ws.sysroot) {
|
||||
let name = to.name(&ws.sysroot);
|
||||
if let (Some(&from), Some(&to)) =
|
||||
(sysroot_crates.get(&from), sysroot_crates.get(&to))
|
||||
{
|
||||
if let Err(_) = crate_graph.add_dep(from, name.clone(), to) {
|
||||
log::error!("cyclic dependency between sysroot crates")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let libstd = ws.sysroot.std().and_then(|it| sysroot_crates.get(&it).map(|&it| it));
|
||||
|
||||
let mut pkg_to_lib_crate = FxHashMap::default();
|
||||
let mut pkg_crates = FxHashMap::default();
|
||||
// Next, create crates for each package, target pair
|
||||
for pkg in ws.cargo.packages() {
|
||||
let mut lib_tgt = None;
|
||||
for tgt in pkg.targets(&ws.cargo) {
|
||||
let root = tgt.root(&ws.cargo);
|
||||
if let Some(file_id) = vfs.load(root) {
|
||||
let file_id = FileId(file_id.0.into());
|
||||
let crate_id = crate_graph.add_crate_root(file_id);
|
||||
if tgt.kind(&ws.cargo) == TargetKind::Lib {
|
||||
lib_tgt = Some(crate_id);
|
||||
pkg_to_lib_crate.insert(pkg, crate_id);
|
||||
}
|
||||
pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Set deps to the std and to the lib target of the current package
|
||||
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
|
||||
if let Some(to) = lib_tgt {
|
||||
if to != from {
|
||||
if let Err(_) =
|
||||
crate_graph.add_dep(from, pkg.name(&ws.cargo).into(), to)
|
||||
{
|
||||
log::error!(
|
||||
"cyclic dependency between targets of {}",
|
||||
pkg.name(&ws.cargo)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(std) = libstd {
|
||||
if let Err(_) = crate_graph.add_dep(from, "std".into(), std) {
|
||||
log::error!("cyclic dependency on std for {}", pkg.name(&ws.cargo))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now add a dep ednge from all targets of upstream to the lib
|
||||
// target of downstream.
|
||||
for pkg in ws.cargo.packages() {
|
||||
for dep in pkg.dependencies(&ws.cargo) {
|
||||
if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
|
||||
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
|
||||
if let Err(_) = crate_graph.add_dep(from, dep.name.clone(), to) {
|
||||
log::error!(
|
||||
"cyclic dependency {} -> {}",
|
||||
pkg.name(&ws.cargo),
|
||||
dep.pkg.name(&ws.cargo)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
crate_graph.extend(ws.to_crate_graph(&mut vfs));
|
||||
}
|
||||
change.set_crate_graph(crate_graph);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue