Update crate graph to only use subcrates of rustc_driver

This commit is contained in:
Daniel McNab 2021-03-07 10:18:01 +00:00
parent 7513867aa2
commit b46605cfcd

View file

@ -2,11 +2,7 @@
//! metadata` or `rust-project.json`) into representation stored in the salsa //! metadata` or `rust-project.json`) into representation stored in the salsa
//! database -- `CrateGraph`. //! database -- `CrateGraph`.
use std::{ use std::{collections::VecDeque, fmt, fs, path::Path, process::Command};
fmt, fs,
path::{Component, Path},
process::Command,
};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId, ProcMacro}; use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId, ProcMacro};
@ -446,37 +442,40 @@ fn cargo_to_crate_graph(
let mut rustc_pkg_crates = FxHashMap::default(); let mut rustc_pkg_crates = FxHashMap::default();
// If the user provided a path to rustc sources, we add all the rustc_private crates // If the user provided a path to rustc sources, we add all the rustc_private crates
// and create dependencies on them for the crates in the current workspace // and create dependencies on them for the crates which opt-in to that
if let Some(rustc_workspace) = rustc { if let Some(rustc_workspace) = rustc {
for pkg in rustc_workspace.packages() { // rustc-dev crates start from 'rustc_driver'
for &tgt in rustc_workspace[pkg].targets.iter() { // Therefore, we collect all crates which are transitive dependencies of rustc_driver
if rustc_workspace[tgt].kind != TargetKind::Lib { if let Some(root_pkg) = rustc_workspace
continue; .packages()
.find(|package| rustc_workspace[*package].name == "rustc_driver")
{
let mut queue = VecDeque::new();
queue.push_back(root_pkg);
while let Some(pkg) = queue.pop_front() {
for dep in &rustc_workspace[pkg].dependencies {
queue.push_back(dep.pkg);
} }
// Exclude alloc / core / std for &tgt in rustc_workspace[pkg].targets.iter() {
if rustc_workspace[tgt] if rustc_workspace[tgt].kind != TargetKind::Lib {
.root continue;
.components() }
.any(|c| c == Component::Normal("library".as_ref())) if let Some(file_id) = load(&rustc_workspace[tgt].root) {
{ let crate_id = add_target_crate_root(
continue; &mut crate_graph,
} &rustc_workspace[pkg],
rustc_build_data_map.and_then(|it| it.get(&rustc_workspace[pkg].id)),
if let Some(file_id) = load(&rustc_workspace[tgt].root) { &cfg_options,
let crate_id = add_target_crate_root( proc_macro_loader,
&mut crate_graph, file_id,
&rustc_workspace[pkg], );
rustc_build_data_map.and_then(|it| it.get(&rustc_workspace[pkg].id)), pkg_to_lib_crate.insert(pkg, crate_id);
&cfg_options, // Add dependencies on the core / std / alloc for rustc
proc_macro_loader, for (name, krate) in public_deps.iter() {
file_id, add_dep(&mut crate_graph, crate_id, name.clone(), *krate);
); }
pkg_to_lib_crate.insert(pkg, crate_id); rustc_pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
// Add dependencies on the core / std / alloc for rustc
for (name, krate) in public_deps.iter() {
add_dep(&mut crate_graph, crate_id, name.clone(), *krate);
} }
rustc_pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
} }
} }
} }
@ -493,7 +492,7 @@ fn cargo_to_crate_graph(
} }
} }
// Add dependencies for all the crates of the current workspace to rustc_private libraries // Add dependencies for all crates which opt in to rustc_private libraries
for dep in rustc_workspace.packages() { for dep in rustc_workspace.packages() {
let name = CrateName::normalize_dashes(&rustc_workspace[dep].name); let name = CrateName::normalize_dashes(&rustc_workspace[dep].name);
@ -507,13 +506,14 @@ fn cargo_to_crate_graph(
continue; continue;
} }
for &from in pkg_crates.get(&pkg).into_iter().flatten() { for &from in pkg_crates.get(&pkg).into_iter().flatten() {
// Avoid creating duplicate dependencies
if !crate_graph[from].dependencies.iter().any(|d| d.name == name) { if !crate_graph[from].dependencies.iter().any(|d| d.name == name) {
add_dep(&mut crate_graph, from, name.clone(), to); add_dep(&mut crate_graph, from, name.clone(), to);
} else { } else {
// eprintln!( eprintln!(
// "Skipped {} for {:?}", "Skipped {} for {:?}",
// &name, &crate_graph[from].display_name &name, &crate_graph[from].display_name
// ); );
} }
} }
} }