This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathloader.rs
188 lines (166 loc) · 5.69 KB
/
loader.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2017 The RLS Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Defines an `AnalysisLoader` trait, which allows to specify directories
//! from which save-analysis JSON files can be read. Also supplies a
//! default implementation `CargoAnalysisLoader` for Cargo-emitted save-analysis
//! files.
use std::env;
use std::ffi::OsStr;
use std::fmt;
use std::path::{Path, PathBuf};
use std::process::Command;
use AnalysisHost;
#[derive(Debug)]
pub struct CargoAnalysisLoader {
pub path_prefix: Option<PathBuf>,
pub target: Target,
}
#[derive(Debug, new)]
pub struct SearchDirectory {
pub path: PathBuf,
// The directory searched must have spans re-written to be based on a new
// path prefix. This happens for example when the std lib crates are compiled
// on the Rust CI, but live in the user's sysroot directory, this adjustment
// (which happens in `lower_span`) means we have the new source location.
pub prefix_rewrite: Option<PathBuf>,
}
impl CargoAnalysisLoader {
pub fn new(target: Target) -> CargoAnalysisLoader {
CargoAnalysisLoader {
path_prefix: None,
target,
}
}
}
/// Allows to specify from where and which analysis files will be considered
/// when reloading data to lower.
pub trait AnalysisLoader: Sized {
fn needs_hard_reload(&self, path_prefix: &Path) -> bool;
fn fresh_host(&self) -> AnalysisHost<Self>;
fn set_path_prefix(&mut self, path_prefix: &Path);
fn abs_path_prefix(&self) -> Option<PathBuf>;
/// Returns every directory in which analysis files are to be considered.
fn search_directories(&self) -> Vec<SearchDirectory>;
}
impl AnalysisLoader for CargoAnalysisLoader {
fn needs_hard_reload(&self, path_prefix: &Path) -> bool {
self.path_prefix.as_ref().map_or(true, |p| p != path_prefix)
}
fn fresh_host(&self) -> AnalysisHost<Self> {
AnalysisHost::new_with_loader(CargoAnalysisLoader {
path_prefix: self.path_prefix.clone(),
.. CargoAnalysisLoader::new(self.target)
})
}
fn set_path_prefix(&mut self, path_prefix: &Path) {
self.path_prefix = Some(path_prefix.to_owned());
}
fn abs_path_prefix(&self) -> Option<PathBuf> {
self.path_prefix.as_ref()
.map(|s| Path::new(s).canonicalize().unwrap().to_owned())
}
fn search_directories(&self) -> Vec<SearchDirectory> {
let path_prefix = self.path_prefix.as_ref().unwrap();
let target = self.target.to_string();
let deps_path = path_prefix
.join("target")
.join("rls")
.join(&target)
.join("deps")
.join("save-analysis");
// FIXME sys_root_path allows to break out of 'sandbox' - is that Ok?
let sys_root_path = sys_root_path();
let target_triple = extract_target_triple(sys_root_path.as_path());
let libs_path = sys_root_path
.join("lib")
.join("rustlib")
.join(&target_triple)
.join("analysis");
let src_path = sys_root_path
.join("lib")
.join("rustlib")
.join("src")
.join("rust")
.join("src");
vec![
SearchDirectory::new(libs_path, Some(src_path)),
SearchDirectory::new(deps_path, None),
]
}
}
// FIXME: This can fail when using a custom toolchain in rustup (often linked to
// `/$rust_repo/build/$target/stage2`)
fn extract_target_triple(sys_root_path: &Path) -> String {
// Extracts nightly-x86_64-pc-windows-msvc from
// $HOME/.rustup/toolchains/nightly-x86_64-pc-windows-msvc
let toolchain = sys_root_path
.iter()
.last()
.and_then(OsStr::to_str)
.expect("extracting toolchain failed");
// Extracts x86_64-pc-windows-msvc from nightly-x86_64-pc-windows-pc
let triple = toolchain
.splitn(2, '-')
.last()
.map(String::from)
.expect("extracting triple failed");
triple
}
fn sys_root_path() -> PathBuf {
env::var("SYSROOT")
.ok()
.map(PathBuf::from)
.or_else(|| {
Command::new(env::var("RUSTC").unwrap_or(String::from("rustc")))
.arg("--print")
.arg("sysroot")
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.map(|s| PathBuf::from(s.trim()))
})
.expect("need to specify SYSROOT or RUSTC env vars, or rustc must be in PATH")
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Target {
Release,
Debug,
}
impl fmt::Display for Target {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Target::Release => write!(f, "release"),
Target::Debug => write!(f, "debug"),
}
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::*;
#[test]
fn windows_path() {
let path = Path::new(
r#"C:\Users\user\.rustup\toolchains\nightly-x86_64-pc-windows-msvc"#,
);
assert_eq!(
extract_target_triple(path),
String::from("x86_64-pc-windows-msvc")
);
}
#[test]
fn unix_path() {
let path = Path::new(
"/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu",
);
assert_eq!(
extract_target_triple(path),
String::from("x86_64-unknown-linux-gnu")
);
}
}