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

Commit 72cd8ae

Browse files
committed
Try to get the target triple from rustc itself
The prior method was trying to pick the triple out of the sysroot path. A FIXME comment already notes that this doesn't work with custom toolchains in rustup. It also fails with distro-installed toolchains, where the sysroot may simply be `/usr`. The output of `rustc -Vv` is a more reliable source, as it contains a line like `host: x86_64-unknown-linux-gnu`. This should be enough to identify the triple for any `rustc`, but just in case, the path-based code is kept as a fallback.
1 parent 0129793 commit 72cd8ae

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

src/loader.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,33 @@ impl AnalysisLoader for CargoAnalysisLoader {
108108
}
109109
}
110110

111+
fn extract_target_triple(sys_root_path: &Path) -> String {
112+
// First try to get the triple from the rustc version output,
113+
// otherwise fall back on the rustup-style toolchain path.
114+
extract_rustc_host_triple()
115+
.unwrap_or_else(|| extract_rustup_target_triple(sys_root_path))
116+
}
117+
118+
fn extract_rustc_host_triple() -> Option<String> {
119+
let rustc = env::var("RUSTC").unwrap_or(String::from("rustc"));
120+
let verbose_version = Command::new(rustc)
121+
.arg("--verbose")
122+
.arg("--version")
123+
.output()
124+
.ok()
125+
.and_then(|out| String::from_utf8(out.stdout).ok())?;
126+
127+
// Extracts the triple from a line like `host: x86_64-unknown-linux-gnu`
128+
verbose_version
129+
.lines()
130+
.find(|line| line.starts_with("host: "))
131+
.and_then(|host| host.split_whitespace().nth(1))
132+
.map(String::from)
133+
}
134+
111135
// FIXME: This can fail when using a custom toolchain in rustup (often linked to
112136
// `/$rust_repo/build/$target/stage2`)
113-
fn extract_target_triple(sys_root_path: &Path) -> String {
137+
fn extract_rustup_target_triple(sys_root_path: &Path) -> String {
114138
// Extracts nightly-x86_64-pc-windows-msvc from
115139
// $HOME/.rustup/toolchains/nightly-x86_64-pc-windows-msvc
116140
let toolchain = sys_root_path
@@ -169,7 +193,7 @@ mod tests {
169193
r#"C:\Users\user\.rustup\toolchains\nightly-x86_64-pc-windows-msvc"#,
170194
);
171195
assert_eq!(
172-
extract_target_triple(path),
196+
extract_rustup_target_triple(path),
173197
String::from("x86_64-pc-windows-msvc")
174198
);
175199
}
@@ -180,8 +204,19 @@ mod tests {
180204
"/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu",
181205
);
182206
assert_eq!(
183-
extract_target_triple(path),
207+
extract_rustup_target_triple(path),
184208
String::from("x86_64-unknown-linux-gnu")
185209
);
186210
}
211+
212+
#[test]
213+
fn target_triple() {
214+
let sys_root_path = sys_root_path();
215+
let target_triple = extract_target_triple(&sys_root_path);
216+
let target_path = sys_root_path
217+
.join("lib")
218+
.join("rustlib")
219+
.join(&target_triple);
220+
assert!(target_path.is_dir(), "{:?} is not a directory!", target_path);
221+
}
187222
}

0 commit comments

Comments
 (0)