diff options
author | Florian Diebold <[email protected]> | 2019-02-05 22:10:49 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-02-09 10:15:25 +0000 |
commit | fcd615e4b76264b4fff7b5be454787bb6a4252ea (patch) | |
tree | 112d17480bd065551a9d18c21ab51326f3e3a295 /crates/ra_lsp_server/src/project_model/sysroot.rs | |
parent | 34398a8756b56c323d3b4b2ef32fbca32d88a105 (diff) |
Extract project model to separate crate
Diffstat (limited to 'crates/ra_lsp_server/src/project_model/sysroot.rs')
-rw-r--r-- | crates/ra_lsp_server/src/project_model/sysroot.rs | 137 |
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 @@ | |||
1 | use std::{ | ||
2 | path::{Path, PathBuf}, | ||
3 | process::Command, | ||
4 | }; | ||
5 | |||
6 | use ra_syntax::SmolStr; | ||
7 | use ra_arena::{Arena, RawId, impl_arena_id}; | ||
8 | |||
9 | use crate::Result; | ||
10 | |||
11 | #[derive(Debug, Clone)] | ||
12 | pub struct Sysroot { | ||
13 | crates: Arena<SysrootCrate, SysrootCrateData>, | ||
14 | } | ||
15 | |||
16 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
17 | pub struct SysrootCrate(RawId); | ||
18 | impl_arena_id!(SysrootCrate); | ||
19 | |||
20 | #[derive(Debug, Clone)] | ||
21 | struct SysrootCrateData { | ||
22 | name: SmolStr, | ||
23 | root: PathBuf, | ||
24 | deps: Vec<SysrootCrate>, | ||
25 | } | ||
26 | |||
27 | impl 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 | |||
82 | impl 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 | |||
97 | const SYSROOT_CRATES: &str = " | ||
98 | std | ||
99 | core | ||
100 | alloc | ||
101 | collections | ||
102 | libc | ||
103 | panic_unwind | ||
104 | proc_macro | ||
105 | rustc_unicode | ||
106 | std_unicode | ||
107 | test | ||
108 | alloc_jemalloc | ||
109 | alloc_system | ||
110 | compiler_builtins | ||
111 | getopts | ||
112 | panic_unwind | ||
113 | panic_abort | ||
114 | rand | ||
115 | term | ||
116 | unwind | ||
117 | build_helper | ||
118 | rustc_asan | ||
119 | rustc_lsan | ||
120 | rustc_msan | ||
121 | rustc_tsan | ||
122 | syntax"; | ||
123 | |||
124 | const STD_DEPS: &str = " | ||
125 | alloc | ||
126 | alloc_jemalloc | ||
127 | alloc_system | ||
128 | core | ||
129 | panic_abort | ||
130 | rand | ||
131 | compiler_builtins | ||
132 | unwind | ||
133 | rustc_asan | ||
134 | rustc_lsan | ||
135 | rustc_msan | ||
136 | rustc_tsan | ||
137 | build_helper"; | ||