]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/project-model/src/sysroot.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / project-model / src / sysroot.rs
1 //! Loads "sysroot" crate.
2 //!
3 //! One confusing point here is that normally sysroot is a bunch of `.rlib`s,
4 //! but we can't process `.rlib` and need source code instead. The source code
5 //! is typically installed with `rustup component add rust-src` command.
6
7 use std::{env, fs, iter, ops, path::PathBuf, process::Command};
8
9 use anyhow::{format_err, Result};
10 use la_arena::{Arena, Idx};
11 use paths::{AbsPath, AbsPathBuf};
12 use rustc_hash::FxHashMap;
13
14 use crate::{utf8_stdout, ManifestPath};
15
16 #[derive(Debug, Clone, Eq, PartialEq)]
17 pub struct Sysroot {
18 root: AbsPathBuf,
19 src_root: AbsPathBuf,
20 crates: Arena<SysrootCrateData>,
21 }
22
23 pub(crate) type SysrootCrate = Idx<SysrootCrateData>;
24
25 #[derive(Debug, Clone, Eq, PartialEq)]
26 pub struct SysrootCrateData {
27 pub name: String,
28 pub root: ManifestPath,
29 pub deps: Vec<SysrootCrate>,
30 }
31
32 impl ops::Index<SysrootCrate> for Sysroot {
33 type Output = SysrootCrateData;
34 fn index(&self, index: SysrootCrate) -> &SysrootCrateData {
35 &self.crates[index]
36 }
37 }
38
39 impl Sysroot {
40 /// Returns sysroot "root" directory, where `bin/`, `etc/`, `lib/`, `libexec/`
41 /// subfolder live, like:
42 /// `$HOME/.rustup/toolchains/nightly-2022-07-23-x86_64-unknown-linux-gnu`
43 pub fn root(&self) -> &AbsPath {
44 &self.root
45 }
46
47 /// Returns the sysroot "source" directory, where stdlib sources are located, like:
48 /// `$HOME/.rustup/toolchains/nightly-2022-07-23-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library`
49 pub fn src_root(&self) -> &AbsPath {
50 &self.src_root
51 }
52
53 pub fn public_deps(&self) -> impl Iterator<Item = (&'static str, SysrootCrate, bool)> + '_ {
54 // core is added as a dependency before std in order to
55 // mimic rustcs dependency order
56 ["core", "alloc", "std"]
57 .into_iter()
58 .zip(iter::repeat(true))
59 .chain(iter::once(("test", false)))
60 .filter_map(move |(name, prelude)| Some((name, self.by_name(name)?, prelude)))
61 }
62
63 pub fn proc_macro(&self) -> Option<SysrootCrate> {
64 self.by_name("proc_macro")
65 }
66
67 pub fn crates(&self) -> impl Iterator<Item = SysrootCrate> + ExactSizeIterator + '_ {
68 self.crates.iter().map(|(id, _data)| id)
69 }
70 }
71
72 impl Sysroot {
73 /// Attempts to discover the toolchain's sysroot from the given `dir`.
74 pub fn discover(dir: &AbsPath, extra_env: &FxHashMap<String, String>) -> Result<Sysroot> {
75 tracing::debug!("discovering sysroot for {}", dir.display());
76 let sysroot_dir = discover_sysroot_dir(dir, extra_env)?;
77 let sysroot_src_dir =
78 discover_sysroot_src_dir_or_add_component(&sysroot_dir, dir, extra_env)?;
79 let res = Sysroot::load(sysroot_dir, sysroot_src_dir)?;
80 Ok(res)
81 }
82
83 pub fn discover_rustc(
84 cargo_toml: &ManifestPath,
85 extra_env: &FxHashMap<String, String>,
86 ) -> Option<ManifestPath> {
87 tracing::debug!("discovering rustc source for {}", cargo_toml.display());
88 let current_dir = cargo_toml.parent();
89 let sysroot_dir = discover_sysroot_dir(current_dir, extra_env).ok()?;
90 get_rustc_src(&sysroot_dir)
91 }
92
93 pub fn with_sysroot_dir(sysroot_dir: AbsPathBuf) -> Result<Sysroot> {
94 let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir).ok_or_else(|| {
95 format_err!("can't load standard library from sysroot {}", sysroot_dir.display())
96 })?;
97 let res = Sysroot::load(sysroot_dir, sysroot_src_dir)?;
98 Ok(res)
99 }
100
101 pub fn load(sysroot_dir: AbsPathBuf, sysroot_src_dir: AbsPathBuf) -> Result<Sysroot> {
102 let mut sysroot =
103 Sysroot { root: sysroot_dir, src_root: sysroot_src_dir, crates: Arena::default() };
104
105 for path in SYSROOT_CRATES.trim().lines() {
106 let name = path.split('/').last().unwrap();
107 let root = [format!("{}/src/lib.rs", path), format!("lib{}/lib.rs", path)]
108 .into_iter()
109 .map(|it| sysroot.src_root.join(it))
110 .filter_map(|it| ManifestPath::try_from(it).ok())
111 .find(|it| fs::metadata(it).is_ok());
112
113 if let Some(root) = root {
114 sysroot.crates.alloc(SysrootCrateData {
115 name: name.into(),
116 root,
117 deps: Vec::new(),
118 });
119 }
120 }
121
122 if let Some(std) = sysroot.by_name("std") {
123 for dep in STD_DEPS.trim().lines() {
124 if let Some(dep) = sysroot.by_name(dep) {
125 sysroot.crates[std].deps.push(dep)
126 }
127 }
128 }
129
130 if let Some(alloc) = sysroot.by_name("alloc") {
131 for dep in ALLOC_DEPS.trim().lines() {
132 if let Some(dep) = sysroot.by_name(dep) {
133 sysroot.crates[alloc].deps.push(dep)
134 }
135 }
136 }
137
138 if let Some(proc_macro) = sysroot.by_name("proc_macro") {
139 for dep in PROC_MACRO_DEPS.trim().lines() {
140 if let Some(dep) = sysroot.by_name(dep) {
141 sysroot.crates[proc_macro].deps.push(dep)
142 }
143 }
144 }
145
146 if sysroot.by_name("core").is_none() {
147 let var_note = if env::var_os("RUST_SRC_PATH").is_some() {
148 " (`RUST_SRC_PATH` might be incorrect, try unsetting it)"
149 } else {
150 ""
151 };
152 anyhow::bail!(
153 "could not find libcore in sysroot path `{}`{}",
154 sysroot.src_root.as_path().display(),
155 var_note,
156 );
157 }
158
159 Ok(sysroot)
160 }
161
162 fn by_name(&self, name: &str) -> Option<SysrootCrate> {
163 let (id, _data) = self.crates.iter().find(|(_id, data)| data.name == name)?;
164 Some(id)
165 }
166 }
167
168 fn discover_sysroot_dir(
169 current_dir: &AbsPath,
170 extra_env: &FxHashMap<String, String>,
171 ) -> Result<AbsPathBuf> {
172 let mut rustc = Command::new(toolchain::rustc());
173 rustc.envs(extra_env);
174 rustc.current_dir(current_dir).args(&["--print", "sysroot"]);
175 tracing::debug!("Discovering sysroot by {:?}", rustc);
176 let stdout = utf8_stdout(rustc)?;
177 Ok(AbsPathBuf::assert(PathBuf::from(stdout)))
178 }
179
180 fn discover_sysroot_src_dir(sysroot_path: &AbsPathBuf) -> Option<AbsPathBuf> {
181 if let Ok(path) = env::var("RUST_SRC_PATH") {
182 if let Ok(path) = AbsPathBuf::try_from(path.as_str()) {
183 let core = path.join("core");
184 if fs::metadata(&core).is_ok() {
185 tracing::debug!("Discovered sysroot by RUST_SRC_PATH: {}", path.display());
186 return Some(path);
187 }
188 tracing::debug!("RUST_SRC_PATH is set, but is invalid (no core: {:?}), ignoring", core);
189 } else {
190 tracing::debug!("RUST_SRC_PATH is set, but is invalid, ignoring");
191 }
192 }
193
194 get_rust_src(sysroot_path)
195 }
196
197 fn discover_sysroot_src_dir_or_add_component(
198 sysroot_path: &AbsPathBuf,
199 current_dir: &AbsPath,
200 extra_env: &FxHashMap<String, String>,
201 ) -> Result<AbsPathBuf> {
202 discover_sysroot_src_dir(sysroot_path)
203 .or_else(|| {
204 let mut rustup = Command::new(toolchain::rustup());
205 rustup.envs(extra_env);
206 rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]);
207 tracing::info!("adding rust-src component by {:?}", rustup);
208 utf8_stdout(rustup).ok()?;
209 get_rust_src(sysroot_path)
210 })
211 .ok_or_else(|| {
212 format_err!(
213 "\
214 can't load standard library from sysroot
215 {}
216 (discovered via `rustc --print sysroot`)
217 try installing the Rust source the same way you installed rustc",
218 sysroot_path.display(),
219 )
220 })
221 }
222
223 fn get_rustc_src(sysroot_path: &AbsPath) -> Option<ManifestPath> {
224 let rustc_src = sysroot_path.join("lib/rustlib/rustc-src/rust/compiler/rustc/Cargo.toml");
225 let rustc_src = ManifestPath::try_from(rustc_src).ok()?;
226 tracing::debug!("checking for rustc source code: {}", rustc_src.display());
227 if fs::metadata(&rustc_src).is_ok() {
228 Some(rustc_src)
229 } else {
230 None
231 }
232 }
233
234 fn get_rust_src(sysroot_path: &AbsPath) -> Option<AbsPathBuf> {
235 let rust_src = sysroot_path.join("lib/rustlib/src/rust/library");
236 tracing::debug!("checking sysroot library: {}", rust_src.display());
237 if fs::metadata(&rust_src).is_ok() {
238 Some(rust_src)
239 } else {
240 None
241 }
242 }
243
244 const SYSROOT_CRATES: &str = "
245 alloc
246 backtrace
247 core
248 panic_abort
249 panic_unwind
250 proc_macro
251 profiler_builtins
252 std
253 stdarch/crates/std_detect
254 test
255 unwind";
256
257 const ALLOC_DEPS: &str = "core";
258
259 const STD_DEPS: &str = "
260 alloc
261 panic_unwind
262 panic_abort
263 core
264 profiler_builtins
265 unwind
266 std_detect
267 test";
268
269 const PROC_MACRO_DEPS: &str = "std";