Skip to content
This repository was archived by the owner on Mar 1, 2019. It is now read-only.

Commit bd82c9b

Browse files
authored
Merge pull request #160 from cuviper/rustc-host-triple
Try to get the target triple from rustc itself
2 parents 0129793 + 5fa0882 commit bd82c9b

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

src/loader.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl AnalysisLoader for CargoAnalysisLoader {
8787
.join("deps")
8888
.join("save-analysis");
8989
// FIXME sys_root_path allows to break out of 'sandbox' - is that Ok?
90+
// FIXME libs_path and src_path both assume the default `libdir = "lib"`.
9091
let sys_root_path = sys_root_path();
9192
let target_triple = extract_target_triple(sys_root_path.as_path());
9293
let libs_path = sys_root_path
@@ -108,9 +109,35 @@ impl AnalysisLoader for CargoAnalysisLoader {
108109
}
109110
}
110111

112+
fn extract_target_triple(sys_root_path: &Path) -> String {
113+
// First try to get the triple from the rustc version output,
114+
// otherwise fall back on the rustup-style toolchain path.
115+
// FIXME: Both methods assume that the target is the host triple,
116+
// which isn't the case for cross-compilation (rust-lang/rls#309).
117+
extract_rustc_host_triple()
118+
.unwrap_or_else(|| extract_rustup_target_triple(sys_root_path))
119+
}
120+
121+
fn extract_rustc_host_triple() -> Option<String> {
122+
let rustc = env::var("RUSTC").unwrap_or(String::from("rustc"));
123+
let verbose_version = Command::new(rustc)
124+
.arg("--verbose")
125+
.arg("--version")
126+
.output()
127+
.ok()
128+
.and_then(|out| String::from_utf8(out.stdout).ok())?;
129+
130+
// Extracts the triple from a line like `host: x86_64-unknown-linux-gnu`
131+
verbose_version
132+
.lines()
133+
.find(|line| line.starts_with("host: "))
134+
.and_then(|host| host.split_whitespace().nth(1))
135+
.map(String::from)
136+
}
137+
111138
// FIXME: This can fail when using a custom toolchain in rustup (often linked to
112139
// `/$rust_repo/build/$target/stage2`)
113-
fn extract_target_triple(sys_root_path: &Path) -> String {
140+
fn extract_rustup_target_triple(sys_root_path: &Path) -> String {
114141
// Extracts nightly-x86_64-pc-windows-msvc from
115142
// $HOME/.rustup/toolchains/nightly-x86_64-pc-windows-msvc
116143
let toolchain = sys_root_path
@@ -169,7 +196,7 @@ mod tests {
169196
r#"C:\Users\user\.rustup\toolchains\nightly-x86_64-pc-windows-msvc"#,
170197
);
171198
assert_eq!(
172-
extract_target_triple(path),
199+
extract_rustup_target_triple(path),
173200
String::from("x86_64-pc-windows-msvc")
174201
);
175202
}
@@ -180,8 +207,19 @@ mod tests {
180207
"/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu",
181208
);
182209
assert_eq!(
183-
extract_target_triple(path),
210+
extract_rustup_target_triple(path),
184211
String::from("x86_64-unknown-linux-gnu")
185212
);
186213
}
214+
215+
#[test]
216+
fn target_triple() {
217+
let sys_root_path = sys_root_path();
218+
let target_triple = extract_target_triple(&sys_root_path);
219+
let target_path = sys_root_path
220+
.join("lib")
221+
.join("rustlib")
222+
.join(&target_triple);
223+
assert!(target_path.is_dir(), "{:?} is not a directory!", target_path);
224+
}
187225
}

0 commit comments

Comments
 (0)