-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
36 lines (33 loc) · 1.2 KB
/
build.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
use std::path::PathBuf;
use std::process::Command;
fn main(){
let (path, lib) = find_llvm_lib();
println!("cargo:rustc-link-search=native={}", path.display());
println!("cargo:rustc-link-lib=dylib={}", lib);
}
fn find_llvm_lib() -> (PathBuf, String) {
// TODO: cargo does not export us a $RUSTC
let sysroot = Command::new("rustc").arg("--print=sysroot").output()
.expect("could not execute rustc");
// FIXME: may be not utf8?
let mut path = String::from_utf8(sysroot.stdout).expect("rustc output not utf-8");
while let Some(ch) = path.pop() {
if !ch.is_whitespace() {
path.push(ch);
break;
}
}
let mut path = PathBuf::from(path);
path.push("lib");
for entry in path.read_dir().expect("could not read dir") {
let entry_path = entry.expect("could not read dir").path();
let entry_path = entry_path.with_extension("");
let fname = entry_path.file_name();
if let Some(Some(st)) = fname.map(|f| f.to_str()) {
if let Some(i) = st.find("rustc_llvm") {
return (path, String::from(&st[i..]));
}
}
}
panic!("could not find rustc_llvm library");
}