diff options
Diffstat (limited to 'crates/ra_project_model/src/sysroot.rs')
-rw-r--r-- | crates/ra_project_model/src/sysroot.rs | 102 |
1 files changed, 56 insertions, 46 deletions
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index 943ff92df..a10ade375 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs | |||
@@ -3,19 +3,19 @@ | |||
3 | use std::{convert::TryFrom, env, ops, path::Path, process::Command}; | 3 | use std::{convert::TryFrom, env, ops, path::Path, process::Command}; |
4 | 4 | ||
5 | use anyhow::{bail, format_err, Result}; | 5 | use anyhow::{bail, format_err, Result}; |
6 | use paths::{AbsPath, AbsPathBuf}; | ||
6 | use ra_arena::{Arena, Idx}; | 7 | use ra_arena::{Arena, Idx}; |
7 | 8 | ||
8 | use crate::output; | 9 | use crate::utf8_stdout; |
9 | use paths::{AbsPath, AbsPathBuf}; | ||
10 | 10 | ||
11 | #[derive(Default, Debug, Clone)] | 11 | #[derive(Default, Debug, Clone, Eq, PartialEq)] |
12 | pub struct Sysroot { | 12 | pub struct Sysroot { |
13 | crates: Arena<SysrootCrateData>, | 13 | crates: Arena<SysrootCrateData>, |
14 | } | 14 | } |
15 | 15 | ||
16 | pub type SysrootCrate = Idx<SysrootCrateData>; | 16 | pub type SysrootCrate = Idx<SysrootCrateData>; |
17 | 17 | ||
18 | #[derive(Debug, Clone)] | 18 | #[derive(Debug, Clone, Eq, PartialEq)] |
19 | pub struct SysrootCrateData { | 19 | pub struct SysrootCrateData { |
20 | pub name: String, | 20 | pub name: String, |
21 | pub root: AbsPathBuf, | 21 | pub root: AbsPathBuf, |
@@ -54,6 +54,8 @@ impl Sysroot { | |||
54 | let src = get_or_install_rust_src(cargo_toml)?; | 54 | let src = get_or_install_rust_src(cargo_toml)?; |
55 | let mut sysroot = Sysroot { crates: Arena::default() }; | 55 | let mut sysroot = Sysroot { crates: Arena::default() }; |
56 | for name in SYSROOT_CRATES.trim().lines() { | 56 | for name in SYSROOT_CRATES.trim().lines() { |
57 | // FIXME: remove this path when 1.47 comes out | ||
58 | // https://github.com/rust-lang/rust/pull/73265 | ||
57 | let root = src.join(format!("lib{}", name)).join("lib.rs"); | 59 | let root = src.join(format!("lib{}", name)).join("lib.rs"); |
58 | if root.exists() { | 60 | if root.exists() { |
59 | sysroot.crates.alloc(SysrootCrateData { | 61 | sysroot.crates.alloc(SysrootCrateData { |
@@ -61,6 +63,15 @@ impl Sysroot { | |||
61 | root, | 63 | root, |
62 | deps: Vec::new(), | 64 | deps: Vec::new(), |
63 | }); | 65 | }); |
66 | } else { | ||
67 | let root = src.join(name).join("src/lib.rs"); | ||
68 | if root.exists() { | ||
69 | sysroot.crates.alloc(SysrootCrateData { | ||
70 | name: name.into(), | ||
71 | root, | ||
72 | deps: Vec::new(), | ||
73 | }); | ||
74 | } | ||
64 | } | 75 | } |
65 | } | 76 | } |
66 | if let Some(std) = sysroot.std() { | 77 | if let Some(std) = sysroot.std() { |
@@ -92,26 +103,40 @@ fn get_or_install_rust_src(cargo_toml: &AbsPath) -> Result<AbsPathBuf> { | |||
92 | let current_dir = cargo_toml.parent().unwrap(); | 103 | let current_dir = cargo_toml.parent().unwrap(); |
93 | let mut rustc = Command::new(ra_toolchain::rustc()); | 104 | let mut rustc = Command::new(ra_toolchain::rustc()); |
94 | rustc.current_dir(current_dir).args(&["--print", "sysroot"]); | 105 | rustc.current_dir(current_dir).args(&["--print", "sysroot"]); |
95 | let rustc_output = output(rustc)?; | 106 | let stdout = utf8_stdout(rustc)?; |
96 | let stdout = String::from_utf8(rustc_output.stdout)?; | ||
97 | let sysroot_path = AbsPath::assert(Path::new(stdout.trim())); | 107 | let sysroot_path = AbsPath::assert(Path::new(stdout.trim())); |
98 | let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); | 108 | let mut src = get_rust_src(sysroot_path); |
99 | 109 | if src.is_none() { | |
100 | if !src_path.exists() { | ||
101 | let mut rustup = Command::new(ra_toolchain::rustup()); | 110 | let mut rustup = Command::new(ra_toolchain::rustup()); |
102 | rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]); | 111 | rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]); |
103 | let _output = output(rustup)?; | 112 | utf8_stdout(rustup)?; |
113 | src = get_rust_src(sysroot_path); | ||
104 | } | 114 | } |
105 | if !src_path.exists() { | 115 | match src { |
106 | bail!( | 116 | Some(r) => Ok(r), |
117 | None => bail!( | ||
107 | "can't load standard library from sysroot\n\ | 118 | "can't load standard library from sysroot\n\ |
108 | {}\n\ | 119 | {}\n\ |
109 | (discovered via `rustc --print sysroot`)\n\ | 120 | (discovered via `rustc --print sysroot`)\n\ |
110 | try running `rustup component add rust-src` or set `RUST_SRC_PATH`", | 121 | try running `rustup component add rust-src` or set `RUST_SRC_PATH`", |
111 | src_path.display(), | 122 | sysroot_path.display(), |
112 | ) | 123 | ), |
124 | } | ||
125 | } | ||
126 | |||
127 | fn get_rust_src(sysroot_path: &AbsPath) -> Option<AbsPathBuf> { | ||
128 | // try the new path first since the old one still exists | ||
129 | let mut src_path = sysroot_path.join("lib/rustlib/src/rust/library"); | ||
130 | if !src_path.exists() { | ||
131 | // FIXME: remove this path when 1.47 comes out | ||
132 | // https://github.com/rust-lang/rust/pull/73265 | ||
133 | src_path = sysroot_path.join("lib/rustlib/src/rust/src"); | ||
134 | } | ||
135 | if src_path.exists() { | ||
136 | Some(src_path) | ||
137 | } else { | ||
138 | None | ||
113 | } | 139 | } |
114 | Ok(src_path) | ||
115 | } | 140 | } |
116 | 141 | ||
117 | impl SysrootCrateData { | 142 | impl SysrootCrateData { |
@@ -121,43 +146,28 @@ impl SysrootCrateData { | |||
121 | } | 146 | } |
122 | 147 | ||
123 | const SYSROOT_CRATES: &str = " | 148 | const SYSROOT_CRATES: &str = " |
124 | std | ||
125 | core | ||
126 | alloc | 149 | alloc |
127 | collections | 150 | core |
128 | libc | 151 | panic_abort |
129 | panic_unwind | 152 | panic_unwind |
130 | proc_macro | 153 | proc_macro |
131 | rustc_unicode | 154 | profiler_builtins |
132 | std_unicode | 155 | rtstartup |
133 | test | 156 | std |
134 | alloc_jemalloc | 157 | stdarch |
135 | alloc_system | ||
136 | compiler_builtins | ||
137 | getopts | ||
138 | panic_unwind | ||
139 | panic_abort | ||
140 | rand | ||
141 | term | 158 | term |
142 | unwind | 159 | test |
143 | build_helper | 160 | unwind"; |
144 | rustc_asan | ||
145 | rustc_lsan | ||
146 | rustc_msan | ||
147 | rustc_tsan | ||
148 | syntax"; | ||
149 | 161 | ||
150 | const STD_DEPS: &str = " | 162 | const STD_DEPS: &str = " |
151 | alloc | 163 | alloc |
152 | alloc_jemalloc | ||
153 | alloc_system | ||
154 | core | 164 | core |
155 | panic_abort | 165 | panic_abort |
156 | rand | 166 | panic_unwind |
157 | compiler_builtins | 167 | profiler_builtins |
158 | unwind | 168 | rtstartup |
159 | rustc_asan | 169 | proc_macro |
160 | rustc_lsan | 170 | stdarch |
161 | rustc_msan | 171 | term |
162 | rustc_tsan | 172 | test |
163 | build_helper"; | 173 | unwind"; |