From e8923713c51bc3484bd98085ad620713959bbc0d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Jan 2019 20:13:08 +0300 Subject: add sysroot boilerplate --- crates/ra_lsp_server/src/project_model/cargo_workspace.rs | 0 crates/ra_lsp_server/src/project_model/sysroot.rs | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 crates/ra_lsp_server/src/project_model/cargo_workspace.rs create mode 100644 crates/ra_lsp_server/src/project_model/sysroot.rs (limited to 'crates/ra_lsp_server/src/project_model') diff --git a/crates/ra_lsp_server/src/project_model/cargo_workspace.rs b/crates/ra_lsp_server/src/project_model/cargo_workspace.rs new file mode 100644 index 000000000..e69de29bb diff --git a/crates/ra_lsp_server/src/project_model/sysroot.rs b/crates/ra_lsp_server/src/project_model/sysroot.rs new file mode 100644 index 000000000..e69de29bb -- cgit v1.2.3 From 66fba88534039ff42a230f1ede3e0a730f61ad3c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Jan 2019 22:21:14 +0300 Subject: split module --- .../src/project_model/cargo_workspace.rs | 173 +++++++++++++++++++++ crates/ra_lsp_server/src/project_model/sysroot.rs | 78 ++++++++++ 2 files changed, 251 insertions(+) (limited to 'crates/ra_lsp_server/src/project_model') diff --git a/crates/ra_lsp_server/src/project_model/cargo_workspace.rs b/crates/ra_lsp_server/src/project_model/cargo_workspace.rs index e69de29bb..138fdee22 100644 --- a/crates/ra_lsp_server/src/project_model/cargo_workspace.rs +++ b/crates/ra_lsp_server/src/project_model/cargo_workspace.rs @@ -0,0 +1,173 @@ +use std::{ + path::{Path, PathBuf}, +}; + +use cargo_metadata::{metadata_run, CargoOpt}; +use ra_syntax::SmolStr; +use ra_arena::{Arena, RawId, impl_arena_id}; +use rustc_hash::FxHashMap; +use failure::format_err; + +use crate::Result; + +/// `CargoWorksapce` represents the logical structure of, well, a Cargo +/// workspace. It pretty closely mirrors `cargo metadata` output. +/// +/// Note that internally, rust analyzer uses a differnet structure: +/// `CrateGraph`. `CrateGraph` is lower-level: it knows only about the crates, +/// while this knows about `Pacakges` & `Targets`: purely cargo-related +/// concepts. +#[derive(Debug, Clone)] +pub struct CargoWorkspace { + packages: Arena, + targets: Arena, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct Package(RawId); +impl_arena_id!(Package); + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct Target(RawId); +impl_arena_id!(Target); + +#[derive(Debug, Clone)] +struct PackageData { + name: SmolStr, + manifest: PathBuf, + targets: Vec, + is_member: bool, + dependencies: Vec, +} + +#[derive(Debug, Clone)] +pub struct PackageDependency { + pub pkg: Package, + pub name: SmolStr, +} + +#[derive(Debug, Clone)] +struct TargetData { + pkg: Package, + name: SmolStr, + root: PathBuf, + kind: TargetKind, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TargetKind { + Bin, + Lib, + Example, + Test, + Bench, + Other, +} + +impl TargetKind { + fn new(kinds: &[String]) -> TargetKind { + for kind in kinds { + return match kind.as_str() { + "bin" => TargetKind::Bin, + "test" => TargetKind::Test, + "bench" => TargetKind::Bench, + "example" => TargetKind::Example, + _ if kind.contains("lib") => TargetKind::Lib, + _ => continue, + }; + } + TargetKind::Other + } +} + +impl Package { + pub fn name(self, ws: &CargoWorkspace) -> &str { + ws.packages[self].name.as_str() + } + pub fn root(self, ws: &CargoWorkspace) -> &Path { + ws.packages[self].manifest.parent().unwrap() + } + pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator + 'a { + ws.packages[self].targets.iter().cloned() + } + #[allow(unused)] + pub fn is_member(self, ws: &CargoWorkspace) -> bool { + ws.packages[self].is_member + } + pub fn dependencies<'a>( + self, + ws: &'a CargoWorkspace, + ) -> impl Iterator + 'a { + ws.packages[self].dependencies.iter() + } +} + +impl Target { + pub fn package(self, ws: &CargoWorkspace) -> Package { + ws.targets[self].pkg + } + pub fn name(self, ws: &CargoWorkspace) -> &str { + ws.targets[self].name.as_str() + } + pub fn root(self, ws: &CargoWorkspace) -> &Path { + ws.targets[self].root.as_path() + } + pub fn kind(self, ws: &CargoWorkspace) -> TargetKind { + ws.targets[self].kind + } +} + +impl CargoWorkspace { + pub fn from_cargo_metadata(cargo_toml: &Path) -> Result { + let meta = metadata_run(Some(cargo_toml), true, Some(CargoOpt::AllFeatures)) + .map_err(|e| format_err!("cargo metadata failed: {}", e))?; + let mut pkg_by_id = FxHashMap::default(); + let mut packages = Arena::default(); + let mut targets = Arena::default(); + + let ws_members = &meta.workspace_members; + + for meta_pkg in meta.packages { + let is_member = ws_members.contains(&meta_pkg.id); + let pkg = packages.alloc(PackageData { + name: meta_pkg.name.into(), + manifest: meta_pkg.manifest_path.clone(), + targets: Vec::new(), + is_member, + dependencies: Vec::new(), + }); + let pkg_data = &mut packages[pkg]; + pkg_by_id.insert(meta_pkg.id.clone(), pkg); + for meta_tgt in meta_pkg.targets { + let tgt = targets.alloc(TargetData { + pkg, + name: meta_tgt.name.into(), + root: meta_tgt.src_path.clone(), + kind: TargetKind::new(meta_tgt.kind.as_slice()), + }); + pkg_data.targets.push(tgt); + } + } + let resolve = meta.resolve.expect("metadata executed with deps"); + for node in resolve.nodes { + let source = pkg_by_id[&node.id]; + for dep_node in node.deps { + let dep = PackageDependency { + name: dep_node.name.into(), + pkg: pkg_by_id[&dep_node.pkg], + }; + packages[source].dependencies.push(dep); + } + } + + Ok(CargoWorkspace { packages, targets }) + } + pub fn packages<'a>(&'a self) -> impl Iterator + 'a { + self.packages.iter().map(|(id, _pkg)| id) + } + pub fn target_by_root(&self, root: &Path) -> Option { + self.packages() + .filter_map(|pkg| pkg.targets(self).find(|it| it.root(self) == root)) + .next() + } +} diff --git a/crates/ra_lsp_server/src/project_model/sysroot.rs b/crates/ra_lsp_server/src/project_model/sysroot.rs index e69de29bb..ae72c9c17 100644 --- a/crates/ra_lsp_server/src/project_model/sysroot.rs +++ b/crates/ra_lsp_server/src/project_model/sysroot.rs @@ -0,0 +1,78 @@ +use std::{ + path::{Path, PathBuf}, + process::Command, +}; + +use ra_syntax::SmolStr; +use rustc_hash::FxHashMap; + +use crate::Result; + +#[derive(Debug, Clone)] +pub struct Sysroot { + crates: FxHashMap, +} + +impl Sysroot { + pub(crate) fn discover(cargo_toml: &Path) -> Result { + let rustc_output = Command::new("rustc") + .current_dir(cargo_toml.parent().unwrap()) + .args(&["--print", "sysroot"]) + .output()?; + if !rustc_output.status.success() { + failure::bail!("failed to locate sysroot") + } + let stdout = String::from_utf8(rustc_output.stdout)?; + let sysroot_path = Path::new(stdout.trim()); + let src = sysroot_path.join("lib/rustlib/src/rust/src"); + + let crates: &[(&str, &[&str])] = &[ + ( + "std", + &[ + "alloc_jemalloc", + "alloc_system", + "panic_abort", + "rand", + "compiler_builtins", + "unwind", + "rustc_asan", + "rustc_lsan", + "rustc_msan", + "rustc_tsan", + "build_helper", + ], + ), + ("core", &[]), + ("alloc", &[]), + ("collections", &[]), + ("libc", &[]), + ("panic_unwind", &[]), + ("proc_macro", &[]), + ("rustc_unicode", &[]), + ("std_unicode", &[]), + ("test", &[]), + // Feature gated + ("alloc_jemalloc", &[]), + ("alloc_system", &[]), + ("compiler_builtins", &[]), + ("getopts", &[]), + ("panic_unwind", &[]), + ("panic_abort", &[]), + ("rand", &[]), + ("term", &[]), + ("unwind", &[]), + // Dependencies + ("build_helper", &[]), + ("rustc_asan", &[]), + ("rustc_lsan", &[]), + ("rustc_msan", &[]), + ("rustc_tsan", &[]), + ("syntax", &[]), + ]; + + Ok(Sysroot { + crates: FxHashMap::default(), + }) + } +} -- cgit v1.2.3 From 8852408bfb358766d59b83f294148fb5eeae26a0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Jan 2019 22:47:05 +0300 Subject: use arena for sysroot --- crates/ra_lsp_server/src/project_model/sysroot.rs | 129 ++++++++++++++-------- 1 file changed, 80 insertions(+), 49 deletions(-) (limited to 'crates/ra_lsp_server/src/project_model') diff --git a/crates/ra_lsp_server/src/project_model/sysroot.rs b/crates/ra_lsp_server/src/project_model/sysroot.rs index ae72c9c17..6c1a1a2a3 100644 --- a/crates/ra_lsp_server/src/project_model/sysroot.rs +++ b/crates/ra_lsp_server/src/project_model/sysroot.rs @@ -4,13 +4,24 @@ use std::{ }; use ra_syntax::SmolStr; -use rustc_hash::FxHashMap; +use ra_arena::{Arena, RawId, impl_arena_id}; use crate::Result; #[derive(Debug, Clone)] pub struct Sysroot { - crates: FxHashMap, + crates: Arena, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct SysrootCrate(RawId); +impl_arena_id!(SysrootCrate); + +#[derive(Debug, Clone)] +struct SysrootCrateData { + name: SmolStr, + path: PathBuf, + deps: Vec, } impl Sysroot { @@ -26,53 +37,73 @@ impl Sysroot { let sysroot_path = Path::new(stdout.trim()); let src = sysroot_path.join("lib/rustlib/src/rust/src"); - let crates: &[(&str, &[&str])] = &[ - ( - "std", - &[ - "alloc_jemalloc", - "alloc_system", - "panic_abort", - "rand", - "compiler_builtins", - "unwind", - "rustc_asan", - "rustc_lsan", - "rustc_msan", - "rustc_tsan", - "build_helper", - ], - ), - ("core", &[]), - ("alloc", &[]), - ("collections", &[]), - ("libc", &[]), - ("panic_unwind", &[]), - ("proc_macro", &[]), - ("rustc_unicode", &[]), - ("std_unicode", &[]), - ("test", &[]), - // Feature gated - ("alloc_jemalloc", &[]), - ("alloc_system", &[]), - ("compiler_builtins", &[]), - ("getopts", &[]), - ("panic_unwind", &[]), - ("panic_abort", &[]), - ("rand", &[]), - ("term", &[]), - ("unwind", &[]), - // Dependencies - ("build_helper", &[]), - ("rustc_asan", &[]), - ("rustc_lsan", &[]), - ("rustc_msan", &[]), - ("rustc_tsan", &[]), - ("syntax", &[]), - ]; + let mut sysroot = Sysroot { + crates: Arena::default(), + }; + for name in SYSROOT_CRATES.trim().lines() { + let path = src.join(format!("lib{}", name)).join("lib.rs"); + if path.exists() { + sysroot.crates.alloc(SysrootCrateData { + name: name.into(), + path, + deps: Vec::new(), + }); + } + } + if let Some(std) = sysroot.by_name("std") { + for dep in STD_DEPS.trim().lines() { + if let Some(dep) = sysroot.by_name(dep) { + sysroot.crates[std].deps.push(dep) + } + } + } + Ok(sysroot) + } - Ok(Sysroot { - crates: FxHashMap::default(), - }) + fn by_name(&self, name: &str) -> Option { + self.crates + .iter() + .find(|(_id, data)| data.name == name) + .map(|(id, _data)| id) } } + +const SYSROOT_CRATES: &str = " +std +core +alloc +collections +libc +panic_unwind +proc_macro +rustc_unicode +std_unicode +test +alloc_jemalloc +alloc_system +compiler_builtins +getopts +panic_unwind +panic_abort +rand +term +unwind +build_helper +rustc_asan +rustc_lsan +rustc_msan +rustc_tsan +syntax"; + +const STD_DEPS: &str = " +alloc_jemalloc +alloc_system +panic_abort +rand +compiler_builtins +unwind +rustc_asan +rustc_lsan +rustc_msan +rustc_tsan +build_helper"; -- cgit v1.2.3 From e35374ec7c26be8de61ec7c6175c2385ee5c006f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Jan 2019 23:05:22 +0300 Subject: special case std --- crates/ra_lsp_server/src/project_model/sysroot.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'crates/ra_lsp_server/src/project_model') diff --git a/crates/ra_lsp_server/src/project_model/sysroot.rs b/crates/ra_lsp_server/src/project_model/sysroot.rs index 6c1a1a2a3..c4028a1fe 100644 --- a/crates/ra_lsp_server/src/project_model/sysroot.rs +++ b/crates/ra_lsp_server/src/project_model/sysroot.rs @@ -25,7 +25,11 @@ struct SysrootCrateData { } impl Sysroot { - pub(crate) fn discover(cargo_toml: &Path) -> Result { + pub(crate) fn std(&self) -> Option { + self.by_name("std") + } + + pub(super) fn discover(cargo_toml: &Path) -> Result { let rustc_output = Command::new("rustc") .current_dir(cargo_toml.parent().unwrap()) .args(&["--print", "sysroot"]) @@ -50,7 +54,7 @@ impl Sysroot { }); } } - if let Some(std) = sysroot.by_name("std") { + if let Some(std) = sysroot.std() { for dep in STD_DEPS.trim().lines() { if let Some(dep) = sysroot.by_name(dep) { sysroot.crates[std].deps.push(dep) -- cgit v1.2.3 From cd00158b1db9dd8565d2db08b4b0ebab9d5c00b3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 11 Jan 2019 00:37:10 +0300 Subject: wire sysroot into crate graph --- crates/ra_lsp_server/src/project_model/sysroot.rs | 27 +++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'crates/ra_lsp_server/src/project_model') diff --git a/crates/ra_lsp_server/src/project_model/sysroot.rs b/crates/ra_lsp_server/src/project_model/sysroot.rs index c4028a1fe..1dbab57f8 100644 --- a/crates/ra_lsp_server/src/project_model/sysroot.rs +++ b/crates/ra_lsp_server/src/project_model/sysroot.rs @@ -20,7 +20,7 @@ impl_arena_id!(SysrootCrate); #[derive(Debug, Clone)] struct SysrootCrateData { name: SmolStr, - path: PathBuf, + root: PathBuf, deps: Vec, } @@ -29,6 +29,10 @@ impl Sysroot { self.by_name("std") } + pub(crate) fn crates<'a>(&'a self) -> impl Iterator + 'a { + self.crates.iter().map(|(id, _data)| id) + } + pub(super) fn discover(cargo_toml: &Path) -> Result { let rustc_output = Command::new("rustc") .current_dir(cargo_toml.parent().unwrap()) @@ -45,11 +49,11 @@ impl Sysroot { crates: Arena::default(), }; for name in SYSROOT_CRATES.trim().lines() { - let path = src.join(format!("lib{}", name)).join("lib.rs"); - if path.exists() { + let root = src.join(format!("lib{}", name)).join("lib.rs"); + if root.exists() { sysroot.crates.alloc(SysrootCrateData { name: name.into(), - path, + root, deps: Vec::new(), }); } @@ -72,6 +76,21 @@ impl Sysroot { } } +impl SysrootCrate { + pub(crate) fn name(self, sysroot: &Sysroot) -> &SmolStr { + &sysroot.crates[self].name + } + pub(crate) fn root(self, sysroot: &Sysroot) -> &Path { + sysroot.crates[self].root.as_path() + } + pub(crate) fn root_dir(self, sysroot: &Sysroot) -> &Path { + self.root(sysroot).parent().unwrap() + } + pub(crate) fn deps<'a>(self, sysroot: &'a Sysroot) -> impl Iterator + 'a { + sysroot.crates[self].deps.iter().map(|&it| it) + } +} + const SYSROOT_CRATES: &str = " std core -- cgit v1.2.3 From 4bf6b91b9dea117647a4120f404a2f0067041035 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 11 Jan 2019 00:40:35 +0300 Subject: minor --- crates/ra_lsp_server/src/project_model/cargo_workspace.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'crates/ra_lsp_server/src/project_model') diff --git a/crates/ra_lsp_server/src/project_model/cargo_workspace.rs b/crates/ra_lsp_server/src/project_model/cargo_workspace.rs index 138fdee22..8f7518860 100644 --- a/crates/ra_lsp_server/src/project_model/cargo_workspace.rs +++ b/crates/ra_lsp_server/src/project_model/cargo_workspace.rs @@ -1,6 +1,4 @@ -use std::{ - path::{Path, PathBuf}, -}; +use std::path::{Path, PathBuf}; use cargo_metadata::{metadata_run, CargoOpt}; use ra_syntax::SmolStr; -- cgit v1.2.3