mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-31 15:47:31 +00:00
Prep work for overriding sysroot
The idea here is to make auto-discovery optional, and to allow to set the path to sysroot directly. This is handy for JSON projects.
This commit is contained in:
parent
81a3404d8f
commit
6b8bc132dc
2 changed files with 61 additions and 54 deletions
|
@ -561,5 +561,5 @@ fn utf8_stdout(mut cmd: Command) -> Result<String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let stdout = String::from_utf8(output.stdout)?;
|
let stdout = String::from_utf8(output.stdout)?;
|
||||||
Ok(stdout)
|
Ok(stdout.trim().to_string())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
//! FIXME: write short doc here
|
//! Loads "sysroot" crate.
|
||||||
|
//!
|
||||||
|
//! One confusing point here is that normally sysroot is a bunch of `.rlib`s,
|
||||||
|
//! but we can't process `.rlib` and need source code instead. The source code
|
||||||
|
//! is typically installed with `rustup component add rust-src` command.
|
||||||
|
|
||||||
use std::{convert::TryFrom, env, ops, path::Path, process::Command};
|
use std::{convert::TryFrom, env, ops, path::PathBuf, process::Command};
|
||||||
|
|
||||||
use anyhow::{bail, format_err, Result};
|
use anyhow::{format_err, Result};
|
||||||
use arena::{Arena, Idx};
|
use arena::{Arena, Idx};
|
||||||
use paths::{AbsPath, AbsPathBuf};
|
use paths::{AbsPath, AbsPathBuf};
|
||||||
|
|
||||||
|
@ -51,29 +55,32 @@ impl Sysroot {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn discover(cargo_toml: &AbsPath) -> Result<Sysroot> {
|
pub fn discover(cargo_toml: &AbsPath) -> Result<Sysroot> {
|
||||||
let src = get_or_install_rust_src(cargo_toml)?;
|
let current_dir = cargo_toml.parent().unwrap();
|
||||||
|
let sysroot_src_dir = discover_sysroot_src_dir(current_dir)?;
|
||||||
|
let res = Sysroot::load(&sysroot_src_dir);
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load(sysroot_src_dir: &AbsPath) -> Sysroot {
|
||||||
let mut sysroot = Sysroot { crates: Arena::default() };
|
let mut sysroot = Sysroot { crates: Arena::default() };
|
||||||
|
|
||||||
for name in SYSROOT_CRATES.trim().lines() {
|
for name in SYSROOT_CRATES.trim().lines() {
|
||||||
// FIXME: remove this path when 1.47 comes out
|
// FIXME: first path when 1.47 comes out
|
||||||
// https://github.com/rust-lang/rust/pull/73265
|
// https://github.com/rust-lang/rust/pull/73265
|
||||||
let root = src.join(format!("lib{}", name)).join("lib.rs");
|
let root = [format!("lib{}/lib.rs", name), format!("{}/src/lib.rs", name)]
|
||||||
if root.exists() {
|
.iter()
|
||||||
|
.map(|it| sysroot_src_dir.join(it))
|
||||||
|
.find(|it| it.exists());
|
||||||
|
|
||||||
|
if let Some(root) = root {
|
||||||
sysroot.crates.alloc(SysrootCrateData {
|
sysroot.crates.alloc(SysrootCrateData {
|
||||||
name: name.into(),
|
name: name.into(),
|
||||||
root,
|
root,
|
||||||
deps: Vec::new(),
|
deps: Vec::new(),
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
let root = src.join(name).join("src/lib.rs");
|
|
||||||
if root.exists() {
|
|
||||||
sysroot.crates.alloc(SysrootCrateData {
|
|
||||||
name: name.into(),
|
|
||||||
root,
|
|
||||||
deps: Vec::new(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(std) = sysroot.std() {
|
if let Some(std) = sysroot.std() {
|
||||||
for dep in STD_DEPS.trim().lines() {
|
for dep in STD_DEPS.trim().lines() {
|
||||||
if let Some(dep) = sysroot.by_name(dep) {
|
if let Some(dep) = sysroot.by_name(dep) {
|
||||||
|
@ -81,62 +88,62 @@ impl Sysroot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(alloc) = sysroot.alloc() {
|
if let Some(alloc) = sysroot.alloc() {
|
||||||
if let Some(core) = sysroot.core() {
|
if let Some(core) = sysroot.core() {
|
||||||
sysroot.crates[alloc].deps.push(core);
|
sysroot.crates[alloc].deps.push(core);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(sysroot)
|
|
||||||
|
sysroot
|
||||||
}
|
}
|
||||||
|
|
||||||
fn by_name(&self, name: &str) -> Option<SysrootCrate> {
|
fn by_name(&self, name: &str) -> Option<SysrootCrate> {
|
||||||
self.crates.iter().find(|(_id, data)| data.name == name).map(|(id, _data)| id)
|
let (id, _data) = self.crates.iter().find(|(_id, data)| data.name == name)?;
|
||||||
|
Some(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_or_install_rust_src(cargo_toml: &AbsPath) -> Result<AbsPathBuf> {
|
fn discover_sysroot_src_dir(current_dir: &AbsPath) -> Result<AbsPathBuf> {
|
||||||
if let Ok(path) = env::var("RUST_SRC_PATH") {
|
if let Ok(path) = env::var("RUST_SRC_PATH") {
|
||||||
let path = AbsPathBuf::try_from(path.as_str())
|
let path = AbsPathBuf::try_from(path.as_str())
|
||||||
.map_err(|path| format_err!("RUST_SRC_PATH must be absolute: {}", path.display()))?;
|
.map_err(|path| format_err!("RUST_SRC_PATH must be absolute: {}", path.display()))?;
|
||||||
return Ok(path);
|
return Ok(path);
|
||||||
}
|
}
|
||||||
let current_dir = cargo_toml.parent().unwrap();
|
|
||||||
let mut rustc = Command::new(toolchain::rustc());
|
let sysroot_path = {
|
||||||
rustc.current_dir(current_dir).args(&["--print", "sysroot"]);
|
let mut rustc = Command::new(toolchain::rustc());
|
||||||
let stdout = utf8_stdout(rustc)?;
|
rustc.current_dir(current_dir).args(&["--print", "sysroot"]);
|
||||||
let sysroot_path = AbsPath::assert(Path::new(stdout.trim()));
|
let stdout = utf8_stdout(rustc)?;
|
||||||
let mut src = get_rust_src(sysroot_path);
|
AbsPathBuf::assert(PathBuf::from(stdout))
|
||||||
if src.is_none() {
|
};
|
||||||
let mut rustup = Command::new(toolchain::rustup());
|
|
||||||
rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]);
|
get_rust_src(&sysroot_path)
|
||||||
utf8_stdout(rustup)?;
|
.or_else(|| {
|
||||||
src = get_rust_src(sysroot_path);
|
let mut rustup = Command::new(toolchain::rustup());
|
||||||
}
|
rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]);
|
||||||
match src {
|
utf8_stdout(rustup).ok()?;
|
||||||
Some(r) => Ok(r),
|
get_rust_src(&sysroot_path)
|
||||||
None => bail!(
|
})
|
||||||
"can't load standard library from sysroot\n\
|
.ok_or_else(|| {
|
||||||
{}\n\
|
format_err!(
|
||||||
(discovered via `rustc --print sysroot`)\n\
|
"\
|
||||||
try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
|
can't load standard library from sysroot
|
||||||
sysroot_path.display(),
|
{}
|
||||||
),
|
(discovered via `rustc --print sysroot`)
|
||||||
}
|
try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
|
||||||
|
sysroot_path.display(),
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_rust_src(sysroot_path: &AbsPath) -> Option<AbsPathBuf> {
|
fn get_rust_src(sysroot_path: &AbsPath) -> Option<AbsPathBuf> {
|
||||||
// try the new path first since the old one still exists
|
// Try the new path first since the old one still exists.
|
||||||
let mut src_path = sysroot_path.join("lib/rustlib/src/rust/library");
|
//
|
||||||
if !src_path.exists() {
|
// FIXME: remove `src` when 1.47 comes out
|
||||||
// FIXME: remove this path when 1.47 comes out
|
// https://github.com/rust-lang/rust/pull/73265
|
||||||
// https://github.com/rust-lang/rust/pull/73265
|
let rust_src = sysroot_path.join("lib/rustlib/src/rust");
|
||||||
src_path = sysroot_path.join("lib/rustlib/src/rust/src");
|
["library", "src"].iter().map(|it| rust_src.join(it)).find(|it| it.exists())
|
||||||
}
|
|
||||||
if src_path.exists() {
|
|
||||||
Some(src_path)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SysrootCrateData {
|
impl SysrootCrateData {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue