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/sysroot.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 crates/ra_lsp_server/src/project_model/sysroot.rs (limited to 'crates/ra_lsp_server/src/project_model/sysroot.rs') 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 --- crates/ra_lsp_server/src/project_model/sysroot.rs | 78 +++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'crates/ra_lsp_server/src/project_model/sysroot.rs') 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/sysroot.rs') 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/sysroot.rs') 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/sysroot.rs') 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