aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/project_model/sysroot.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server/src/project_model/sysroot.rs')
-rw-r--r--crates/ra_lsp_server/src/project_model/sysroot.rs137
1 files changed, 0 insertions, 137 deletions
diff --git a/crates/ra_lsp_server/src/project_model/sysroot.rs b/crates/ra_lsp_server/src/project_model/sysroot.rs
deleted file mode 100644
index 49210ac7a..000000000
--- a/crates/ra_lsp_server/src/project_model/sysroot.rs
+++ /dev/null
@@ -1,137 +0,0 @@
1use std::{
2 path::{Path, PathBuf},
3 process::Command,
4};
5
6use ra_syntax::SmolStr;
7use ra_arena::{Arena, RawId, impl_arena_id};
8
9use crate::Result;
10
11#[derive(Debug, Clone)]
12pub struct Sysroot {
13 crates: Arena<SysrootCrate, SysrootCrateData>,
14}
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub struct SysrootCrate(RawId);
18impl_arena_id!(SysrootCrate);
19
20#[derive(Debug, Clone)]
21struct SysrootCrateData {
22 name: SmolStr,
23 root: PathBuf,
24 deps: Vec<SysrootCrate>,
25}
26
27impl Sysroot {
28 pub(crate) fn std(&self) -> Option<SysrootCrate> {
29 self.by_name("std")
30 }
31
32 pub(crate) fn crates<'a>(&'a self) -> impl Iterator<Item = SysrootCrate> + 'a {
33 self.crates.iter().map(|(id, _data)| id)
34 }
35
36 pub(super) fn discover(cargo_toml: &Path) -> Result<Sysroot> {
37 let rustc_output = Command::new("rustc")
38 .current_dir(cargo_toml.parent().unwrap())
39 .args(&["--print", "sysroot"])
40 .output()?;
41 if !rustc_output.status.success() {
42 failure::bail!("failed to locate sysroot")
43 }
44 let stdout = String::from_utf8(rustc_output.stdout)?;
45 let sysroot_path = Path::new(stdout.trim());
46 let src = sysroot_path.join("lib/rustlib/src/rust/src");
47 if !src.exists() {
48 failure::bail!(
49 "can't load standard library from sysroot\n\
50 {:?}\n\
51 try running `rustup component add rust-src`",
52 src,
53 );
54 }
55
56 let mut sysroot = Sysroot { crates: Arena::default() };
57 for name in SYSROOT_CRATES.trim().lines() {
58 let root = src.join(format!("lib{}", name)).join("lib.rs");
59 if root.exists() {
60 sysroot.crates.alloc(SysrootCrateData {
61 name: name.into(),
62 root,
63 deps: Vec::new(),
64 });
65 }
66 }
67 if let Some(std) = sysroot.std() {
68 for dep in STD_DEPS.trim().lines() {
69 if let Some(dep) = sysroot.by_name(dep) {
70 sysroot.crates[std].deps.push(dep)
71 }
72 }
73 }
74 Ok(sysroot)
75 }
76
77 fn by_name(&self, name: &str) -> Option<SysrootCrate> {
78 self.crates.iter().find(|(_id, data)| data.name == name).map(|(id, _data)| id)
79 }
80}
81
82impl SysrootCrate {
83 pub(crate) fn name(self, sysroot: &Sysroot) -> &SmolStr {
84 &sysroot.crates[self].name
85 }
86 pub(crate) fn root(self, sysroot: &Sysroot) -> &Path {
87 sysroot.crates[self].root.as_path()
88 }
89 pub(crate) fn root_dir(self, sysroot: &Sysroot) -> &Path {
90 self.root(sysroot).parent().unwrap()
91 }
92 pub(crate) fn deps<'a>(self, sysroot: &'a Sysroot) -> impl Iterator<Item = SysrootCrate> + 'a {
93 sysroot.crates[self].deps.iter().map(|&it| it)
94 }
95}
96
97const SYSROOT_CRATES: &str = "
98std
99core
100alloc
101collections
102libc
103panic_unwind
104proc_macro
105rustc_unicode
106std_unicode
107test
108alloc_jemalloc
109alloc_system
110compiler_builtins
111getopts
112panic_unwind
113panic_abort
114rand
115term
116unwind
117build_helper
118rustc_asan
119rustc_lsan
120rustc_msan
121rustc_tsan
122syntax";
123
124const STD_DEPS: &str = "
125alloc
126alloc_jemalloc
127alloc_system
128core
129panic_abort
130rand
131compiler_builtins
132unwind
133rustc_asan
134rustc_lsan
135rustc_msan
136rustc_tsan
137build_helper";