Skip to content

Commit 477a67c

Browse files
committed
More robust way to find rust's shared codegen llvm lib
1 parent 953a29c commit 477a67c

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

src/path.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::env;
2-
use std::path::PathBuf;
2+
use std::path::{Path, PathBuf};
33
use std::process::Command;
44

55
use failure::Error;
@@ -89,7 +89,7 @@ fn collect_possible_paths() -> Result<Vec<PathBuf>, Error> {
8989
cargo_path.pop();
9090

9191
if let Some(toolchain) = cargo_path.file_name() {
92-
let arch = extract_arch(toolchain.to_str().unwrap());
92+
let arch = env::var("HOST").unwrap_or_else(|_| extract_arch(toolchain.to_str().unwrap()));
9393

9494
paths.push(
9595
cargo_path
@@ -101,6 +101,21 @@ fn collect_possible_paths() -> Result<Vec<PathBuf>, Error> {
101101
}
102102
}
103103

104+
if let Some(rustc) = find_rustc() {
105+
if let Ok(output) = Command::new(&rustc).args(&["--print", "sysroot"]).output() {
106+
let mut sysroot = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
107+
if let Some(arch) = find_arch(&rustc, &sysroot) {
108+
paths.push(
109+
sysroot
110+
.join("lib")
111+
.join("rustlib")
112+
.join(arch)
113+
.join("codegen-backends"),
114+
);
115+
}
116+
}
117+
}
118+
104119
if let Ok(output) = Command::new("rustup").args(&["which", "rustc"]).output() {
105120
let mut rustc_path = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
106121
rustc_path.pop();
@@ -122,6 +137,35 @@ fn collect_possible_paths() -> Result<Vec<PathBuf>, Error> {
122137
Ok(paths)
123138
}
124139

140+
// Fails if using nightly build from a specific date
141+
// e.g. nightly-2018-11-30-x86_64-unknown-linux-gnu
125142
fn extract_arch(toolchain: &str) -> String {
126143
toolchain.split('-').skip(1).collect::<Vec<_>>().join("-")
127144
}
145+
146+
fn find_rustc() -> Option<PathBuf> {
147+
if let Some(path) = env::var_os("RUSTC") {
148+
Some(path.into())
149+
} else if let Ok(output) = Command::new("rustup").args(&["which", "rustc"]).output() {
150+
Some(String::from_utf8_lossy(&output.stdout).trim().into())
151+
} else {
152+
None
153+
}
154+
}
155+
156+
fn find_arch(rustc: &Path, sysroot: &Path) -> Option<String> {
157+
if let Ok(path) = env::var("HOST") {
158+
Some(path)
159+
} else if let Ok(output) = Command::new(&rustc).args(&["-vV"]).output() {
160+
for line in String::from_utf8_lossy(&output.stdout).lines() {
161+
if line.starts_with("host") {
162+
return Some(line.trim_start_matches("host:").trim().to_string());
163+
}
164+
}
165+
None
166+
} else if let Some(toolchain) = sysroot.file_name() {
167+
Some(extract_arch(toolchain.to_str().unwrap()))
168+
} else {
169+
None
170+
}
171+
}

0 commit comments

Comments
 (0)